我目前正在使用TWS Brokerage API
对于请求市场数据,它们具有异步函数调用。因此,例如,要获取股票的数据,您将调用reqMKtData(..),如下所示。
self.reqMktData(1003, ContractSamples.AAPLStock(), "", False, False, [])
然后,您将使用另一个名为tickPrice的函数异步接收价格数据。
def tickPrice(self, reqId: TickerId, tickType: TickType, price: float, attrib: TickAttrib):
super().tickPrice(reqId, tickType, price, attrib)
print("Tick Price. Ticker Id:", reqId, "tickType:", tickType, "Price:", price, "CanAutoExecute:", attrib.canAutoExecute, "PastLimit", attrib.pastLimit)
我想调用reqMktData,然后在下一行代码中获得股票价格。我该怎么做?
self.reqMktData(1003, ContractSamples.AAPLStock(), "", False, False, [])
aaplPrice = # Wait for tickPrice to execute, then assign the price value from tickPrice to aaplPrice