Historical Data

Warm Up Periods

Introduction

LEAN supports an automated fast-forward system called "Warm Up". It simulates winding back the clock from the time you deploy the algorithm. In a backest, this is the StartDate of your algorithm. In live trading, it's the current date. Warm Up is a great way to prepare your algorithm and its indicators for trading.

Set Warm Up Periods

You can set a warm-up period based on a period of time or a number of bars. To set a warm-up, in your algorithm's Initialize method, call the SetWarmUp method.

// Wind time back 7 days from start
SetWarmUp(TimeSpan.FromDays(7));

// Feed in 100 bars before start date
SetWarmUp(100);
# Wind time back 7 days from start
self.SetWarmUp(timedelta(7))

# Feed in 100 bars before start date
self.SetWarmUp(100)

If you pass a timedeltaTimeSpan argument, the warm-up period consists of that timedeltaTimeSpan before the start date and pipes in data that matches the resolution of your data subscriptions.

If you pass just an integer argument, LEAN calculates the start of the warm-up period for each of your security subscriptions by using the number of bars you specify, the resolution of each security, and the trading calendar of each security. After it calculates the start time of the warm-up period for each security, it sets the earliest start time as the start of the algorithm warm-up period. For instance, in the following example, the warm-up period consists of 100 minute resolution bars for SPY and as many second resolution bars for BTCUSD that occur from the start of the SPY warm-up period to the algorithm StartDate.

AddEquity("SPY", Resolution.Minute, fillDataForward: false);
AddCrypto("BTCUSD", Resolution.Second, fillDataForward: false);
SetWarmUp(100);
self.AddEquity("SPY", Resolution.Minute, fillDataForward=False)
self.AddCrypto("BTCUSD", Resolution.Second, fillDataForward=False)
self.SetWarmUp(100)

To use a specific resolution of data for the warm-up period, pass a resolution argument to the SetWarmUp method.

// Feed in 100 days of daily data before the start date
SetWarmUp(TimeSpan.FromDays(100), Resolution.Daily);

// Feed in data for 100 trading days of daily data before the start date
SetWarmUp(100, Resolution.Daily);
# Feed in 100 days of daily data before the start date
self.SetWarmUp(timedelta(days=100), Resolution.Daily)

# Feed in data for 100 trading days before the start date
self.SetWarmUp(100, Resolution.Daily)

If you pass a timedeltaTimeSpan and a resolution, the warm-up period consists of the time period and resolution you set, regardless of the resolution of your data subscriptions.

If you pass an integer and a resolution, the warm-up period consists of the number of bars and resolution you set, regardless of the resolution of your data subscriptions. In this case, LEAN calculates the start of the warm-up period for each of your security subscriptions just like it does when you only pass an integer argument. After it calculates the start time of the warm-up period for each security, it sets the earliest start time as the start of the algorithm warm-up period.

If you pass a resolution argument and you registered indicators or consolidators for automatic updates, the warm-up period resolution must be less than or equal to the lowest resolution of your automatic indicators and consolidators. For instance, in the following example, the indicators use minute resolution data, so you can set the warm-up period to use tick, second, or minute resolution data.

_spy = AddEquity("SPY", Resolution.Minute, fillDataForward: false).Symbol;
_btc = AddCrypto("BTCUSD", Resolution.Second, fillDataForward: false).Symbol;

_spySma = SMA(_spy, 10);
_btcSMA = SMA(_btc, 10, Resolution.Minute);

SetWarmUp(100, Resolution.Minute);
self.spy = self.AddEquity("SPY", Resolution.Minute, fillDataForward=False).Symbol
self.btc = self.AddCrypto("BTCUSD", Resolution.Second, fillDataForward=False).Symbol

self.spy_sma = self.SMA(self.spy, 10)
self.btc_sma = self.SMA(self.btc, 10, Resolution.Minute)

self.SetWarmUp(100, Resolution.Minute)

Warm Up Vs Reality

You may need to distinguish warm-up data from real data. To check if the algorithm is currently in a warm up period, use the IsWarmingUp property.

// In Initialize
var emaFast = EMA("IBM", 50);
var emaSlow = EMA("IBM", 100);
SetWarmup(100);

// In OnData: Don't run if the indicators aren't ready
if (IsWarmingUp) return;
# In Initialize
self.emaFast = self.EMA("IBM", 50);
self.emaSlow = self.EMA("IBM", 100);
self.SetWarmup(100);

// In OnData: Don't run if the indicators aren't ready
if self.IsWarmingUp: return

Event Handler

The OnWarmUpFinished event handler executes when the warm up period ends, even if you don't set a warm up period.

public override void OnWarmupFinished()
{
    // Done warming up
}
def OnWarmUpFinished(self) -> None:
    pass # Done warming up

Limitations

You can't place trades during the warm-up period because they would be operating on fictional fast-forwarded data.

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: