簡體   English   中英

無法使用IbPy Python API連接到IB TWS

[英]Unable to connect to IB TWS using IbPy python api

我正在嘗試使用從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)。

您的代碼沒有錯,只需確認您指定了合同元組即可。 這兩個交易所最近都建立了您要為其提供數據的比特幣合約--它們可能還不能使所有功能正常運行--該對象可能是在指與您的賬戶類型有關的特定內容。

根據我的經驗,我發現使用了Anaconda Python發行版,

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

提供最一致的安裝。 一口氣提供您所需的一切...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM