简体   繁体   中英

Unable to connect to IB TWS using IbPy python api

I am trying to pull historical market data for various tickers using the code I found from https://github.com/blampe/IbPy/

Within this repository, there is an example of how to request market data in file "fancy_marketdata.py". However, I am having trouble establishing a connection using following code :

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)

When I get to "con.connect()", it gives the following error message:

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'

Is it because I am using python 3.6? My TWS version 968.2h. Please any help would be greatly appreciated! The contract that I am trying to pull market data for is the two new futures contracts by CBOE and CME (GXBT and BRR, respectively) for bitcoin trading.

Nothing wrong with your code, just confirm you are specifying the contract tuple. Both of these exchanges recently set up the Bitcoin contracts you are requesting data for - - they may not have everything functioning smoothly yet - - the object is probably referring to something specific regarding your account type.

Based on my experience, I have found using the Anaconda Python distribution from

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

provides the most consistent installs. Provides everything the way you need it in one fell swoop...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM