0

I've been working on this for the past week but can't see if figure out how to do this in Pine. When only the most recent occurrence of a condition is true, how do I identity and reference the past 8 previous candle highs which is higher than the high[1] (ie, the candle prior to the trigger in my screenshot)? They don't have to be higher highs. The high of the historical candle must just be higher than the trigger -1 candle. Hopefully this makes sense.

enter image description here

I've tried using for loops, valuewhen(), and a few other things but can't seem to get it to work.

TFG
  • 3
  • 1

1 Answers1

0

If you want to get the highest value of high on previous 8 bars, use ta.highest() function on each bar. Then you can get the value of ta.highest only when your condition is met, using ta.valuewhen function:

//@version=5
indicator("My script", overlay = true)

highest8 = ta.highest(high, 8)
condition = ta.cross(ta.sma(close, 10), ta.sma(close, 20))
value = ta.valuewhen(condition, highest8[1], 0)

if condition
    label.new(bar_index, high, str.tostring(value), color = color.green, textcolor = color.white)

mr_statler
  • 1,913
  • 2
  • 3
  • 14
  • Thanks for your response. Unfortunately your code isn’t quite what I need. It’s finding the highest high of the previous 8 bars. – TFG Nov 07 '22 at 12:57