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
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