Inclusive bounds and positive steps
The examples below are complete, finite v6 indicators. The first builds a text trace without reading array indices. Add it to a chart and compare its label with the table. The expected sequences are derived from the documented counter rules, not recorded chart executions.
| Header | Expected counter values | Iterations |
|---|---|---|
| for i = 0 to 3 | 0, 1, 2, 3 | 4 |
| for i = 3 to 0 by 2 | 3, 1 | 2; the next value would pass 0 |
| for i = 0 to -1 | 0, -1 | 2; descending, not empty |
| for i = 2 to 2 | 2 | 1 |
//@version=6
indicator("Loop direction trace", overlay=true)
if barstate.islastconfirmedhistory
string ascending = ""
for i = 0 to 3
ascending += str.tostring(i) + ","
string descending = ""
for i = 3 to 0 by 2
descending += str.tostring(i) + ","
string zeroToMinusOne = ""
for i = 0 to -1
zeroToMinusOne += str.tostring(i) + ","
string equalBounds = ""
for i = 2 to 2
equalBounds += str.tostring(i) + ","
if ascending != "0,1,2,3," or descending != "3,1,"
runtime.error("Unexpected positive-step trace")
if zeroToMinusOne != "0,-1," or equalBounds != "2,"
runtime.error("Unexpected inclusive-bound trace")
label.new(bar_index, high, ascending + " | " + descending + " | " + zeroToMinusOne + " | " + equalBounds)