Export historical data and save them to csv files

Apparently, you cannot do it using Quantopian or zipline. But you can get historical data from Interactive Brokers and save them to csv files using IBridgePy easily.  Also, you can specify a datetime in past as the ending time of the historical data request.

In the following example, historical data of “SPY” ending by Jun 6th 2017, 9:35AM Eastern Time is requested and 10 minute bars are expected.

import datetime as dt
import pytz
import pandas as pd
from sys import exit
def initialize(context):
    pass
    
def handle_data(context, data):
    hist = request_historical_data(symbol('SPY'), barSize = '1 min',
           goBack = '600 S',
           endTime=pytz.timezone('US/Eastern').\
           localize(dt.datetime(2017,6,6,9,35)))
    hist.to_csv('C:\your folder path\xx.csv')
    exit()

Print out the retrieved historical data and a few points need attentions.

  1. There are 10 lines in total as expected.
  2. The time period of market close are skipped.
  3. The timestamp of the last line is Jun 6th 2017, 9:34AM instead of the requested datetime of Jun 6th 2017, 9:35AM
                          close   high   low  open   volume
2017-06-05 15:55:00-04:00 244.02 244.05 244.00 244.01 2871
2017-06-05 15:56:00-04:00 244.01 244.03 243.99 244.03 2222
2017-06-05 15:57:00-04:00 243.97 244.01 243.94 244.01 3909
2017-06-05 15:58:00-04:00 243.91 243.98 243.91 243.98 4895
2017-06-05 15:59:00-04:00 243.97 243.99 243.91 243.92 12661
2017-06-06 09:30:00-04:00 243.27 243.34 243.23 243.31 3813
2017-06-06 09:31:00-04:00 243.22 243.29 243.19 243.27 2377
2017-06-06 09:32:00-04:00 243.26 243.26 243.21 243.22 1405
2017-06-06 09:33:00-04:00 243.23 243.37 243.22 243.26 3299
2017-06-06 09:34:00-04:00 243.21 243.23 243.18 243.23 1744

1 thought on “Export historical data and save them to csv files”

Leave a Comment