Created
November 15, 2019 19:02
-
-
Save highfestiva/2f00767fa9a5191472926a19a3a24de1 to your computer and use it in GitHub Desktop.
Optimized and simplified BitMEX's cross-margin calculation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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