TradeOnReplayDOCSSearch the whole guidetradeonreplay.com

Guide / Scripts

How a script runs

A script runs one bar at a time, from the oldest bar to the newest, on the candles the replay has reached.

A bar's value cannot depend on a later bar, because later bars are not there. That is what makes a replay honest: the indicator shows what it would have shown at that moment. On a higher-timeframe pane the newest bar is the partial one, so it grows as you step.

Stepping and re-running

  • Stepping forward runs only the new bar.
  • Stepping back, jumping, editing the script, or a higher-timeframe bar changing in place re-runs from the start. The output is the same either way.
  • barstate.islast, last_bar_index and timenow mean “the bar at the cursor”. A script that reads them is re-run on every step instead of extended. That is correct, only slower.

Calls that are skipped on some bars

A ta.* call inside an if or inside barstate.islast still sees every bar. When its arguments depend on nothing but the bar (series, constants, inputs), the bars it skipped are fed through first. So this works:

if barstate.islast
    hh = ta.highest(high, 30)
    line.new(bar_index - 30, hh, bar_index, hh)
Exception

If an argument is a variable that changes from bar to bar, the skipped bars cannot be replayed, and the call sees only the bars it ran on. Move the call out of the if and use its result inside.

No clock, no randomness

There is no wall clock, and math.random() is refused. A script always gives the same output for the same candles. A loop that never ends is stopped after 500,000 iterations on one bar, with an error on the line.