xmd79

RSI S/R

May 14th, 2022
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.91 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. // © atolelole
  3.  
  4. //@version=5
  5. indicator("RSI S/R", overlay=true, max_bars_back=3000)
  6.  
  7. length = input(21, group="Pivot points")
  8. max_h_pivot_points = input(5, group="Pivot points")
  9. max_l_pivot_points = input(5, group="Pivot points")
  10. min_difference = input.float(0.0, group="Pivot points")
  11. channel_extension_length = input(14, group="Channel fib")
  12. source = input(close, group="Pivot points")
  13.  
  14. var ph_index = array.new_int()
  15. var pl_index = array.new_int()
  16. var ph_value = array.new_float()
  17. var pl_value = array.new_float()
  18.  
  19. interp(l, h, s) => l + (h - l) * s
  20.  
  21. ph = ta.pivothigh(source, length, length)
  22. if ph
  23. ok = true
  24.  
  25. ph := high[length]
  26.  
  27. if array.size(ph_index) > 0
  28. if array.size(ph_index) >= max_h_pivot_points
  29. array.shift(ph_index)
  30. array.shift(ph_value)
  31.  
  32. for i = 0 to array.size(ph_value) - 1
  33. if math.abs(ph - array.get(ph_value, i)) < min_difference
  34. ok := false
  35. if ph > array.get(ph_value, i)
  36. array.set(ph_value, i , ph)
  37. array.set(ph_index, i , bar_index[length])
  38.  
  39. if ok
  40. array.push(ph_index, bar_index[length])
  41. array.push(ph_value, ph)
  42.  
  43. pl = ta.pivotlow(source, length, length)
  44. if pl
  45. ok = true
  46.  
  47. pl := low[length]
  48.  
  49. if array.size(pl_index) > 0
  50. if array.size(pl_index) >= max_l_pivot_points
  51. array.shift(pl_index)
  52. array.shift(pl_value)
  53.  
  54. for i = 0 to array.size(pl_value) - 1
  55. if math.abs(pl - array.get(pl_value, i)) < min_difference
  56. ok := false
  57. if pl < array.get(pl_value, i)
  58. array.set(pl_value, i , pl)
  59. array.set(pl_index, i , bar_index[length])
  60. if ok
  61. array.push(pl_index, bar_index[length])
  62. array.push(pl_value, pl)
  63.  
  64. var lines = array.new_line()
  65. var labels = array.new_label()
  66. var fibs = array.new_line()
  67.  
  68. var line line_fc_up = line(na)
  69. var line line_fc_down = line(na)
  70. var linefill fill_fc = linefill(na)
  71.  
  72. if barstate.isconfirmed
  73. ph_count = array.size(ph_index)
  74. pl_count = array.size(pl_index)
  75.  
  76. for i = 0 to array.size(lines)
  77. if array.size(lines) > 0
  78. line.delete(array.shift(lines))
  79.  
  80. for i = 0 to array.size(labels)
  81. if array.size(labels) > 0
  82. label.delete(array.shift(labels))
  83.  
  84. if ph_count > 0
  85. for i = 0 to ph_count - 1
  86. alpha = 80//math.min(100 - int((i / (ph_count - 1)) * 100), 80)
  87. clr = color.from_gradient(100 - alpha, 0, 100, #E8E965, #99d31b)
  88. if input.bool(true, "Draw high pivot labels", group="Pivot points")
  89. array.push(labels, label.new(array.get(ph_index, i), array.get(ph_value, i), "", style=label.style_label_down, color=color.new(clr, alpha), size=size.tiny))
  90. if input.bool(true, "Draw high pivot lines", group="Pivot points")
  91. array.push(lines, line.new(array.get(ph_index, i), array.get(ph_value, i), bar_index + channel_extension_length, array.get(ph_value, i) , color=color.new(clr, 60), style=line.style_dotted, extend=extend.none))
  92. if pl_count > 0
  93. for i = 0 to pl_count - 1
  94. alpha = 80//math.min(100 - int((i / (pl_count - 1)) * 100), 80)
  95. clr = color.from_gradient(100 - alpha, 0, 100, #64C5C7, #FA5032)
  96. if input.bool(true, "Draw low pivot labels", group="Pivot points")
  97. array.push(labels, label.new(array.get(pl_index, i), array.get(pl_value, i), "", style=label.style_label_up , color=color.new(#ff4500, alpha), size=size.tiny))
  98. if input.bool(true, "Draw low pivot lines", group="Pivot points")
  99. array.push(lines, line.new(array.get(pl_index, i), array.get(pl_value, i), bar_index + channel_extension_length, array.get(pl_value, i) , color=color.new(#ff4500, 60), style=line.style_dotted, extend=extend.none))
  100.  
  101. if input.bool(true, "Draw channel high", group="Channel fib")
  102. if ph_count > 1
  103. tmp_arr = array.new_line()
  104. for i = 0 to ph_count - 1
  105. for j = 0 to ph_count - 1
  106. h0 = array.get(ph_value, i)
  107. h1 = array.get(ph_value, j)
  108. h0i = array.get(ph_index, i) //array.indexof(ph_value, h0))
  109. h1i = array.get(ph_index, j) //array.indexof(ph_value, h1))
  110. if h0i < h1i
  111. array.push(tmp_arr, line.new(h0i, h0, h1i, h1, color=color.new(#99d31b, 0), extend=extend.none, style=line.style_dashed))
  112. if h0i > h1i
  113. array.push(tmp_arr, line.new(h1i, h1, h0i, h0, color=color.new(#99d31b, 0), extend=extend.none, style=line.style_dashed))
  114.  
  115. int min_ind = na
  116. float min_val = 100000.0
  117. for i = 0 to array.size(tmp_arr) - 1
  118. lp = line.get_price(array.get(tmp_arr, i), bar_index)
  119. if lp > high
  120. if min_val > math.abs(lp - close)
  121. min_val := math.abs(lp - close)
  122. min_ind := i
  123.  
  124. if not na(min_ind)
  125. line.delete(line_fc_up[1])
  126. best_line = array.get(tmp_arr, min_ind)
  127. line_fc_up := line.new(line.get_x1(best_line), line.get_y1(best_line), line.get_x2(best_line), line.get_y2(best_line), color=color.new(#99d31b, 0), extend=extend.none, style=line.style_solid)
  128.  
  129. for i = 0 to array.size(tmp_arr) - 1
  130. if array.size(tmp_arr) > 0
  131. line.delete(array.shift(tmp_arr))
  132.  
  133. for l in labels
  134. if label.get_x(l) == line.get_x1(line_fc_up) or label.get_x(l) == line.get_x2(line_fc_up)
  135. label.set_color(l, color.new(#99d31b, 0))
  136.  
  137. for l in lines
  138. if line.get_x1(l) == line.get_x1(line_fc_up) or line.get_x1(l) == line.get_x2(line_fc_up)
  139. line.set_color(l, color.new(#99d31b, 0))
  140. line.set_style(l, line.style_dashed)
  141.  
  142. line.set_y2(line_fc_up, line.get_price(line_fc_up, bar_index + channel_extension_length))
  143. line.set_x2(line_fc_up, bar_index + channel_extension_length)
  144.  
  145. if input.bool(true, "Draw channel low", group="Channel fib")
  146. if pl_count > 1
  147. tmp_arr = array.new_line()
  148. for i = 0 to pl_count - 1
  149. for j = 0 to pl_count - 1
  150. l0 = array.get(pl_value, i)
  151. l1 = array.get(pl_value, j)
  152. l0i = array.get(pl_index, i) //array.indexof(pl_value, l0))
  153. l1i = array.get(pl_index, j) //array.indexof(pl_value, l1))
  154. if l0i < l1i
  155. array.push(tmp_arr, line.new(l0i, l0, l1i, l1, color=color.new(#FA5032, 0), extend=extend.none, style=line.style_dashed))
  156. if l0i > l1i
  157. array.push(tmp_arr, line.new(l1i, l1, l0i, l0, color=color.new(#FA5032, 0), extend=extend.none, style=line.style_dashed))
  158.  
  159. int min_ind = na
  160. float min_val = 100000.0
  161. for i = 0 to array.size(tmp_arr) - 1
  162. lp = line.get_price(array.get(tmp_arr, i), bar_index)
  163. if lp < low
  164. if min_val > math.abs(lp - close)
  165. min_val := math.abs(lp - close)
  166. min_ind := i
  167.  
  168. if not na(min_ind)
  169. line.delete(line_fc_down[1])
  170. best_line = array.get(tmp_arr, min_ind)
  171. line_fc_down := line.new(line.get_x1(best_line), line.get_y1(best_line), line.get_x2(best_line), line.get_y2(best_line), color=color.new(#FA5032, 0), extend=extend.none, style=line.style_solid)
  172.  
  173. for i = 0 to array.size(tmp_arr) - 1
  174. if array.size(tmp_arr) > 0
  175. line.delete(array.shift(tmp_arr))
  176.  
  177. for l in labels
  178. if label.get_x(l) == line.get_x1(line_fc_down) or label.get_x(l) == line.get_x2(line_fc_down)
  179. label.set_color(l, color.new(#FA5032, 0))
  180.  
  181. for l in lines
  182. if line.get_x1(l) == line.get_x1(line_fc_down) or line.get_x1(l) == line.get_x2(line_fc_down)
  183. line.set_color(l, color.new(#FA5032, 0))
  184. line.set_style(l, line.style_dashed)
  185.  
  186. line.set_y2(line_fc_down, line.get_price(line_fc_down, bar_index + channel_extension_length))
  187. line.set_x2(line_fc_down, bar_index + channel_extension_length)
  188.  
  189. if not na(line_fc_up) and not na(line_fc_down) and input.bool(true, "Fill channel background", group="Channel fib")
  190. linefill.delete(fill_fc[1])
  191. fill_color = color.new(color.from_gradient(ta.rsi(close, 14), 30, 70, color.red, color.green), 97)
  192. fill_fc := linefill.new(line_fc_up, line_fc_down, fill_color)
  193.  
  194. if not na(line_fc_up) and not na(line_fc_down) and input.bool(true, "Draw fib lines", inline="fiblines", group="Channel fib")
  195. for i = 0 to array.size(fibs) - 1
  196. if array.size(fibs) > 0
  197. line.delete(array.shift(fibs))
  198.  
  199. left = math.min(line.get_x1(line_fc_up), line.get_x1(line_fc_down))
  200. right = bar_index + channel_extension_length
  201. left_val = interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 0.236)
  202. right_val = interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 0.236)
  203. array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
  204. left_val := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 0.382)
  205. right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 0.382)
  206. array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
  207. left_val := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 0.5)
  208. right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 0.5)
  209. array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
  210. left_val := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 0.618)
  211. right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 0.618)
  212. array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
  213. left_val := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 0.75)
  214. right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 0.75)
  215. array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
  216. left_val := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 1.618)
  217. right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 1.618)
  218. array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
  219.  
  220. if input.bool(false, "Draw fib lines > 1.618", inline="fiblines", group="Channel fib")
  221. left_val := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 2.618)
  222. right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 2.618)
  223. array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
  224. left_val := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 3.618)
  225. right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 3.618)
  226. array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
  227. left_val := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 4.236)
  228. right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 4.236)
  229. array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
  230.  
  231.  
  232.  
Advertisement
Add Comment
Please, Sign In to add comment