xmd79

Gann Fan

Dec 1st, 2022
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.99 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=5
  5. indicator("Gann Fan", overlay = true, max_bars_back = 400)
  6. // get colors into the array
  7. var color [] colors = array.from(
  8. input(defval = color.red, title = "1/2", inline = "c1", group = "colors"),
  9. input(defval = color.green, title = "1/3", inline = "c1", group = "colors"),
  10. input(defval = #0097a7, title = "1/", inline = "c1", group = "colors"),
  11. input(defval = color.olive, title = "1/8", inline = "c1", group = "colors"),
  12. input(defval = color.teal, title = "2/1", inline = "c2", group = "colors"),
  13. input(defval = #9598a1, title = "3/1", inline = "c2", group = "colors"),
  14. input(defval = color.yellow, title = "4/1", inline = "c2", group = "colors"),
  15. input(defval = #a5d6a7, title = "8/1", inline = "c2", group = "colors"),
  16. input(defval = color.blue, title = "1/1", inline = "c3", group = "colors"))
  17. //Transparency or the bg colors
  18. transp = input.int(defval = 80, title = "Transparency", minval = 0, maxval = 100, tooltip = "Possible values are from 0 (not transparent) to 100 (invisible)")
  19. // labels
  20. showlabels = input.bool(defval = true, title = "Labels", inline = "labels")
  21. labelloc = input.int(defval = 50, title = "| Label location", minval = 1, maxval = 300, inline = "labels")
  22. lstyle1i = input.string(defval = 'Solid', title = "Line Style", options = ['Solid', 'Dashed', 'Dotted'], inline = "lstyle")
  23. lstyle2i = input.string(defval = 'Dashed', title = "", options = ['Solid', 'Dashed', 'Dotted'], inline = "lstyle")
  24. loopbackprd = input.int(defval = 280, title = "Loopback Period", tooltip = "Loopback period to search Highest/Lowest Pivot Points")
  25. pperiod = input.int(defval = 5, title = "Pivot Period")
  26.  
  27. // calculate middle line, using this line we will calculate other lines
  28. float l_ = ta.lowest(292)
  29. float h_ = ta.highest(292)
  30. float perc = (h_ - l_) /82
  31. float hh = h_ + perc * 10
  32. float ll = l_ - perc * 8
  33. float middle = (hh - ll) / 131.0
  34. var lstyle1 = lstyle1i == 'Solid' ? line.style_solid : lstyle1i == 'Dashed' ? line.style_dashed : line.style_dotted
  35. var lstyle2 = lstyle2i == 'Solid' ? line.style_solid : lstyle2i == 'Dashed' ? line.style_dashed : line.style_dotted
  36. // levels
  37. var divs = array.from(2.0, 3.0, 4.0, 8.0)
  38.  
  39. // remove old pivot points
  40. remove_old_pivots(pval, ploc)=>
  41. for x = array.size(ploc) - 1 to 0
  42. if bar_index - array.get(ploc, x) > loopbackprd
  43. array.pop(ploc)
  44. array.pop(pval)
  45.  
  46. // get highest/lowest locations
  47. var pivothighs = array.new_float(0, na)
  48. var pivothighsloc = array.new_int(0, na)
  49. var pivotlows = array.new_float(0, na)
  50. var pivotlowsloc = array.new_int(0, na)
  51. ph = ta.pivothigh(pperiod, pperiod)
  52. pl = ta.pivotlow(pperiod, pperiod)
  53. if ph
  54. array.unshift(pivothighsloc, bar_index - pperiod)
  55. array.unshift(pivothighs, ph)
  56. remove_old_pivots(pivothighs, pivothighsloc)
  57. if pl
  58. array.unshift(pivotlowsloc, bar_index - pperiod)
  59. array.unshift(pivotlows, pl)
  60. remove_old_pivots(pivotlows, pivotlowsloc)
  61.  
  62. l_bar_index = array.size(pivotlowsloc) > 0 ? array.get(pivotlowsloc, array.lastindexof(pivotlows, array.min(pivotlows))) : na
  63. h_bar_index = array.size(pivothighsloc) > 0 ? array.get(pivothighsloc, array.lastindexof(pivothighs, array.max(pivothighs))) : na
  64. lwest = low[bar_index - l_bar_index]
  65. hghest = high[bar_index - h_bar_index]
  66.  
  67. // get line style
  68. get_style(x)=>
  69. x == 1 ? l_bar_index <= h_bar_index ? lstyle1 : lstyle2 : l_bar_index > h_bar_index ? lstyle1 : lstyle2
  70.  
  71. // remove old lines/labels/linefills
  72. remove_old_lll(lines, labels, linefills)=>
  73. for x = 0 to array.size(lines) - 1
  74. line.delete(array.get(lines, x))
  75. for x = 0 to array.size(labels) - 1
  76. label.delete(array.get(labels, x))
  77. for x = 0 to array.size(linefills) - 1
  78. linefill.delete(array.get(linefills, x))
  79.  
  80. // get bar_index using y location and start point
  81. line_get_bar_index(gline, x_bar_index, yloc, mult)=>
  82. ystart = line.get_price(gline, x_bar_index)
  83. slope = math.abs(ystart - line.get_price(gline, x_bar_index + 1))
  84. int ret = x_bar_index + math.floor((yloc - ystart) / slope) * mult
  85.  
  86. // draw fan lines from lowest
  87. if l_bar_index > 0 and l_bar_index < bar_index
  88. var uplines = array.new_line(9, na)
  89. var ulinefill = array.new_linefill(8, na)
  90. var labels = array.new_label(9, na)
  91. remove_old_lll(uplines, labels, ulinefill)
  92.  
  93. // draw 1/1 line, show its label and keep its y level
  94. array.set(uplines, 0, line.new(x1 = l_bar_index, y1 = lwest, x2 = l_bar_index + 1, y2 = lwest + middle, extend = extend.right, color = array.get(colors, 8), style = get_style(1)))
  95. xloc = l_bar_index + labelloc
  96. yloc11 = line.get_price(array.get(uplines, 0), xloc)
  97. if showlabels
  98. array.set(labels, 0, label.new(x = xloc, y = yloc11, text = "1/1", textcolor = array.get(colors, 8), style = label.style_none))
  99.  
  100. // draw other fan lines, labels, linefills
  101. for x = 0 to 3
  102. array.set(uplines, x + 1, line.new(x1 = l_bar_index, y1 = lwest, x2 = bar_index, y2 = lwest + (line.get_price(array.get(uplines, 0), bar_index) - lwest) / array.get(divs,x),
  103. extend = extend.right,
  104. color = array.get(colors, x),
  105. style = get_style(1)))
  106. array.set(ulinefill, x, linefill.new(array.get(uplines, x + 1), array.get(uplines, x), color.new(array.get(colors, x), transp)))
  107. if showlabels
  108. yloc = line.get_price(array.get(uplines, x + 1), xloc)
  109. array.set(labels, x + 1, label.new(x = xloc, y = yloc, text = "1/" + str.tostring(array.get(divs, x)), textcolor = array.get(colors, x), style = label.style_none))
  110.  
  111. array.set(uplines, x + 5, line.new(x1 = l_bar_index, y1 = lwest, x2 = l_bar_index + 1, y2 = lwest + middle * array.get(divs,x),
  112. extend = extend.right,
  113. color = array.get(colors, x + 4),
  114. style = get_style(1)))
  115. array.set(ulinefill, x + 4, linefill.new(array.get(uplines, x + 5), array.get(uplines, x == 0 ? 0 : x + 4), color.new(array.get(colors, x + 4), transp)))
  116. if showlabels
  117. xlocc = line_get_bar_index(array.get(uplines, x + 5), l_bar_index, yloc11, 1)
  118. array.set(labels, x + 5, label.new(x = xlocc, y = yloc11, text = str.tostring(array.get(divs, x)) + "/1", textcolor = array.get(colors, x + 4), style = label.style_none))
  119.  
  120. // draw fan lines from highest
  121. if h_bar_index > 0 and h_bar_index < bar_index
  122. var downlines = array.new_line(9, na)
  123. var labels = array.new_label(9, na)
  124. var dlinefill = array.new_linefill(8, na)
  125. remove_old_lll(downlines, labels, dlinefill)
  126.  
  127. // draw 1/1 line, show its label and keep its y level
  128. array.set(downlines, 0, line.new(x1 = h_bar_index, y1 = hghest, x2 = h_bar_index + 1, y2 = hghest - middle, extend = extend.right, color = array.get(colors, 8), style = get_style(2)))
  129. xloc = h_bar_index + labelloc
  130. yloc11 = line.get_price(array.get(downlines, 0), xloc)
  131. if showlabels
  132. array.set(labels, 0, label.new(x = xloc, y = yloc11, text = "1/1", textcolor = array.get(colors, 8), style = label.style_none))
  133.  
  134. // draw other fan lines, labels, linefills
  135. for x = 0 to 3
  136. array.set(downlines, x + 1, line.new(x1 = h_bar_index, y1 = hghest, x2 = bar_index, y2 = hghest - (hghest - line.get_price(array.get(downlines, 0), bar_index)) / array.get(divs,x),
  137. extend = extend.right,
  138. color = array.get(colors, x + 4),
  139. style = get_style(2)))
  140. array.set(dlinefill, x, linefill.new(array.get(downlines, x + 1), array.get(downlines, x), color.new(array.get(colors, x + 4), transp)))
  141. if showlabels
  142. yloc = line.get_price(array.get(downlines, x + 1), xloc)
  143. array.set(labels, x + 1, label.new(x = xloc, y = yloc, text = str.tostring(array.get(divs, x)) + "/1", textcolor = array.get(colors, x + 4), style = label.style_none))
  144.  
  145. array.set(downlines, x + 5, line.new(x1 = h_bar_index, y1 = hghest, x2 = h_bar_index + 1, y2 = hghest - middle * array.get(divs,x),
  146. extend = extend.right,
  147. color = array.get(colors, x),
  148. style = get_style(2)))
  149. array.set(dlinefill, x + 4, linefill.new(array.get(downlines, x + 5), array.get(downlines, x == 0 ? 0 : x + 4), color.new(array.get(colors, x), transp)))
  150. if showlabels
  151. xlocc = line_get_bar_index(array.get(downlines, x + 5), h_bar_index, yloc11, -1)
  152. array.set(labels, x + 5, label.new(x = xlocc, y = yloc11, text = "1/" + str.tostring(array.get(divs, x)), textcolor = array.get(colors, x), style = label.style_none))
  153.  
Add Comment
Please, Sign In to add comment