Historical Data
Getting Started
Introduction
There are two ways to request historical data in your algorithms: direct historical data requests and indirect algorithm warm up.
History Requests
History requests return all of the data you request as a single object. Follow these steps to get some historical data:
- Create a new project.
- In the
initialize
Initialize
method, add an asset. - Call the
history
History
method with the asset's Symbol and a lookback period.
Projects contain files to run backtests, launch research notebooks, perform parameter optimizations, and deploy live trading strategies. You need to create projects in order to create strategies and share your work with other members.
The process to create a new project depends on if you use the Cloud Platform, Local Platform, or CLI.
The Initialize
initialize
method is the entry point of your algorithm where you define a series of settings, including security subscriptions, starting cash balances, and warm-up periods.
LEAN only calls the Initialize
initialize
method one time, at the start of your algorithm.
var spy = AddEquity("SPY");
spy = self.add_equity('SPY')
Symbol
objects are a way to identify or "finger-print" tradable assets so that no further database look-up is required.
All QuantConnect and LEAN Algorithm API methods use Symbol
objects to identify assets.
var history = History(spy.Symbol, 5, Resolution.Daily);
history = self.history(spy.symbol, 5, Resolution.DAILY)
Warm Up Periods
Warm-up simulates winding back the clock from the time you deploy the algorithm. In a backest, this is the start date of your algorithm. In live trading, it's the current date. Follow these steps to add a warm-up period to the start of your algorithm:
- Create a new project.
- In the
initialize
Initialize
method, set the backtest dates and add an asset. - In the
initialize
Initialize
method, call theset_warm_up
SetWarmUp
method with the warm-up duration. - In the
on_data
OnData
method, log the time and warm-up state.
The process to create a new project depends on if you use the Cloud Platform, Local Platform, or CLI.
SetStartDate(2024, 12, 1); SetEndDate(2024, 12, 2); AddEquity("SPY", Resolution.Daily);
self.set_start_date(2024, 12, 1) self.set_end_date(2024, 12, 2) self.add_equity("SPY", Resolution.DAILY)
SetWarmUp(10, Resolution.Daily);
self.set_warm_up(10, Resolution.DAILY)
Log($"IsWarmingUp at {Time}: {IsWarmingUp}");
self.log(f"self.is_warming_up at {self.time}: {self.is_warming_up}")
Algorithm starting warm up... IsWarmingUp at 11/22/2024 4:00:00 PM: True IsWarmingUp at 11/25/2024 4:00:00 PM: True IsWarmingUp at 11/26/2024 4:00:00 PM: True IsWarmingUp at 11/27/2024 4:00:00 PM: True IsWarmingUp at 11/29/2024 1:00:00 PM: True Algorithm finished warming up. IsWarmingUp at 12/2/2024 4:00:00 PM: False
Algorithm starting warm up... self.is_warming_up at 2024-11-22 16:00:00: True self.is_warming_up at 2024-11-25 16:00:00: True self.is_warming_up at 2024-11-26 16:00:00: True self.is_warming_up at 2024-11-27 16:00:00: True self.is_warming_up at 2024-11-29 13:00:00: True Algorithm finished warming up. self.is_warming_up at 2024-12-02 16:00:00: False
For more information about warm-up, see Warm Up Periods.