无法通过IBKR TWS Python API下载数据



通过IBKR TWS Python API运行简单的数据下载请求以下教程之一:https://www.youtube.com/watch?time_continue=1065&v=GmTPDzcko6k

调试会产生app.reqContractDetails(1, contract)返回None

有关此问题的信息似乎有限

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract

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

def error(self, reqId, errorCode, errorString):
    print("Error: ", reqId, " ", errorCode, " ", errorString)

def contractDetails(self, reqId, contractDetails):
    print("contractDetails: ", reqId, " ", contractDetails)

def main():
    app = TestApp()
    app.connect("127.0.0.1", 7496, 0)
    contract = Contract()
    contract.symbol = "BYND"
    contract.secType = "STK"
    contract.exchange = "SMART"
    contract.currency = "USD"
    contract.primaryExchange = "NASDAQ"
    print (app.reqContractDetails(1, contract))
    app.run()
    app.reqContractDetails(2, contract)

if __name__ == "__main__":
    main()

实际:

"C:UsersErix LiechtensteinAppDataLocalProgramsPythonPython37-32python.exe" "C:/TWS API/source/pythonclient/HelloWorld.py"
None
ERROR -1 2104 Market data farm connection is OK:eufarm
ERROR -1 2104 Market data farm connection is OK:cashfarm
ERROR -1 2104 Market data farm connection is OK:usopt
ERROR -1 2104 Market data farm connection is OK:usfarm
ERROR -1 2106 HMDS data farm connection is OK:ushmds

更新:

根据布莱恩的建议尝试了以下内容 - 仍然没有:(什么做:

class TestApp(EWrapper, EClient):
    def __init__(self, addr, port, client_id):
       EWrapper.__init__(self)
       EClient.__init__(self, self)
       self.connect(addr, port, client_id)
       thread = Thread(target=self.run)
       thread.start()

def error(self, reqId, errorCode, errorString):
    print("Error: ", reqId, " ", errorCode, " ", errorString)

def contractDetails(self, reqId, contractDetails):
    print("contractDetails: ", reqId, " ", contractDetails)

def main():
    app = TestApp("127.0.0.1", 4002, 0)
    app.run()
    contract = Contract()
    contract.symbol = "BYND"
    contract.secType = "STK"
    contract.exchange = "SMART"
    contract.currency = "USD"
    contract.primaryExchange = "NASDAQ"
    app.reqContractDetails(1, contract)
    time.sleep(3)
    app.disconnect()
if __name__ == "__main__":
    main()

问题是您的客户端线程没有等待服务器的响应。也就是说,线程调用reqContractDetails需要等到运行contractDetails线程完成。

延迟主线程的一种简单方法是调用 time.sleep 。下面的代码创建一个协定,并将线程设置为休眠三秒钟。

contract = Contract()
...
client.reqContractDetails(1, contract)
time.sleep(3)
client.disconnect()

如果这不能解决您的问题,您可能需要尝试从构造函数启动线程:

class TestApp(EWrapper, EClient):
    def __init__(self, addr, port, client_id):
       EWrapper.__init__(self)
       EClient.__init__(self, self)
       # Connect and launch thread
       self.connect(addr, port, client_id)
       thread = Thread(target=self.run)
       thread.start()

祝你好运!

我不

认为这是我的建议。 这是链接问题中的确切代码,带有一些注释。

请注意,第一个代码中的print (app.reqContractDetails(1, contract))是打印不返回任何内容的方法调用。 该程序是异步的,这意味着您请求某些内容,稍后将返回到回调方法,在这种情况下,reqContractDetails稍后将数据返回给contractDetails

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import *
from ibapi.contract import *
from ContractSamples import ContractSamples

class TestApp(EClient, EWrapper):
    def __init__(self):
        EClient.__init__(self, self)
    def nextValidId(self, orderId:int):
        print("id", orderId)
        # this is where IB suggest starting your interaction with the API 
        # since the connection is established
        contract = Contract()
        contract.symbol = "IBKR"
        contract.secType = "STK"
        contract.exchange = "SMART"
        contract.currency = "USD"
        contract.primaryExchange = "NASDAQ"
        # now you can request data and wait for it in the other method
        self.reqContractDetails(10, contract)
    def error(self, reqId:TickerId, errorCode:int, errorString:str):
        print("Error: ", reqId, "", errorCode, "", errorString)
    def contractDetails(self, reqId:int, contractDetails:ContractDetails):
        # when IB's servers have time they will send the details.
        print("contractDetail: ", reqId, " ", contractDetails)
    def contractDetailsEnd(self, reqId:int):
        # when all details have been sent you will get this callback
        print("end, disconnecting")
        # so may as well disconnect so the socket doesn't hang
        self.disconnect()
def main():
    app = TestApp()
    app.connect("127.0.0.1", 4002, 0) #7496, 0)
    app.run()
if __name__ == "__main__":
    main()

相关内容

  • 没有找到相关文章

最新更新