I want to show the highest price in the past as a line every time the price advances by the specified parameter

I want to show the highest price in the past as a line every time the price advances by the specified parameter. For example, I want to draw the highest of the past 50 bars as a line every time the price advances by 50 bars, but these codes draw the highest level of every 50th bar as a line.
My Codes :
[The way I want it to be :]
(https://i.stack.imgur.com/09H5L.jpg)

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

var float higher = 0
var counter = 0
counter := counter + 1
if counter == barLength
    counter := 0
    higher := ta.highest(barLength )

counterPlot = plot(higher, title = 'higher', color = color.new(color.green,0))

>Solution :

Do not use functions like ta.highest in a local scope. You will break the history and get unreliable return values. Instead, put it in global scope and use its value conditionally.

Also, if you want the your lines to be shown as in your screenshot, you need to use the offset argument of the plot(). Because you want to draw something on the past bars.

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

h = ta.highest(barLength)

var float higher = 0
var counter = 0
counter := counter + 1
if counter == barLength
    counter := 0
    higher := h

counterPlot = plot(higher, title = 'higher', color = color.new(color.green,0), offset=-barLength)
bgcolor((counter == 0) ? color.new(color.blue, 85) : na)

enter image description here

Leave a Reply