PRELOADBARS
PRELOADBARS is a DEFPARAM parameter that sets how many historical bars ProBacktest and ProOrder load before a strategy starts, so indicators start accurate.
Syntax
DEFPARAM PRELOADBARS = number_of_barsParameters
| Name | Type | Default | Description |
|---|---|---|---|
number_of_bars | integer | platform default | Number of historical bars loaded before the first bar on which the strategy executes. Size it to the longest lookback used by any indicator in the code. |
How it works
PRELOADBARS is one of the parameters accepted by the DEFPARAM instruction, alongside settings such as CUMULATEORDERS, FLATAFTER, FLATBEFORE, and NOCASHUPDATE. Like every DEFPARAM line, it must appear at the top of the code, before any other instruction, and it is evaluated once at startup rather than on every bar.
Its purpose is indicator warm-up. A 100-bar moving average needs 100 past closes before it produces a stable value. Without enough preloaded history, indicator readings on the first bars of a backtest or a live run are computed from a truncated series, which can differ from the values shown on the chart and trigger spurious signals. Preloading n bars gives every indicator up to n bars of context before the first trading decision, without generating any orders on those historical bars.
Sizing the value is a trade-off. It should comfortably exceed the longest lookback in the code, including nested lookbacks such as an average of an average, but oversized preloads increase memory use and startup time. A common rule is the longest period plus a margin of 25 to 50 percent.
Examples
Example 1, Warming up a 100-bar moving average (ProOrder)
// Preload 150 bars so the 100 bar average is valid from the first decision
DEFPARAM PRELOADBARS = 150
myMA = average[100](close)
IF NOT ShortOnMarket AND close[1] < myMA[1] THEN
SELLSHORT 1 SHARES AT MARKET
ENDIF
SET STOP %TRAILING 2The 100-bar average requires 100 closes, so preloading 150 bars guarantees correct values from the strategy's first executed bar. This is the reference example from the source documentation with rewritten comments.
Example 2, Breakout on a 200-bar channel (ProBacktest)
// Preload enough history for the 200 bar channel
DEFPARAM PRELOADBARS = 250
upperBand = highest[200](high[1])
IF NOT onmarket AND close > upperBand THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
SET STOP %TRAILING 1.5Without the preload, the 200-bar highest would be computed from fewer bars at the start of the test, making early breakout signals fire on an artificially low channel.
Example 3, Combining several DEFPARAM settings (ProOrder)
// All DEFPARAM lines must precede any other instruction
DEFPARAM CUMULATEORDERS = FALSE
DEFPARAM PRELOADBARS = 500
slowMA = average[200](close)
fastMA = average[50](close)
IF NOT onmarket AND fastMA CROSSES OVER slowMA THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
IF longonmarket AND fastMA CROSSES UNDER slowMA THEN
SELL AT MARKET
ENDIFPRELOADBARS sits with the other DEFPARAM declarations at the top of the file. The 500-bar preload covers the 200-bar average with a wide margin.
Common errors and gotchas
- DEFPARAM placement. All DEFPARAM lines, including PRELOADBARS, must come before any other statement. A DEFPARAM after the first regular instruction fails to compile.
- Preload smaller than the lookback. If the value is below the longest indicator period, early signals are computed from truncated data and may not match chart values. Backtest entries near the start of the test are the usual symptom.
- Hidden nested lookbacks. An
average[50]of anaverage[100]effectively needs about 150 bars. Size the preload to the combined depth, not just the largest single period. - Limited by available history. The platform can only preload bars that exist in the data feed for the chosen timeframe. Requesting more than is available results in fewer preloaded bars, so very long lookbacks on high timeframes may still start partially warmed up.
Related instructions
DEFPARAM, the instruction that PRELOADBARS is a parameter of.CumulateOrders, DEFPARAM parameter controlling order accumulation.FLATAFTER, DEFPARAM parameter forcing a flat position after a time.FLATBEFORE, DEFPARAM parameter blocking positions before a time.NOCASHUPDATE, DEFPARAM parameter freezing capital between trades.Average, moving average whose lookback drives the preload size.BarIndex, counts bars since the start of the loaded data.ONCE, initializes a variable a single time at startup.
