如何正确检查币安订单的订单状态

时间:2021-02-20 08:59:51

标签: python api python-requests binance

我正在尝试创建一个卖单,然后不断检查它是否已完成,但在循环中进行一两次迭代后,它显示订单状态已完成,而订单实际上或有时尚未完成它说订单不存在。

我的代码有问题还是有更好的方法?


# SELL
try:
    #LIMITSELL

    client.order_limit_sell(symbol=pair,quantity=quantity,price=sellPrice)
    orderId=client.get_all_orders(symbol=pair,limit=1)[0]['orderId']
    print('Sell order placed at {}\n'.format(sellPrice))

    while True:
        openOrders = client.get_all_orders(symbol=pair,limit=1)[0]
        if openOrders['status']=='FILLED':
            print("Sold: {} at {}".format(quantity,sellPrice))
        
            exit(0)
        print(".")

我也尝试通过使用 orderId 来使用 client.get_order(symbol=pair,orderId=orderId),但它仍然做同样的事情。

1 个答案:

答案 0 :(得分:1)

order = client.order_limit_sell(symbol=pair,quantity=quantity,price=sellPrice)
orderId = order["orderId"]
print('Sell order placed at {}\n'.format(sellPrice))
    while True:
        currentOrder = client.get_order(symbol=pair,orderId=orderId)
        if currentOrder['status']=='FILLED':
            print("Sold: {} at {}".format(quantity,sellPrice))
            break
        print(".")

更好的方法是使用 Python 中的内置线程模块创建另一个线程。