Search code examples
pine-scriptpine-script-v5pine-script-v4

In Pinescript I want to show the highest price level every 50 bars


In Pinescript I want to show the highest price level every 50 bars, but no matter what I do I can only show the current high.

//@version=5
indicator(title='Deneme', shorttitle='Deneme', overlay=true, timeframe='')
barLength = input.int(50,minval=1,title='Uzunluk')

counter = 0
higher = 0.0

for i = 0 to bar_index
    if counter == 49
        higher := high[50]
    counter := counter+1
    if counter == 50
        counter := 0
    
counterPlot = plot(higher != 0 ? higher : na, title = 'sayac', color = color.new(color.green,0))





Solution

  • There is a built in function for that - ta.highest()

    //@version=5
    indicator(title='Deneme', shorttitle='Deneme', overlay=true, timeframe='')
    barLength = input.int(50,minval=1,title='Uzunluk')
    
    higher = ta.highest(barLength)
    plot(higher)
    

    EDIT:

    If you want to see just the last value, it might be easier (visually) to use line:

    //@version=5
    indicator(title='Deneme', shorttitle='Deneme', overlay=true)
    barLength = input.int(50,minval=1,title='Uzunluk')
    higher = ta.highest(barLength)
    
    if barstate.islast
        line.new(bar_index - 1, higher, bar_index, higher, extend = extend.both)