Trendlines with Breaks [LUX]

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #192796 quote
    Kee
    Participant
    New

    This code is from trading view, please help me to convert. Thank you in advance.

    // 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=5
    indicator("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 = close
    n = 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*k
    
    slope_ph := ph ? slope : slope_ph[1]
    slope_pl := pl ? slope : slope_pl[1]
    
    upper := ph ? ph : upper[1] - slope_ph
    lower := pl ? pl : lower[1] + slope_pl
    //----
    single_upper = 0
    single_lower = 0
    single_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 = na
    var line dn_l = na
    var label recent_up_break = na
    var label recent_dn_break = na
    
    if 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')
    #192805 quote
    Nicolas
    Keymaster
    Master

    Hi, 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..)

    #192807 quote
    Nicolas
    Keymaster
    Master

    Ok, 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.

    length = 14
    k = 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 = 0
    lower = 0
    slopeph = 0
    slopepl = 0
    src = close
    n = max(1,barindex)
    //----
    once cp = length
    /* = ta.pivothigh(length,length)
    pl = ta.pivotlow(length,length)*/
    if high[cp] >= highest[2*cp+1](high) then
    LH = 1
    else
    LH=0
    endif
    if low[cp] <= lowest[2*cp+1](low)  then
    LL= -1
    else
    LL=0
    endif
    if LH=1 then
    ph = high[cp]
    endif
    if LL  = -1 then
    pl=low[cp]
    endif
    //slope
    if method = 0 then 
    slope = AverageTrueRange[length](close)/length*k
    elsif method = 1 then 
    slope = STD[length](close)/length*k
    else
    //'Linreg' => math.abs(ta.sma(src*bar_index,length)-ta.sma(src,length)*ta.sma(bar_index,length))/ta.variance(n,length)/2*k
    ivalue=n
    mperiod=length
    m=0
    s=0
    oldm=0
    for k=0 to mperiod-1 do
    oldm = m
    m    = m+(ivalue[k]-m)/(1.0+k)
    s    = s+(ivalue[k]-m)*(ivalue[k]-oldm)
    next
    variance = (s/(mperiod-1))
    slope = abs(average[length](src*barindex)-average[length](src)*average[length](barindex))/variance/2*k
    endif
    
    //slope_ph := ph ? slope : slope_ph[1]
    //upper := ph ? ph : upper[1] - slope_ph
    if ph<>ph[1]then 
    slopeph=slope
    upper=ph
    else
    slopeph=slopeph[1]
    upper=upper[1] - slopeph
    endif
    
    //slope_pl := pl ? slope : slope_pl[1]
    //lower := pl ? pl : lower[1] + slope_pl
    if pl<>pl[1] then 
    slopepl=slope
    lower=pl
    else
    slopepl=slopepl[1]
    lower=lower[1] + slopepl
    endif
    //----
    singleupper = 0
    singlelower = 0
    //single_upper := src[length] > upper ? 0 : ph ? 1 : single_upper[1]
    if src[length]>upper then 
    singleupper=0
    else
    if ph<>ph[1] then 
    singleupper=1
    else
    singleupper=singleupper[1]
    endif
    endif
    //single_lower := src[length] < lower ? 0 : pl ? 1 : single_lower[1]
    if src[length]<lower then 
    singlelower=0
    else
    if pl<>pl[1] then 
    singlelower=1
    else
    singlelower=singlelower[1]
    endif 
    endif 
    
    //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 then 
    drawtext("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 then
    drawtext("B",barindex[length],high[length]) coloured("red")
    endif
    
    if 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)
    endif
    if 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)
    endif
    
    return
    GraHal thanked this post
    #192816 quote
    Kee
    Participant
    New

    Noted. I will do that next time. I am sorry. New user here.

    #192817 quote
    Kee
    Participant
    New

    https://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.

    #192922 quote
    Nicolas
    Keymaster
    Master

    Yes 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.

    #211728 quote
    kumar_AK
    Participant
    Junior

    hi 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.

    // inpout values
    length = 14
    k = 1.0
    method = 0 //'Slope Calculation Method' 0='Atr' 1='Stdev' 2='Linreg'
    //show = input(false,'Show Only Confirmed Breakouts')
    
    //--initialise variables
    upper = 0
    lower = 0
    slopeph = 0
    
    src = close
    n = max(1,barindex)
    //----
    once cp = length
    
    if high[cp] >= highest[2*cp+1](high) then
    LH = 1
    else
    LH=0
    endif
    if low[cp] <= lowest[2*cp+1](low)  then
    LL= -1
    else
    LL=0
    endif
    if LH=1 then
    ph = high[cp]
    endif
    if LL  = -1 then
    pl=low[cp]
    endif
    //slope
    if method = 0 then
    slope = AverageTrueRange[length](close)/length*k
    elsif method = 1 then
    slope = STD[length](close)/length*k
    else
    //'Linreg' => math.abs(ta.sma(src*bar_index,length)-ta.sma(src,length)*ta.sma(bar_index,length))/ta.variance(n,length)/2*k
    ivalue=n
    mperiod=length
    m=0
    s=0
    oldm=0
    for k=0 to mperiod-1 do
    oldm = m
    m    = m+(ivalue[k]-m)/(1.0+k)
    s    = s+(ivalue[k]-m)*(ivalue[k]-oldm)
    next
    variance = (s/(mperiod-1))
    slope = abs(average[length](src*barindex)-average[length](src)*average[length](barindex))/variance/2*k
    endif
    
    //slope_ph := ph ? slope : slope_ph[1]
    //upper := ph ? ph : upper[1] - slope_ph
    if ph<>ph[1]then
    slopeph=slope
    upper=ph
    else
    slopeph=slopeph[1]
    upper=upper[1] - slopeph
    endif
    
    singleupper = 0
    singlelower = 0
    //single_upper := src[length] > upper ? 0 : ph ? 1 : single_upper[1]
    if src[length]>upper then
    singleupper=0
    else
    if ph<>ph[1] then
    singleupper=1
    else
    singleupper=singleupper[1]
    endif
    endif
    //single_lower := src[length] < lower ? 0 : pl ? 1 : single_lower[1]
    if src[length]<lower then
    singlelower=0
    else
    if pl<>pl[1] then
    singlelower=1
    else
    singlelower=singlelower[1]
    endif
    endif
    
    //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")
    
    #211777 quote
    Nicolas
    Keymaster
    Master

    Change the last line with:

    SCREENER[upperbreakout]

    conditions to test must be under brackets, parenthesis are used for sorting criteria.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

Trendlines with Breaks [LUX]


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Kee @kee Participant
Summary

This topic contains 7 replies,
has 3 voices, and was last updated by Nicolas
2 years, 10 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 05/06/2022
Status: Active
Attachments: 1 files
Logo Logo
Loading...