我有兴趣将 ibpy 与 Interactive Brokers API 一起使用,以获取给定 100 只股票的实时报价数据。下面的代码来自网络上的示例,适用于一只股票。有人能告诉我如何同时处理 100 只股票吗?
Python脚本:
from ib.opt import ibConnection, message
from ib.ext.Contract import Contract
from time import sleep
def my_callback_handler(msg):
inside_mkt_bid = ''
inside_mkt_ask = ''
if msg.field == 1:
inside_mkt_bid = msg.price
print 'bid', inside_mkt_bid
elif msg.field == 2:
inside_mkt_ask = msg.price
print 'ask', inside_mkt_ask
tws = ibConnection()
tws.register(my_callback_handler, message.tickSize, message.tickPrice)
tws.connect()
c = Contract()
c.m_symbol = "DATA"
c.m_secType = "STK"
c.m_exchange = "SMART"
c.m_currency = "USD"
tws.reqMktData(1,c,"",False)
sleep(25)
print 'All done'
tws.disconnect()
命令行输出:
Server Version: 76
TWS Time at connection:20150508 13:42:02 EST
bid 111.42
ask 111.5
bid 111.43
bid 111.44
bid 111.42
bid 111.38
bid 111.32
ask 111.44
All done