APIError(code=-2019): Margin is insufficient

Hello, I’m developing a cryptocurrency bot using the Binance API

  • Account Balance: 8.06 USD
  • Leverage: 20
  • Entry Price: 3157.13 USD
  • Quantity: 0.046

(Entry Price x Quantity) / Leverage = Balance needed
(3157.13 x 0.046) / 20 = 7.26 USD

Although I have 8.06 USD in my account, when I try to open a short position, I get the following error;

APIError(code=-2019): Margin is insufficient.

I don’t always get this error only in some orders. But I always have enough balance in my account. What do you think is the reason for this?

Many thanks in advance for your help :slight_smile:

I also got same problem , Even though I have more than enough balance , it’s raising margin insufficent error for only some orders.
Have you solved this problem, if yes, kindly share here so it will be helpful for me and for may who looking for this

Hi. Could you double check your account balance with this endpoint GET /fapi/v2/account for that specific asset?


Btw, if the commission fee is set to pay with the quote asset, please also consider it as part of the necessary cost.

I’m sharing how I solved this problem so that it can help anyone who encounters the same problem.

I wrote a function called get_av_balance.

After obtaining the data related to my account with the code;

account_info = client.futures_account()

With the code below, I transfer the USDT amount in my futures wallet to the av_balance variable. The important point here is that I get the amount that I will transfer to the variable using the “availableBalance” parameter. If there is a USDT amount reserved by another order, this parameter is calculated by deducting it. In this way, I transfer my fully usable USDT amount to the av_balance variable in the form of balance that can be traded.

av_balance = None
for asset in account_info["assets"]:
	if asset["asset"] == "USDT":
		av_balance = float(asset["availableBalance"])

Then, with the code below, I format the USDT amount as a 2-digit decimal and return it from the function.

if len(account_info) > 0:
	av_balance = float("{:.2f}".format(av_balance))
    return av_balance

I shared the code of the get_av_balance function below. I hope it helps.

def get_av_balance(client, time):

    while True:
        try:
            account_info = client.futures_account()

            av_balance = None
            for asset in account_info["assets"]:
                if asset["asset"] == "USDT":
                    av_balance = float(asset["availableBalance"])

            if len(account_info) > 0:
                av_balance = float("{:.2f}".format(av_balance))
                return av_balance

        except Exception as e:
            print("Account Error:", e)
            time.sleep(1)
            pass
1 Like