Trendlines with Breaks [LUX]
Forums › ProRealTime English forum › ProBuilder support › Trendlines with Breaks [LUX]
- This topic has 7 replies, 3 voices, and was last updated 1 year ago by Nicolas.
-
-
05/06/2022 at 3:05 AM #192796
This code is from trading view, please help me to convert. Thank you in advance.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/// © LuxAlgo//@version=5indicator("Trendlines With Breaks [LUX]",overlay=true)length = input.int(14)k = input.float(1.,'Slope',minval=0,step=.1)method = input.string('Atr','Slope Calculation Method',options=['Atr','Stdev','Linreg'])show = input(false,'Show Only Confirmed Breakouts')//----upper = 0.,lower = 0.slope_ph = 0.,slope_pl = 0.src = closen = bar_index//----ph = ta.pivothigh(length,length)pl = ta.pivotlow(length,length)slope = switch method'Atr' => ta.atr(length)/length*k'Stdev' => ta.stdev(src,length)/length*k'Linreg' => math.abs(ta.sma(src*bar_index,length)-ta.sma(src,length)*ta.sma(bar_index,length))/ta.variance(n,length)/2*kslope_ph := ph ? slope : slope_ph[1]slope_pl := pl ? slope : slope_pl[1]upper := ph ? ph : upper[1] - slope_phlower := pl ? pl : lower[1] + slope_pl//----single_upper = 0single_lower = 0single_upper := src[length] > upper ? 0 : ph ? 1 : single_upper[1]single_lower := src[length] < lower ? 0 : pl ? 1 : single_lower[1]upper_breakout = single_upper[1] and src[length] > upper and (show ? src > src[length] : 1)lower_breakout = single_lower[1] and src[length] < lower and (show ? src < src[length] : 1)plotshape(upper_breakout ? low[length] : na,"Upper Break",shape.labelup,location.absolute,#26a69a,-length,text="B",textcolor=color.white,size=size.tiny)plotshape(lower_breakout ? high[length] : na,"Lower Break",shape.labeldown,location.absolute,#ef5350,-length,text="B",textcolor=color.white,size=size.tiny)//----var line up_l = navar line dn_l = navar label recent_up_break = navar label recent_dn_break = naif ph[1]line.delete(up_l[1])label.delete(recent_up_break[1])up_l := line.new(n-length-1,ph[1],n-length,upper,color=#26a69a,extend=extend.right,style=line.style_dashed)if pl[1]line.delete(dn_l[1])label.delete(recent_dn_break[1])dn_l := line.new(n-length-1,pl[1],n-length,lower,color=#ef5350,extend=extend.right,style=line.style_dashed)if ta.crossover(src,upper-slope_ph*length)label.delete(recent_up_break[1])recent_up_break := label.new(n,low,'B',color=#26a69a,textcolor=color.white,style=label.style_label_up,size=size.small)if ta.crossunder(src,lower+slope_pl*length)label.delete(recent_dn_break[1])recent_dn_break := label.new(n,high,'B',color=#ef5350,textcolor=color.white,style=label.style_label_down,size=size.small)//----plot(upper,'Upper',color = ph ? na : #26a69a,offset=-length)plot(lower,'Lower',color = pl ? na : #ef5350,offset=-length)alertcondition(ta.crossover(src,upper-slope_ph*length),'Upper Breakout','Price broke upper trendline')alertcondition(ta.crossunder(src,lower+slope_pl*length),'Lower Breakout','Price broke lower trendline')05/06/2022 at 7:53 AM #192805Hi, please next time add a description and a screenshot in order for us to understand what the code is about and how it could be translated into prorealtime code.
I’ll have to make some simplification on that code in order to get it ready in a short time (no trendlines extension and no deletion of previous ones, will see how it goes..)
05/06/2022 at 8:39 AM #192807Ok, here is a non finalized translation, in fact I spent 1 hour recoding it while I understood the indicator is repainting, it plots the breakouts signals in the past, and only the confirmed ones. So it is useless in real time trading.
Anyway, for the record here is what I did so far.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127length = 14k = 1.0 //('Slope',minval=0,step=.1)method = 0 //'Slope Calculation Method' 0='Atr' 1='Stdev' 2='Linreg'//show = input(false,'Show Only Confirmed Breakouts')//----upper = 0lower = 0slopeph = 0slopepl = 0src = closen = max(1,barindex)//----once cp = length/* = ta.pivothigh(length,length)pl = ta.pivotlow(length,length)*/if high[cp] >= highest[2*cp+1](high) thenLH = 1elseLH=0endifif low[cp] <= lowest[2*cp+1](low) thenLL= -1elseLL=0endifif LH=1 thenph = high[cp]endifif LL = -1 thenpl=low[cp]endif//slopeif method = 0 thenslope = AverageTrueRange[length](close)/length*kelsif method = 1 thenslope = STD[length](close)/length*kelse//'Linreg' => math.abs(ta.sma(src*bar_index,length)-ta.sma(src,length)*ta.sma(bar_index,length))/ta.variance(n,length)/2*kivalue=nmperiod=lengthm=0s=0oldm=0for k=0 to mperiod-1 dooldm = mm = m+(ivalue[k]-m)/(1.0+k)s = s+(ivalue[k]-m)*(ivalue[k]-oldm)nextvariance = (s/(mperiod-1))slope = abs(average[length](src*barindex)-average[length](src)*average[length](barindex))/variance/2*kendif//slope_ph := ph ? slope : slope_ph[1]//upper := ph ? ph : upper[1] - slope_phif ph<>ph[1]thenslopeph=slopeupper=phelseslopeph=slopeph[1]upper=upper[1] - slopephendif//slope_pl := pl ? slope : slope_pl[1]//lower := pl ? pl : lower[1] + slope_plif pl<>pl[1] thenslopepl=slopelower=plelseslopepl=slopepl[1]lower=lower[1] + slopeplendif//----singleupper = 0singlelower = 0//single_upper := src[length] > upper ? 0 : ph ? 1 : single_upper[1]if src[length]>upper thensingleupper=0elseif ph<>ph[1] thensingleupper=1elsesingleupper=singleupper[1]endifendif//single_lower := src[length] < lower ? 0 : pl ? 1 : single_lower[1]if src[length]<lower thensinglelower=0elseif pl<>pl[1] thensinglelower=1elsesinglelower=singlelower[1]endifendif//upper_breakout = single_upper[1] and src[length] > upper and (show ? src > src[length] : 1)upperbreakout = singleupper[1] and src[length] > upper and src > src[length]//lower_breakout = single_lower[1] and src[length] < lower and (show ? src < src[length] : 1)lowerbreakout = singlelower[1] and src[length] < lower and src < src[length]//plotshape(upper_breakout ? low[length] : na,"Upper Break",shape.labelup,location.absolute,#26a69a,-length,text="B",textcolor=color.white,size=size.tiny)if upperbreakout thendrawtext("B",barindex[length],low[length]) coloured("green")endif//plotshape(lower_breakout ? high[length] : na,"Lower Break",shape.labeldown,location.absolute,#ef5350,-length,text="B",textcolor=color.white,size=size.tiny)if lowerbreakout thendrawtext("B",barindex[length],high[length]) coloured("red")endifif ph[1]<>ph[2] then//line.delete(up_l[1])//label.delete(recent_up_break[1])//up_l := line.new(n-length-1,ph[1],n-length,upper,color=#26a69a,//extend=extend.right,style=line.style_dashed)drawray(n-length-1,ph[1],n-length,upper) coloured(38,166,154)endifif pl[1]<>pl[2] then/*line.delete(dn_l[1])label.delete(recent_dn_break[1])dn_l := line.new(n-length-1,pl[1],n-length,lower,color=#ef5350,extend=extend.right,style=line.style_dashed)*/drawray(n-length-1,pl[1],n-length,lower) coloured(239,83,80)endifreturn1 user thanked author for this post.
05/06/2022 at 12:57 PM #19281605/06/2022 at 1:00 PM #192817https://www.tradingview.com/script/IYL88A1N-Trendlines-with-Breaks-LUX/
I don’t know how to add picture. But here is the link to how it should looks like.
05/09/2022 at 8:59 AM #192922Yes i know how it should look like, but I think you did not understand what I explained 🙂
That indicators plots the trendlines and signals in the past, which is useless if you agree, that’s why I stopped spending time on a 1:1 conversion and post the code in its current state.
03/19/2023 at 11:13 AM #211728hi Nicolas,
i recently started using the luxalgo indicator on trading view.
i wanted to use this logic for pre-real code screener for identifying break outs.
screener code for breakout candles12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091// inpout valueslength = 14k = 1.0method = 0 //'Slope Calculation Method' 0='Atr' 1='Stdev' 2='Linreg'//show = input(false,'Show Only Confirmed Breakouts')//--initialise variablesupper = 0lower = 0slopeph = 0src = closen = max(1,barindex)//----once cp = lengthif high[cp] >= highest[2*cp+1](high) thenLH = 1elseLH=0endifif low[cp] <= lowest[2*cp+1](low) thenLL= -1elseLL=0endifif LH=1 thenph = high[cp]endifif LL = -1 thenpl=low[cp]endif//slopeif method = 0 thenslope = AverageTrueRange[length](close)/length*kelsif method = 1 thenslope = STD[length](close)/length*kelse//'Linreg' => math.abs(ta.sma(src*bar_index,length)-ta.sma(src,length)*ta.sma(bar_index,length))/ta.variance(n,length)/2*kivalue=nmperiod=lengthm=0s=0oldm=0for k=0 to mperiod-1 dooldm = mm = m+(ivalue[k]-m)/(1.0+k)s = s+(ivalue[k]-m)*(ivalue[k]-oldm)nextvariance = (s/(mperiod-1))slope = abs(average[length](src*barindex)-average[length](src)*average[length](barindex))/variance/2*kendif//slope_ph := ph ? slope : slope_ph[1]//upper := ph ? ph : upper[1] - slope_phif ph<>ph[1]thenslopeph=slopeupper=phelseslopeph=slopeph[1]upper=upper[1] - slopephendifsingleupper = 0singlelower = 0//single_upper := src[length] > upper ? 0 : ph ? 1 : single_upper[1]if src[length]>upper thensingleupper=0elseif ph<>ph[1] thensingleupper=1elsesingleupper=singleupper[1]endifendif//single_lower := src[length] < lower ? 0 : pl ? 1 : single_lower[1]if src[length]<lower thensinglelower=0elseif pl<>pl[1] thensinglelower=1elsesinglelower=singlelower[1]endifendif//upper_breakout = single_upper[1] and src[length] > upper and (show ? src > src[length] : 1)upperbreakout = singleupper[1] and src[length] > upper and src > src[length]SCREENER(upperbreakout as "close")03/20/2023 at 10:31 AM #211777 -
AuthorPosts