How to calculate maximum quantity in futures with python-binance?

I have a Python script that buys and sells in Futures with leverage set to 2. However, when I try to calculate the quantity to buy for each order using my maximum available funds, I encounter an “Insufficient Margin” error. I’m trying to understand how Binance calculates the maximum quantity I can buy with 100% of my funds.

For example:

  • I have $10,000.
  • I want to buy at a price of $20,000.
  • I expect to be able to buy 1 BTC with my leverage of 2.

However, the calculated quantity is lower than expected. What am I missing?

Here are the relevant code snippets I’m currently using for the calculations:

# Calculating the balance with leverage:
account_info = client.futures_account()
balance = float(account_info['totalWalletBalance'])
total_usdt_with_leverage = float(balance * self.leverage)

quantity = float(format(total_usdt_with_leverage / <price_to_buy>, '.3f'))

Please note that <price_to_buy> should be replaced with the actual price at which you want to buy.

Anyone can help me to perform this calculation?

Hello, the quantity calculation takes in account quantity precision and notional in attention (check exchangeInfo endpoint).
I haven’t looked into this post, but it seems to touch similar questions as yours, please have a look: Max quantity calculation - #5 by yovanoc

Note: we don’t support python-binance (not official lib).