Advertisement
xmd79

Fibonacci compression

Jan 17th, 2023
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.22 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. // © Greg_007
  3.  
  4. //@version=5
  5. indicator(title="Fibonacci compression" , shorttitle="Fib compression", overlay=true)
  6. var length = input.int(300, title="How many bars to look back?")
  7. min_length = 50
  8. bool searchBull = input.bool(true, title="Search for bullish structures, uncheck for bearish")
  9. float fib1 = input.float(defval=0.786, title="Minumum fib level for 2nd leg", options=[0.786,0.618])
  10. float fib2 = input.float(0.618, title="Minumum fib level for 3rd leg")
  11. float fib3 = input.float(0.618, title="Minumum fib level for 4th leg")
  12. float fib4 = input.float(defval=0.5, title="Minumum fib level for 5th leg", options=[0.5,0.382,0.236])
  13. float high1 = 0.0
  14. float high2 = 0.0
  15. float high3 = 0.0
  16. float low1 = 0.0
  17. float low2 = 0.0
  18. float low3 = 0.0
  19. arr_all_highs = array.new_float(length, 0) //will contain the highs
  20. arr_all_lows = array.new_float(length, 0) //will contain the lows
  21. arr_cumul_vol = array.new_float(length, 0) //will contain the cumulative volume
  22. //temporary arrays
  23. b = array.new_float()
  24. c = array.new_float()
  25. d = array.new_float()
  26. e = array.new_float()
  27. f = array.new_float()
  28. int positionH1 = 0
  29. int positionH2 = 0
  30. int positionH3 = 0
  31. int positionL1 = 0
  32. int positionL2 = 0
  33. int positionL3 = 0
  34. var cumVol = 0.0
  35. float cumVolH1 = 0.0
  36. float cumVolH2 = 0.0
  37. float cumVolH3 = 0.0
  38. float cumVolL1 = 0.0
  39. float cumVolL2 = 0.0
  40. float cumVolL3 = 0.0
  41. string volDivergence = ""
  42. label l_H1 = na
  43. label l_H2 = na
  44. label l_H3 = na
  45. label l_L1 = na
  46. label l_L2 = na
  47. label l_L3 = na
  48. label l_info = na
  49.  
  50. //FUNCTIONS
  51. check_volume(volH1, volH2, volH3, volL1, volL2, volL3) =>
  52. txt = ""
  53. if volH3 > volH2 and volH2 > volH1
  54. txt := "Strong Volume uptrend\nin favour of a long"
  55. else
  56. if volH2 > volH1
  57. txt := "Volume in favour\nof a long"
  58. else
  59. if volL3 < volL2 and volL2 < volL1
  60. txt := "Strong Volume downtrend\nin favour of a short"
  61. else
  62. if volL2 < volL1
  63. txt := "Volume in favour\nof a short"
  64.  
  65. drawPivotLines(H, posH, L, posL) =>
  66. pivotLine = line.new(bar_index - length + posH + 1, H, bar_index - length + posL + 1, L, color = color.purple)
  67. line.delete(pivotLine[1])
  68.  
  69. calcDrawTPlevels(A, B, C) =>
  70. TP1 = math.round_to_mintick(A + ((B - C) * 0.236))
  71. TP2 = math.round_to_mintick(A + ((B - C) * 0.618))
  72. TP3 = math.round_to_mintick(A + ((B - C) * 1))
  73. TP1_line = line.new(bar_index,TP1, bar_index + 100, TP1, color=#00aeff, width=2, style=line.style_dashed)
  74. TP2_line = line.new(bar_index,TP2, bar_index + 100, TP2, color=#00ff15, width=2, style=line.style_dashed)
  75. TP3_line = line.new(bar_index,TP3, bar_index + 100, TP3, color=#00ff15, width=2, style=line.style_dashed)
  76. line.delete(TP1_line[1])
  77. line.delete(TP2_line[1])
  78. line.delete(TP3_line[1])
  79. TP1Label = label.new(bar_index + 101 , TP1, text = "TP1 - "+ str.tostring(TP1), color=#00aeff, style=label.style_label_left)
  80. label.delete(TP1Label[1])
  81. TP2Label = label.new(bar_index + 101 , TP2, text = "TP2 - "+ str.tostring(TP2), color=#00ff15, style=label.style_label_left)
  82. label.delete(TP2Label[1])
  83. TP3Label = label.new(bar_index + 101 , TP3, text = "TP3 - "+ str.tostring(TP3), color=#00ff15, style=label.style_label_left)
  84. label.delete(TP3Label[1])
  85. [TP1, TP2, TP3]
  86.  
  87. // END FUNCTIONS
  88.  
  89. if close > open
  90. cumVol += nz(volume)
  91. else
  92. cumVol -= nz(volume)
  93.  
  94. if barstate.islast
  95. i = length - 1
  96. while i > -1
  97. array.set(arr_all_highs, length - 1 - i, high[i])
  98. array.set(arr_all_lows,length - 1 - i, low[i])
  99. array.set(arr_cumul_vol,length - 1 - i, cumVol[i])
  100. i -= 1
  101. if searchBull
  102. //Search High 1
  103. high1 := array.max(arr_all_highs)
  104. positionH1 := array.indexof(arr_all_highs, high1)
  105. cumVolH1 := array.get(arr_cumul_vol, positionH1)
  106.  
  107. if positionH1 < length - min_length //Don't search if the high is too close to the current candle
  108. //Search Low 1
  109. b := array.slice(arr_all_lows, positionH1 + 1, array.size(arr_all_lows))
  110. len = array.size(b)
  111. low1 := array.min(b)
  112. positionL1 := length - array.size(b) + array.indexof(b, low1)
  113. cumVolL1 := array.get(arr_cumul_vol, positionL1)
  114. //Search High 2
  115. if positionL1 + 1 == length //avoid slicing index error when we already reached the current candle
  116. c := array.slice(arr_all_highs, positionL1, array.size(arr_all_highs))
  117. else
  118. c := array.slice(arr_all_highs, positionL1 + 1, array.size(arr_all_highs))
  119. high2 := array.max(c)
  120. positionH2 := length - array.size(c) + array.indexof(c, high2)
  121. cumVolH2 := array.get(arr_cumul_vol, positionH2)
  122.  
  123. if high2 >= low1 + ((high1 - low1) * fib1)
  124. l_H1 := label.new(bar_index - length + positionH1 + 1,high1,"High", style = label.style_label_down, textcolor=color.yellow)
  125. l_L1 := label.new(bar_index - length + positionL1 + 1,low1,"Low", style = label.style_label_up, textcolor=color.yellow)
  126. l_H2 := label.new(bar_index - length + positionH2 + 1,high2,"Lower high", style = label.style_label_down, textcolor=color.yellow)
  127. drawPivotLines(high1, positionH1, low1, positionL1)
  128. drawPivotLines(high2, positionH2, low1, positionL1)
  129. //Search Low 2
  130. if positionH2 + 1 == length
  131. d := array.slice(arr_all_lows, positionH2, array.size(arr_all_lows))
  132. else
  133. d := array.slice(arr_all_lows, positionH2 + 1, array.size(arr_all_lows))
  134. low2 := array.min(d)
  135. positionL2 := length - array.size(d) + array.indexof(d, low2)
  136. cumVolL2 := array.get(arr_cumul_vol, positionL2)
  137. if low2 <= high2 - ((high2 - low1) * fib2)
  138. l_L2 := label.new(bar_index - length + positionL2 + 1,low2,"Higher low", style = label.style_label_up, textcolor=color.yellow)
  139. drawPivotLines(high2, positionH2, low2, positionL2)
  140. [TP1, TP2, TP3] = calcDrawTPlevels(high2, high1, low1)
  141. //Search High 3
  142. if positionL2 + 1 == length //avoid slicing index error when we already reached the current candle
  143. e := array.slice(arr_all_highs, positionL2, array.size(arr_all_highs))
  144. else
  145. e := array.slice(arr_all_highs, positionL2 + 1, array.size(arr_all_highs))
  146. high3 := array.max(e)
  147. positionH3 := length - array.size(e) + array.indexof(e, high3)
  148. cumVolH3 := array.get(arr_cumul_vol, positionH3)
  149. if high3 >= low2 + ((high2 - low2) * fib3)
  150. l_H3 := label.new(bar_index - length + positionH3 + 1,high3,"2nd lower high", style = label.style_label_down, textcolor=color.yellow)
  151. drawPivotLines(high3, positionH3, low2, positionL2)
  152. //Search Low 3
  153. if positionH3 + 1 == length //avoid slicing index error when we already reached the current candle
  154. f := array.slice(arr_all_lows, positionH3, array.size(arr_all_lows))
  155. else
  156. f := array.slice(arr_all_lows, positionH3 + 1, array.size(arr_all_lows))
  157. low3 := array.min(f)
  158. positionL3 := length - array.size(f) + array.indexof(f, low3)
  159. cumVolL3 := array.get(arr_cumul_vol, positionL3)
  160. if low3 <= high3 - ((high3 - low2) * fib4)
  161. l_L3 := label.new(bar_index - length + positionL3 + 1,low3,"2nd higher low", style = label.style_label_up, textcolor=color.yellow)
  162. drawPivotLines(high3, positionH3, low3, positionL3)
  163. stoploss = line.new(bar_index - length + positionL3 + 1,low3, bar_index + 100, low3, color=#ff0000, width=2)
  164. line.delete(stoploss[1])
  165. SLLabel = label.new(bar_index + 101 , low3, text = "Stop/Loss "+ str.tostring(low3), color=#ff0000, style=label.style_label_left)
  166. label.delete(SLLabel[1])
  167. lineL3to0618 = line.new(bar_index - length + positionL3 + 1,low3, bar_index + 50,TP2, color = color.green, style=line.style_arrow_right)
  168. line.delete(lineL3to0618[1])
  169. volDivergence := check_volume(cumVolH1, cumVolH2, cumVolH3, cumVolL1, cumVolL2, cumVolL3)
  170. l_info := label.new(bar_index + 10 ,low, "info: " + volDivergence, style = label.style_label_left, textcolor=color.yellow)
  171. label.delete(l_info[1])
  172.  
  173. else
  174. //Search Low 1
  175. low1 := array.min(arr_all_lows)
  176. positionL1 := array.indexof(arr_all_lows, low1)
  177. cumVolL1 := array.get(arr_cumul_vol, positionL1)
  178. if positionL1 < length - min_length //Don't search if the low is too close to the current candle
  179. //Search High 1
  180. b := array.slice(arr_all_highs, positionL1 + 1, array.size(arr_all_highs))
  181. high1 := array.max(b)
  182. positionH1 := length - array.size(b) + array.indexof(b, high1)
  183. cumVolH1 := array.get(arr_cumul_vol, positionH1)
  184. //Search Low 2
  185. if positionH1 + 1 == length //avoid slicing index error when we already reached the current candle
  186. c := array.slice(arr_all_lows, positionH1, array.size(arr_all_lows))
  187. else
  188. c := array.slice(arr_all_lows, positionH1 + 1, array.size(arr_all_lows))
  189. low2 := array.min(c)
  190. positionL2 := length - array.size(c) + array.indexof(c, low2)
  191. cumVolL2 := array.get(arr_cumul_vol, positionL2)
  192. if low2 <= high1 + ((low1 - high1) * fib1)
  193. l_L1 := label.new(bar_index - length + positionL1 + 1,low1,"Low", style = label.style_label_up, textcolor=color.yellow)
  194. l_H1 := label.new(bar_index - length + positionH1 + 1,high1,"High", style = label.style_label_down, textcolor=color.yellow)
  195. l_L2 := label.new(bar_index - length + positionL2 + 1,low2,"Higher low", style = label.style_label_up, textcolor=color.yellow)
  196. drawPivotLines(high1, positionH1, low1, positionL1)
  197. drawPivotLines(high1, positionH1, low2, positionL2)
  198. //Search High 2
  199. if positionL2 + 1 == length //avoid slicing index error when we already reached the current candle
  200. d := array.slice(arr_all_highs, positionL2, array.size(arr_all_highs))
  201. else
  202. d := array.slice(arr_all_highs, positionL2 + 1, array.size(arr_all_highs))
  203. high2 := array.max(d)
  204. positionH2 := length - array.size(d) + array.indexof(d, high2)
  205. cumVolH2 := array.get(arr_cumul_vol, positionH2)
  206. if high2 >= low2 - ((low2 - high1) * fib2)
  207. l_H2 := label.new(bar_index - length + positionH2 + 1,high2,"Lower high", style = label.style_label_down, textcolor=color.yellow)
  208. drawPivotLines(high2, positionH2, low2, positionL2)
  209. [TP1, TP2, TP3] = calcDrawTPlevels(low2, low1, high1)
  210. //Search Low 3
  211. if positionH2 + 1 == length //avoid slicing index error when we already reached the current candle
  212. e := array.slice(arr_all_lows, positionH2, array.size(arr_all_lows))
  213. else
  214. e := array.slice(arr_all_lows, positionH2 + 1, array.size(arr_all_lows))
  215. low3 := array.min(e)
  216. positionL3 := length - array.size(e) + array.indexof(e, low3)
  217. cumVolL3 := array.get(arr_cumul_vol, positionL3)
  218. if low3 <= high2 + ((low2 - high2) * fib3)
  219. l_L3 := label.new(bar_index - length + positionL3 + 1,low3,"2nd higher low", style = label.style_label_up, textcolor=color.yellow)
  220. drawPivotLines(high2, positionH2, low3, positionL3)
  221. //Search High 3
  222. if positionL3 + 1 == length //avoid slicing index error when we already reached the current candle
  223. f := array.slice(arr_all_highs, positionL3, array.size(arr_all_highs))
  224. else
  225. f := array.slice(arr_all_highs, positionL3 + 1, array.size(arr_all_highs))
  226. high3 := array.max(f)
  227. positionH3 := length - array.size(f) + array.indexof(f, high3)
  228. cumVolH3 := array.get(arr_cumul_vol, positionH3)
  229. if high3 >= low3 - ((low3 - high2) * fib4)
  230. l_H3 := label.new(bar_index - length + positionH3 + 1,high3,"2nd lower high" + str.tostring(high3), style = label.style_label_down, textcolor=color.yellow)
  231. drawPivotLines(high3, positionH3, low3, positionL3)
  232. stoploss = line.new(bar_index - length + positionH3 + 1,high3, bar_index + 100, high3, color=#ff0000, width=2)
  233. line.delete(stoploss[1])
  234. SLLabel = label.new(bar_index + 101 , high3, text = "Stop/Loss "+ str.tostring(high3), color=#ff0000, style=label.style_label_left)
  235. label.delete(SLLabel[1])
  236. lineH3to0618 = line.new(bar_index - length + positionH3 + 1,high3, bar_index + 50,TP2, color = color.green, style=line.style_arrow_right)
  237. line.delete(lineH3to0618[1])
  238. volDivergence := check_volume(cumVolH1, cumVolH2, cumVolH3, cumVolL1, cumVolL2, cumVolL3)
  239. l_info := label.new(bar_index + 10 ,high,"info: " + volDivergence, style = label.style_label_left, textcolor=color.yellow)
  240. label.delete(l_info[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement