Skip to content

Calculate unrealized gain/loss for open positions

POST
/api/calculate/unrealized-gl
curl --request POST \
--url https://api.qeychain.xyz/api/calculate/unrealized-gl \
--header 'Content-Type: application/json' \
--data '{ "current_prices": { "additionalProperty": 1 }, "transactions": [ {} ], "address": "example", "owned_wallets": [ "example" ] }'

Calculates unrealized gain/loss for all open lots based on provided current prices.

For each open lot: unrealized_gl = (current_price - cost_per_unit) * remaining_quantity

Aggregates by:
- Individual lot level
- Asset level (per token)
- Portfolio level (total)

Requires either:
- `transactions`: Pre-fetched transactions to process, OR
- `address`: Wallet address to fetch transactions from Zerion

Plus `current_prices`: Dict mapping token_key (chain:symbol) or symbol to current USD price.
Media typeapplication/json
UnrealizedGLRequest

Request for unrealized gain/loss calculation.

object
current_prices
required
Current Prices

Current prices by token_key (chain:symbol) or symbol. Example: {‘ethereum:eth’: 3500.00, ‘eth’: 3500.00}

object
key
additional properties
Any of:
number
transactions
Any of:
Array<object>
object
key
additional properties
any
address
Any of:
string
owned_wallets
Any of:
Array<string>
Examplegenerated
{
"current_prices": {
"additionalProperty": 1
},
"transactions": [
{}
],
"address": "example",
"owned_wallets": [
"example"
]
}

Successful Response

Media typeapplication/json
UnrealizedGLResponse

Portfolio-level unrealized gain/loss response.

object
by_asset
By Asset

Unrealized G/L by asset (keyed by token_key)

object
key
additional properties
AssetUnrealizedGLResponse

Unrealized G/L aggregated by asset.

object
symbol
required
Symbol

Token symbol

string
chain
required
Chain

Blockchain network

string
quantity
required
Quantity

Total quantity held

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
current_price
required
Current Price

Current market price

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
cost_basis
required
Cost Basis

Total cost basis

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
current_value
required
Current Value

Current market value

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
unrealized_gl
required
Unrealized Gl

Unrealized gain/loss

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
unrealized_gl_percent
required
Unrealized Gl Percent

Unrealized G/L as percentage

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
lots
Lots

Per-lot breakdown

Array<object>
LotUnrealizedGLResponse

Unrealized G/L for a single lot.

object
lot_id
required
Lot Id

Lot identifier

string
remaining_quantity
required
Remaining Quantity

Remaining quantity in lot

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
cost_per_unit
required
Cost Per Unit

Cost basis per unit

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
current_price
required
Current Price

Current market price

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
cost_basis
required
Cost Basis

Total cost basis (remaining)

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
current_value
required
Current Value

Current market value

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
unrealized_gl
required
Unrealized Gl

Unrealized gain/loss

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
acquisition_date
Any of:
string format: date-time
is_untracked
Is Untracked

Whether lot has unknown cost basis

boolean
portfolio_total
required
Portfolio Total

Total unrealized G/L across all assets

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
portfolio_unrealized_gain
required
Portfolio Unrealized Gain

Sum of positive unrealized G/L

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
portfolio_unrealized_loss
required
Portfolio Unrealized Loss

Sum of negative unrealized G/L (as positive number)

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
total_cost_basis
required
Total Cost Basis

Total cost basis of open positions

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
total_current_value
required
Total Current Value

Total current market value of open positions

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
portfolio_unrealized_gl_percent
required
Portfolio Unrealized Gl Percent

Portfolio unrealized G/L as percentage of cost basis

string
/^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$/
Example
{
"by_asset": {
"additionalProperty": {
"lots": [
{
"is_untracked": false
}
]
}
}
}

Bad Request

Media typeapplication/json
ErrorResponse

Standard error response.

object
detail
required
Detail

Error message

string
error_code
Any of:
string
Examplegenerated
{
"detail": "example",
"error_code": "example"
}

Not Found

Media typeapplication/json
ErrorResponse

Standard error response.

object
detail
required
Detail

Error message

string
error_code
Any of:
string
Examplegenerated
{
"detail": "example",
"error_code": "example"
}

Validation Error

Media typeapplication/json
HTTPValidationError
object
detail
Detail
Array<object>
ValidationError
object
loc
required
Location
Array
msg
required
Message
string
type
required
Error Type
string
Examplegenerated
{
"detail": [
{
"loc": [
"example"
],
"msg": "example",
"type": "example"
}
]
}

Internal Server Error

Media typeapplication/json
ErrorResponse

Standard error response.

object
detail
required
Detail

Error message

string
error_code
Any of:
string
Examplegenerated
{
"detail": "example",
"error_code": "example"
}