---
title: "Getting Started"
description: "There are two ways to request historical data in your algorithms: direct historical data requests and indirect algorithm warm up."
canonical: https://www.quantconnect.com/docs/v2/writing-algorithms/historical-data/getting-started
markdown: https://www.quantconnect.com/docs/v2/writing-algorithms/historical-data/getting-started.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/03%20Writing%20Algorithms/20%20Historical%20Data/01%20Getting%20Started
modified: 2026-10-29
index: https://www.quantconnect.com/llms.txt
---

# Getting Started

[Documentation](https://www.quantconnect.com/docs/v2.md) > [Writing Algorithms](https://www.quantconnect.com/docs/v2/writing-algorithms.md) > Historical Data > Getting Started

> Code examples are shown in Python. Append `?lang=csharp` to this URL for the C# version of this page.

## Introduction

There are two ways to request historical data in your algorithms: direct [historical data requests](https://www.quantconnect.com/docs/v2/writing-algorithms/historical-data/history-requests.md) and indirect [algorithm warm up](https://www.quantconnect.com/docs/v2/writing-algorithms/historical-data/warm-up-periods.md).

## 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.
   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](https://www.quantconnect.com/docs/v2/cloud-platform/projects/getting-started.md#03-Create-Projects), [Local Platform](https://www.quantconnect.com/docs/v2/local-platform/projects/getting-started.md#03-Create-Projects), or [CLI](https://www.quantconnect.com/docs/v2/lean-cli/projects/project-management.md#02-Create-Projects).
2. In the `initialize` method, [add an asset](https://www.quantconnect.com/docs/v2/writing-algorithms/securities/requesting-data.md).
   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.
   ```python
   spy = self.add_equity('SPY')
   ```
3. Call the `history` method with the asset's [Symbol](https://www.quantconnect.com/docs/v2/writing-algorithms/key-concepts/security-identifiers.md) and a lookback period.
   `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.
   ```python
   history = self.history(spy.symbol, 5, Resolution.DAILY)
   ```
   | symbol | time | close | high | low | open | volume |
   | --- | --- | --- | --- | --- | --- | --- |
   | SPY | 2025-02-12 16:00:00 | 603.36 | 604.54 | 598.520 | 599.26 | 38247065.0 |
   | SPY | 2025-02-13 16:00:00 | 609.73 | 609.94 | 603.215 | 604.43 | 36445673.0 |
   | SPY | 2025-02-14 16:00:00 | 609.70 | 610.99 | 609.080 | 609.98 | 23498431.0 |
   | SPY | 2025-02-18 16:00:00 | 611.49 | 611.49 | 608.390 | 610.86 | 24030531.0 |
   | SPY | 2025-02-19 16:00:00 | 612.93 | 613.23 | 609.560 | 610.04 | 26847903.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](https://www.quantconnect.com/docs/v2/writing-algorithms/initialization.md#02-Set-Dates) 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.
   The process to create a new project depends on if you use the [Cloud Platform](https://www.quantconnect.com/docs/v2/cloud-platform/projects/getting-started.md#03-Create-Projects), [Local Platform](https://www.quantconnect.com/docs/v2/local-platform/projects/getting-started.md#03-Create-Projects), or [CLI](https://www.quantconnect.com/docs/v2/lean-cli/projects/project-management.md#02-Create-Projects).
2. In the `initialize` method, [set the backtest dates](https://www.quantconnect.com/docs/v2/writing-algorithms/initialization.md#02-Set-Dates) and [add an asset](https://www.quantconnect.com/docs/v2/writing-algorithms/securities/requesting-data.md#02-Add-Assets).
   ```python
   self.set_start_date(2024, 12, 1)
   self.set_end_date(2024, 12, 2)
   self.add_equity("SPY", Resolution.DAILY)
   ```
3. In the `initialize` method, call the `set_warm_up` method with the warm-up duration.
   ```python
   self.set_warm_up(10, Resolution.DAILY)
   ```
4. In the `on_data` method, [log](https://www.quantconnect.com/docs/v2/writing-algorithms/logging.md) the [time](https://www.quantconnect.com/docs/v2/writing-algorithms/key-concepts/time-modeling/timeslices.md) and [warm-up state](https://www.quantconnect.com/docs/v2/writing-algorithms/historical-data/warm-up-periods.md#07-Warm-Up-vs-History-Requests).
   ```python
   self.log(f"self.is_warming_up at {self.time}: {self.is_warming_up}")
   ```
   ```python
   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](https://www.quantconnect.com/docs/v2/writing-algorithms/historical-data/warm-up-periods.md).

## Examples

The following examples demonstrate some common practices for historical data.

### Example 1: Simple Moving Average

The following example demonstrates weekly-resampled historical data to calculate the 50-week Simple Moving Average to implement an SMA-cross strategy.

```python
class HistoricalDataAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2024, 9, 1)
        self.set_end_date(2024, 12, 31)
        self.set_cash(100000)

        # Request SPY data for historical data call and trading.
        self.spy = self.add_equity("SPY").symbol
        # Set a scheduled event to trade SPY with a weekly SMA signal.
        self.schedule.on(self.date_rules.week_start(self.spy), self.time_rules.after_market_open(self.spy, 1), self.trade)

    def trade(self) -> None:
        # Historical data call to calculate signal.
        history = self.history(self.spy, 252, Resolution.DAILY)
        # Obtain the weekly close price to calculate the weekly SMA.
        weekly_close = history.unstack(0).close.resample("W").last()
        # Calculate the current 50-week SMA as a trading signal.
        sma50 = weekly_close.rolling(50).mean().iloc[-1, 0]

        # Trade the SMA cross strategy.
        price = self.securities[self.spy].price
        if price > sma50 and not self.portfolio[self.spy].is_long:
            self.set_holdings(self.spy, 1)
        elif price < sma50 and not self.portfolio[self.spy].is_short:
            self.set_holdings(self.spy, -1)
```
