Home » Technical Analysis Functions » ta.pivothigh in Pine Script

ta.pivothigh in Pine Script

Photo of author
Published on

Pine Script, the powerful scripting language used on TradingView, offers a function to detect such pivotal highs, namely the ta.pivothigh function. In this tutorial, we’ll delve deep into understanding this function, its syntax, and how to incorporate it into your scripts.

What are Pivot Highs?

A Pivot High is a bar that is higher than the bars before (left of it) and after it (right of it). These points often act as potential resistance levels in charts, making them essential for traders to identify.

Syntax of ta.pivothigh

The ta.pivothigh function comes with two overloads:

  1. ta.pivothigh(leftbars, rightbars) → series float: This uses the default source of closing prices.
  2. ta.pivothigh(source, leftbars, rightbars) → series float: This allows you to define a custom source.

Arguments:

leftbars (series int/float)

This specifies how many bars to the left of a potential pivot are considered. The current bar needs to be higher than these to qualify as a pivot high.

rightbars (series int/float)

This specifies the number of bars to the right. The current bar should be higher than these bars for it to be a pivot high.

Basic Example

Let’s start with the given example:

//@version=5
indicator("PivotHigh", overlay=true)
leftBars = input(2)
rightBars=input(2)
ph = ta.pivothigh(leftBars, rightBars)
plot(ph, style=plot.style_cross, linewidth=3, color=color.red, offset=-rightBars)

Explaining the Code:

  • The script begins by defining an indicator called “PivotHigh” that overlays on the main chart.
  • We then take user inputs for the number of left and right bars to consider for identifying a pivot high.
  • Using ta.pivothigh, we calculate the pivot high values.
  • Finally, the pivot highs are plotted using red crosses on the chart.

Advanced Use Case: Labeling Pivot Highs with Dates

To make our script more informative, let’s label each pivot high with its corresponding date:

//@version=5
indicator("PivotHigh with Date", overlay=true)
leftBars = input(2)
rightBars=input(2)
ph = ta.pivothigh(leftBars, rightBars)

// Check if the current bar is a pivot high
isPivot = not na(ph)

// Fetch the date for the pivot high bar
dateLabel = isPivot ? tostring(year) + "-" + tostring(month) + "-" + tostring(dayofmonth) : ""

// Plot the pivot high and label it with the date
plot(ph, style=plot.style_cross, linewidth=3, color=color.red, offset=-rightBars)
label.new(x=bar_index, y=ph, text=dateLabel, style=label.style_label_up, color=color.red, size=size.small)
ta.pivothigh in Pine Script

In this version, whenever a pivot high is detected, a label is created right above the pivot point, displaying the date of the pivot high.

Remarks:

  • If either leftbars or rightbars are series, the max_bars_back function should be used for the ‘source’ variable. This ensures that the script looks back adequately to account for dynamic look-back periods.

Key Takeaways:

  • Pivot highs are essential resistance points in technical analysis.
  • The ta.pivothigh function in Pine Script allows for easy identification of these pivot highs.
  • Enhancing visual representation, such as adding date labels, can provide more context and information for traders.

Conclusion

The ta.pivothigh function is a robust tool for traders and script writers on TradingView. By understanding its intricacies, you can enhance your chart analysis and potentially identify crucial resistance levels more effectively. As always, it’s essential to combine pivot highs with other technical analysis tools and insights for a holistic trading strategy.

Leave a Comment