Warmup Period

Let's look at this simple piece of QuantScript code:

1 close5BarsAgo = close[5]
2
3 enter long when close > close5BarsAgo

If we were to debug this strategy, it would make sense for

close[5]
to be
unknown
for the first 5 bars, right? It should have a value on the sixth execution of the script i.e. the sixth bar - that is when we could go back in time 5 bars, to the first bar.

It makes perfect sense. However, imagine having a 1-day Trading Frequency strategy, which uses an SMA with a period of 200 bars to produce signals. Following our current logic, that SMA would have an

unknown
value for the first 200 bars of the backtest. That's almost a year's worth of backtesting data doing nothing!

This approach would not be ideal. So QuantScript operates a bit differently.

Indeed, if you debug the strategy above, you will find that

close5BarsAgo
has a value on the very first bar.

This is due to the fact that QuantScript executes your code an additional 500 times before it gets to the first bar of the backtest. If you have a strategy with 1000 Max Data Bars, QuantScript will actually load 500 extra, for a total of 1500 bars.

The signals produced during these 500 extra executions are ignored and are not part of the backtest.

These extra executions are what's called the warmup period. It exists to make sure time travelling works as expected from the very first bar, making backtests more meaningful.

To prove that this works, let's take our double-SMA strategy we wrote earlier, change it to use a single SMA with a period of, let's say 400, and see if it has a value on the very first bar.

Here's what our strategy looks like:

1 function Rises(value):
2 value > value[1]
3
4 function Falls(value):
5 value < value[1]
6
7 function SMA(applyTo, period):
8 sum = 0
9 for distance from 0 to (period - 1)
10 sum = sum + applyTo[distance]
11 sum / period
12
13 sma400 = SMA(applyTo: open, period: 400)
14
15 enter long when Rises(sma400)
16 exit long when Falls(sma400)

Paste that code into the QuantScript editor, click on Debug and, indeed,

sma400
has a value starting from the first bar.

This concludes the warmup period section.

However...

When we stop debugging and click on Calculate Backtest, another issue we need to be aware of pops up.