Home » Pinescript Syntax » Understanding For Loops in Pine Script

Understanding For Loops in Pine Script

Photo of author
Published on

Introduction

Pine Script is a domain-specific language for coding custom technical analysis indicators and strategies on TradingView. A fundamental element in many programming languages, including Pine Script, is the loop, and one of the most commonly used types is the “for” loop. In this tutorial, we’ll explore the syntax and functionalities of for loops in Pine Script and provide unique examples to illustrate their use in trading scripts.

Syntax of For Loops in Pine Script

The basic syntax of a for loop in Pine Script is as follows:

for <identifier> = <expression> to <expression>[ by <expression>]
<local_block_loop>
  • <identifier>: Name of the loop’s counter variable.
  • <expression>: Determines the start and end points of the loop. Optionally, you can also specify the step size with by.
  • <local_block_loop>: The block of code to execute in each iteration of the loop.

Components of a For Loop

  1. Initialization: Sets the starting value of the counter.
  2. Condition: Determines when the loop will stop based on the end value of the counter.
  3. Iteration Statement: Controls the increment or decrement of the counter after each loop iteration (default is +1 or -1 based on start and end values).

Example 1: Counting High and Low Bars

In this example, we count the number of bars with a high greater or lower than the current bar’s high over a user-defined lookback period.

//@version=5
indicator("`for` loop")
lookbackInput = input.int(50, "Lookback in bars", minval = 1, maxval = 4999)
higherBars = 0
lowerBars = 0
if barstate.islast
    var label lbl = label.new(na, na, "", style = label.style_label_left)
    for i = 1 to lookbackInput
        if high[i] > high
            higherBars += 1
        else if high[i] < high
            lowerBars += 1
    label.set_xy(lbl, bar_index, high)
    label.set_text(lbl, str.tostring(higherBars, "# higher bars\n") + str.tostring(lowerBars, "# lower bars"))
For Loops in Pine Script

Explanation

  • lookbackInput: Defines the number of bars to look back.
  • for i = 1 to lookbackInput: Loops from 1 to the user-defined lookback period.
  • Inside the loop, we compare the high of each past bar (high[i]) to the bar’s high (high). Depending on the comparison, we increment either higherBars or lowerBars.
  • Finally, a label is created to display the count of higher and lower bars.

Key Takeaway

For loops in Pine Script are powerful tools for iterating over a range of values or array elements. They are particularly useful in trading scripts for analyzing historical data, managing arrays of graphical objects, and implementing complex trading logic.

Conclusion

Understanding for loops in Pine Script is crucial for developing advanced trading strategies and indicators. By mastering this concept, you can efficiently process and analyze financial data, enhancing your trading analysis and decision-making process. Remember to experiment with different use cases to fully grasp the versatility of for loops in Pine Script.

Leave a Comment