我无论如何都找不到识别特定股票流以便正确取消它的方法..?我确信这很简单,但我很蠢。
在普通 IB API 中,您可以指定一个 tickid,但似乎 Insync 正在以某种方式处理它。问题是我需要取消市场数据流并启动它们。 这是我的代码:
from ib_insync import *
# Connect to IB Gateway or TWS
ib = IB()
ib.connect('127.0.0.1', 7496, clientId=5)
# Define the contract for the index
instr = Stock(symbol='SPY', exchange='SMART', currency='USD')
# Request market data
ticker = ib.reqMktData(instr, snapshot=False, regulatorySnapshot=False)
# Wait for data to populate
ib.sleep(1)
# Access the tickId from the ticker object
tick_id = ticker.contract.conId
print(f"Contract ID (conId): {tick_id}")
# Print current market data
print(f"Last price: {ticker.last}")
print(f"Bid: {ticker.bid}")
print(f"Ask: {ticker.ask}")
# Cancel the market data stream
ib.cancelMktData(tick_id)
# Disconnect
ib.disconnect()
我收到错误:cancelMktData:找不到合约 0 的 reqId。
我尝试添加一个ticid:
ticker = ib.reqMktData(stock, '', False, False, TICKID)
但这似乎也不起作用。
将
ib.cancelMktData(tick_id)
更改为 ib.cancelMktData(instr)
基于ib_insync源代码:
def cancelMktData(self, contract: Contract):
"""
Unsubscribe from realtime streaming tick data.
Args:
contract: The exact contract object that was used to
subscribe with.
"""
ticker = self.ticker(contract)
reqId = self.wrapper.endTicker(ticker, 'mktData') if ticker else 0
if reqId:
self.client.cancelMktData(reqId)
else:
self._logger.error(
'cancelMktData: ' f'No reqId found for contract {contract}')