dydxprotocol / dydx-v3-python

Python client for dYdX (API v3)
Apache License 2.0
304 stars 174 forks source link

liquidation price formula #199

Open saleh-mir opened 1 year ago

saleh-mir commented 1 year ago

Hi there,

I have been following the formula on the documentation but my numbers are always about 3 percent off. is there any example code that I can look into?

this is my code:

maintenance_margin_fraction = float(self.vars['market_info'][symbol]['maintenanceMarginFraction'])
oracle_price = float(self.client.public.get_markets(symbol).data['markets'][symbol]['oraclePrice'])

is_long = position_type == 'long'
is_short = position_type == 'short'
is_close = position_type == 'close'

try:
    total_account_value = self.vars['quote_balance']
except KeyError:
    self.fetch_account_balance()
    total_account_value = self.vars['quote_balance']
total_maintenance_margin_requirement = 0
for s in self.exchange_trading_symbols:

    total_maintenance_margin_requirement += abs(
        qty * oracle_price * float(self.vars['market_info'][symbol]['maintenanceMarginFraction'])
    )
    total_account_value += abs(qty * oracle_price)
if is_long:
    return oracle_price * (
                1 - (maintenance_margin_fraction * total_account_value / total_maintenance_margin_requirement))
elif is_short:
    return oracle_price * (
                1 + (maintenance_margin_fraction * total_account_value / total_maintenance_margin_requirement))
else:
    return None
traderblakeq commented 1 year ago
saleh-mir commented 1 year ago
  • Did you find a solution ?.

Unfortunatly no.