2

我目前正在使用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
4

0 回答 0