-
-
Save highfestiva/b71e76f51eed84d56c1be8ebbcc286b5 to your computer and use it in GitHub Desktop.
#!/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)) |
- 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). - Not supported, you have to figure it out yourself.
Doesn’t Binance use the mark price for this calculation?
@thomasd3 that field is not present in their calculator, and unfortunately I no longer remember the details of my reverse engineering six months ago.
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.
Hi,
Do you have any plan to support for hedge mode liquidation?
Haha, I don't even know what hedging is... Let me just do some financial reading up...
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.
Great tool, thanks for sharing! How can I remove the rounding though?
Why can't I input both wallet balance and leverage?
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?
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?
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.
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
@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]
@highfestiva can you please drop the liquidation price formula for USDT FUTURES?
@numairawan What do you mean?
@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.
https://www.binance.com/en-IN/support/faq/b3c689c1f50a44cabb3a84e663b81d93
This formula is kinda hard to understand.
Reverse engineer the code above or use the formula.
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
@zaidanali028 either balance should be used OR leverage. Never both nor neither. Sama sama!
Thanks boss,will work on that today
Interesting, that sneaky move affects my bot. Will look into it.
Updated the algorithm.
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
> ```
What did you type?
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`
Learn how to read.
Hello, please help me with
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
deposit :
2000;239.7;150;45
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