Search code examples
pine-scripttradingalgorithmic-trading

Trying to find the avearage value over Last pivots


I am looking to find the average of last N pivots based on a length. For example, Finding the average of last N pivot highs and last N pivot lows using Pine Script


Solution

  • To get the average of a serie, you can use ta.sma(serie_name, N) with N the number of data used to calculate the average.

    You should try this :

    //@version=5
    indicator(Pivot average", overlay=true)
    N = 10
    
    leftBars = input(2)
    rightBars=input(2)
    ph = ta.pivothigh(leftBars, rightBars)
    average = ta.sma(ph, N)
    plot(average)