pine-scriptpine-script-v5

Short term high/ mark on historical bar


I am completely new to programming, please be patient. I am trying to mark all short-term highs. The script is finding all the highs I am looking for, but the problem is that the calculation is done on the current bar and accordingly, it's plotting the result on the current bar where the high was calculated. However, I need to plot it on the one before the current high[1].The actual short term high where it occured.

Could you please suggest how to approach this problem and any suggestions on how this script could be written in a more proper way would be very much appreciated. Thanks!!!

//@version=5
indicator("My script", overlay=true)
var float highestHigh = na
if ta.highest(high,3) == high[1]
    highestHigh := high[1]
else 
    highestHigh := na

plotshape(highestHigh, shape.circle, color = color.green)

Tryed with for to caunt, indexing but its always geting the current bar.


Solution

  • To plot the shape on the prior candles you can use the offset= parameter of the plotshape() function:

    plotshape(highestHigh, shape.circle, color = color.green, offset = -1)
    

    Or, for example, use label.new() instead of plotshape, which can be drawn at any point on the chart and later modified with the setters:

    if highestHigh
        label.new(bar_index[1], high[1], style = label.style_xcross, size = size.tiny)