Sidechair

to

Browse the reference

Separates the first and last value of a for loop: for i = 1 to 10 counts from 1 through 10.

Try it in the editor

to appears only in for loops. The value before it is where the counter starts and the value after it is where the loop stops, and the loop includes both. for i = 0 to 4 runs five times, with i equal to 0, 1, 2, 3 and 4.

If the first value is larger than the second the loop counts down: for i = 4 to 0 also runs five times, starting at 4. Either value can be an expression, such as for i = 1 to length - 1.

Remarks

  • When the end is smaller than the start and you did not mean to count down, the loop still runs, in the other direction. Check the bounds when the end comes from a variable such as an input.
  • In v6 the end expression is evaluated before every pass.

Example

Pine ScriptCount down with toOpen in editor
//@version=6
indicator("To bounds")
up = 0
down = 0
for i = 0 to 4
    up += i
for i = 4 to 0
    down += i * 10
plot(up, "Up", color.green, 2)
plot(down, "Down", color.red, 2)

See also

Runs on Sidechair