Research

Historical Data

Introduction

In the QuantConnect Research environment you can import historical price data for all assets we support. This allows you to manipulate the data and perform matrix and vector operations easily. The API methods for requesting data are identical to the QuantConnect History methods. It requires adding the asset to the QuantBook, then submitting a history request for the prices.

Adding Assets

You must add the assets you'd like to analyze to your QuantBook to request historical data for them. Adding assets uses the same API as QCAlgorithm. For more information on these methods see the Initializing Algorithms section.

qb = QuantBook()
spy = qb.AddEquity("SPY")   # add equity data
eur = qb.AddForex("EURUSD") # add forex data
btc = qb.AddCrypto("BTCUSD")# add crypto data
goog = qb.AddOption("GOOG") # add goog options
es  = qb.AddFuture("ES")    # add SP 500 futures

Equity and Forex Historical Data

Using the the QuantBook history method qb.History(Symbol[], Period, Resolution) we can pass in an array of Symbol objects. It supports resolutions Minute, Hour and Daily. This can be easily done by passing in the Securities keys as demonstrated below:

# Get historical data of last 10 days for all subscribed assets with time span
h1 = qb.History(qb.Securities.Keys, timedelta(days=10), Resolution.Daily)

# Get historical data of last 10 daily bars for specified symbol 
h2 = qb.History(["SPY"], 10, Resolution.Daily)

You can also use the start date and the end date to retrieve a period of history data with the method qb.History(Symbols, StartDate, EndDate, Resolution). The format of start and end date are python datetime.

The return value of history request is a multi-index pandas dataframe. The first level of the index is symbol, the second level is time. To get the columns required you can apply operations on the pandas dataframes to retrieve the columns you need:

# Fetch the open price of "SPY"
h2.loc[“SPY”]["open"]

# Fetch the ask close price of “EURUSD”
h1.loc["EURUSD"]["askclose"]

# Fetch the close price of all subscribed assets
h1['close'].unstack(level=0)
Request History of all Subscribed Assets in QuantConnect Research
Request History of Specified Asset in QuantConnect Research
Fetch the Opening Price of Specified Asset in QuantConnect Research
Fetch Ask Close Price of Forex in QuantConnect Research
Fetch the Closing Price of all Subscribed Assets in QuantConnect Research

Futures Historical Data

To request future data in the research environment you need to add the underlying asset to the QuantBook as demonstrated above by the symbols. This returns a Security object.

es = qb.AddFuture("ES") # add S&P 500 futures

Once you have the Security object for the underlying asset you must set the filter you'd like to apply on the universe of available contracts. Futures have a contracts for every expiration date combination.

For Futures you can optionally to set the contract expiration date range SetFilter(minExpiry, maxExpiry). The default filter is set to SetFilter(timedelta(0), timedelta(35)).

# filter future contracts
es.SetFilter(timedelta(0), timedelta(180))

Finally to request price data for the filtered contracts we have created a helper which fetches the history in a single request:

# request the SP500 futures history from 01/11/2017 10:10 to 01/12/2017 12:10
future_history = qb.GetFutureHistory(es.Symbol, datetime(2017, 1, 11, 10, 10), datetime(2017, 1, 13, 12, 10))

# Get all futures data as a dataframe
future_history.GetAllData()

# Fetch expiration dates of all futures contracts
future_history.GetExpiryDates()
Request Futures History in QuantConnect Research
Fetch Futures Expiry in QuantConnect Research

Options Historical Data

To request options data in the research environment you need to add the underlying asset to the QuantBook as demonstrated above by the symbols. This returns a Security object.

goog = qb.AddOption("GOOG") # add the GOOG options

Once you have the Security object for the underlying asset you must set the filter you'd like to apply on the universe of available contracts. Options have contracts for every strike-expiration date combination.

In Options this filter is set with the SetFilter(minStrike, maxStrike, minExpiry, maxExpiry) method. The strike is an integer number and the expiry is time span. If not specified, the default filter is SetFilter(-1, 1, timedelta(0), timedelta(35)).

# filter option contracts
goog.SetFilter(-2, 2, timedelta(0), timedelta(180))

Finally, to fetch the historical data associated with this underlying asset and filter you should use the helper method we've created which fetches all the data in one request:

# request the GOOG options history from 01/11/2017 10:10 to 01/12/2017 12:10
option_history = qb.GetOptionHistory(goog.Symbol, datetime(2017, 1, 11, 10, 10), datetime(2017, 1, 13, 12, 10))

# Fetch strikes of all options contracts 
option_history.GetStrikes()

# Fetch expiration dates of all options contracts
option_history.GetExpiryDates()

# Get all the options data as a dataframe
option_history.GetAllData()
Request Options History in QuantConnect Research
Fetch Option Expiry in QuantConnect Research
Fetch Option Strike in QuantConnect Research

You can also see our Tutorials and Videos. You can also get in touch with us via Chat.

Did you find this page Helpful ?