Skip to content

Instantly share code, notes, and snippets.

@highfestiva
Last active April 16, 2024 06:13
Show Gist options
  • Save highfestiva/b71e76f51eed84d56c1be8ebbcc286b5 to your computer and use it in GitHub Desktop.
Save highfestiva/b71e76f51eed84d56c1be8ebbcc286b5 to your computer and use it in GitHub Desktop.
CLI Binance liquidation calculation formula
#!/usr/bin/env python3
'''2021-03-26: Reverse-engineer by searching for the following terms in features*.js:
- bracketMaintenanceMarginRate
- cumFastMaintenanceAmount
- bracketNotionalFloor
- bracketNotionalCap'''
# (max) position, maintenance margin, maintenance amount
maint_lookup_table = [
( 50_000, 0.4, 0),
( 250_000, 0.5, 50),
( 1_000_000, 1.0, 1_300),
( 10_000_000, 2.5, 16_300),
( 20_000_000, 5.0, 266_300),
( 50_000_000, 10.0, 1_266_300),
(100_000_000, 12.5, 2_516_300),
(200_000_000, 15.0, 5_016_300),
(300_000_000, 25.0, 25_016_300),
(500_000_000, 50.0, 100_016_300),
]
def binance_btc_liq_balance(wallet_balance, contract_qty, entry_price):
for max_position, maint_margin_rate_pct, maint_amount in maint_lookup_table:
maint_margin_rate = maint_margin_rate_pct / 100
liq_price = (wallet_balance + maint_amount - contract_qty*entry_price) / (abs(contract_qty) * (maint_margin_rate - (1 if contract_qty>=0 else -1)))
base_balance = liq_price * abs(contract_qty)
if base_balance <= max_position:
break
return round(liq_price, 2)
def binance_btc_liq_leverage(leverage, contract_qty, entry_price):
wallet_balance = abs(contract_qty) * entry_price / leverage
print('[Wallet-balance-equivalent of %s] '%wallet_balance, end='')
return binance_btc_liq_balance(wallet_balance, contract_qty, entry_price)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--wallet-balance', type=float, help='wallet balance in USDT')
parser.add_argument('--contract-quantity', required=True, type=float, help='contract quantity in BTC, negative for shorts')
parser.add_argument('--entry-price', required=True, type=float, help='entry price in USDT')
parser.add_argument('--leverage', type=int, help='leverage to use instead of wallet balance')
options = parser.parse_args()
assert (options.leverage is None) != (options.wallet_balance is None)
if options.leverage:
print(binance_btc_liq_leverage(options.leverage, options.contract_quantity, options.entry_price))
else:
print(binance_btc_liq_balance(options.wallet_balance, options.contract_quantity, options.entry_price))
@highfestiva
Copy link
Author

If you try Binance's liquidator calculator, you'll notice that leverage is irrelevant to liquidation. They don't define it the same way BitMEX does. Remove the round call?

@BaptisteGarcin
Copy link

Yes thanks, I removed round. Available balance is relevant to liquidation price though in cross mode and in isolated mode you can add or remove margin to your position changing your liquidation price, how would you calculate that?

@highfestiva
Copy link
Author

Good question, on BitMEX it's just as easy as lerping between the two positions with respect to their sizes, but I assume it's more complicated on Binance. You could always try and compare to reality.

@zaidanali028
Copy link

hello highfestiva,am currently working on a nodejs version of your script but unfortunately,am having some issues,
so this line, pct,amt = [(mr,ma) for p,mr,ma in maint_lookup_table if pos<p][0],you assigned mr to pct and ma to amt and then looped through each line, but what I dont understand is ,if the pos argument is less than the p from the list,before the return statement,there is 0 index[0], please what is it doing there kindly explain it to me so i can move on,thank you

@highfestiva
Copy link
Author

@zaidanali028 write it out as a for loop unless you understand it:

lst = []
for p,mr,ma in maint_lookup_table:
    if pos < p:
        lst.append((mr,ma))
pct,amt = lst[0]

@numairawan
Copy link

@highfestiva can you please drop the liquidation price formula for USDT FUTURES?

@highfestiva
Copy link
Author

@numairawan What do you mean?

@numairawan
Copy link

@highfestiva the formula/method that you are using to calculate the liquidation price. I am making a chrome extension for risk management and I want to show the real-time liquidation price when opening the position on Binance future.

@numairawan
Copy link

@highfestiva
Copy link
Author

Reverse engineer the code above or use the formula.

@zaidanali028
Copy link

Thank you @highfestiva,i was able to understamd the broken down pieces u explained,I need one more explanation and thats all....

On line 41,that is assert (options.leverage is None) != (options.wallet_balance is None).Please break it down for me,I know assert() but how u used it here is a bit advanced,I will be much grateful if you help me break here down too,thanks

@highfestiva
Copy link
Author

@zaidanali028 either balance should be used OR leverage. Never both nor neither. Sama sama!

@zaidanali028
Copy link

Thanks boss,will work on that today

@highfestiva
Copy link
Author

Interesting, that sneaky move affects my bot. Will look into it.

@highfestiva
Copy link
Author

Updated the algorithm.

@Mr-Stone-de
Copy link

Mr-Stone-de commented May 31, 2021

Hi highfestive
could you please light me in for the following error that I'm facing, in run time?

usage: -c [-h] [--wallet-balance WALLET_BALANCE] --contract-quantity
          CONTRACT_QUANTITY --entry-price ENTRY_PRICE [--leverage LEVERAGE]
-c: error: the following arguments are required: --contract-quantity, --entry-price
Traceback (most recent call last):
  File "<string>", line 47, in <module>
  File "/usr/lib/python3.8/argparse.py", line 1780, in parse_args
args, argv = self.parse_known_args(args, namespace)
  File "/usr/lib/python3.8/argparse.py", line 1812, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
  File "/usr/lib/python3.8/argparse.py", line 2046, in _parse_known_args
self.error(_('the following arguments are required: %s') %
File "/usr/lib/python3.8/argparse.py", line 2533, in error
self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
  File "/usr/lib/python3.8/argparse.py", line 2520, in exit
_sys.exit(status)
SystemExit: 2
> ```

@highfestiva
Copy link
Author

What did you type?

@Rajan244
Copy link

Rajan244 commented Aug 12, 2021

Hello, I am a beginner and trying to run this code. but getting this error do you know why?
`usage: ipykernel_launcher.py [-h] [--wallet-balance WALLET_BALANCE] --contract-quantity CONTRACT_QUANTITY
--entry-price ENTRY_PRICE [--leverage LEVERAGE]
ipykernel_launcher.py: error: the following arguments are required: --contract-quantity, --entry-price
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2`

@MikeMaxNow @highfestiva

@highfestiva
Copy link
Author

Learn how to read.

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