xmd79

ZigZag Multi Time Frame with Fibonacci Retracement

Dec 1st, 2021
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © LonesomeTheBlue
  3.  
  4. //@version=4
  5. study("ZigZag Multi Time Frame with Fibonacci Retracement", "ZZMTF", overlay = true, max_bars_back = 1000)
  6. tf = input(defval = 'D', title="ZigZag Resolution", type = input.resolution)
  7. prd = input(defval = 2, title="ZigZag Period", minval = 2, maxval = 10)
  8. showzigzag = input(defval = false, title = "Show Zig Zag")
  9. showfibo = input(defval = true, title = "Show Fibonacci Ratios")
  10. colorfulfibo = input(defval = false, title = "Colorful Fibonacci Levels")
  11. labelcol = input(defval = color.blue, title = "Text Color")
  12. fibolinecol = input(defval = color.lime, title = "Line Color")
  13. upcol = input(defval = color.lime, title = "Zigzag Line Colors", inline = "zzcol")
  14. dncol = input(defval = color.red, title = "", inline = "zzcol")
  15. labelloc = input(defval = "Left", title = "Label Location", options = ["Left", "Right"])
  16. enable236 = input(defval = true, title = "Enable Level 0.236")
  17. enable382 = input(defval = true, title = "Enable Level 0.382")
  18. enable500 = input(defval = true, title = "Enable Level 0.500")
  19. enable618 = input(defval = true, title = "Enable Level 0.618")
  20. enable786 = input(defval = true, title = "Enable Level 0.786")
  21.  
  22. bool newbar = change(time(tf)) != 0
  23.  
  24. bi = valuewhen(newbar, bar_index, prd - 1)
  25. len = bar_index - bi + 1
  26.  
  27. float ph = na
  28. float pl = na
  29. ph := highestbars(high, nz(len, 1)) == 0 ? high : na
  30. pl := lowestbars(low, nz(len, 1)) == 0 ? low : na
  31.  
  32. var dir = 0
  33. dir := iff(ph and na(pl), 1, iff(pl and na(ph), -1, dir))
  34. var max_array_size = 50
  35. var zigzag = array.new_float(0)
  36. oldzigzag = array.copy(zigzag)
  37.  
  38. add_to_zigzag(value, bindex)=>
  39. array.unshift(zigzag, bindex)
  40. array.unshift(zigzag, value)
  41. if array.size(zigzag) > max_array_size
  42. array.pop(zigzag)
  43. array.pop(zigzag)
  44.  
  45. update_zigzag(value, bindex)=>
  46. if array.size(zigzag) == 0
  47. add_to_zigzag(value, bindex)
  48. else
  49. if (dir == 1 and value > array.get(zigzag, 0)) or (dir == -1 and value < array.get(zigzag, 0))
  50. array.set(zigzag, 0, value)
  51. array.set(zigzag, 1, bindex)
  52. 0.
  53.  
  54. bool dirchanged = (dir != dir[1])
  55. if ph or pl
  56. if dirchanged
  57. add_to_zigzag(dir == 1 ? ph : pl, bar_index)
  58. else
  59. update_zigzag(dir == 1 ? ph : pl, bar_index)
  60.  
  61. if showzigzag and array.size(zigzag) >= 4 and array.size(oldzigzag) >= 4
  62. var line zzline = na
  63. if array.get(zigzag, 0) != array.get(oldzigzag, 0) or array.get(zigzag, 1) != array.get(oldzigzag, 1)
  64. if array.get(zigzag, 2) == array.get(oldzigzag, 2) and array.get(zigzag, 3) == round(array.get(oldzigzag, 3))
  65. line.delete(zzline)
  66. zzline := line.new(x1 = round(array.get(zigzag, 1)), y1 = array.get(zigzag, 0), x2 = round(array.get(zigzag, 3)), y2 = array.get(zigzag, 2), color = dir == 1 ? upcol : dncol, width = 2)
  67.  
  68. if not showzigzag and array.size(zigzag) >= 6
  69. var line zzline = na
  70. line.delete(zzline)
  71. zzline := line.new(x1 = round(array.get(zigzag, 3)), y1 = array.get(zigzag, 2), x2 = round(array.get(zigzag, 5)), y2 = array.get(zigzag, 4), color = dir == 1 ? upcol : dncol, width = 2, style = line.style_dotted)
  72.  
  73. var fibo_ratios = array.new_float(0)
  74. var fibo_colors = array.new_color(10)
  75. var shownlevels = 1
  76. if barstate.isfirst
  77. array.push(fibo_ratios, 0.000)
  78. if enable236
  79. array.push(fibo_ratios, 0.236)
  80. shownlevels := shownlevels + 1
  81. if enable382
  82. array.push(fibo_ratios, 0.382)
  83. shownlevels := shownlevels + 1
  84. if enable500
  85. array.push(fibo_ratios, 0.500)
  86. shownlevels := shownlevels + 1
  87. if enable618
  88. array.push(fibo_ratios, 0.618)
  89. shownlevels := shownlevels + 1
  90. if enable786
  91. array.push(fibo_ratios, 0.786)
  92. shownlevels := shownlevels + 1
  93. for x = 1 to 5
  94. array.push(fibo_ratios, x)
  95. array.push(fibo_ratios, x + 0.272)
  96. array.push(fibo_ratios, x + 0.414)
  97. array.push(fibo_ratios, x + 0.618)
  98.  
  99. // set colors
  100. array.set(fibo_colors, 0, color.lime)
  101. array.set(fibo_colors, 1, color.silver)
  102. array.set(fibo_colors, 2, color.gray)
  103. array.set(fibo_colors, 3, color.red)
  104. array.set(fibo_colors, 4, color.purple)
  105. array.set(fibo_colors, 5, color.fuchsia)
  106. array.set(fibo_colors, 6, color.olive)
  107. array.set(fibo_colors, 7, color.navy)
  108. array.set(fibo_colors, 8, color.teal)
  109. array.set(fibo_colors, 9, color.orange)
  110.  
  111. var fibolines = array.new_line(0)
  112. var fibolabels = array.new_label(0)
  113. if showfibo and array.size(zigzag) >= 6 and barstate.islast
  114. if array.size(fibolines) > 0
  115. for x = 0 to array.size(fibolines) - 1
  116. line.delete(array.get(fibolines, x))
  117. label.delete(array.get(fibolabels, x))
  118.  
  119. diff = array.get(zigzag, 4) - array.get(zigzag, 2)
  120. stopit = false
  121. for x = 0 to array.size(fibo_ratios) - 1
  122. if stopit and x > shownlevels
  123. break
  124. fibo_line_col = colorfulfibo ? array.get(fibo_colors, ((timenow / 1000) * x) % 10) : fibolinecol
  125. array.unshift(fibolines,
  126. line.new(x1 = round(array.get(zigzag, 5)),
  127. y1 = array.get(zigzag, 2) + diff * array.get(fibo_ratios, x),
  128. x2 = bar_index,
  129. y2 = array.get(zigzag, 2) + diff * array.get(fibo_ratios, x),
  130. color = fibo_line_col,
  131. extend = extend.right))
  132. label_x_loc = labelloc == "Left" ? round(array.get(zigzag, 5)) - 1 : bar_index
  133. txt = labelloc == "Left" ? "" : "                  "
  134. array.unshift(fibolabels,
  135. label.new( x = label_x_loc,
  136. y = array.get(zigzag, 2) + diff * array.get(fibo_ratios, x),
  137. text = txt + tostring(array.get(fibo_ratios, x), '#.###') + "(" + tostring(round_to_mintick(array.get(zigzag, 2) + diff * array.get(fibo_ratios, x))) + ")",
  138. textcolor = labelcol,
  139. style = label.style_none))
  140. if (dir == 1 and array.get(zigzag, 2) + diff * array.get(fibo_ratios, x) > array.get(zigzag, 0)) or
  141. (dir == -1 and array.get(zigzag, 2) + diff * array.get(fibo_ratios, x) < array.get(zigzag, 0))
  142. stopit := true
  143.  
Advertisement
Add Comment
Please, Sign In to add comment