我正在尝试使用https://github.com/blampe/IbPy/
中找到的代码提取各种代码的历史市场数据在此存储库中,有一个如何在文件“fancy_marketdata.py”中请求市场数据的示例。但是,我使用以下代码建立连接时遇到问题:
from ib.opt import ibConnection, message
from ib.ext.Contract import Contract
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
con = ibConnection(port=7496,clientId=100)
con.registerAll(watcher)
showBidAskOnly = True # 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 = 1
contractTuple = ('QQQQ', 'STK', 'SMART', 'USD', '', 0.0, '')
stkContract = makeStkContract(contractTuple)
con.reqMktData(tickId, stkContract, '', False)
sleep(10)
con.cancelMktData(tickId)
sleep(1)
con.disconnect()
sleep(1)
当我到达“con.connect()”时,它会显示以下错误消息:
Server Version: 76
<error id=None, errorCode=None, errorMsg='Receiver' object has no attribute 'managedAccounts'>TWS Time at connection:20171225 13:38:01 EST
Exception in thread EReader:
Traceback (most recent call last):
File "/Users/XYZ/anaconda3/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/Users/XYZ/anaconda3/ib/IbPy-master/ib/ext/EReader.py", line 113, in run
self.m_parent.close()
File "/Users/XYZ/anaconda3/ib/IbPy-master/ib/ext/EClientSocket.py", line 1714, in close
self.wrapper().connectionClosed()
AttributeError: 'Receiver' object has no attribute 'connectionClosed'
是因为我使用的是python 3.6吗?我的TWS版本968.2h。请任何帮助将不胜感激!我试图提取市场数据的合约是CBOE和CME(分别是GXBT和BRR)用于比特币交易的两个新期货合约。
答案 0 :(得分:0)
您的代码没有错,只是确认您指定了合同元组。 这两个交易所最近建立了您要求数据的比特币合约 - - 它们可能没有一切顺利运行 - - 该对象可能是指您的帐户类型的具体内容。
根据我的经验,我发现使用了
中的Anaconda Python发行版https://www.anaconda.com/download/
提供最一致的安装。一举提供您所需要的一切......