简体   繁体   English

如何确保使用币安 python 执行交易

[英]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)   

It looks to me like you using contradicting logic in your while loop.在我看来,您在 while 循环中使用了矛盾的逻辑。

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.语法while SoldCheck:将在布尔变量 SoldCheck 为 True 时运行循环 - 您已将其静态设置为 False,因此循环将不会运行。

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 (ie 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 条件(即状态已填充)时,您将 SoldCheck 标志恢复为 False 以停止循环,因为您不再需要取消订单。

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.编辑:您正在尝试下一个 MARKET 订单,根据定义,该订单与 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. currentOrder 是市价单时的状态未设置为“已完成”,因此循环永远不会退出。
  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?为了帮助理解这个问题,您能否提供原始 create_order 调用、get_order 调用和 cancel_order 的输出?

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM