Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to calculate correct Liquidation price #201

Open
Abhimanyu121 opened this issue Aug 26, 2022 · 9 comments
Open

Not able to calculate correct Liquidation price #201

Abhimanyu121 opened this issue Aug 26, 2022 · 9 comments

Comments

@Abhimanyu121
Copy link

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
Copy link

Any updates for this issue?

@sniper365
Copy link

Hi @Abhimanyu121

Did you find the right answer for this issue?

@asyncio0x
Copy link

Would also be interested!

@sniper365
Copy link

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

@asyncio0x
Copy link

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
Copy link

sniper365 commented Mar 28, 2023

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
Copy link
Author

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
Copy link

sniper365 commented Apr 5, 2023

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
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants