fbpx

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.

Advertisements

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
Advertisements

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 RUN_ME.py and uncomment the file “example_get_historical_data.py“:

# fileName = 'example_show_positions.py'
fileName = 'example_get_historical_data.py'
# fileName = 'example_show_real_time_prices.py'
# fileName = 'example_place_order.py'
# fileName = 'example_get_contract_details.py'
# fileName = 'example_get_option_greeks.py'
# fileName = 'example_security_screener.py'

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 “example_get_historical_data.py” located in Strategies/example_get_historical_data.py:

def initialize(context):
    # IB offers free historical data for FOREX. Please refer to this YouTube tutorial https://youtu.be/JkyxLYD2RBk
    context.security = 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.
    # http://www.ibridgepy.com/ibridgepy-documentation/#request_historical_data
    print ('Historical Data of %s' % (str(context.security, ),))
    #hist = request_historical_data(context.security, '1 day', '5 D')
    hist = request_historical_data(context.security, '30 mins', '2 Y')
    hist.to_csv('Output/SPY.csv')
    print(hist)
    print(hist.iloc[-1]['close'])
Advertisements

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

python RUN_ME.py
or
python2 RUN_ME.py

If you are getting this error:

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

Interesting for you:  Is Zorro project worth trying for algorithmic trading?

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:

Dateclosehighlowopenvolume
2019-01-28 09:30:00-05:00262.71263.42262.33263.3999610
Output/SPY.csv

Leave a Reply

%d bloggers like this: