Skip to content

Instantly share code, notes, and snippets.

@highfestiva
Last active April 16, 2024 06:13
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • 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))
@pkokosak
Copy link

pkokosak commented Jun 7, 2020

Hello, please help me with

  1. I try calculation with your program and I got from binance different result

a real example:
python binance-liquidation-calculator.py --contract-quantity 150 --entry-price 239.7 --leverage 45
Result : 235.31

Binance result: 236.40

Can you explain the difference?
2. Calculation cumulative liquidation price for different leverage
My source parameters:
Balance account 2000 and I will no longer add

Input parameters:
Balance account; entry_price; contract_qty; options.leverage

  1. deposit :
    2000;239.7;150;45

  2. deposit add 1. deposit:
    1760,result_liquid from 1.deposit ;5;2

How correctly calculate cumulative result_liquid after 2.deposit ?

Thank you very much
Petr

@highfestiva
Copy link
Author

  1. Binance's calculator only cares for "wallet balance," which must a legacy nomenclature from when they only had cross margin trading. They mean "margin balance." So instead do ./binance-liquidation-calc.py --contract-quantity 150 --entry-price 239.7 --wallet-balance 637 (and use the same balance as you're previously entered in Binance's own calculator, which is 637 USDT).
  2. Not supported, you have to figure it out yourself.

@thomasd3
Copy link

Doesn’t Binance use the mark price for this calculation?

@highfestiva
Copy link
Author

@thomasd3 that field is not present in their calculator, and unfortunately I no longer remember the details of my reverse engineering six months ago.

@thomasd3
Copy link

The formula on their site spans 3 pages: there is the main formula, tables for je different maintenance margins and another page that explains how to calculate the unrealized pnl.
There are two issues:

  • the unrealized pnl takes the mark price but the main formula seems to consider that only for the other positions than the one you are calculating the liquidation price for. Live support doesn’t know how to explain that and I have an open ticket on the topic which hasn’t been answered yet. My guess is that they made the doc in an incremental disconnected fashion and the formula is wrong.
  • the calculator on the site is not using the full formula, there is actually a small text about it at the bottom right of the window where they say that actual calculations take other things in consideration.

@highfestiva
Copy link
Author

highfestiva commented Sep 28, 2020 via email

@opoet7
Copy link

opoet7 commented Nov 9, 2020

Hi,
Do you have any plan to support for hedge mode liquidation?

@highfestiva
Copy link
Author

Haha, I don't even know what hedging is... Let me just do some financial reading up...

@opoet7
Copy link

opoet7 commented Nov 10, 2020

IMG_20201110_090202_964
In hedge mode can be open two position(long and sell) concurrently.

@highfestiva
Copy link
Author

Nope, but I suppose they're treated as two isolated positions? If not feel free to add the code and send it to me, and I'll update here as well.

@BaptisteGarcin
Copy link

Great tool, thanks for sharing! How can I remove the rounding though?
Why can't I input both wallet balance and leverage?

@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