I’m trying to modify the code below written by “geneclash” and it works in a way which I’m not interested. I want to see the code runs on each timefram, lookback from i.e 500 or less last bars and for a swing high, create a line (MSH), and also a line for the last and closest swing low to that swing high (MSL), then if price crossovers the MSH, will wait till the new swing high is being created (all needed candles must be closed and the new swing high is confirmed) and the new swing high will be the new MSH and the old MSH will be deleted and again the last and closest swing low to that swing high, will be the new MSL (in this case the new MSL will be replced immediately because either a new swing low has been created or the old MSL is still the last and closest MSL and can be used. On the other hand. if price crossunder the MSL, the last and closest swing high will be still in use or the new MSH will be updated and when a new swing low is confirmed, the new MSL will be plotted. But this code create MSH and MSL and wont update it in the way I’m fond of. I’ll be appreciated if you kindly guid me how to overcome this issue.
//@version=5
indicator("MSH & MSL",overlay=true,max_bars_back = 5000)
drawLine = input(false,title="Draw Vertical Line to Latest Alert")
LabelsOffset = input(25)
isRegularFractal(mode) =>
ret = mode == 1 ? high[4] < high[3] and high[3] < high[2] and high[2] > high[1] and high[1] > high[0] : mode == -1 ? low[4] > low[3] and low[3] > low[2] and low[2] < low[1] and low[1] < low[0] : false
dt = time - time[1]
GetLastPivots() =>
var float LastPh_v = na
var float LastPl_v = na
var int LastPh_t = na
var int LastPl_t = na
filteredtopf = isRegularFractal(1)
filteredbotf = isRegularFractal(-1)
if filteredtopf
LastPh_v := high[2]
LastPh_t := time[2]
if filteredbotf
LastPl_v := low[2]
LastPl_t := time[2]
[LastPh_v,LastPh_t,LastPl_v,LastPl_t]
ShowY = input.bool(true,title="Use Monthly Levels")
ColorHY = input.color(color.black,inline='y1',title="High")
ColorLY = input.color(color.black,inline='y1',title="Low ")
StyleY = input.string("Dotted",options=["Solid","Dotted","Dashed"],inline='y1',title="")
WidthY = input.int(1,title="",inline="y1")
Show1 = input.bool(true,title="Use Weekly Levels")
ColorH1 = input.color(color.black,inline='l1',title="High")
ColorL1 = input.color(color.black,inline='l1',title="Low ")
Style1 = input.string("Dotted",options=["Solid","Dotted","Dashed"],inline='l1',title="")
Width1 = input.int(1,title="",inline="l1")
Show2 = input.bool(true,title="Use Daily Levels")
ColorH2 = input.color(color.black,inline='l2',title="High")
ColorL2 = input.color(color.black,inline='l2',title="Low")
Style2 = input.string("Dotted",options=["Solid","Dotted","Dashed"],inline='l2',title="")
Width2 = input.int(1,title="",inline="l2")
Show3 = input.bool(true,title="Show 4H Levels")
ColorH3 = input.color(color.black,inline='l3',title="High")
ColorL3 = input.color(color.black,inline='l3',title="Low ")
Style3 = input.string("Dotted",options=["Solid","Dotted","Dashed"],inline='l3',title="")
Width3 = input.int(1,title="",inline="l3")
Show4 = input.bool(true,title="Show 1H Levels")
ColorH4 = input.color(color.black,inline='l4',title="High")
ColorL4 = input.color(color.black,inline='l4',title="Low")
Style4 = input.string("Dotted",options=["Solid","Dotted","Dashed"],inline='l4',title="")
Width4 = input.int(1,title="",inline="l4")
LabelColor = input.color(color.purple)
TextColor = input.color(color.black)
StyleToEnum(s) =>
if s == 'Solid'
line.style_solid
else if s == 'Dotted'
line.style_dotted
else
line.style_dashed
f_chartTfInMinutes() =>
float _return = timeframe.multiplier * (
timeframe.isseconds ? 1. / 60 :
timeframe.isminutes ? 1. :
timeframe.isdaily ? 60. * 24 :
timeframe.isweekly ? 60. * 24 * 7 :
timeframe.ismonthly ? 60. * 24 * 31.4375 : na)
[LastPh_vY,LastPh_tY_,LastPl_vY,LastPl_tY_] = request.security(syminfo.tickerid,"3M", GetLastPivots(), lookahead = barmerge.lookahead_on)
[LastPh_v1,LastPh_t1_,LastPl_v1,LastPl_t1_] = request.security(syminfo.tickerid,"W", GetLastPivots(), lookahead = barmerge.lookahead_on)
[LastPh_v2,LastPh_t2_,LastPl_v2,LastPl_t2_] = request.security(syminfo.tickerid,"D", GetLastPivots(), lookahead = barmerge.lookahead_on)
[LastPh_v3,LastPh_t3_,LastPl_v3,LastPl_t3_] = request.security(syminfo.tickerid,"240", GetLastPivots(), lookahead = barmerge.lookahead_on)
[LastPh_v4,LastPh_t4_,LastPl_v4,LastPl_t4_] = request.security(syminfo.tickerid,"60", GetLastPivots(), lookahead = barmerge.lookahead_on)
line lhY = na, line llY = na
line lh1 = na, line ll1 = na
line lh2 = na, line ll2 = na
line lh3 = na, line ll3 = na
line lh4 = na, line ll4 = na
label lblhY = na, label lbllY = na
label lblh1 = na, label lbll1 = na
label lblh2 = na, label lbll2 = na
label lblh3 = na, label lbll3 = na
label lblh4 = na, label lbll4 = na
FindT(t,v) =>
int result = time
begin = (time - t) / dt
if begin < 5000
for i = begin to 0
if high[i] == v or low[i] == v
result := t + (begin-i) * dt
break
result
else
t
LastPl_tY = FindT(LastPl_tY_,LastPl_vY)
LastPh_tY = FindT(LastPh_tY_,LastPh_vY)
LastPl_t1 = FindT(LastPl_t1_,LastPl_v1)
LastPh_t1 = FindT(LastPh_t1_,LastPh_v1)
LastPl_t2 = FindT(LastPl_t2_,LastPl_v2)
LastPh_t2 = FindT(LastPh_t2_,LastPh_v2)
LastPl_t3 = FindT(LastPl_t3_,LastPl_v3)
LastPh_t3 = FindT(LastPh_t3_,LastPh_v3)
LastPl_t4 = FindT(LastPl_t4_,LastPl_v4)
LastPh_t4 = FindT(LastPh_t4_,LastPh_v4)
if barstate.islast
if ShowY and timeframe.ismonthly
lhY := line.new(LastPh_tY,LastPh_vY,time,LastPh_vY,xloc=xloc.bar_time,color=ColorHY,style=StyleToEnum(StyleY),extend=extend.right,width=WidthY)
line.delete(lhY[1])
llY := line.new(LastPl_tY,LastPl_vY,time,LastPl_vY,xloc=xloc.bar_time,color=ColorLY,style=StyleToEnum(StyleY),extend=extend.right,width=WidthY)
line.delete(llY[1])
lblhY := label.new(bar_index + LabelsOffset,LastPh_vY,"Monthly MSH",color=LabelColor,style=label.style_none,textcolor=TextColor,textalign=text.align_right,size=size.small)
label.delete(lblhY[1])
lbllY := label.new(bar_index + LabelsOffset,LastPl_vY,"Monthly MSL",color=LabelColor,style=label.style_none,textcolor=TextColor,textalign=text.align_right,size=size.small)
label.delete(lbllY[1])
if Show1 and timeframe.isweekly
lh1 := line.new(LastPh_t1,LastPh_v1,time,LastPh_v1,xloc=xloc.bar_time,color=ColorH1,style=StyleToEnum(Style1),extend=extend.right,width=Width1)
line.delete(lh1[1])
ll1 := line.new(LastPl_t1,LastPl_v1,time,LastPl_v1,xloc=xloc.bar_time,color=ColorL1,style=StyleToEnum(Style1),extend=extend.right,width=Width1)
line.delete(ll1[1])
lblh1 := label.new(bar_index + LabelsOffset,LastPh_v1,"Weekly MSH",color=LabelColor,style=label.style_none,textcolor=TextColor,textalign=text.align_right,size=size.small)
label.delete(lblh1[1])
lbll1 := label.new(bar_index + LabelsOffset,LastPl_v1,"Weekly MSL",color=LabelColor,style=label.style_none,textcolor=TextColor,textalign=text.align_right,size=size.small)
label.delete(lbll1[1])
if Show2 and timeframe.isdaily
lh2 := line.new(LastPh_t2,LastPh_v2,time,LastPh_v2,xloc=xloc.bar_time,color=ColorH2,style=StyleToEnum(Style2),extend=extend.right,width=Width2)
line.delete(lh2[1])
ll2 := line.new(LastPl_t2,LastPl_v2,time,LastPl_v2,xloc=xloc.bar_time,color=ColorL2,style=StyleToEnum(Style2),extend=extend.right,width=Width2)
line.delete(ll2[1])
lblh2 := label.new(bar_index + LabelsOffset,LastPh_v2,"Daily MSH",color=LabelColor,style=label.style_none,textcolor=TextColor,textalign=text.align_right,size=size.small)
label.delete(lblh2[1])
lbll2 := label.new(bar_index + LabelsOffset,LastPl_v2,"Daily MSL",color=LabelColor,style=label.style_none,textcolor=TextColor,textalign=text.align_right,size=size.small)
label.delete(lbll2[1])
if Show3 and f_chartTfInMinutes() == 240
lh3 := line.new(LastPh_t3,LastPh_v3,time,LastPh_v3,xloc=xloc.bar_time,color=ColorH3,style=StyleToEnum(Style3),extend=extend.right,width=Width3)
line.delete(lh3[1])
ll3 := line.new(LastPl_t3,LastPl_v3,time,LastPl_v3,xloc=xloc.bar_time,color=ColorL3,style=StyleToEnum(Style3),extend=extend.right,width=Width3)
line.delete(ll3[1])
lblh3 := label.new(bar_index + LabelsOffset,LastPh_v3,"4H MSH",color=LabelColor,style=label.style_none,textcolor=TextColor,textalign=text.align_right,size=size.small)
label.delete(lblh3[1])
lbll3 := label.new(bar_index + LabelsOffset,LastPl_v3,"4H MSL",color=LabelColor,style=label.style_none,textcolor=TextColor,textalign=text.align_right,size=size.small)
label.delete(lbll3[1])
if Show4 and f_chartTfInMinutes() <= 60
lh4 := line.new(LastPh_t4,LastPh_v4,time,LastPh_v4,xloc=xloc.bar_time,color=ColorH4,style=StyleToEnum(Style4),extend=extend.right,width=Width4)
line.delete(lh4[1])
ll4 := line.new(LastPl_t4,LastPl_v4,time,LastPl_v4,xloc=xloc.bar_time,color=ColorL4,style=StyleToEnum(Style4),extend=extend.right,width=Width4)
line.delete(ll4[1])
lblh4 := label.new(bar_index + LabelsOffset,LastPh_v4,"1H MSH",color=LabelColor,style=label.style_none,textcolor=TextColor,textalign=text.align_right,size=size.small)
label.delete(lblh4[1])
lbll4 := label.new(bar_index + LabelsOffset,LastPl_v4,"1H MSL",color=LabelColor,style=label.style_none,textcolor=TextColor,textalign=text.align_right,size=size.small)
label.delete(lbll4[1])
enter image description here
In this image price has crossed the old MSH, so when the new swing high has been created, the new MSH (green ray) will be placed on that swing high, since the MSL (red ray) has been placed on the last and closest swing low to the current price. but the code at the moment providesthe MSH and MSL (black dotted lines) which I’m not interested into them.
I hope myquestion and explanations wereclear.
MetMet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.