Help porting Donchian Trend Ribbon

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #214596 quote
    Lavallette
    Participant
    Average

    Hello All,

    I’m interested in Donchian Trend Ribbon available in TradingView https://www.tradingview.com/script/PxLqmP8o-Donchian-Trend-Ribbon/ .

    I started porting it to ProRealTime but I’m struggling. This is my code so far. It could be improved with loops etc. but I wanted to keep it simple until I have something working.

    Len = 30
    
    hh = Highest[Len]
    ll = Lowest[Len]
    
    maintrend = 0
    IF close > hh[1] THEN
    maintrend = 1
    ELSIF close < ll[1] THEN
    maintrend = -1
    ENDIF
    
    trend = 0
    IF close > hh[1] THEN
    trend = 1
    ELSIF close < ll[1] THEN
    trend = -1
    ENDIF
    
    //a=0
    IF maintrend = 1 THEN
    IF trend = 1 THEN
    a = 1
    ELSE
    a = 1
    endif
    endif
    IF maintrend = -1 THEN
    IF trend = -1 THEN
    a = -1
    ELSE
    a = -1
    endif
    endif
    ////////////////////
    hh = Highest[Len-1]
    ll = Lowest[Len-1]
    trend = 0
    IF close > hh[1] THEN
    trend = 1
    ELSIF close < ll[1] THEN
    trend = -1
    ENDIF
    
    //a1=0
    IF maintrend = 1 THEN
    IF trend = 1 THEN
    a1 = 1
    ELSE
    a1 = 1
    endif
    endif
    IF maintrend = -1 THEN
    IF trend = -1 THEN
    a1 = -1
    ELSE
    a1 = -1
    endif
    endif
    ////////////////////
    hh = Highest[Len-2]
    ll = Lowest[Len-2]
    
    trend = 0
    IF close > hh[1] THEN
    trend = 1
    ELSIF close < ll[1] THEN
    trend = -1
    ENDIF
    
    //a2=0
    IF maintrend = 1 THEN
    IF trend = 1 THEN
    a2 = 1
    ELSE
    a2 = 1
    endif
    endif
    IF maintrend = -1 THEN
    IF trend = -1 THEN
    a2 = -1
    ELSE
    a2 = -1
    endif
    endif
    ////////////////////
    hh = Highest[Len-3]
    ll = Lowest[Len-3]
    
    trend = 0
    IF close > hh[1] THEN
    trend = 1
    ELSIF close < ll[1] THEN
    trend = -1
    ENDIF
    
    //a3=0
    IF maintrend = 1 THEN
    IF trend = 1 THEN
    a3 = 1
    ELSE
    a3 = 1
    endif
    endif
    IF maintrend = -1 THEN
    IF trend = -1 THEN
    a3 = -1
    ELSE
    a3 = -1
    endif
    endif
    ////////////////////
    hh = Highest[Len-4]
    ll = Lowest[Len-4]
    
    trend = 0
    IF close > hh[1] THEN
    trend = 1
    ELSIF close < ll[1] THEN
    trend = -1
    ENDIF
    
    //a4=0
    IF maintrend = 1 THEN
    IF trend = 1 THEN
    a4 = 1
    ELSE
    a4 = 1
    endif
    endif
    IF maintrend = -1 THEN
    IF trend = -1 THEN
    a4 = -1
    ELSE
    a4 = -1
    endif
    endif
    ////////////////////
    hh = Highest[Len-5]
    ll = Lowest[Len-5]
    
    trend = 0
    IF close > hh[1] THEN
    trend = 1
    ELSIF close < ll[1] THEN
    trend = -1
    ENDIF
    
    //a5=0
    IF maintrend = 1 THEN
    IF trend = 1 THEN
    a5 = 1
    ELSE
    a5 = 1
    endif
    endif
    IF maintrend = -1 THEN
    IF trend = -1 THEN
    a5 = -1
    ELSE
    a5 = -1
    endif
    endif
    ////////////////////
    hh = Highest[Len-6]
    ll = Lowest[Len-6]
    
    trend = 0
    IF close > hh[1] THEN
    trend = 1
    ELSIF close < ll[1] THEN
    trend = -1
    ENDIF
    
    //a6=0
    IF maintrend = 1 THEN
    IF trend = 1 THEN
    a6 = 1
    ELSE
    a6 = 1
    endif
    endif
    IF maintrend = -1 THEN
    IF trend = -1 THEN
    a6 = -1
    ELSE
    a6 = -1
    endif
    endif
    ////////////////////
    hh = Highest[Len-7]
    ll = Lowest[Len-7]
    
    trend = 0
    IF close > hh[1] THEN
    trend = 1
    ELSIF close < ll[1] THEN
    trend = -1
    ENDIF
    
    //a7=0
    IF maintrend = 1 THEN
    IF trend = 1 THEN
    a7 = 1
    ELSE
    a7 = 1
    endif
    endif
    IF maintrend = -1 THEN
    IF trend = -1 THEN
    a7 = -1
    ELSE
    a7 = -1
    endif
    endif
    ////////////////////
    hh = Highest[Len-8]
    ll = Lowest[Len-8]
    
    //trend = 0
    IF close > hh[1] THEN
    trend = 1
    ELSIF close < ll[1] THEN
    trend = -1
    ENDIF
    
    //a8=0
    IF maintrend = 1 THEN
    IF trend = 1 THEN
    a8 = 1
    ELSE
    a8 = 1
    endif
    endif
    IF maintrend = -1 THEN
    IF trend = -1 THEN
    a8 = -1
    ELSE
    a8 = -1
    endif
    endif
    ////////////////////
    hh = Highest[Len-9]
    ll = Lowest[Len-9]
    
    //trend = 0
    IF close > hh[1] THEN
    trend = 1
    ELSIF close < ll[1] THEN
    trend = -1
    ENDIF
    
    //a9=0
    IF maintrend = 1 THEN
    IF trend = 1 THEN
    a9 = 1
    ELSE
    a9 = 1
    endif
    endif
    IF maintrend = -1 THEN
    IF trend = -1 THEN
    a9 = -1
    ELSE
    a9 = -1
    endif
    endif
    
    return a + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9
    As I’m not sure it is possible to achieve the same kind in graphical display (with all the plots) I wanted to replace it with a value, i.e. summing the different results. Any advice/help would be more than welcomed. Thanks. This is the TradinvView code
    // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
    // © LonesomeTheBlue
    
    //@version=4
    study("Donchian Trend Ribbon", precision = 0)
    dlen = input(defval = 20, title = "Donchian Channel Period", minval = 10)
    
    dchannel(len)=>
        float hh = highest(len)
        float ll = lowest(len)
        
        int trend = 0
        trend := close > hh[1] ? 1 : close < ll[1] ? -1 : nz(trend[1])
        
    dchannelalt(len, maintrend)=>
        float hh = highest(len)
        float ll = lowest(len)
        
        int trend = 0
        trend := close > hh[1] ? 1 : close < ll[1] ? -1 : nz(trend[1])
        maintrend == 1  ? trend == 1  ? #00FF00ff :  #00FF009f :
         maintrend == -1 ? trend == -1 ? #FF0000ff :  #FF00009f :
         na
    
    maintrend = dchannel(dlen)
    
    plot( 5, color = dchannelalt(dlen - 0, maintrend), style = plot.style_columns, histbase= 0)
    plot(10, color = dchannelalt(dlen - 1, maintrend), style = plot.style_columns, histbase= 5)
    plot(15, color = dchannelalt(dlen - 2, maintrend), style = plot.style_columns, histbase=10)
    plot(20, color = dchannelalt(dlen - 3, maintrend), style = plot.style_columns, histbase=15)
    plot(25, color = dchannelalt(dlen - 4, maintrend), style = plot.style_columns, histbase=20)
    plot(30, color = dchannelalt(dlen - 5, maintrend), style = plot.style_columns, histbase=25)
    plot(35, color = dchannelalt(dlen - 6, maintrend), style = plot.style_columns, histbase=30)
    plot(40, color = dchannelalt(dlen - 7, maintrend), style = plot.style_columns, histbase=35)
    plot(45, color = dchannelalt(dlen - 8, maintrend), style = plot.style_columns, histbase=40)
    plot(50, color = dchannelalt(dlen - 9, maintrend), style = plot.style_columns, histbase=45)
    
    Screenshot-2023-05-12-at-11.57.39.png Screenshot-2023-05-12-at-11.57.39.png
    #214652 quote
    JS
    Participant
    Veteran

    Hi,

    Perhaps for inspiration…

    Once Len = 20
    
    hh = Highest[Len](High)
    ll = Lowest[Len](Low)
    
    For i=0 to 9
    IF close > hh[Len-i] THEN
    DrawRectangle(BarIndex,i,BarIndex+1,i+1)Coloured(0,255,0) //BorderColor(0,255,0)
    ElsIf close < ll[Len-i] then
    DrawRectangle(BarIndex,i,BarIndex+1,i+1)Coloured(255,0,0) //BorderColor(255,0,0)
    EndIf
    Next
    a=0
    b=10
    Return a Coloured(0,0,0), b Coloured(0,0,0)
    Lavallette thanked this post
    Scherm­afbeelding-2023-05-13-om-20.29.47.png Scherm­afbeelding-2023-05-13-om-20.29.47.png
    #214861 quote
    Lavallette
    Participant
    Average
    JS, Thanks a lot for your inspiration which helped a lot. I came up with the below code which is almost correct I believe.
    Len = 20
    
    hh = Highest[Len](High)
    ll = Lowest[Len](Low)
    
    IF close > hh[1] THEN
    trend = 1
    ELSIF close < ll[1] THEN
    trend = -1
    else
    trend = trend[1]
    ENDIF
    
    maintrend = trend
    
    
    For i = 0 to 9
    hh = Highest[Len-i](High)
    ll = Lowest[Len-i](Low)
    
    IF close > hh[Len-i] THEN
    trend = 1  
    ElsIf close < ll[Len-i] then
    trend = -1   
    else
    trend = trend[1]
    EndIf
    
    if maintrend = 1 then
    if trend = 1 then
    DrawRectangle(BarIndex,i,BarIndex+1,i+1) Coloured("GREEN") BorderColor("GREEN")
    else
    DrawRectangle(BarIndex,i,BarIndex+1,i+1) Coloured("DARKGREEN") BorderColor("DARKGREEN")
    endif
    endif
    if maintrend = -1 then
    if trend = -1 then
    DrawRectangle(BarIndex,i,BarIndex+1,i+1) Coloured("RED") BorderColor("RED")
    else
    DrawRectangle(BarIndex,i,BarIndex+1,i+1) Coloured("DARKRED") BorderColor("DARKRED")
    endif
    endif
    Next
    
    Return 0 Coloured(0,0,0,0), 10 Coloured(0,0,0,0)
    I’m however still very surprised with the differences between PRT and TV. See screen capture as an example but I get more or less the same kind of discrepancies with all the instruments. Does anybody have an idea what could be the reason? Thanks. PS: If somebody confirm the code is 100% correct I could publish it to the Library but I don’t want to do it if it contains errors.
    Screenshot-2023-05-17-at-20.44.28.jpg Screenshot-2023-05-17-at-20.44.28.jpg
    #214935 quote
    Lavallette
    Participant
    Average
    I believe I’m almost there with the latest version below but there is still a problem as for the same bar index the colors never change!!! Could anybody spot where the problem is? See image for a comparaison between the below code and TV. Thanks
    Len = 20
    
    if barindex > Len then
    hh = HIGHEST[Len](HIGH)
    ll = LOWEST[Len] (LOW)
    
    maintrend = 0
    if close > hh[1] then
    maintrend = 1
    elsif close < ll[1] then
    maintrend = -1
    else
    maintrend = maintrend[1]
    endif
    
    yaxis = 0
    for i = 20 downto 11 do
    hhh = HIGHEST[i](HIGH)
    lll = LOWEST[i] (LOW)
    
    trend = 0
    if close > hhh[1] then
    trend = 1
    elsif close < lll[1] then
    trend = -1
    else
    trend = trend[1]
    endIf
    
    if maintrend = 1 then
    if trend = 1 then
    DrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("GREEN") BorderColor("GREEN")
    elsif trend = -1 then
    DrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("DARKGREEN") BorderColor("DARKGREEN")
    endif
    elsif maintrend = -1 then
    if trend = -1 then
    DrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("RED") BorderColor("RED")
    elsif trend = 1 then
    DrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("DARKRED") BorderColor("DARKRED")
    endif
    endif
    yaxis = yaxis + 1
    next
    endif
    
    Return 0 Coloured(0,0,0,0), 10 Coloured(0,0,0,0)
    Screenshot-2023-05-20-at-15.30.49.jpg Screenshot-2023-05-20-at-15.30.49.jpg
    #214941 quote
    JS
    Participant
    Veteran

    Hi @Lavallette

    In the TV code, each bar is divided into 10 pieces:

    plot( 5, color = dchannelalt(dlen – 0, maintrend), style = plot.style_columns, histbase= 0)

    plot(10, color = dchannelalt(dlen – 1, maintrend), style = plot.style_columns, histbase= 5)

    plot(15, color = dchannelalt(dlen – 2, maintrend), style = plot.style_columns, histbase=10)

    plot(20, color = dchannelalt(dlen – 3, maintrend), style = plot.style_columns, histbase=15)

    plot(25, color = dchannelalt(dlen – 4, maintrend), style = plot.style_columns, histbase=20)

    plot(30, color = dchannelalt(dlen – 5, maintrend), style = plot.style_columns, histbase=25)

    plot(35, color = dchannelalt(dlen – 6, maintrend), style = plot.style_columns, histbase=30)

    plot(40, color = dchannelalt(dlen – 7, maintrend), style = plot.style_columns, histbase=35)

    plot(45, color = dchannelalt(dlen – 8, maintrend), style = plot.style_columns, histbase=40)

    plot(50, color = dchannelalt(dlen – 9, maintrend), style = plot.style_columns, histbase=45)

    Thus, each column (Bar) in the indicator consists of 10 pieces of 5 high (total 50 high). These pieces also have a different color so that you can see a difference per column (Bar)…

    In the PRT code, 1 rectangle is drawn per bar with 1 color and therefore you see no difference in color in the same bar index…

    What you can do is also make a subdivision (10 pieces) and draw a rectangle with a certain color ten times per bar…

    (Total height column is 50 divided into 10 pieces of 5)
    Lavallette thanked this post
    #214947 quote
    Lavallette
    Participant
    Average
    Thanks. Is this not what my code does? 10 rectangles are drawn per bar of size 1 by 1. Vertical y-axis goes from 0 to 10. 4 Colors are possible, Green , DarkGreen, Red or DarkRed. This is visible on the capture below. But why are some rectangles on the same x-axis not of 2 different colours?!?! Something is obviously wrong but I cannot find what! Still scratching my head.
    Screenshot-2023-05-20-at-18.15.58.png Screenshot-2023-05-20-at-18.15.58.png
    #214950 quote
    JS
    Participant
    Veteran

    Hi @Lavallette

    It’s starting to look more and more like it… 😉

    Len = 20
    
    if barindex > Len then
    hh = (HIGHEST[Len](HIGH))[1]
    ll = (LOWEST[Len] (LOW))[1]
    
    maintrend = 0
    if close > hh then
    maintrend = 1
    elsif close < ll then
    maintrend = -1
    else
    maintrend = maintrend[1]
    endif
    
    yaxis = 0
    for i = 20 downto 11 do
    hhh = (HIGHEST[i](HIGH))[1]
    lll = (LOWEST[i] (LOW))[1]
    
    trend = 0
    if close > hhh then
    trend = 1
    elsif close < lll then
    trend = -1
    else
    trend = trend[1]
    endIf
    
    if maintrend = 1 then
    if trend = 1 then
    DrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("GREEN") BorderColor("GREEN")
    elsif trend = -1 then
    DrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("DARKGREEN") BorderColor("DARKGREEN")
    endif
    elsif maintrend = -1 then
    if trend = -1 then
    DrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("RED") BorderColor("RED")
    elsif trend = 1 then
    DrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("DARKRED") BorderColor("DARKRED")
    endif
    endif
    yaxis = yaxis + 1
    next
    endif
    
    Return 0 Coloured(0,0,0,0), 10 Coloured(0,0,0,0)

    Scherm­afbeelding-2023-05-20-om-21.06.06.png Scherm­afbeelding-2023-05-20-om-21.06.06.png Scherm­afbeelding-2023-05-20-om-21.15.55.png Scherm­afbeelding-2023-05-20-om-21.15.55.png
    #214972 quote
    Lavallette
    Participant
    Average
    @SJ Thanks again for your time. I don’t understand why
    hhh = (HIGHEST[i](HIGH))[1]
    is different from
    hhh = HIGHEST[i](HIGH)
    and then referencing it with
    hhh[1]
    I would have thought it would be the same. Could this be a bug? Your latest version gives better result but still as you can see not completely correct.https://www.tradingview.com/script/PxLqmP8o-Donchian-Trend-Ribbon/ Could it be that way the “for loop” manages the hhh and lll series? In case you still want to spend some time on this thread you can test it as well from https://www.tradingview.com/script/PxLqmP8o-Donchian-Trend-Ribbon/ .   Anyway thanks a lot.
    Screenshot-2023-05-21-at-11.05.09.jpg Screenshot-2023-05-21-at-11.05.09.jpg
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

Help porting Donchian Trend Ribbon


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Lavallette @lavallette Participant
Summary

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

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 05/12/2023
Status: Active
Attachments: 8 files
Logo Logo
Loading...