jeudi 2 septembre 2021

Python Binance market order issues

Below I will explain my issue of incorrectly sending orders to Binance via my algorithm which I would be grateful for any help on.

I have created a model to send market buy and market sell orders to Binance based on my models output. I have a dataframe column named action, with the latest (bottom of the column) value being used as a signal to send orders to Binance. The action column can either take the form of 1 (buy order) -1 (sell order) or 0 (do nothing). It is a impossibility that the action column can have two 1's or two -1's back to back. There will always be a buy order followed by a sell order (possibly separated by 'do nothing orders'). I have checked the action column in the dataframe and can confirm that there are never any instances where this rule is broken.

The issue I have is that when I run my program in a loop I often get scenarios where the program will (try to) send multiple buy orders back to back without a sell order splitting the orders up, or vice versa where the program sends multiple sell orders back to back without a buy order separating them out. Based on the values in the action column this is not possible, so I suspect my amateur Python abilities have failed me in a likely obvious way to more tenured programmers.

I would be very grateful for any suggestions as to how I could resolve this issue and ensure that my market orders are being sent to the exchange as per the models logic.

def bot():
    
    while True:
        tick = datetime.datetime.now()
        print(tick)
    
# Import the data from Binance. Final outcome is data points for closing price only in a data frame. 

        candles = client.get_historical_klines("ADAUSDT", Client.KLINE_INTERVAL_1MINUTE, "1 day→ago UTC")
        data = pd.DataFrame({'close':np.asarray(candles)[:,4]})
        data['close'] = data['close'].astype(float)

# I have removed the model logic though the model will create a 1, -1 or 0 depending on what action should be taken.
# This action is located in the 'action' column seen below. 


        data['action'] = data['position_signal'].diff()

        action_final = float(data['action'][-1:])

        close = float(data['close'][-1:])

        balancebuy = float(client.get_asset_balance(asset='ADA', recvWindow=10000)['free'])
        balancesell = float(client.get_asset_balance(asset='USDT', recvWindow=10000)['free'])
        maxBuy = round(balancesell / close * .99)
        
        if action_final ==1:
            client.create_order(
                symbol='ADAUSDT',
                side=client.SIDE_BUY,
                type=client.ORDER_TYPE_MARKET,
                quantity=maxBuy,
                recvWindow=10000) 
            print('Buy.')


        elif action_final ==-1:
            client.create_order(
                symbol='ADAUSDT',
                side=client.SIDE_SELL,
                type=client.ORDER_TYPE_MARKET,
                quantity=round(balancebuy - 0.1),
                recvWindow=10000) 
            print('Sell.')


        else:
            print('No trade.') 
            
        time.sleep(60.0 - ((time.time() - starttime) % 60.0))
        
bot()

My initial thoughts of what my issue could be (which I haven't yet been able to conclude fully)are;

  • Poor structure regarding my if statement
  • Incorrectly assigning "action" which I pass through my if statement
  • Something more Binance specific in regards to sending orders to the exchange

Once again I would be very grateful for any help or suggestions as I have been stuck on this question for a considerable amount of time! Cheers!

Jack

Aucun commentaire:

Enregistrer un commentaire