无法使用IBPY Python API连接到IB TWS



我正在尝试使用我从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。请任何帮助将不胜感激!我试图提取市场数据的合同是比特币交易的两项新期货合约(分别为GXBT和BRR)。

您的代码没有错,只需确认您指定合同元组即可。这两个交易所最近都设置了您要求数据的比特币合同 - 它们可能还没有所有功能顺利进行 - 该对象可能是指有关您的帐户类型的特定内容。

根据我的经验,我发现使用

的anaconda python分布

https://www.anaconda.com/download/

提供最一致的安装。提供您需要的一切,一口气...

最新更新