Skip to main content
On a forming bar the script runs again on every tick, so anything computed from close, high or low of that bar keeps moving until the bar closes (a script that does this is said to repaint). To keep a result from moving, base it on a bar that has closed. There are three ways; the first is the most flexible.

Latch the value on barstate.isconfirmed

barstate.isconfirmed is true on historical bars (all but the last one while the market is open) and, on the forming bar, false until the bar closes. Assign a static variable only when it is true, and plot the variable:
A static value is committed when a bar closes. On the forming bar each run starts from the last committed value, so settled holds the previous bar’s close and does not follow the ticks. On history every bar is confirmed (bar the last, while the market is open), so settled equals close.

Other ways

  • Read the previous bar. high[1] - low[1] rests on a bar that has closed. The cost is one bar of delay.
  • Run only at bar close. With calc_on_every_tick=false in indicator, the script runs on a forming bar only when the bar closes. History is unaffected.
Two related tools. A variable declared intra keeps its value across the repeated runs of one forming bar and starts over on the next bar (see Values that persist across bars). A strategy has three flags that decide when it runs and fills: calc_on_every_tick, calc_on_order_fills and process_orders_on_close (see Strategies and backtesting).

Alerts

alert fires at most once per bar, on realtime bars only. Guard it with barstate.isconfirmed so it fires on the closed bar’s values:

The mistake to avoid

Reading close on the forming bar and treating it as final. The compiler cannot warn about it, because the script is valid; the value simply changes with each tick. See Recalculation and confirmation and How to tell a forming bar from one that has closed.