Search code examples
pythonapibinance

How to make sure a trade is executed with binance python


I need to sell my coins when the algorithm takes the decision. But if i create an order of sell and then the price drops, obviously no one will buy at that price. Therefore I need to check if the order is filled and if it is not I need to redo the order. (Maybe there is a better solution to that I'm open to all kinds of answers)

Here is my try at it : (it will sometimes do the upper scenario and execute the code after this part with an order which was not executed yet which will make the program crash).

        #sellOrder
        sellOrder = client.create_order(symbol = hparams['MARKET_STRING'], side = 'SELL', type = 'MARKET', quantity = hparams['TRADE_TOKEN_AMOUNT'])
        print("Selling ...")
        print(sellOrder)
        sellOrderID = sellOrder['orderId']
        time.sleep(30)
        soldCheck = True
        while soldCheck:
            currentOrder = client.get_order(symbol = hparams['MARKET_STRING'], orderId = sellOrderID)
            if currentOrder['status'] == 'FILLED':
                soldCheck = False
            else:
                #cancel orber 
                client.cancel_order(symbol = hparams['MARKET_STRING'], orderId = sellOrderID)
                time.sleep(1)
                sellOrder = client.create_order(symbol = hparams['MARKET_STRING'], side = 'SELL', type = 'MARKET', quantity = hparams['TRADE_TOKEN_AMOUNT'])
                sellOrderID = sellOrder['orderId']
                time.sleep(5)   

Solution

  • It looks to me like you using contradicting logic in your while loop.

    If I've understood correctly, you've run into an issue with executing the code that is within the loop?

    The syntax while SoldCheck: will run a loop while the boolean variable SoldCheck is True - you have set this statically to False, so the loop will not run.

    Try this:

    #sellingorder
    sellOrder = client.create_order(symbol = "DOGEEUR", side = 'SELL', type = 'MARKET', quantity = float(50.0))
    print("Selling ...")
    print(sellOrder)
    sellOrderID = sellOrder['orderId']
    
    time.sleep(5) #sleeping while it sells
    #Check order was sold TODO
    soldCheck = True #true = not sold TODO
    while soldCheck:
        currentOrder = client.get_order(symbol = "DOGEEUR", orderId = sellOrderID)
        if currentOrder['status'] == "FILLED":
            soldCheck = False #sold
        else:
            #cancel order
            client.cancel_order(symbol = "DOGEEUR", orderId = sellOrderID)
            time.sleep(1)
            sellOrder = client.create_order(symbol = "DOGEEUR", side = 'SELL', type = 'MARKET', quantity = float(50.0))
            sellOrderID = sellOrder['orderId']
            time.sleep(5)
    

    Then, when the if condition is satisfied (i.e. status filled) then you are reverting the SoldCheck flag to False to stop the loop as you no longer need to cancel the order.

    If the above solved your issue, please can you accept the answer? If i think of a better way to do it i'll add to here, but the above should solve your problem.

    EDIT: You are attempting to place a MARKET order, which by definition is not affected by price fluctuations in the same way as LIMIT. A Market order means that you TAKE the best price the market is willing to give you - this trade should be filled immediately.

    If error is within the given function (hard to say without detail on how this function is called) I believe it is one of:

    1. status of currentOrder when it is a market order is not set to 'Filled' and as such the loop is never exiting.
    2. the cancel order call is not cancelling the original order as it is already filled, but you dont check if the order got successfully cancelled before attempting to place another.

    To help understand the issue, could you please provide the output from original create_order call, get_order call and cancel_order?

    Thanks