Calculating liquidation price
Learn how to calculate any trade's liquidation price
Last updated
Was this helpful?
Learn how to calculate any trade's liquidation price
Last updated
Was this helpful?
Note: This guide works on Arbitrum Sepolia and requires exact beta SDK version of 0.2.12-rc15
To calculate liquidation price, we need to use getLiquidationPrice()
function.
To calculate trade's liquidation price, we need to find a trade and its first. TradeContainer is unique object per single trade and is containing all its information.
By inspecting allTrades
of backend's response, we're able to find all open trades & orders (each represented by single, unique object). Lets pick last market trade (trade.type
= 0 ) at time of writing this guide, which is:
Respective trade
and initialAccFees
objects will be used as first and third arguments of SDK's getLiquidationPrice()
while liquidationParams
and tradeInfo
will be helpful when crafting fourth argument (context
).
With pairs
and trade's pairIndex
we compute feeIndex
:
pairs[trade.pairIndex].feeIndex
We've now covered 3 arguments and are left with last one, the GetLiquidationPriceContext
.
currentBlock
tradingVariables.currentBlock
groups
and pairs
tradingVariables.collaterals[trade.collateralIndex - 1].borrowingFees
liquidationParams
pairSpreadP
To get pairSpreadP
inspect pairs
key of backends /trading-variables
response. It is an array of objects corresponding to each pair. Use trade.pairIndex
to select proper pair:
tradingVariables.pairs[trade.pairIndex].spreadP
collateralPriceUsd
Its similar to getting groups
and pairs
but select prices
key after accessing proper collaterals
entry:
tradingVariables.collaterals[trade.collateralIndex - 1].prices.collateralPriceUsd
contractsVersion
tradeInfo.contractsVersion
At the end, the context passed to getLiquidationPrice()
should look similar to this:
Wrapping all of the above, we should pass trade
, fee
, initialAccFees
and context
to SDK's getLiquidationPrice()
. This should return single, float number. Per example above the return liquidation price is:
4.702126243307295
which matches gTrade's UI:
This response is of type, therefore we need to properly convert it to type first (SDK uses ). Converted and normalised trade will look like this:
Lets focus on second getLiquidationPrice()
argument now, which is .
To find proper object, we need to compute feeIndex
for given trade first. By calling backend's we're getting pairs
and fees
arrays.
Remember that backend values should be normalised, see vs and vs
Now we are able to find our by simply accessing fees[feeIndex]
. Resulting object should look similar to this:
There are few context parameters that we can pass here. All of them are coming from backend's endpoint or discussed above.
Its current block of underlying network. Use any valid block or backend's response:
To get groups
and pairs
we need to inspect collaterals
backend response. It is an array of objects corresponding to each supported collateral. Use trade.collateralIndex
to select proper collateral object, then access its borrowingFees:
Remember that backend values should be normalised, see vs
Available directly on
Should be normalised, see vs
Available on :