简体   繁体   English

为什么我的 reqMktData 线程没有与 IB TWS Python API 一起运行?

[英]Why is my reqMktData thread not running with IB TWS Python API?

I can't figure out why my streamData thread is not running, while the my con_thread is running fine.我无法弄清楚为什么我的 streamData 线程没有运行,而我的 con_thread 运行良好。 I know from using this piece of code in larger scripts that they do, in fact, receive the market data just fine from using this function and thread, so I can't understand why the con_thread being alive is True, while the stream_thread being alive is False.我通过在更大的脚本中使用这段代码知道他们确实可以通过使用这个 function 和线程很好地接收市场数据,所以我不明白为什么 con_thread 活着是真的,而 stream_thread 活着是假的。 Any idea?任何想法?

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

class TradingApp(EWrapper, EClient):

    def __init__(self):
        EClient.__init__(self,self)

    def error(self, reqId, errorCode, errorString):
        print("Error. Id: ", reqId, " Code: ", errorCode, " Msg: ", errorString)

Underlying = Contract()
Underlying.localSymbol = "MESU2"
Underlying.secType = "FUT"
Underlying.currency = "USD"
Underlying.exchange = "GLOBEX"

def websocket_con():
    app.run()

def streamData():
    app.reqMarketDataType(2)
    app.reqMktData(3002, Underlying, "", False, False, [])

app = TradingApp()
app.connect("127.0.0.1", 7497, 3002)

time.sleep(1)

con_thread = threading.Thread(target=websocket_con, daemon=True)
con_thread.start()
time.sleep(1)

stream_thread = threading.Thread(target=streamData())
stream_thread.start()
time.sleep(1)

while True:
    print("conthread",con_thread.is_alive())
    print("streamthread",stream_thread.is_alive())
    time.sleep(1)

First of all please update how you are passing the function argument to the thread.首先,请更新您如何将 function 参数传递给线程。 No parenthesis after the function name. function 名称后没有括号。 You are passing the function as a parameter not executing the function.您将 function 作为不执行 function 的参数传递。

stream_thread = threading.Thread(target=streamData)

Secondly, why do you expect the stream_thread.is_alive() to return True?其次,你为什么期望 stream_thread.is_alive() 返回 True? The function is complete and the websocket connection has been established and therefore it returns false. function 已完成,websocket 连接已建立,因此返回 false。 It will return True only till the streamData function is incomplete.只有在流数据 function 不完整之前,它才会返回 True。 If you want to test it, add a line time.sleep(10) within your streamData function and you will see it returning True for the stream_thread for 10 seconds.如果您想测试它,请在您的 streamData function 中添加一行 time.sleep(10) ,您将看到它为 stream_thread 返回 True 10 秒。

However, please note that your streaming is happening in the background although your code is not handling the streaming data.但是,请注意,您的流式传输是在后台进行的,尽管您的代码并未处理流式传输数据。 You will need an appropriate EWrapper function in your TradingApp.您将需要在您的 TradingApp 中使用合适的 EWrapper function。 Something like below像下面的东西

def tickPrice(self, reqId, tickType, price, attrib):
    super().tickPrice(reqId, tickType, price, attrib)
    print("TickPrice. TickerId:", reqId, "tickType:", tickType, "Price:", price)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM