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

时间:2019-07-19 12:56:58

标签: python api interactive-brokers tws

通过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:\Users\Erix Liechtenstein\AppData\Local\Programs\Python\Python37-32\python.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

2 个答案:

答案 0 :(得分:0)

问题是您的客户端线程没有在等待服务器的响应。也就是说,调用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()

祝你好运!

答案 1 :(得分:0)

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

请注意,第一个代码中的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", 7496, 0)
    app.run()

if __name__ == "__main__":
    main()