book
Checkout our new book! Hands on AI Trading with Python, QuantConnect, and AWS Learn More arrow

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:

  1. Create a new project.
  2. 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.

  3. In the initialize method, add an asset.
  4. The 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 method one time, at the start of your algorithm.

    Select Language:
    spy = self.add_equity('SPY')
  5. Call the history method with the asset's Symbol and a lookback period.
  6. 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.

    Select Language:
    history = self.history(spy.symbol, 5, Resolution.DAILY)
    closehighlowopenvolume
    symboltime
    SPY2025-02-12 16:00:00603.36604.54598.520599.2638247065.0
    2025-02-13 16:00:00609.73609.94603.215604.4336445673.0
    2025-02-14 16:00:00609.70610.99609.080609.9823498431.0
    2025-02-18 16:00:00611.49611.49608.390610.8624030531.0
    2025-02-19 16:00:00612.93613.23609.560610.0426847903.0

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:

  1. Create a new project.
  2. The process to create a new project depends on if you use the Cloud Platform, Local Platform, or CLI.

  3. In the initialize method, set the backtest dates and add an asset.
  4. Select Language:
    self.set_start_date(2024, 12, 1)
    self.set_end_date(2024, 12, 2)
    self.add_equity("SPY", Resolution.DAILY)
  5. In the initialize method, call the set_warm_up method with the warm-up duration.
  6. Select Language:
    self.set_warm_up(10, Resolution.DAILY)
  7. In the on_data method, log the time and warm-up state.
  8. Select Language:
    self.log(f"self.is_warming_up at {self.time}: {self.is_warming_up}")
    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.

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: