Example of requesting historical data

request_data

request_data(historyData= None, realTimePrice = None, contractDetails= None, marketSnapShot=None, waitForFeedbackinSeconds=30,repeat=3)

historyData: a list of requests of historical data. Each request is defined by a tuple with three elements, security, timeframe, goBack. IB supports the following timeframes: 1 sec, 5 secs, 15 secs, 30 secs, 1 min, 2 mins, 3 mins, 5 mins, 15 mins, 30 mins, 1 hour, 1 day. IB supports the following goBack: Valid values include any integer followed by a space and then S (seconds), D (days) or W (week). If no unit is specified, seconds is used.

As an example, the following is used to request 1 minute bar historical data of SPY, go back to 1 day and daily bar historical data of AAPL, stock of Apple, Inc.

request_data(historyData=[(symbol('SPY'), '1 min', '1 D'),
                              (symbol('AAPL'), '1 day', '10 D')])

Another example, user can use reqHistParam to clearly define parameters

import datetime as dt
def initialize(context):
    context.sec=superSymbol(secType='FUT', symbol='CL', currency='USD',
                            expiry='201707', exchange = 'NYMEX')

def handle_data(context, data):
    request_data(historyData=[reqHistParam(security=context.sec, 
                            barSize='1 hour',  goBack='100 D',
                            endTime=dt.datetime.now())])
    print data[context.sec].hist['1 hour'].tail()        
    exit()