Skip to content

Instantly share code, notes, and snippets.

@mattj161
Last active September 18, 2023 14:39
Show Gist options
  • Save mattj161/21bec86528700b7e4cb025730485a2a7 to your computer and use it in GitHub Desktop.
Save mattj161/21bec86528700b7e4cb025730485a2a7 to your computer and use it in GitHub Desktop.
Binance liquidation calculator (based
# BTC-USDT
x125_lookup_table = [
( 50_000, 0.004, 0),
( 250_000, 0.005, 50),
( 1_000_000, 0.01, 1_300),
( 10_000_000, 0.02, 16_300),
( 20_000_000, 0.05, 266_300),
( 50_000_000, 0.1, 1_266_300),
( 100_000_000, 0.125, 2_516_300),
( 200_000_000, 0.15, 5_016_300),
( 300_000_000, 0.25, 25_016_300),
(5_000_000_000, 0.50, 100_016_300),
]
# ETH-USDT
x100_lookup_table = [
( 10_000, 0.005, 0),
( 100_000, 0.0065, 15),
( 500_000, 0.01, 365),
( 1_000_000, 0.02, 5_365),
( 2_000_000, 0.05, 35_365),
( 5_000_000, 0.1, 135_365),
( 10_000_000, 0.125, 260_365),
( 20_000_000, 0.15, 510_365),
(5_000_000_000, 0.25, 2_510_365),
]
# ADA-USDT, BNB-USDT, DOT-USDT, EOS-USDT, ETCU-SDT, LINK-USDT, LTC-USDT, TRX-USDT, XLMUSDT, XMR-USDT, XRP-USDT, XTZ-USDT, BCH-USDT
x75_lookup_table = [
( 10_000, 0.0065, 0),
( 50_000, 0.01, 35),
( 250_000, 0.02, 535),
( 1_000_000, 0.05, 8_035),
( 2_000_000, 0.1, 58_035),
( 5_000_000, 0.125, 108_035),
( 10_000_000, 0.15, 233_035),
(5_000_000_000, 0.25, 1_233_035),
]
# All other USDT pairs
x50_lookup_table = [
( 5_000, 0.01, 0),
( 25_000, 0.025, 75),
( 100_000, 0.05, 700),
( 250_000, 0.1, 5_700),
( 1_000_000, 0.125, 11_950),
(5_000_000_000, 0.5, 386_950),
]
def get_liquidation_price(position_size, wallet_balance, entry_price, lookup_table):
notional_value = position_size * entry_price
for maximum_notional, margin_rate_percentage, maintenance_amount in lookup_table:
if notional_value <= maximum_notional:
liquidation_price = (wallet_balance + maintenance_amount - notional_value) / (abs(position_size) * (margin_rate_percentage - (1 if position_size >= 0 else -1)))
return round(liquidation_price, 2)
test_pairs = [
{
"pair": "LTC-USDT",
"wallet_balance" : 9_947.75,
"entry_price": 221.99,
"position_size": 447.155,
"target_liquidation_price": 202.60
},
{
"pair": "ZEC-USDT",
"wallet_balance" : 6_894.68,
"entry_price": 167.55,
"position_size": 74.573,
"target_liquidation_price": 75.99
}
]
for pair in test_pairs:
pair_name = pair["pair"]
lookup_table = None
if pair_name == "BTC-USDT":
lookup_table = x125_lookup_table
elif pair_name == "ETH-USDT":
lookup_table = x100_lookup_table
elif pair_name in ["ADA-USDT", "BNB-USDT", "DOT-USDT", "EOS-USDT", "ETC-USDT", "LINK-USDT", "LTC-USDT", "TRX-USDT", "XLM-USDT", "XMR-USDT", "XRP-USDT", "XTZ-USDT", "BCH-USDT"]:
lookup_table = x75_lookup_table
else:
lookup_table = x50_lookup_table
liquidation_price = get_liquidation_price(pair["position_size"], pair["wallet_balance"], pair["entry_price"], lookup_table)
assert liquidation_price == pair["target_liquidation_price"]
print(liquidation_price)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment