Skip to content

Instantly share code, notes, and snippets.

@highfestiva
Created November 15, 2019 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save highfestiva/2f00767fa9a5191472926a19a3a24de1 to your computer and use it in GitHub Desktop.
Save highfestiva/2f00767fa9a5191472926a19a3a24de1 to your computer and use it in GitHub Desktop.
Optimized and simplified BitMEX's cross-margin calculation
def long_liq_price(wallet_balance_btc, qty, price):
usd = qty * int(-100000000 / price - 0.5)
a = wallet_balance_btc * 99925056.20784412 + 0.011498875843117663 * usd
a *= a>0
x = usd*1.004831 - a
return -100000000 / int(x / qty)
def short_liq_price(wallet_balance_btc, qty, price):
usd = qty * int(-100000000 / price - 0.5)
a = wallet_balance_btc * 99925056.20784412 - 0.011498875843117663 * usd
a *= a>0
x = usd*0.995 - a
if x < 0:
return 100000000
return -100000000 / (int(x / qty) - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment