Sidechair

while

Browse the reference

Repeats a block for as long as a condition stays true, checking the condition before each pass.

Try it in the editor

while condition runs its block, then tests the condition again, and stops the first time it is false. If the condition is false the first time, the block never runs. Something inside the loop has to change the condition, or the loop never ends.

break leaves the loop at once and continue skips to the next test. while is an expression that returns the last value evaluated in the block. Reach for it when you do not know the number of passes in advance, such as walking back until a condition holds; use for when the count is known.

Syntax

[variables = | :=] while condition statements | continue | break return_expression

Remarks

  • A while loop with a condition that never becomes false stops the script with a runtime error.
  • On Sidechair: all the loops on one bar together are limited to 500,000 passes.

Example

Pine ScriptBars since the last higher highOpen in editor
//@version=6
indicator("While loop")
n = 0
while n < 100 and high[n + 1] <= high and not na(high[n + 1])
    n += 1
plot(n, "Bars since a higher high", color.blue, style = plot.style_columns)

See also

Runs on Sidechair