Help porting Donchian Trend Ribbon
Forums › ProRealTime English forum › ProBuilder support › Help porting Donchian Trend Ribbon
- This topic has 7 replies, 2 voices, and was last updated 1 year ago by Lavallette.
-
-
05/12/2023 at 11:10 AM #214596
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.
PRT Donchian Channel123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269Len = 30hh = Highest[Len]ll = Lowest[Len]maintrend = 0IF close > hh[1] THENmaintrend = 1ELSIF close < ll[1] THENmaintrend = -1ENDIFtrend = 0IF close > hh[1] THENtrend = 1ELSIF close < ll[1] THENtrend = -1ENDIF//a=0IF maintrend = 1 THENIF trend = 1 THENa = 1ELSEa = 1endifendifIF maintrend = -1 THENIF trend = -1 THENa = -1ELSEa = -1endifendif////////////////////hh = Highest[Len-1]ll = Lowest[Len-1]trend = 0IF close > hh[1] THENtrend = 1ELSIF close < ll[1] THENtrend = -1ENDIF//a1=0IF maintrend = 1 THENIF trend = 1 THENa1 = 1ELSEa1 = 1endifendifIF maintrend = -1 THENIF trend = -1 THENa1 = -1ELSEa1 = -1endifendif////////////////////hh = Highest[Len-2]ll = Lowest[Len-2]trend = 0IF close > hh[1] THENtrend = 1ELSIF close < ll[1] THENtrend = -1ENDIF//a2=0IF maintrend = 1 THENIF trend = 1 THENa2 = 1ELSEa2 = 1endifendifIF maintrend = -1 THENIF trend = -1 THENa2 = -1ELSEa2 = -1endifendif////////////////////hh = Highest[Len-3]ll = Lowest[Len-3]trend = 0IF close > hh[1] THENtrend = 1ELSIF close < ll[1] THENtrend = -1ENDIF//a3=0IF maintrend = 1 THENIF trend = 1 THENa3 = 1ELSEa3 = 1endifendifIF maintrend = -1 THENIF trend = -1 THENa3 = -1ELSEa3 = -1endifendif////////////////////hh = Highest[Len-4]ll = Lowest[Len-4]trend = 0IF close > hh[1] THENtrend = 1ELSIF close < ll[1] THENtrend = -1ENDIF//a4=0IF maintrend = 1 THENIF trend = 1 THENa4 = 1ELSEa4 = 1endifendifIF maintrend = -1 THENIF trend = -1 THENa4 = -1ELSEa4 = -1endifendif////////////////////hh = Highest[Len-5]ll = Lowest[Len-5]trend = 0IF close > hh[1] THENtrend = 1ELSIF close < ll[1] THENtrend = -1ENDIF//a5=0IF maintrend = 1 THENIF trend = 1 THENa5 = 1ELSEa5 = 1endifendifIF maintrend = -1 THENIF trend = -1 THENa5 = -1ELSEa5 = -1endifendif////////////////////hh = Highest[Len-6]ll = Lowest[Len-6]trend = 0IF close > hh[1] THENtrend = 1ELSIF close < ll[1] THENtrend = -1ENDIF//a6=0IF maintrend = 1 THENIF trend = 1 THENa6 = 1ELSEa6 = 1endifendifIF maintrend = -1 THENIF trend = -1 THENa6 = -1ELSEa6 = -1endifendif////////////////////hh = Highest[Len-7]ll = Lowest[Len-7]trend = 0IF close > hh[1] THENtrend = 1ELSIF close < ll[1] THENtrend = -1ENDIF//a7=0IF maintrend = 1 THENIF trend = 1 THENa7 = 1ELSEa7 = 1endifendifIF maintrend = -1 THENIF trend = -1 THENa7 = -1ELSEa7 = -1endifendif////////////////////hh = Highest[Len-8]ll = Lowest[Len-8]//trend = 0IF close > hh[1] THENtrend = 1ELSIF close < ll[1] THENtrend = -1ENDIF//a8=0IF maintrend = 1 THENIF trend = 1 THENa8 = 1ELSEa8 = 1endifendifIF maintrend = -1 THENIF trend = -1 THENa8 = -1ELSEa8 = -1endifendif////////////////////hh = Highest[Len-9]ll = Lowest[Len-9]//trend = 0IF close > hh[1] THENtrend = 1ELSIF close < ll[1] THENtrend = -1ENDIF//a9=0IF maintrend = 1 THENIF trend = 1 THENa9 = 1ELSEa9 = 1endifendifIF maintrend = -1 THENIF trend = -1 THENa9 = -1ELSEa9 = -1endifendifreturn a + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9As 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
PRT code to Translate123456789101112131415161718192021222324252627282930313233343536// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © LonesomeTheBlue//@version=4study("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 = 0trend := close > hh[1] ? 1 : close < ll[1] ? -1 : nz(trend[1])dchannelalt(len, maintrend)=>float hh = highest(len)float ll = lowest(len)int trend = 0trend := close > hh[1] ? 1 : close < ll[1] ? -1 : nz(trend[1])maintrend == 1 ? trend == 1 ? #00FF00ff : #00FF009f :maintrend == -1 ? trend == -1 ? #FF0000ff : #FF00009f :namaintrend = 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)05/13/2023 at 7:32 PM #214652Hi,
Perhaps for inspiration…
Donchian Trend Ribbon123456789101112131415Once Len = 20hh = Highest[Len](High)ll = Lowest[Len](Low)For i=0 to 9IF close > hh[Len-i] THENDrawRectangle(BarIndex,i,BarIndex+1,i+1)Coloured(0,255,0) //BorderColor(0,255,0)ElsIf close < ll[Len-i] thenDrawRectangle(BarIndex,i,BarIndex+1,i+1)Coloured(255,0,0) //BorderColor(255,0,0)EndIfNexta=0b=10Return a Coloured(0,0,0), b Coloured(0,0,0)1 user thanked author for this post.
05/17/2023 at 7:53 PM #214861JS,
Thanks a lot for your inspiration which helped a lot.
I came up with the below code which is almost correct I believe.
Donchian Trend Ribbon123456789101112131415161718192021222324252627282930313233343536373839404142434445Len = 20hh = Highest[Len](High)ll = Lowest[Len](Low)IF close > hh[1] THENtrend = 1ELSIF close < ll[1] THENtrend = -1elsetrend = trend[1]ENDIFmaintrend = trendFor i = 0 to 9hh = Highest[Len-i](High)ll = Lowest[Len-i](Low)IF close > hh[Len-i] THENtrend = 1ElsIf close < ll[Len-i] thentrend = -1elsetrend = trend[1]EndIfif maintrend = 1 thenif trend = 1 thenDrawRectangle(BarIndex,i,BarIndex+1,i+1) Coloured("GREEN") BorderColor("GREEN")elseDrawRectangle(BarIndex,i,BarIndex+1,i+1) Coloured("DARKGREEN") BorderColor("DARKGREEN")endifendifif maintrend = -1 thenif trend = -1 thenDrawRectangle(BarIndex,i,BarIndex+1,i+1) Coloured("RED") BorderColor("RED")elseDrawRectangle(BarIndex,i,BarIndex+1,i+1) Coloured("DARKRED") BorderColor("DARKRED")endifendifNextReturn 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.
05/20/2023 at 2:34 PM #214935I 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
Latest version1234567891011121314151617181920212223242526272829303132333435363738394041424344454647Len = 20if barindex > Len thenhh = HIGHEST[Len](HIGH)ll = LOWEST[Len] (LOW)maintrend = 0if close > hh[1] thenmaintrend = 1elsif close < ll[1] thenmaintrend = -1elsemaintrend = maintrend[1]endifyaxis = 0for i = 20 downto 11 dohhh = HIGHEST[i](HIGH)lll = LOWEST[i] (LOW)trend = 0if close > hhh[1] thentrend = 1elsif close < lll[1] thentrend = -1elsetrend = trend[1]endIfif maintrend = 1 thenif trend = 1 thenDrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("GREEN") BorderColor("GREEN")elsif trend = -1 thenDrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("DARKGREEN") BorderColor("DARKGREEN")endifelsif maintrend = -1 thenif trend = -1 thenDrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("RED") BorderColor("RED")elsif trend = 1 thenDrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("DARKRED") BorderColor("DARKRED")endifendifyaxis = yaxis + 1nextendifReturn 0 Coloured(0,0,0,0), 10 Coloured(0,0,0,0)05/20/2023 at 3:59 PM #214941Hi @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)
1 user thanked author for this post.
05/20/2023 at 5:20 PM #214947Thanks.
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.
05/20/2023 at 8:14 PM #214950Hi @Lavallette
It’s starting to look more and more like it… 😉
Donchian Trend Ribbon1234567891011121314151617181920212223242526272829303132333435363738394041424344454647Len = 20if barindex > Len thenhh = (HIGHEST[Len](HIGH))[1]ll = (LOWEST[Len] (LOW))[1]maintrend = 0if close > hh thenmaintrend = 1elsif close < ll thenmaintrend = -1elsemaintrend = maintrend[1]endifyaxis = 0for i = 20 downto 11 dohhh = (HIGHEST[i](HIGH))[1]lll = (LOWEST[i] (LOW))[1]trend = 0if close > hhh thentrend = 1elsif close < lll thentrend = -1elsetrend = trend[1]endIfif maintrend = 1 thenif trend = 1 thenDrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("GREEN") BorderColor("GREEN")elsif trend = -1 thenDrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("DARKGREEN") BorderColor("DARKGREEN")endifelsif maintrend = -1 thenif trend = -1 thenDrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("RED") BorderColor("RED")elsif trend = 1 thenDrawRectangle(BarIndex,yaxis,BarIndex+1,yaxis+1) Coloured("DARKRED") BorderColor("DARKRED")endifendifyaxis = yaxis + 1nextendifReturn 0 Coloured(0,0,0,0), 10 Coloured(0,0,0,0)05/21/2023 at 10:22 AM #214972@SJ Thanks again for your time.
I don’t understand why
1hhh = (HIGHEST[i](HIGH))[1]is different from
1hhh = HIGHEST[i](HIGH)and then referencing it with
1hhh[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.
-
AuthorPosts
Find exclusive trading pro-tools on