简体   繁体   English

互动经纪商 python API 每个应用实例仅传输一个订单

[英]Interactive brokers python API only trasnmiting a single order per app instance

I am building an app to place orders through the Python API an am having an issue with the consistency of order transmission where only one order can be received and executed per app instance.我正在构建一个应用程序来通过 Python API 下订单,我遇到了订单传输的一致性问题,每个应用程序实例只能接收和执行一个订单。

As an example if I run the below code it will execute and transmit a sample order indefinitely, no matter how many times I run the script.例如,如果我运行以下代码,无论我运行脚本多少次,它都会无限期地执行和传输样本订单。

from ib_insync import *

# connect to Interactive Brokers 
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=3) #4002 / 7497

stock = Stock("AAPL", 'SMART', 'USD')

order = MarketOrder('BUY', 10)
trade = ib.placeOrder(stock, order)
print('Done')

However if I define the order part as a function and run it, it will only transmit an order once no matter how many times I call the function.但是,如果我将订单部分定义为 function 并运行它,无论我调用多少次 function,它都只会发送一次订单。 If I restart the script then I can send another order with the same function but again only once.如果我重新启动脚本,那么我可以发送另一个具有相同 function 的订单,但只能再次发送一次。

Is there a way around this as I want to be able to send other orders without having to restart the app.有没有办法解决这个问题,因为我希望能够发送其他订单而无需重新启动应用程序。

IB requires a unique, ascending value, orderId to accompany each order. IB 需要一个唯一的升序值 orderId 来伴随每个订单。 One simple method of accomplishing this is to use a timestamp, as follows:完成此操作的一种简单方法是使用时间戳,如下所示:

stock = Stock("AAPL", 'SMART', 'USD')

order = MarketOrder('BUY', 10)

curr_dt = datetime.now()
order.orderId = int(round(curr_dt.timestamp()))

trade = ib.placeOrder(stock, order)

Note that this will not work if at least one second hasn't elapsed between orders;请注意,如果订单之间没有经过至少一秒钟,这将不起作用; otherwise the second order will get the same timestamp and orderId.否则第二个订单将获得相同的时间戳和 orderId。 Should this occur, you'll have to find an alternate way to generate the orderId.如果发生这种情况,您必须找到另一种方法来生成 orderId。

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

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