Skip to content
Algo Trade Analytics Docs

while - Pine Script Keyword

The while statement allows the conditional iteration of a local code block.

variable_declaration = while condition
continue
break
return_expression
  • The local code block after the initial while line must be indented with four spaces or a tab. For the while loop to terminate, the boolean expression following while must eventually become false, or a break must be executed.
//@version=6indicator("while")// This is a simple example of calculating a factorial using a while loop.int i_n = input.int(10, "Factorial Size", minval=0)int counter   = i_nint factorial = 1while counter > 0    factorial := factorial * counter    counter   := counter - 1plot(factorial)