This new demand is not a “little screener” 😉 Please open a new topic for this.
Below is the code that returns the nearest unbroken 2 tops and 2 bottoms levels.
defparam drawonlastbaronly=false
if day<>day[1] or intradaybarindex=0 then
unset($peak)
unset($peakbar)
unset($trough)
unset($troughbar)
i=0
j=0
endif
Peak1 = (high[3] >= high[4]) AND (high[3] > high[5]) AND (high[3] > high[6])
Peak2 = (high[3] >= high[2]) AND (high[3] > high[1]) AND (high[3] > high[0])
Peak = Peak1 AND Peak2
if peak and barindex-lastbar>1 then
// drawpoint(barindex[3],high[3],2) coloured("cyan")
$peak[i]=high[3]
$peakbar[i]=barindex[3]
lastbar=barindex
i=i+1
endif
Trough1 = (low[3] <= low[4]) AND (low[3] < low[5]) AND (low[3] < low[6])
Trough2 = (low[3] <= low[2]) AND (low[3] < low[1]) AND (low[3] < low[0])
Trough = Trough1 AND Trough2
if Trough and barindex-lastbar>1 then
//drawpoint(barindex[3],low[3],2) coloured("crimson")
$trough[j]=low[3]
$troughbar[j]=barindex[3]
lastbar=barindex
j=j+1
endif
if islastbarupdate then
if i>0 then
hhcount=0
hhnearest=0
for k = 0 to i-1 do
drawpoint($peakbar[k], $peak[k],2) coloured("cyan")
for l = 0 to i-1 do
if $peakbar[l]<>$peakbar[k] and abs($peakbar[l]-$peakbar[k])>1 then
diff = abs(round($peak[l],2) - round($peak[k],2))
if diff = 0 then //equal price
//find breakout
period = max(2,max($peakbar[l],$peakbar[k]) - min($peakbar[l],$peakbar[k]))
decay = max(1,barindex-max($peakbar[l],$peakbar[k]))
hh = highest[period](high)[decay]
if hh<=max($peak[l],$peak[k]) then //no breakout at that time
drawsegment($peakbar[l],$peak[l], $peakbar[k], $peak[k]) coloured("cyan")
currPeriod = max(2,barindex-max($peakbar[l],$peakbar[k]))
currHH = highest[max(1,currPeriod)](high)
//test if no broken since (real time)
if currHH<=max($peak[l],$peak[k]) then //no breakout since
drawsegment($peakbar[l],$peak[l], barindex, $peak[l]) coloured("cyan") style(dottedline4)
hhcount=hhcount+1
//store nearest hh level
if $peak[l]-close<hhnearest or hhnearest=0 then
hhnearest = $peak[l]
endif
endif
endif
endif
endif
next
next
endif
if j>0 then
llcount=0
llnearest=0
for k = 0 to j-1 do
drawpoint($troughbar[k], $trough[k],2) coloured("crimson")
for l = 0 to i-1 do
if $troughbar[l]<>$troughbar[k] and abs($troughbar[l]-$troughbar[k])>1 then
diff = abs(round($trough[l],2) - round($trough[k],2))
if diff = 0 then //equal price
//find breakout
period = max(2,max($troughbar[l],$troughbar[k]) - min($troughbar[l],$troughbar[k]))
decay = max(1,barindex-max($troughbar[l],$troughbar[k]))
ll = lowest[period](low)[decay]
if ll>=min($trough[l],$trough[k]) then //no breakout at that time
drawsegment($troughbar[l],$trough[l], $troughbar[k], $trough[k]) coloured("crimson")
currPeriod = max(2,barindex-max($peakbar[l],$peakbar[k]))
currLL = lowest[currPeriod](low)
//test if no broken since (real time)
if currLL>=min($trough[l],$trough[k]) then //no breakout since
drawsegment($troughbar[l],$trough[l], barindex, $trough[l]) coloured("crimson") style(dottedline4)
llcount=llcount+1
//store nearest ll level
if close-$trough[l]<llnearest or llnearest=0 then
llnearest = $trough[l]
endif
endif
endif
endif
endif
next
next
endif
endif
return hhcount as "unbroken highs count", llcount as "unbroken lows count", hhnearest as "nearest unbroken highs" coloured(0,0,0,0), llnearest as "nearest unbroken lows" coloured(0,0,0,0)