Micke
Micke

Reputation: 35

Pine script negative for loop

Why is this label printed? Maybe I don't fully understand how pine script loops works. Does it go backwards as well?

indicator("lab", overlay = true)

for i = 0 to -1
    label.new(bar_index, high, str.tostring(i))

plot(close)

Upvotes: 0

Views: 1844

Answers (1)

njzk2
njzk2

Reputation: 39386

Yes.

From https://www.tradingview.com/pine-script-docs/en/v4/language/Loops.html

The expression in by <expression> is optional. It is the step by which the loop counter is increased or decreased on each iteration of the loop. Its default value is 1 when start value < end value. It is -1 when start value > end value. The step (+1 or -1) used as the default is determined by the start and end values.

(emphasis mine)

Upvotes: 0

Related Questions