Binance API : Automatised bot lot size issue

I am trying to build my own bot on Binance API availaible on Python.

What I am currently trying is to make order of buying/selling BTC, based on the amount of money/BTC availaible in my Binance account.

Then, the code is supposed to convert this amount of cash in BTC and make buy/sell orders :slight_smile:

if order_book[-1]=="BUY":
    for balance in account_info['balances']:
      if float(balance['free']) > 0 and balance['asset']=="BTC":
        total_btc += float(balance['free'])
        
    
  for balance in account_info['balances']:
    if float(balance['free']) > 0 and balance['asset']=="USDT":
      total_balance += float(balance['free'])
  

So based on the loops above, total_balance gives me the amount of cash I currently have.

Based on that, I can make a buy order :

client.create_order(
symbol='BTCUSDT',
side='BUY',
type='MARKET',
quantity=float(round(float(total_balance)/float(client.get_ticker(symbol='BTCUSDT'```

The main struggle here is that, here I'm trying to convert in BTC or whatever cryptocurrency in the "quantity" argument, and most often I have the following error :

BinanceAPIException: APIError(code=-1013): Filter failure: LOT_SIZE

How can I make this code run everytime without getting a Lot Size error, i.e how can I convert precisely the amount of cash I have into BTC and make the buy order?

EDIT: For example, in my case, the quantity here corresponds to 0.00095922 BTCUSDT, but I have the Lot Size error. When searching, I found that Min Quantity is way lower than the current trade:


{
“filterType”: “LOT_SIZE”,
“minQty”: “0.00000100”,
“maxQty”: “9000.00000000”,
“stepSize”: “0.00000100”
}

I understood that the quantity cannot be up to 6 decimals, so I need to round it. The problem is that sometimes it is rounding up, and I don't have the amount of cash sufficient for the buy order.

The error you’re encountering, “BinanceAPIException: APIError(code=-1013): Filter failure: LOT_SIZE”, indicates that the quantity you are trying to buy does not meet the required precision set by the Binance LOT_SIZE filter.

To resolve this, you need to ensure that the quantity of BTC you are buying adheres to the step size of the trading pair. Here’s a modified version of your code that includes the necessary steps to avoid this error:

Get the LOT_SIZE filter information for the trading pair.
Calculate the quantity to buy based on your available balance and the current price of BTC.
Round the quantity to the appropriate number of decimal places as specified by the step size.

Try below:

from binance.client import Client

Initialize the Binance client with your API key and secret

client = Client(api_key=‘your_api_key’, api_secret=‘your_api_secret’)

Fetch account information

account_info = client.get_account()

Initialize total balances

total_btc = 0.0
total_balance = 0.0

Check balances for BTC and USDT

for balance in account_info[‘balances’]:
if float(balance[‘free’]) > 0:
if balance[‘asset’] == “BTC”:
total_btc += float(balance[‘free’])
elif balance[‘asset’] == “USDT”:
total_balance += float(balance[‘free’])

Get current price of BTCUSDT

btc_price = float(client.get_symbol_ticker(symbol=‘BTCUSDT’)[‘price’])

Fetch the LOT_SIZE filter for BTCUSDT

symbol_info = client.get_symbol_info(‘BTCUSDT’)
lot_size_filter = next(f for f in symbol_info[‘filters’] if f[‘filterType’] == ‘LOT_SIZE’)
step_size = float(lot_size_filter[‘stepSize’])

Calculate the quantity to buy, ensuring it adheres to the step size

quantity_to_buy = total_balance / btc_price
quantity_to_buy = quantity_to_buy - (quantity_to_buy % step_size)

Place the buy order

try:
order = client.create_order(
symbol=‘BTCUSDT’,
side=‘BUY’,
type=‘MARKET’,
quantity=quantity_to_buy
)
print(“Order placed successfully:”, order)
except Exception as e:
print(“An error occurred:”, e)

1 Like