Skip to content

Instantly share code, notes, and snippets.

@highfestiva
Last active November 8, 2024 03:23
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

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