Advertisement
xmd79

Fibonacci Structure Trend Channel (Expo)

Sep 8th, 2023
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. // 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/
  2. // © Zeiierman {
  3.  
  4. //@version=5
  5. indicator("Fibonacci Structure Trend Channel (Expo)",overlay=true, max_labels_count=500)
  6. //~~}
  7.  
  8. // ~~ Tooltips {
  9. t1 = "Determines the Fibonacci retracement level used for calculating the Trend Channel. The higher the value, the more significant the pullback before a reversal. Options include 0.236, 0.382, 0.50, 0.618, and 0.786."
  10. t2 = "Allows you to set a custom Fibonacci level. Enable this to override the default Fibonacci options. Value should be between 0.001 and 0.999. Higher values will mean more significant pullbacks before a reversal."
  11. t3 = "Enables or disables color-coding the price bars based on the current trend direction. Green for bullish and Red for bearish."
  12. t4 = "Determines the sensitivity of structure lines by setting the lookback period. A smaller value will produce more lines but might include noise. A larger value will be less sensitive but could miss shorter-term structures."
  13. t5 = "Enables or disables the Fibonacci Channel. This channel is calculated around the Fibonacci Trend and can provide additional insight into potential price action."
  14. t6 = "Controls the width of the Fibonacci Channel. The higher the value, the wider the channel. Width is based on the ATR (Average True Range)."
  15. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  16.  
  17. // ~~ Inputs {
  18. fibonacci = input.float(0.382,"Fibonacci Level  ",options=[0.236,0.382,0.50,0.618,0.786], group="Fib Settings", tooltip=t1)
  19. custom = input.bool(false,"Custom Value",inline="custom", group="Fib Settings")
  20. customfib = input.float(0.5,"",.001,.999,.001,inline="custom", group="Fib Settings", tooltip=t2)
  21. bull = input.color(color.lime,"Bullish Color",inline="col", group="")
  22. bear = input.color(color.red,"Bearish Color",inline="col", group="")
  23. barcol = input.bool(false,"Barcolor",inline="col", group="", tooltip=t3)
  24. structure = input.bool(true,title="Structure",inline="Structure", group="Fibonacci Structure")
  25. sen = input.int(5,title="", minval=1, maxval=200, inline="Structure", group="Fibonacci Structure", tooltip=t4)
  26. s_bull = input.color(color.rgb(203, 68, 140),"",inline="Structure color", group="Fibonacci Structure")
  27. s_bear = input.color(color.rgb(95, 75, 188),"", inline="Structure color", group="Fibonacci Structure")
  28. Channel = input.bool(false, title="Fibonacci Channel",inline="", group="Fibonacci Channel", tooltip=t5)
  29. mult = input.float(3.5, minval=1, maxval=10, step=0.1, title="Fib Channel width",inline="", group="Fibonacci Channel", tooltip=t6)
  30. max_col = input.color(color.rgb(95, 75, 188),"Upper Channel",inline="max", group="Fibonacci Channel")
  31. min_col = input.color(color.rgb(203, 68, 140),"Lower Channel",inline="max", group="Fibonacci Channel")
  32. levels = input.bool(true,"Fibonacci Levels",inline="level", group="Show Levels")
  33. lvl_lab = input.bool(true,"Labels",inline="level", group="Show Levels")
  34. line_s = input.string("Dotted","",options=["Solid","Dotted","Dashed"],inline="style", group="Show Levels")
  35. width = input.int(1, title=" ", inline="style", group="Show Levels")
  36. info = input.bool(true,"Fib Trend Information", group="Show Levels")
  37. Level_0236 = input.color(#F44336,"0.236",inline="fibcol", group="Color Fib Levels")
  38. Level_0382 = input.color(#81C784,"0.382",inline="fibcol", group="Color Fib Levels")
  39. Level_050 = input.color(#4CAF50,"0.50",inline="fibcol", group="Color Fib Levels")
  40. Level_0618 = input.color(#009688,"0.618",inline="fibcol", group="Color Fib Levels")
  41. Level_0786 = input.color(#64B5F6,"0.786",inline="fibcol", group="Color Fib Levels")
  42. Level_0 = input.color(#5f5f5f,"",inline="fibcol", group="Color Fib Levels")
  43. line_style = switch line_s
  44. "Solid" => line.style_solid
  45. "Dotted"=> line.style_dotted
  46. "Dashed"=> line.style_dashed
  47. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  48.  
  49. // ~~ Variables {
  50. b = bar_index
  51. var pos = 0
  52. var hi = high
  53. var lo = low
  54. var hloc = b
  55. var lloc = b
  56. var retrace = 0.0
  57. var t = string(na)
  58. fib = custom?customfib:fibonacci
  59. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  60.  
  61. // ~~ Track high/low and calculate fibonacci level {
  62. if pos>=0
  63. if high<retrace
  64. pos := -1
  65. lo := low
  66. lloc := time
  67. retrace := lo+(hi-lo)*fib
  68. t := str.tostring(dayofmonth)+"/"+str.tostring(month)+"/"+str.tostring(year)+" "
  69. if structure
  70. label.new(math.round(math.avg(hloc,lloc)),retrace[1],"▼",xloc.bar_time,
  71. color=color(na),style=label.style_label_up,textcolor=bear)
  72.  
  73. else if high>hi
  74. hi := high
  75. hloc := time
  76. retrace := hi-(hi-lo)*fib
  77. if structure and hi[1]==hi[sen]
  78. pos += 1
  79. line.new(hloc[1],hi[1],hloc,hi[1],xloc.bar_time,color=s_bear)
  80. if pos<=0
  81. if low>retrace
  82. pos := 1
  83. hi := high
  84. hloc := time
  85. retrace := hi-(hi-lo)*fib
  86. t := str.tostring(dayofmonth)+"/"+str.tostring(month)+"/"+str.tostring(year)+" "
  87. if structure
  88. label.new(math.round(math.avg(hloc,lloc)),retrace[1],"▲",xloc.bar_time,
  89. color=color(na),style=label.style_label_down,textcolor=bull)
  90.  
  91. else if low<lo
  92. lo := low
  93. lloc := time
  94. retrace := lo+(hi-lo)*fib
  95. if structure and lo[1]==lo[sen]
  96. pos := pos-1
  97. line.new(lloc[1],lo[1],lloc,lo[1],xloc.bar_time,color=s_bull)
  98. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  99.  
  100. // ~~ Fibonacci Channel {
  101. ma = ta.wma(retrace,10)
  102. atr = ta.sma(ta.atr(500)*mult,20)
  103. max = (ma + atr)
  104. min = (ma - atr)
  105. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  106.  
  107. // ~~ Plot & Barcolor {
  108. plot(retrace,"Fibonacci Trend",pos>0?bull:bear)
  109. plot(Channel?max:na,"Upper Fibonacci Channel",max_col)
  110. plot(Channel?min:na,"Lower Fibonacci Channel",min_col)
  111. barcolor(barcol?pos>0?bull:bear:na)
  112. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  113.  
  114. // ~~ Levels {
  115. if levels or lvl_lab
  116. var lines = array.new<line>(7)
  117. var labels= array.new<label>(7)
  118. var lvls = array.from(0,0.236,0.382,0.50,0.618,0.786,1)
  119. var cols = array.from(Level_0,Level_0236,Level_0382,Level_050,Level_0618,Level_0786,Level_0)
  120. //Create fibonacci level lines
  121. if na(lines.get(0))
  122. for [i,c] in cols
  123. lines.pop().delete()
  124. labels.pop().delete()
  125. lines.unshift(line.new(na,na,na,na,xloc.bar_time,color=c,style=line_style,width=width))
  126. labels.unshift(label.new(na,na,str.tostring(lvls.get(i)*100,format.percent),
  127. color=color(na),style=label.style_label_left,textcolor=c))
  128. lines.push(line.new(na,na,na,na,xloc.bar_time,color=cols.get(0),style=line_style,width=width))
  129. //Update xy
  130. for [i,lvl] in lvls
  131. if levels
  132. lines.get(i).set_xy1(lvl==0?pos<0?hloc:lloc:pos>0?hloc:lloc,pos>0?lo+(hi-lo)*lvl:hi-(hi-lo)*lvl)
  133. lines.get(i).set_xy2(timenow,pos>0?lo+(hi-lo)*lvl:hi-(hi-lo)*lvl)
  134. if lvl_lab
  135. labels.get(i).set_xy(b,pos>0?lo+(hi-lo)*lvl:hi-(hi-lo)*lvl)
  136. if levels
  137. lines.get(7).set_xy1(pos<0?hloc:lloc,pos<0?hi:lo)
  138. lines.get(7).set_xy2(pos>0?hloc:lloc,pos<0?lo:hi)
  139. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  140.  
  141. // ~~ Table {
  142. if barstate.islast and info
  143. var tbl = table.new(position.top_right,1,3,chart.bg_color,chart.fg_color,2)
  144. col = pos>0?color.new(bull,75):color.new(bear,75)
  145. tbl.cell(0,0,pos>0?"Bullish":"Bearish",bgcolor=col,
  146. text_color=chart.fg_color)
  147. tbl.cell(0,1,"Trend Level: "+str.tostring(retrace,format.mintick),bgcolor=col,
  148. text_color=chart.fg_color)
  149. tbl.cell(0,2,"Trend started: "+t,bgcolor=col,
  150. text_color=chart.fg_color)
  151. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement