How to get historical data from IB using IBridgePy?

IbridgePy Interactive Brokers

The first step in building automated trading strategies is to get the raw data from reliable sources. Even when there are plenty of options to get the historical financial data, having a good source for free would not hurt.


But why we use Interactive Broker after all?

Interactive Brokers (IB) is a well-known trading brokerage with global access to stocks, options, futures, currencies, bonds, and funds. For all these assets, we have access to limited historical data.

To get the task done, we need to configure the Trade Workstation as well as download and configure IBridgePy. Let’s do it!

Getting Trader Workstation ready

You can either start Trader Workstation or IB Gateway to connect with your account (You should have one already):

First of all, go to Settings in the API-Settings section and make sure:

  1. The socket port is 7456.
  2. The checkbox of “Enable ActiveX and Socket Clients” is checked.
API – Settings

Now that the Broker (server-side) is solved, it is time to get ready the client-side.

Getting IBridgePy ready

You need to download IBridgePy_Mac_Python27_64 (Here!)

It is better to use IBridgePy on Python 2.7. It is more stable.

Then, you should edit the file and uncomment the file ““:

# fileName = ''
fileName = ''
# fileName = ''
# fileName = ''
# fileName = ''
# fileName = ''
# fileName = ''

Now, you should edit the account code in the same file.

# !!!!!! IMPORTANT  !!!!!!!!!!!!!!!!!
# accountCode = 'DU1868499'  # You need to change it to your own IB account number

Then, it is time to edit the file “” located in Strategies/

def initialize(context):
    # IB offers free historical data for FOREX. Please refer to this YouTube tutorial = symbol('SPY')
    # context.secList = symbols('AAPL', 'GOOG')

def handle_data(context, data):
    # Method 1: IBridgePy function request_historical_data(str_security, barSize, goBack)
    # Users have more controls on this function.
    print ('Historical Data of %s' % (str(, ),))
    #hist = request_historical_data(, '1 day', '5 D')
    hist = request_historical_data(, '30 mins', '2 Y')

Then you should run (Ensure you are running on python 2.7):


If you are getting this error:

Please check these instructions on opening a Mac app from an unidentified developer to overcome the above error.

Please also ensure you are using the latest version, if you are getting the following error:
broker_client_factory.CallBacks::_print_version_and_exit:EXIT IBridgePy version= 12.2.8

Finally, we get the raw data directly from the broker in the file Output/SPY.csv with the following structure:

2019-01-28 09:30:00-05:00262.71263.42262.33263.3999610

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: