dydxprotocol / v3-client

TypeScript client for dYdX (v3 API).
Apache License 2.0
107 stars 53 forks source link

Not able to calculate correct Liquidation price #201

Open Abhimanyu121 opened 2 years ago

Abhimanyu121 commented 2 years ago

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 "

sniper365 commented 1 year ago

Any updates for this issue?

sniper365 commented 1 year ago

Hi @Abhimanyu121

Did you find the right answer for this issue?

asyncio0x commented 1 year ago

Would also be interested!

sniper365 commented 1 year ago

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

asyncio0x commented 1 year ago

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...

sniper365 commented 1 year ago

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...

Abhimanyu121 commented 1 year ago

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);
sniper365 commented 1 year ago

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?

sniper365 commented 1 year ago

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?