正在TWS API中请求nextOrderID不工作

2024-06-11 16:29:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用python通过TWSAPI下订单。我的问题是获取下一个有效的订单ID

以下是我正在使用的:

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import TickerId
from ibapi import contract, order, common
from threading import Thread

class ib_class(EWrapper, EClient):

    def __init__(self, addr, port, client_id):
        EClient.__init__(self, self)
        self.connect(addr, port, client_id) # Connect to TWS
        thread = Thread(target=self.run, daemon=True)  # Launch the client thread
        thread.start()

    def error(self, reqId:TickerId, errorCode:int, errorString:str):
        if reqId > -1:
            print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)

    def nextValidId(self, orderId: int):
        self.nextValidId = orderId

ib_api = ib_class("127.0.0.1", 7496, 1)

orderID = ib_api.nextValidId(0)

这给了我:

TypeError: 'int' object is not callable

Tags: from订单importselfclientdefthreadclass
1条回答
网友
1楼 · 发布于 2024-06-11 16:29:20

nextValidId方法是一种包装器方法。从客户端,您需要调用reqIds来获取订单ID

ib_api.reqIds(-1)

reqIds的参数无关紧要。而且,它没有返回值。相反,reqIds向IB发送消息,当收到响应时,将调用包装器的nextValidId方法

Algorithmic Trading with Interactive Brokers

相关问题 更多 >