I’m looking for some help to write code to define the distance to the last fractal high. In other words, what is the current distance to the last higher high (when there was at least 1 bar break lower). I’ve attached an example and drawn what I am defining as the fractal low (purple dot) and the distance I am trying to calculate (red lines). I assume this has to use an array of sorts but I’m not very well versed in those yet.
I don’t know what indicator you used, so I made this one:
// Fractls
//
// https://www.prorealcode.com/topic/distance-to-last-fractal-high/
//
ONCE P = 15
ONCE HH = 0
ONCE HHprev = 0
ONCE LL = 0
ONCE LLprev = 0
ONCE LastFractal = 0
FractalDistance = 0
Offset = average[20,0](range)
Bullish = close > open
Bearish = close < open
HIGHfractal = Bullish[1] AND Bearish AND (high[1] > high[2]) AND (high[1] > high) AND (high[1] = highest[P](high))
LOWfractal = Bearish[1] AND Bullish AND (low[1] < low[2]) AND (low[1] < low) AND (low[1] = lowest[P](low))
//
IF HIGHfractal AND ((LastFractal = 0) OR (LastFractal = -1)) THEN
LastFractal = 1
HHprev = HH
HH = high[1]
DrawText("●",BarIndex[1],high[1] + Offset,dialog,standard,15) coloured("Green")
IF HHprev <> 0 THEN
Gap = HH - HHprev
DrawText("#Gap#",BarIndex[1],high[1] + Offset*2,dialog,standard,15) coloured("Green")
ENDIF
ENDIF
//
IF LOWfractal AND ((LastFractal = 0) OR (LastFractal = 1)) THEN
LastFractal = -1
LLprev = LL
LL = low[1]
DrawText("●",BarIndex[1],low[1] - Offset,dialog,standard,15) coloured("Red")
IF LLprev <> 0 THEN
Gap = LLprev - LL
DrawText("#Gap#",BarIndex[1],low[1] - Offset*2,dialog,standard,15) coloured("Red")
ENDIF
ENDIF
//
RETURN