1
$\begingroup$

I am having problems retrieving 14 days historical data from IB. I recently made the switch from MATLAB to Python so I have a lot of catching up to do. Where do I go wrong? All help is appreciated.

import sys

import pandas as pd

from time import sleep, strftime

from ib.ext.Contract import Contract



#Make connection
conn = Connection.create(port=7496, clientId=999)
conn.connect()

def var2(msg):
    print(msg)

conn.register(var2, message.historicalData)
def var3(msg):
    print(msg)


if __name__ == '__main__':
    #conn = ibConnection()
    conn.register(var2, 'UpdateAccountValue')
    conn.register(var3, message.tickSize, message.tickPrice)
    conn.connect()

    def inner():

        qqqq = Contract()
        qqqq.m_secType = "STK" 
        qqqq.m_symbol = "ABN"
        qqqq.m_currency = "EUR"
        qqqq.m_exchange = "AEB"
        endtime = strftime('%Y%m%d %H:%M:%S')
        conn.reqHistoricalData(1,qqqq,endtime,"14 D","1 min","BID",1,1)
        data = reqHistoricalData


conn.disconnect()
$\endgroup$
  • $\begingroup$ You didn't call the inner() function. $\endgroup$ – hcheung Aug 2 '17 at 1:41
  • $\begingroup$ Can you give an example? $\endgroup$ – Bondeaux Aug 2 '17 at 11:04
  • $\begingroup$ You defined a function def inner() in your code, but you never call the function, put inner() on the line right after conn.connect() so it call the inner() function. If you still have error, post your error code. $\endgroup$ – hcheung Aug 3 '17 at 11:15
  • $\begingroup$ Your code has several mistake other than the one that I mentioned above. I could clean-up code for you, however I don't use IbPy anymore. IbPy is a third-party implementation that provided very limited documentation. If you are new to IB python programming, I would suggest to switch to Interactive Broker native IB API software package and it has excellent API documentation. $\endgroup$ – hcheung Aug 4 '17 at 1:49
0
$\begingroup$

You have several mistakes in your code, one obvious is having an inner() function without calling it. Here is an example that I clean up based on your code. I added a few comments in my code the explain what's going on. Also see my comments on your original post.

from time import sleep, strftime
from ib.ext.Contract import Contract


def historical_data_handler(msg):
    # The response data callback function
    # print (msg.reqId, msg.date, msg.open, msg.close, msg.high, msg.low)
    print(msg)


if __name__ == '__main__':
    # Establish IB connection, make sure you have the correct port, clientId
    conn = ibConnection(host='127.0.0.1', port=7496, clientId=999)

    # Register the response callback function and type of data to be returned
    conn.register(historical_data_handler, message.historicalData)
    conn.connect()

    # Establish a Contract object and the params for the request
    req = Contract()
    req.m_secType = "STK" 
    req.m_symbol = "ABN"
    req.m_currency = "EUR"
    req.m_exchange = "AEB"
    endtime = strftime('%Y%m%d %H:%M:%S')
    conn.reqHistoricalData(1,req,endtime,"14 D","1 min","BID",1,1)

    # Make sure the connection don't get disconnected prior the response data return
    sleep(5)
    conn.disconnect()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.