IsLastBarUpdate
IsLastBarUpdate in ProBuilder returns 1 while the current bar is the most recent update, letting scripts skip heavy code on historical bars for performance.
Syntax
if IsLastBarUpdate then
// code executed only on the last updated bar
endifHow it works
A ProBuilder script runs once per bar across the entire loaded history, then again on every tick of the live bar. IsLastBarUpdate distinguishes those phases. It returns 1 when the bar being processed is the latest update, in real time or the final bar of a closed instrument, and 0 while the script is still working through older bars.
In a backtest the behavior differs slightly: the flag returns 1 from the backtest start date onward and 0 only for the preloaded history before that date. This allows strategies to keep preparation code cheap over the warm-up data while running full logic across the tested period.
The main use case is performance. Loops that scan hundreds of bars, or drawing code that repaints large regions, produce identical results whether they run on every historical bar or once at the end. Gating them behind IsLastBarUpdate removes the redundant passes, which shortens indicator refresh times and reduces the load of live strategies. It pairs naturally with defparam drawonlastbaronly = true, which applies the same idea to drawing output.
Examples
Example 1, Drawing gap rectangles once (Indicator)
defparam drawonlastbaronly=true
X = 1000
if IsLastBarUpdate then
// Scan the last 1000 bars for down gaps, only on the final update
for i = 1 to X do
if open[i] < low[i+1] then
drawrectangle(barindex[i+1], close[i+1], barindex, open[i]) coloured(255,0,0,50) bordercolor(255,0,0)
endif
next
endif
returnThe 1000-bar scan and the rectangle drawing run a single time, on the latest update, instead of once per historical bar. Without the guard, the same work would repeat thousands of times for an identical result.
Example 2, Level line drawn only at the end (Indicator)
myLevel = Highest[100](high)
// Draw the resistance line once instead of on every bar
IF IsLastBarUpdate THEN
DRAWHLINE(myLevel) COLOURED(0, 0, 255)
ENDIF
RETURNThe 100-bar high is computed on each bar as usual, but the horizontal line is only drawn during the final update, keeping the chart responsive.
Example 3, Equity plot from the backtest start (ProBacktest)
// The flag is 1 from the backtest start date onward
IF IsLastBarUpdate THEN
GRAPH STRATEGYPROFIT AS "equity"
ENDIF
IF close CROSSES OVER Average[20](close) AND NOT ONMARKET THEN
BUY 1 CONTRACT AT MARKET
ELSIF close CROSSES UNDER Average[20](close) AND LONGONMARKET THEN
SELL AT MARKET
ENDIFDuring a backtest, IsLastBarUpdate is true for the tested period and false for the preloaded warm-up bars, so the equity curve is plotted only where trades can actually occur.
Common errors and gotchas
- State never accumulates. Variables assigned only inside an IsLastBarUpdate block have not been updated on historical bars. Calculations that depend on bar-by-bar accumulation must stay outside the guard.
- Backtest semantics differ. In a backtest the flag is 1 for every bar from the start date, not just the final bar. Code assuming it fires once will run on the entire tested range.
- Signals cannot be gated by it. Wrapping entry or exit conditions in IsLastBarUpdate changes strategy behavior between backtest and live execution. Reserve it for drawing, logging, and diagnostics.
- Repeated firing on live ticks. On a live chart the block executes on every tick of the forming bar, not once. Drawing code should tolerate being re-run, or be combined with
drawonlastbaronly.
Related instructions
DEFPARAM, sets script parameters such as drawonlastbaronly.DrawOnLastBarOnly, restricts drawing output to the final bar.CalculateOnLastBars, limits how many bars the script computes at all.BarIndex, bar counter used to anchor drawings during the final pass.DRAWRECTANGLE, drawing primitive commonly wrapped in the guard.FOR, loop construct whose historical re-runs the guard eliminates.ONCE, one-time initialization, a complementary optimization tool.RETURN, ends the indicator and outputs its values.
