Back to TradingView PineScript for CFD Traders
Part 2

Loops: for and while

Pine's loops are different from what you may know — bounded for, single-pass semantics, and the cap on iterations.

Intermediate8 min readBeginner → AdvancedLesson 07 / 17
Try it in TradingView
Scan a small lookback window with for

The loop only covers 2 bars — that's the point. Pine loops are for small, fixed windows; everything else should use built-ins or arrays.

//@version=5
indicator("Lesson 7 - Loops", overlay=true)

// Is this bar's low below the previous two lows?
threeBarLow = true
for i = 1 to 2
    if low >= low[i]
        threeBarLow := false

plotshape(threeBarLow, "3-bar low", shape.triangleup, location.belowbar,
          color=color.green, size=size.tiny)

// Count up-closes over the last 5 bars.
upCount = 0
for i = 0 to 4
    if close[i] > open[i]
        upCount := upCount + 1

// Show the count on the last bar only.
if barstate.islast
    label.new(bar_index, high, "Up closes (last 5): " + str.tostring(upCount),
              style=label.style_label_down, color=color.teal, textcolor=color.white)
To run it
  1. 1. Open any chart in TradingView and click Pine Editor at the bottom.
  2. 2. Select everything in the editor and paste this over it, then click Save.
  3. 3. Click Add to chart. Open the Strategy Tester tab if it is a strategy.

For loops are bounded

Pine for loops must have a known length at compile time. They cannot be infinite.

for i = 0 to 49
    sum += close[i]

This sums the last 50 closes. The bound 0 to 49 is the iteration count.

While loops are single-pass

while loops in Pine execute once per bar with internal state. They cannot run across the chart history inside a single bar — there is no way to "wait for the next bar". This is the most common gotcha for people coming from Python.

The 100,000-iteration cap

Pine limits the total iterations a single bar's execution can use. If you nest a for inside a while and the inner loop runs 100,000 times, your script will throw a runtime error. The cap exists to keep the chart responsive. In practice, the cap is rarely hit unless you do something pathological.

What loops are useful for in Pine

Most Pine work is vectorised and you never need a loop. The places a for shines: building a custom rolling calculation the built-ins don't cover, scanning a small lookback window for a pattern, or iterating over an array (covered next lesson).

A pattern-recognition example

Detect a three-bar pattern: bar N's low is below bars N-1 and N-2:

threeBarLow = true
for i = 1 to 2
    if low >= low[i]
        threeBarLow := false
plotshape(threeBarLow, style=shape.triangleup, location=location.belowbar)

When not to use loops

If you find yourself writing for i = 0 to 9999 to scan the entire chart for something, you almost certainly want an array or a built-in function. Pine is designed so that 90% of indicator work doesn't need loops at all.

What you just did

Lesson 07 of 17 in TradingView PineScript for CFD Traders. When you have run the examples or read the section, tick it off and move to the next lesson.