How to get HH/LL/LH/HL sequence for last 50 candles?

Viewed 28

I'm trying to review the previous 50th candles to sequence the pivots in terms of HH/LL/LH/HL for the last four major pivots

EXAMPLE: If the 50th price candle open is 50:

  • first pivot is at 75 (HH)
  • the price drops and second pivot is at 20 (LL)
  • the price rally slightly and third pivot is at 60 (LH)
  • the four pivot is at 40 (HL)

The return string for the example would be : "HH-LL-LH-HL"

I got as far as finding the HH and LL using ta.highest, but then I'm stuck. I do loop through the 50 candle to find the bar-index of that highest. Any one have some clever suggestions how to track the last four pivots in terms of HH/LL/HL/LH ? Appreciate your time and help.

//@version=5
indicator("HHLL", "HHLL", true, format.price, max_labels_count=51, max_lines_count=50)

tgFibLineStyle  = input.string(line.style_dotted, options=[line.style_solid, line.style_dotted, line.style_dashed], title="style", group='Indicators', inline="tg2")
showSuggTargets         = input.bool(false, title="Enable Targets,", group="Show Target Lines", inline="ST1")
tgsUpperColor           = input.color(color.yellow, title="H", inline='ST1', group ="Show Target Lines")
lookbackInput   = input.int(50, "(Lookbook)",  minval = 1, maxval = 100)
_isDebugLabelActive=input.bool(true, title="Debug Label")

StartLine = line.new(x1=bar_index[lookbackInput], y1=high[lookbackInput], x2=bar_index[lookbackInput], y2=high[lookbackInput], extend = extend.both, color=tgsUpperColor, style=tgFibLineStyle)
line.delete(StartLine[1])

periodHigh      = ta.highest(high, lookbackInput)
periodLow       = ta.lowest(low, lookbackInput)

barsbackHI=0
barsbackLO=0

for i = 0 to lookbackInput
    barsbackHI := periodHigh == high[i] ? i : barsbackHI
    barsbackLO := periodLow == low[i] ? i : barsbackLO
    
hh=high[barsbackHI]
ll=low[barsbackLO]

labHi = label.new(bar_index-barsbackHI, hh, 'HH '+ str.tostring(hh, '######.#####') , color= color.green , textcolor=color.white, style=label.style_label_down)
labLo = label.new(bar_index-barsbackLO, ll, 'LL '+ str.tostring(ll, '######.#####') , color= color.red , textcolor=color.white, style=label.style_label_up)
label.delete(labHi[1])
label.delete(labLo[1])

debugText=""
if _isDebugLabelActive
    debugText := "periodHigh: " + str.tostring(periodHigh) + "\nperiodLow: " + str.tostring(periodLow) 
    debugLabel = label.new( x=bar_index, y=close-(close *.003), textalign=text.align_left, text = debugText, yloc = yloc.price, color = color.black, textcolor = color.white, size=size.normal, style=label.style_label_upper_left)
    label.delete(debugLabel[1])
0 Answers
Related