Binance API - Buying and selling functions - python - pythonbinance lib

Hi people,

has anyone made proper working functions for buying and selling a pair like “DOGEUSDT” or any other pair (margin trading),
I have tried my best but I end up with different errors like - Lot size failure, Insufficient funds, or the most commonly known price filter errors of this API

the functions I came up with are below as follows, can someone tell me what am I missing

  1. This function is to come up with buy/short quantity and it works pretty fairly

     def buy_quantity(symbol,asset):
         a = 0
         while a < 3:
             try:
                 bal = pd.DataFrame(client.get_margin_account()['userAssets'])
                 bal = bal[bal['asset']==asset]
                 bal = float(bal['free']) * 0.90
                 break
            except:
                 print("can't extract asset value..retrying")
                 a+=1
         price = float(client.get_recent_trades(symbol=symbol)[0]['price'])    
         stepSize = float(client.get_symbol_info(symbol)['filters'][2]['stepSize'])
         precision = int(round(-math.log(stepSize, 10), 0)) 
         quantity = (bal / price)*0.9995 # Trading fee taken in consideration 
         quantity = round(quantity,precision)
         return quantity
    
  2. This function is to cover the short, it basically tries to give you the borrowed quantity

    def sell_quantity1(asset):
         a = 0
         while a < 3:
             try:
                 order = pd.DataFrame(client.get_margin_account()['userAssets'])
                 order = order[order['asset']==asset]
                 order = float(order['borrowed'])
                 break
             except:
                 print("can't extract borrowed order book...retrying")
                 a+=1
         order = round(order,5)
         return order
    
  3. This function is to sell the long position

     def sell_quantity2(asset):
         a = 0
         while a < 3:
             try:
                 order = pd.DataFrame(client.get_margin_account()['userAssets'])
                 order = order[order['asset']==asset]
                 order = float(order['free'])
                 break
             except:
                 print("can't extract order book...retrying")
                 a+=1
         order = round(order,5)
         return order
    

I think the functions 2 & 3 should work properly like the function 1 but they don’t, some or the other error pops out
Lot size failure, Insufficient funds, or something else, what am I missing here can someone please help me I am struggling from a couple of days

Thanks in advance

Hi.
Based on the code and the errors you faced, here are some ideas to help you prevent from those errors.(Let me assume there is no other preprocessing added on your side.)

  1. Assume that there is no huge and sudden change on the market price, to fetch the recent trade price can avoid the price filter error in a short time. Though I’m not sure how you use those functions, this is done in your 1st function but it’s not in your 2nd and 3rd ones.
    price = float(client.get_recent_trades(symbol=symbol)[0]['price'])
    
  2. This part is to cope with the lot size. Please check if your fixed rounding factor is valid or not. (I assume that the asset in your 2nd and 3rd function represents the base asset.)
    stepSize = float(client.get_symbol_info(symbol)['filters'][2]['stepSize'])
    precision = int(round(-math.log(stepSize, 10), 0)) 
    quantity = (bal / price)*0.9995 # Trading fee taken in consideration 
    quantity = round(quantity,precision)
    

Another idea is that as you are doing the margin trade, you have to know how much you can borrow and how much you already take. You can check this endpoint to know your quota.