IB_Insync - 只有一个订单自动提交至 TWS;后续订单不通过

问题描述 投票:0回答:1

我一直在使用 NGROK、Flask 和 Redis 将 IB_Insync、Asyncio、bot 接口引入 TWS。

第一个 Webhook 有效负载完美传输到 TWS 并且下订单,但通过此 IB_Insync 机器人传输的所有后续订单都不会通过。没有显示任何错误,并且 Webhook 有效负载已通过 NGROK 成功传输并显示在 Asyncio 消息显示屏中 - 所以我知道已收到数据警报。当我停止脚本并重新启动时 - TWS 接受传输的第一个订单,但同样不接受以下订单。

我最初认为这可能是由于 TWS 需要唯一的 orderId,因此尝试了多种方法使用 IB_insync reqID 方法来做到这一点;包括 ib.client.getReqId(),我什至尝试导入 IB_API 并使用 EWrpper nextValidID 等,但无法让它在异步函数中工作。

尽管如此......通过对 StackOverFlow、IB_Insync 文档和 insync@groups 论坛的广泛研究,其中讨论了类似的问题;我偶然发现了 Erwald deWit(IB_Insync 的创建者)的帖子,其中明确表示 IB_Insync 在下订单时会自动发出 orderID。进一步的研究已确定为每个 IB_Insync 订单分配唯一的 orderID。这解释了为什么在同步文档中没有任何内容(我可以找到)如何在每个新订单上调用/创建唯一的 orderId,因此为什么脚本不创建唯一的订单号可能不是问题。

这可能与异步函数中下的订单有关吗? (我已经尝试删除,但我的新(尽管快速发展)编码知识无法让它发挥作用)如果这是一个可能的解决方案,有人可以帮助指导我如何将合同和订单语法放置在异步函数之外并仍然接收消息输入数据?

我希望有人能够看到代码中的哪些位置可能会阻止 TWS 在第一次下达订单后识别/接受订单。

有人经历过类似的挑战并找到解决这个问题的方法吗?

谢谢 - 我非常感谢您对此的关注和帮助

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import Order
import redis, json
from ib_insync import *
import asyncio, time, random

class TradingApp(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)

    def nextValidId(self, orderId):
        super().nextValidId(orderId)
        self.nextValidId = orderId
        print("NextValidId:", orderId)


# connect to Interactive Brokers 
ib = IB()
ib.connectAsync('127.0.0.1', 7497, clientId=1)

# connect to Redis and subscribe to tradingview messages
r = redis.Redis(host='localhost', port=6379, db=0)
p = r.pubsub()
p.subscribe('tradingview')

async def check_messages():
   print(f"{time.time()} - checking for tradingview webhook messages")
    message = p.get_message()
    if message is not None and message['type'] == 'message':
        print(message)

        message_data = json.loads(message['data'])

        future = Future(message_data ['ticker'], message_data['contractExpDate'], 'CME') # Future(message_data['ticker'], message_data['secId'], message_data['exchange']) 
        order = LimitOrder(message_data['strategy']['order_action'], message_data['strategy']['order_contracts'], message_data['strategy']['limit_price'])
        #order.orderId = ib.client.getReqId() # get new orderID
        #order = MarketOrder(message_data['strategy']['order_action'], message_data['strategy']['order_contracts'])
        trade = ib.placeOrder(future, order)

async def run_periodically(interval, periodic_function):
    while True:
        await asyncio.gather(asyncio.sleep(interval), periodic_function())

asyncio.run(run_periodically(1, check_messages))

ib.run()
python tws ib-api ib-insync
1个回答
0
投票

今天我也遇到了同样的问题,一个订单下到TWS后,后面的订单甚至都无法到达TWS,所以这实际上是IB的问题。 我发现这个答案作为解决方法:需要使用 ib.sleep() 使用 ib_insync 发送多个订单 到目前为止,这个 ib.sleep() 确实工作得很好。我不知道为什么对此的讨论如此之少,我想如果 IB 不能正确支持多个订单,这将是一个大问题。 欢迎任何建议,谢谢

© www.soinside.com 2019 - 2024. All rights reserved.