defparam calculateonlastbars = 1000
p = 200
tests = 3
if barindex >=tests then
level = (high + max(open,close))/2
count = 1
draw = 0
for a = 1 to p
if close[a] > level then
break
endif
if high[a] >= level and max(close[a],open[a]) <= level then
count = count + 1
if count = tests then
startindex = barindex-a
draw = 1
break
endif
endif
next
if draw = 1 then
drawsegment(startindex,level,barindex,level) coloured(0,128,0)
lastlevel = level
else
drawsegment(barindex-1,lastlevel,barindex,lastlevel) coloured(0,128,0)
endif
level2 = (low + min(open,close))/2
count = 1
draw2 = 0
for a = 1 to p
if close[a] < level2 then
break
endif
if low[a] <= level2 and min(close[a],open[a]) >= level2 then
count = count + 1
if count = tests then
startindex2 = barindex-a
draw2 = 1
break
endif
endif
next
if draw2 = 1 then
drawsegment(startindex2,level2,barindex,level2) coloured(128,0,0)
lastlevel2 = level2
else
drawsegment(barindex-1,lastlevel2,barindex,lastlevel2) coloured(128,0,0)
endif
endif
return