Search code examples
pine-scriptpine-script-v5

Barssince the highest bar in the last X bars


How do I find the number of bars that have passed since the highest point in the last X bars.

Let's say X = 10. This doesn't work:

float h = ta.highest(high, 10) 
float d = ta.barssince(high == h)

Because in this case the code would measure the distance to the last candle which was the highest to the previous 10 candles (not to the current candle), see the image below.

enter image description here


Solution

  • in this case use

    d = ta.highestbars(10)
    

    or d = math.abs(ta.highestbars(10)) to get positive valuesenter image description here