Instructions/probuilder

CalculateOnLastBars

DEFPARAM CalculateOnLastBars limits a ProBuilder indicator calculation to the most recent n bars, cutting computation time and memory use on large charts.

Syntax

probuilder
DEFPARAM CalculateOnLastBars = n

Parameters

NameTypeDefaultDescription
nintegerall loaded barsNumber of bars, counted back from the most recent, on which the indicator is evaluated.

How it works

By default, an indicator is evaluated on every bar loaded into the chart. On instruments with hundreds of thousands of bars, or with scripts that contain loops, arrays, or drawing calls, that full sweep can be slow. CalculateOnLastBars tells the platform to run the script only on the last n bars and skip everything older.

The instruction is a DEFPARAM, so it belongs at the top of the script, before any executable statement. It configures how the indicator is scheduled rather than computing anything itself, and it applies to the whole script, not to individual lines.

The trade-off is accuracy at the boundary. Bars older than the window are simply not evaluated, so the indicator plots nothing there, and calculations near the start of the window may lack the history they expect. A 200-period average computed on the first bar inside a 300-bar window cannot see the bars before the window and will differ from the value a full calculation would produce. Recursive constructs, such as exponential averages or manually carried variables, are affected most, because their state normally accumulates over the entire history.

Sensible values of n therefore include generous headroom above the longest lookback in the script. For purely display-oriented tools, dashboards, or scan-style overlays that only care about recent bars, aggressive values are fine and produce large speedups. The instruction applies to indicators; automated strategies control their historical depth with PRELOADBARS instead.

Examples

Example 1, Restricting a moving average to recent bars (Indicator)

probuilder
// Evaluate this indicator on the last 100 bars only
DEFPARAM CalculateOnLastBars = 100
myMovingAverage = Average[100](close)
RETURN myMovingAverage AS "MA 100"

The average is computed and plotted only on the last 100 bars. Because the lookback equals the window, the earliest plotted values are built from fewer bars than a full calculation would use.

Example 2, Speeding up a loop-heavy script (Indicator)

probuilder
// Heavy per-bar loop, so limit evaluation to the last 500 bars
DEFPARAM CalculateOnLastBars = 500

// Count how many of the previous 50 closes are below the current close
strength = 0
FOR i = 1 TO 50 DO
  IF close > close[i] THEN
    strength = strength + 1
  ENDIF
NEXT
RETURN strength AS "Closes beaten (of 50)"

The 50-iteration loop runs on 500 bars instead of the whole chart, which keeps the indicator responsive on instruments with long histories.

Example 3, Dashboard drawn on the live bar (Indicator)

probuilder
// Only the newest bars matter for a text dashboard
DEFPARAM CalculateOnLastBars = 10
DEFPARAM DrawOnLastBarOnly = true

r = RSI[14](close)
a = AverageTrueRange[14](close)

// Print current readings next to the last bar
DRAWTEXT("RSI: #r#", barindex + 5, close, sansserif, standard, 14)
DRAWTEXT("ATR: #a#", barindex + 5, close - a, sansserif, standard, 14)
RETURN

Combining CalculateOnLastBars with DrawOnLastBarOnly keeps a text dashboard cheap: the script only ever evaluates a handful of recent bars and draws once.

Common errors and gotchas

  • Boundary values are distorted. Calculations near the start of the window lack older history, so averages, summations, and recursive values differ from a full-chart calculation there. Set n well above the longest lookback in the script.
  • DEFPARAM placement. The line must appear at the top of the script before executable code. Placing it after calculations causes a compilation error.
  • Older bars show nothing. The indicator is blank before the window. This is expected behavior, not a data problem, but it surprises users comparing against the same formula without the parameter.
  • Indicator scope. CalculateOnLastBars is an indicator optimization. Strategies manage their historical depth with PRELOADBARS, and using the wrong one for the context has no effect or fails to compile.
  • DEFPARAM, declares script-level parameters such as this one.
  • DrawOnLastBarOnly, limits drawing output to the most recent bar.
  • IsLastBarUpdate, true only during the final bar update, for guarding heavy code.
  • PRELOADBARS, controls how much history a strategy loads before starting.
  • BarIndex, zero-based position of the current bar in the loaded data.
  • ONCE, initializes a variable a single time on the first evaluated bar.