我已经看了我在这里的所有帮助,但它似乎不起作用,我在编程方面相对较新,并且将不胜感激。我需要能够将Apple的股票价格下载到变量中并打印。我正在使用互动经纪人TWS的演示版。
from ib.ext.Contract import Contract
from ib.opt import ibConnection, message
from time import sleep
# print all messages from TWS
def watcher(msg):
print msg
# show Bid and Ask quotes
def my_BidAsk(msg):
if msg.field == 1:
print ('%s:%s: bid: %s' % (contractTuple[0],
contractTuple[6], msg.price))
elif msg.field == 2:
print ('%s:%s: ask: %s' % (contractTuple[0], contractTuple[6], msg.price))
def makeStkContract(contractTuple):
newContract = Contract()
newContract.m_symbol = contractTuple[0]
newContract.m_secType = contractTuple[1]
newContract.m_exchange = contractTuple[2]
newContract.m_currency = contractTuple[3]
newContract.m_expiry = contractTuple[4]
newContract.m_strike = contractTuple[5]
newContract.m_right = contractTuple[6]
print ('Contract Values:%s,%s,%s,%s,%s,%s,%s:' % contractTuple)
return newContract
if __name__ == '__main__':
con = ibConnection()
con.registerAll(watcher)
showBidAskOnly = False # set False to see the raw messages
if showBidAskOnly:
con.unregister(watcher, message.tickSize, message.tickPrice,
message.tickString, message.tickOptionComputation)
con.register(my_BidAsk, message.tickPrice)
con.connect()
sleep(1)
tickId = 59
# Note: Option quotes will give an error if they aren't shown in TWS
contractTuple = ('AAPL', 'STK', 'SMART', 'USD', '', 0.0, '')
#contractTuple = ('QQQQ', 'OPT', 'SMART', 'USD', '20070921', 47.0, 'CALL')
#contractTuple = ('ES', 'FUT', 'GLOBEX', 'USD', '200709', 0.0, '')
#contractTuple = ('ES', 'FOP', 'GLOBEX', 'USD', '20070920', 1460.0, 'CALL')
#contractTuple = ('EUR', 'CASH', 'IDEALPRO', 'USD', '', 0.0, '')
stkContract = makeStkContract(contractTuple)
print ('* * * * REQUESTING MARKET DATA * * * *')
con.reqMktData(tickId, stkContract, 'AAPL', False)
sleep(15)
print ('* * * * CANCELING MARKET DATA * * * *')
con.cancelMktData(tickId)
sleep(1)
con.disconnect()
sleep(1)
这是我从ibpy中拥有的代码。
我假设您在粘贴代码时就弄乱了格式。
如果您收到错误回调,则可能看到了诸如"无效通用tick"之类的东西。您将" AAPL"放在指定所需的壁虱类型的位置。只需将这个空的tick滴留空。
con.reqMktData(tickId, stkContract, '', False)
我不确定演示使用的端口和ID,但是您可以在此处指定,如果不是7496,0(默认值)。
例如。con = ibConnection(port = 7497, clientId = 123)