dydxprotocol/v3-client

Not able to calculate correct Liquidation price

Abhimanyu121 opened this issue · 7 comments

I am trying to get the liquidation price of an order, I have implemented the same formula given in the docs
Close Price (Short) = P × (1 + (M × V / W))
Close Price (Long) = P × (1 − (M × V / W))

But the prices I am getting are different not even in 1-5% delta

https://docs.dydx.exchange/?json#liquidations
Here P( Oracle Price), V( Equity), M ( Maintenance margin fraction) the 3 i am getting from the getMarket and getAccount API's itself.

for W: i am using the below formula (https://docs.dydx.exchange/?json#portfolio-margining)
Total Maintenance Margin Requirement = Σ abs(Si × Pi × Mi)
Where
S is the size of the position (positive if long, negative if short)
P is the oracle price for the market
M is the maintenance margin fraction for the market
I am taking this data from getPositions API where I am only using the positions which are open i.e "Position.status == OPEN "

Any updates for this issue?

Hi @Abhimanyu121

Did you find the right answer for this issue?

Would also be interested!

Hey @Kampfk3ks7
You did calculate the liquidation price correctly before? Or whatever are your thoughts?

I used the exact description provided in the official documentation to calculate it. This seems to work for a single position but not for multiple positions. For multiple position the liq. price is always 50% closer than it should be...

Yeah.. same to me. Very strange... or all we are thinking something wrong..?
dydx dev team! Could you please answer this issue?

I used the exact description provided in the official documentation to calculate it. This seems to work for a single position but not for multiple positions. For multiple position the liq. price is always 50% closer than it should be...

Hey @sniper365 and @Kampfk3ks7 we solved it quite some time back so I don't remember exact solution but can you try this code snippet

          final size = double.parse(position.entryPrice ) *
              double.parse(position.size);
          final equity = double.parse(state.userAccount.equity);
          final equityLiquidationDifference = equity - W;
          final percent = ((size - equityLiquidationDifference) / size);
          final liqPrice =
              (double.parse(state.selectedMarketResponse.oraclePrice) *
                  percent);

image

Hi @Abhimanyu121

Currently I have two positions like above

I tried as following your way

So for BTC liq.price, in summary , (final size - (equity - W)) / final size * oracle.price ?

UNI M: 0.05
BTC M: 0.03
equity: 48.39
BTC oracle.price: 27978
UNI oracle.price: 6.204
BTC size: 0.005
UNI size: 25
BTC entry price: 28358

The result by fomula is 20788
Real liq.price is 20465

If so, I tried, but sometimes the difference with my real liq.price on dydx (around few hundreds gap)

When you did before, all are working well?
What am I wrong?

Hey @sniper365 and @Kampfk3ks7 we solved it quite some time back so I don't remember exact solution but can you try this code snippet

          final size = double.parse(position.entryPrice ) *
              double.parse(position.size);
          final equity = double.parse(state.userAccount.equity);
          final equityLiquidationDifference = equity - W;
          final percent = ((size - equityLiquidationDifference) / size);
          final liqPrice =
              (double.parse(state.selectedMarketResponse.oraclePrice) *
                  percent);

Hi @Abhimanyu121 and @Kampfk3ks7

Have you had a chance to check my calculation by following @Abhimanyu121 's fomula?