1

我已经查看了这里所有对我有帮助的东西,但它似乎不起作用,我在编程方面相对较新,任何回复都将不胜感激。我需要能够将 Apple 的股票价格下载到变量中并打印出来。我正在使用盈透证券交易平台的演示版。

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 获得的代码。

4

1 回答 1

1

我假设您在粘贴代码时只是弄乱了格式。否则它永远不会起作用。

如果您收到错误回调,您可能会看到“无效的通用刻度”之类的内容。你把'AAPL'放在你指定你想要的刻度类型的位置。只需将其留空以获取正常滴答声。

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

我不确定演示使用的端口和 ID,但如果不是 7496, 0(默认值),您可以在此处指定。

例如。con = ibConnection(port = 7497, clientId = 123)

于 2016-12-15T04:31:29.323 回答