Search code examples
pine-scriptpine-script-v5tradingtradingview-api

Get high and low of previous condition candle


I am trying to figure out how to get the high and low of the most recent previous candle that meets one of my conditions. I know to get a prior candles high you can use the [1], but if I do not know the number of how far back the previous candle is, how can I approach this?

high and low of most recent candle that has met condition

I have tried to use the calling prior data function with brackets, however, the problem is I am unaware as to how far back the last occurrence of the condition being met is, and it is always changing so I can not have a static number inside the brackets.


Solution

  • When your condition is met, you should record the bar_index :

    if myconditionismet then
        BarIndexWhenConditionIsMet := bar_index
    

    Then you can use BarIndexWhenConditionIsMet to retrieve the high and the low your are looking for.