第二次运行时,Python IBAPI reqContractDetails将不会返回结果 [英] Python IBAPI reqContractDetails won't return result when run second time

查看:162
本文介绍了第二次运行时,Python IBAPI reqContractDetails将不会返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想请求合同详细信息,并且在我第一次运行代码时它有效,但是当我再次按下run时,它将不会返回任何内容.如果我退出TWS和pycharm,然后重试我是python的新手,并且不了解它的整体工作原理,它将起作用.

I want to request contract details and it worked the first time I run the code, but when I press run again, it won't return anything. It will work if I quit TWS and pycharm and try again I am new to python and don't understand how it works overall, pls help.

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 error(self, reqId:TickerId, errorCode:int, errorString:str):
        print("Error: ", reqId, "", errorCode, "", errorString)

    def contractDetails(self, reqId:int, contractDetails:ContractDetails):
        print("contractDetail: ", reqId, " ", contractDetails)


def main():
    app = TestApp()

    app.connect("127.0.0.1", 7496, 0)

    contract = Contract()
    contract.symbol = "AAPL"
    contract.secType = "STK"
    contract.exchange = "SMART"
    contract.currency = "USD"
    contract.primaryExchange = "NASDAQ"

    app.reqContractDetails(10, contract)

    app.run()


if __name__ == "__main__":
    main()

没有错误消息,只是没有结果:进程已完成,退出代码为0".

No error msg, just no result: "Process finished with exit code 0".

推荐答案

app.run()启动从套接字读取的线程.您的程序永远不会停止运行,因此始终连接到TWS.尝试单击TWS中的数据菜单按钮.它显示您的所有连接.您将看到客户端0保持连接状态.显然,关闭pyCharm会杀死该程序.

app.run() starts a thread to read from the socket. Your program never stops running so is always connected to TWS. Try clicking on the data menu button in TWS. It shows all your connections. You will see client 0 stays connected. Obviously closing pyCharm kills the program.

在程序准备就绪之前,您还需要合同细节.您应该等待nextValidId,然后发送请求.接收到数据后,您可以停止程序,它将释放clientId供以后的连接使用.这是我更改程序的方法.

You also request the contract details before the program is ready. You should wait for nextValidId and then send the request. When the data has been recieved, then you can stop your program and it will free up the clientId for future connections. Here is how I would change your program.

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)
        contract = Contract()
        contract.symbol = "IBKR"
        contract.secType = "STK"
        contract.exchange = "SMART"
        contract.currency = "USD"
        contract.primaryExchange = "NASDAQ"

        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):
        print("contractDetail: ", reqId, " ", contractDetails)

    def contractDetailsEnd(self, reqId:int):
        print("end, disconnecting")
        self.disconnect()

def main():
    app = TestApp()

    app.connect("127.0.0.1", 7496, 0)
    app.run()

if __name__ == "__main__":
    main()

这篇关于第二次运行时,Python IBAPI reqContractDetails将不会返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆