IbPy: I Can't Get Market Data

左心房为你撑大大i 提交于 2019-12-11 06:46:28

问题


I have looked at everything in here that my help me, but it doesn't seem to work, I am relatively new at programming, and any responses will be greatly appreciated. I need to be able to download the stock price of Apple into a variable and print it. I am using the demo version of Interactive Brokers' 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)

This is the code I have from IbPy.


回答1:


I'm assuming you just messed up the formatting when pasting the code. It would never work otherwise.

If you were getting error callbacks you may have seen something like "invalid generic ticks". You put 'AAPL' in the position where you specify which type of ticks you want. Just leave this empty for normal ticks.

con.reqMktData(tickId, stkContract, '', False)

I'm not sure what port and id the demo uses but you can specify here if not 7496, 0 (the defaults).

eg. con = ibConnection(port = 7497, clientId = 123)



来源:https://stackoverflow.com/questions/41119411/ibpy-i-cant-get-market-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!