Exclusive 22% OFF All Quantra by QuantInsti Courses for IBridgePy Users Master algorithmic trading from beginner to advanced — Python for trading, machine learning strategies, options trading, and more.
HUI22 Use HUI22 for 22% off
HUI7 Stack HUI7 for an additional 7% off
Browse Courses →
← Back to Blog

Is there any way for me to save history data using IBridgePy?

April 24, 2017
A: Yes. The historical data received in IBridgePy from the IB server are saved in data[symbol('xxx')].hist that is a pandas dataframe. Therefore, you can save it to a csv file by pd.dataframe.to_csv("Path_to_your_local_folder") and use it later in anyway. Example: Request historical data of AAPL and SPY and save them to local folder.
def initialize(context):
    pass

def handle_data(context, data):
    request_data(historyData=[(symbol('SPY'), '1 min', '1 D'),(symbol('AAPL'), '1 day', '10 D') ])
    print data[symbol('SPY')].hist['1 min'].tail()
    print data[symbol('AAPL')].hist['1 day'].tail()
    data[symbol('AAPL')].hist['1 day'].to_csv('AAPL_hist.csv')
    data[symbol('SPY')].hist['1 min'].to_csv('SPY_hist.csv')
    exit()