for - Pine Script Keyword
Overview
Section titled “Overview”Creates a count-controlled loop, which uses a counter variable to manage the iterative executions of its local code block. The loop continues new iterations until the counter reaches a specified final value.
Syntax
Section titled “Syntax”[variables =|:=] for counter = from_num to to_num [by step_num] statements | continue | break return_expressionRemarks
Section titled “Remarks”- Modifying a loop’s to_num value during an iteration does not change the direction of the loop’s counter. For a loop that counts upward, setting the to_num to a value less than the from_num value on an iteration stops the loop immediately after that iteration ends. Likewise, a loop that counts downward stops after an iteration where the to_num value becomes greater than the from_num value.
Example
Section titled “Example”//@version=6indicator("`for` loop with a step")a = array.from(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)sum = 0.0for i = 0 to 9 by 5 // Because the step is set to 5, we are adding only the first (0) and the sixth (5) value from the array `a`. sum += array.get(a, i)plot(sum)