Selling gives me 'Insufficient balance' error

Hi everyone,

When I trye to sell it gives me error: APIError(code=-2010): Account has insufficient balance for requested action. Even though I should have enough balance left to pay for the fees. What can possibly be wrong with my code ?
Here is my sell code:

                pr = client.get_symbol_ticker(symbol=symbol)
                price=float(pr['price'])
                bal = client.get_asset_balance(asset='RIF')
                quantity = ((float(bal['free'])) / price)  # 0.995
                sym_info = client.get_symbol_info(symbol)
                filters = sym_info['filters']
                for f in filters:
                    print("filter~~~~~~~~~~~~ " + str(f))
                    if f['filterType'] == 'LOT_SIZE':
                        step_size = float(f['stepSize'])
                        break
                precision = int(round(-math.log(step_size, 10), 0))
                quantity = float(round(quantity*0.99, precision))
                order = client.create_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)

Any help would be much appreciated.

Hello, Gerald:

Interesting questions. There is a lot we don’t know about your situation, so it’s hard to offer suggestions. But I can think of two things to try.

  1. Try to separate the effects of your code from the effects of the API you are calling from the state of the account the API connects to.

  2. Tell us what this API is, and give a link to the documentation for the client structure and the create_order API entry point.

  3. Consider where you are most likely to get an answer. If the problem is with the code that prepares parameters for the API call, this is a good place to ask questions. If the problem is with an API call, maybe a forum related to that API will be more likely to get you a helpful answer.

Regarding the first point, here are some things to try.

Instead calling client.create_order, print the values for symbol and quantity. Do they have the values you expect them to have? This shows you the behaviour of your code separate from the behaviour of the API.

Instead of all your computation code, put in two assignment statements. Give symbol and quantity simple explicit values. Then call client.create_order with those values. Does the call work, or does it return the APIError? This shows you the behaviour of the API separate from the behaviour of your code.

Do you have a user interface which does the same thing as client.create_order? What if you use that interface to create an order with the same symbol and quantity as your API call? Does the order succeed? This lets you isolate the state of your account from the behaviour of your API.

I hope these suggestions are helpful for you.

1 Like

Yeah before I even consider writing a forum post I have went trough countless google searches to find the solution but I just don’t see what is wrong here. Yes I singled out the values and there doesn’t seem anything wrong with them. The broker I’m using is Binance. It’s weird because my buy function doesn’t give me any problems.

If there is anyone who can post a working code or someone who knows what I’ve done wrong that would be very helpfull.

So what values does your code compute for symbol and quantity?

What happens when you call client.create_order() with those values?

Here are the values I get for symbol and quantity:
symbol =RIFUSDT
quantity= 3671.0
When I call client.create_order() is when the error happens.

Great! Thank you for the information.

What happens if you run this code:

order = client.create_order(symbol='RIFUSDT', side='SELL', type='MARKET', quantity=3671.0)

Does that fail with the same error?

If so, I think you have just proved that your problem is with the Binance create_order() API, and not with the Python code earlier in your example.

Thanks for your help, the problem is solved now. Something was indeed wrong with my order setting but I had it solved by the Binance support team.

Cheers !