xmd79

Fibonacci Ranges (Real-Time) [LuxAlgo]

Sep 19th, 2023
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.30 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. // © LuxAlgo
  3.  
  4. //@version=5
  5. indicator("Fibonacci Ranges (Real-Time) [LuxAlgo]", shorttitle='LuxAlgo - Fibonacci Ranges (Real-Time)', max_lines_count=500, max_labels_count=500, overlay=true)
  6.  
  7. //------------------------------------------------------------------------------
  8. //Settings
  9. //-----------------------------------------------------------------------------{
  10. z = '       '
  11. y = '          '
  12. x = '             '
  13. L = input.int ( 5 , 'L' , minval =1 , group = 'Swing Settings' )
  14. R = input.int ( 1 , 'R' , minval =1 , group = 'Swing Settings' )
  15. c_ = input.string( 'Channel + Shadows' , 'Channel' , options=['Channel', 'Channel + Shadows', 'None'], group = 'Fibonacci Channel' )
  16. opt= input.string('0.000 - 1.000', 'Break level' , group = 'Fibonacci Channel'
  17. , options= ['-0.382 - 1.382', '0.000 - 1.000', '0.236 - 0.764', '0.382 - 0.618'] )
  18. c = input.int ( 2 , 'Break count' , minval =0 , group = 'Fibonacci Channel' )
  19. sb = input.bool ( true , z+x+ 'Show breaks' , group = 'Fibonacci Channel' )
  20. sF = input.bool ( false , z+y+ 'Latest Fibonacci' , group = 'Fibonacci' )
  21. F0 = input.color ( #08998164 , '      ' , inline='f' , group = 'Fibonacci' )
  22. F_ = input.color ( #08998180 , '' , inline='f' , group = 'Fibonacci' )
  23. Fm = input.color ( #1e42b070 , '' , inline='f' , group = 'Fibonacci' )
  24. Fp = input.color ( #f2364580 , '' , inline='f' , group = 'Fibonacci' )
  25. F1 = input.color ( #f2364564 , '' , inline='f' , group = 'Fibonacci' )
  26.  
  27. Fc = c_ != 'None' ? true : false
  28. fl = c_ == 'Channel + Shadows' ? true : false
  29.  
  30. n = bar_index
  31.  
  32. //-----------------------------------------------------------------------------}
  33. //User Defined Types
  34. //-----------------------------------------------------------------------------{
  35. type piv
  36. int b
  37. float p
  38. int d
  39.  
  40. type fib
  41. line n0_382
  42. line _0_000
  43. line _0_236
  44. line _0_382
  45. line _0_500
  46. line _0_618
  47. line _0_764
  48. line _1_000
  49. line _1_382
  50. line diagon
  51. label lab1
  52. label lab2
  53. bool active
  54. int count
  55.  
  56. type fibLast
  57. linefill lf_0
  58. linefill lfM0
  59. linefill lfM1
  60. linefill lf_1
  61. line ln_0_5
  62. line diagon
  63. label lab
  64.  
  65. //-----------------------------------------------------------------------------}
  66. //Variables
  67. //-----------------------------------------------------------------------------{
  68. var piv[] pivs = array.new<piv>()
  69.  
  70. var f = fib .new (
  71. n0_382 = line.new (na, na, na, na, color=F0),
  72. _0_000 = line.new (na, na, na, na, color=F0),
  73. _0_236 = line.new (na, na, na, na, color=F_),
  74. _0_382 = line.new (na, na, na, na, color=F_),
  75. _0_500 = line.new (na, na, na, na, color=Fm),
  76. _0_618 = line.new (na, na, na, na, color=Fp),
  77. _0_764 = line.new (na, na, na, na, color=Fp),
  78. _1_000 = line.new (na, na, na, na, color=F1),
  79. _1_382 = line.new (na, na, na, na, color=F1),
  80. lab1 = label.new(na, na , color=color.gray
  81. ,style= label.style_label_center
  82. , yloc= yloc.price , size= size.tiny),
  83. lab2 = label.new(na, na , color=color.gray
  84. ,style= label.style_label_center
  85. , yloc= yloc.price , size= size.tiny),
  86. diagon = line.new (na, na, na, na
  87. ,color=color.silver ,style=line.style_dashed),
  88. active = false, count = 0
  89. )
  90.  
  91. var fibLast fLast = fibLast.new(
  92. lf_0 = linefill.new(
  93. line .new(na, na, na, na, color=F0)
  94. , line .new(na, na, na, na, color=F0)
  95. , color .new(F0, 75)
  96. ),
  97. lfM0 = linefill.new(
  98. line .new(na, na, na, na, color=F_)
  99. , line .new(na, na, na, na, color=F_)
  100. , color .new(F_, 75)
  101. ),
  102. lfM1 = linefill.new(
  103. line .new(na, na, na, na, color=Fp)
  104. , line .new(na, na, na, na, color=Fp)
  105. , color .new(Fp, 75)
  106. ),
  107. lf_1 = linefill.new(
  108. line .new(na, na, na, na, color=F1)
  109. , line .new(na, na, na, na, color=F1)
  110. , color .new(F1, 75)
  111. ),
  112. ln_0_5 = line.new(na, na, na, na, color=color.blue ),
  113. diagon = line.new(na, na, na, na, color=color.silver
  114. , style=line.style_dashed),
  115. lab = label.new( na, na , color=color.gray )
  116. )
  117.  
  118. var line ln = line.new(na, na, na, na)
  119. bool brokeU = false
  120. bool brokeD = false
  121.  
  122. //-----------------------------------------------------------------------------}
  123. //Execution
  124. //-----------------------------------------------------------------------------{
  125. ph = ta.pivothigh(L, R)
  126. pl = ta.pivotlow (L, R)
  127.  
  128. a1 = switch opt
  129. '-0.382 - 1.382' => -0.382
  130. '0.000 - 1.000' => 0
  131. '0.236 - 0.764' => 0.236
  132. '0.382 - 0.618' => 0.382
  133.  
  134. a2 = switch opt
  135. '-0.382 - 1.382' => 1.382
  136. '0.000 - 1.000' => 1
  137. '0.236 - 0.764' => 0.764
  138. '0.382 - 0.618' => 0.618
  139.  
  140. if ph
  141. if pivs.size() > 0
  142. get = pivs.first()
  143. if get.d > 0 and ph > get.p
  144. get.b := n -R
  145. get.p := high[R]
  146. if get.d < 0 and ph > get.p
  147. pivs.unshift(piv.new(n -R, high[R] , 1))
  148. else
  149. pivs .unshift(piv.new(n -R, high[R] , 1))
  150.  
  151. if pivs.size() > 1 and not f.active and Fc
  152. get1 = pivs.get(1) , idx1 = get1.b , diff = high[R] - get1.p
  153. f.n0_382.set_xy1(n -R, high[R] + (diff * 0.382)), f.n0_382.set_xy2(n + 8, high[R] + (diff * 0.382))
  154. f._0_000.set_xy1(n -R, high[R] ), f._0_000.set_xy2(n + 8, high[R] )
  155. f._0_236.set_xy1(n -R, high[R] - (diff * 0.236)), f._0_236.set_xy2(n + 8, high[R] - (diff * 0.236))
  156. f._0_382.set_xy1(n -R, high[R] - (diff * 0.382)), f._0_382.set_xy2(n + 8, high[R] - (diff * 0.382))
  157. f._0_500.set_xy1(n -R, high[R] - (diff * 0.500)), f._0_500.set_xy2(n + 8, high[R] - (diff * 0.500))
  158. f._0_618.set_xy1(n -R, high[R] - (diff * 0.618)), f._0_618.set_xy2(n + 8, high[R] - (diff * 0.618))
  159. f._0_764.set_xy1(n -R, high[R] - (diff * 0.764)), f._0_764.set_xy2(n + 8, high[R] - (diff * 0.764))
  160. f._1_000.set_xy1(n -R, get1.p ), f._1_000.set_xy2(n + 8, get1.p )
  161. f._1_382.set_xy1(n -R, get1.p - (diff * 0.382)), f._1_382.set_xy2(n + 8, get1.p - (diff * 0.382))
  162. f.lab1 .set_xy (n +8, high[R] - (diff * a1 )), f.lab2 .set_xy (n + 8, high[R] - (diff * a2 ))
  163. f.diagon.set_xy1(idx1, get1.p ), f.diagon.set_xy2(n -R, high[R] )
  164. f.active := true
  165.  
  166. if pl
  167. if pivs.size() > 0
  168. get = pivs.first()
  169. if get.d < 0 and pl < get.p
  170. get.b := n -R
  171. get.p := low [R]
  172. if get.d > 0 and pl < get.p
  173. pivs.unshift(piv.new(n -R, low [R] ,-1))
  174. else
  175. pivs .unshift(piv.new(n -R, low [R] ,-1))
  176.  
  177. if pivs.size() > 1 and not f.active and Fc
  178. get1 = pivs.get(1) , idx1 = get1.b , diff = get1.p - low [R]
  179. f.n0_382.set_xy1(n -R, low [R] - (diff * 0.382)), f.n0_382.set_xy2(n + 8, low [R] - (diff * 0.382))
  180. f._0_000.set_xy1(n -R, low [R] ), f._0_000.set_xy2(n + 8, low [R] )
  181. f._0_236.set_xy1(n -R, low [R] + (diff * 0.236)), f._0_236.set_xy2(n + 8, low [R] + (diff * 0.236))
  182. f._0_382.set_xy1(n -R, low [R] + (diff * 0.382)), f._0_382.set_xy2(n + 8, low [R] + (diff * 0.382))
  183. f._0_500.set_xy1(n -R, low [R] + (diff * 0.500)), f._0_500.set_xy2(n + 8, low [R] + (diff * 0.500))
  184. f._0_618.set_xy1(n -R, low [R] + (diff * 0.618)), f._0_618.set_xy2(n + 8, low [R] + (diff * 0.618))
  185. f._0_764.set_xy1(n -R, low [R] + (diff * 0.764)), f._0_764.set_xy2(n + 8, low [R] + (diff * 0.764))
  186. f._1_000.set_xy1(n -R, get1.p ), f._1_000.set_xy2(n + 8, get1.p )
  187. f._1_382.set_xy1(n -R, get1.p + (diff * 0.382)), f._1_382.set_xy2(n + 8, get1.p + (diff * 0.382))
  188. f.lab1 .set_xy (n +8, low [R] + (diff * a1 )), f.lab2 .set_xy (n + 8, low [R] + (diff * a2 ))
  189. f.diagon.set_xy1(idx1, get1.p ), f.diagon.set_xy2(n -R, low [R] )
  190. f.active := true
  191.  
  192. if pivs.size() >= 2
  193. max = math.max(pivs.first().p, pivs.get(1).p )
  194. min = math.min(pivs.first().p, pivs.get(1).p )
  195. dif = max - min
  196. _0i = pivs.first( ).b, _0p = pivs.first( ).p
  197. _1i = pivs.get (1).b, _1p = pivs.get (1).p
  198. d = _0p < _1p ? -dif : dif
  199. x2 = math.max(last_bar_index , _0i) + 8
  200. l0_1 = fLast. lf_0.get_line1(), l0_2 = fLast. lf_0.get_line2()
  201. lM01 = fLast. lfM0.get_line1(), lM02 = fLast. lfM0.get_line2()
  202. lM11 = fLast. lfM1.get_line1(), lM12 = fLast. lfM1.get_line2()
  203. l1_1 = fLast. lf_1.get_line1(), l1_2 = fLast. lf_1.get_line2()
  204.  
  205. if (ph or pl) and sF
  206. l0_1 .set_xy1(_0i, _0p + (d * 0.382)), l0_1 .set_xy2( x2, _0p + (d * 0.382))
  207. l0_2 .set_xy1(_0i, _0p ), l0_2 .set_xy2( x2, _0p )
  208. lM01 .set_xy1(_0i, _0p - (d * 0.236)), lM01 .set_xy2( x2, _0p - (d * 0.236))
  209. lM02 .set_xy1(_0i, _0p - (d * 0.382)), lM02 .set_xy2( x2, _0p - (d * 0.382))
  210. lM11 .set_xy1(_0i, _0p - (d * 0.618)), lM11 .set_xy2( x2, _0p - (d * 0.618))
  211. lM12 .set_xy1(_0i, _0p - (d * 0.764)), lM12 .set_xy2( x2, _0p - (d * 0.764))
  212. l1_1 .set_xy1(_0i, _0p - d ), l1_1 .set_xy2( x2, _0p - d )
  213. l1_2 .set_xy1(_0i, _1p - (d * 0.382)), l1_2 .set_xy2( x2, _1p - (d * 0.382))
  214. fLast.ln_0_5.set_xy1(_0i, _0p - (d * 0.5 )), fLast.ln_0_5.set_xy2( x2, _0p - (d * 0.5 ))
  215. fLast.diagon.set_xy1(_0i, _0p ), fLast.diagon.set_xy2(_1i, _1p )
  216. fLast.lab .set_xy (_0i, _0p )
  217. fLast.lab .set_style (_1p > _0p ? label.style_label_up : label.style_label_down )
  218. else
  219. l0_1 .set_x2 ( n + 8 )
  220. l0_2 .set_x2 ( n + 8 )
  221. lM01 .set_x2 ( n + 8 )
  222. lM02 .set_x2 ( n + 8 )
  223. lM11 .set_x2 ( n + 8 )
  224. lM12 .set_x2 ( n + 8 )
  225. l1_1 .set_x2 ( n + 8 )
  226. l1_2 .set_x2 ( n + 8 )
  227. fLast.ln_0_5.set_x2 ( n + 8 )
  228.  
  229. if f.active
  230. lv1 = opt == '0.000 - 1.000' ? f._0_000 : opt == '0.236 - 0.764' ? f._0_236 : opt == '0.382 - 0.618' ? f._0_382 : f.n0_382
  231. lv2 = opt == '0.000 - 1.000' ? f._1_000 : opt == '0.236 - 0.764' ? f._0_764 : opt == '0.382 - 0.618' ? f._0_618 : f._1_382
  232.  
  233. max = math.max(lv1.get_y2(), lv2.get_y2())
  234. min = math.min(lv1.get_y2(), lv2.get_y2())
  235.  
  236. if close < max and close > min
  237. f.count := 0
  238. else
  239. if close > max or close < min
  240. f.count += 1
  241. if f.count > c
  242. f.n0_382.set_xy1(na, na), f.n0_382.set_xy2(na, na)
  243. f._0_000.set_xy1(na, na), f._0_000.set_xy2(na, na)
  244. f._0_236.set_xy1(na, na), f._0_236.set_xy2(na, na)
  245. f._0_382.set_xy1(na, na), f._0_382.set_xy2(na, na)
  246. f._0_500.set_xy1(na, na), f._0_500.set_xy2(na, na)
  247. f._0_618.set_xy1(na, na), f._0_618.set_xy2(na, na)
  248. f._0_764.set_xy1(na, na), f._0_764.set_xy2(na, na)
  249. f._1_000.set_xy1(na, na), f._1_000.set_xy2(na, na)
  250. f._1_382.set_xy1(na, na), f._1_382.set_xy2(na, na)
  251. f.diagon.set_xy1(na, na), f.diagon.set_xy2(na, na)
  252. f.lab1 .set_xy (na, na), f.lab2 .set_xy (na, na)
  253. f.active := false
  254.  
  255. if close > max
  256. brokeU := true
  257. else
  258. brokeD := true
  259.  
  260. else
  261. f.n0_382.set_x2(n + 8)
  262. f._0_000.set_x2(n + 8)
  263. f._0_236.set_x2(n + 8)
  264. f._0_382.set_x2(n + 8)
  265. f._0_500.set_x2(n + 8)
  266. f._0_618.set_x2(n + 8)
  267. f._0_764.set_x2(n + 8)
  268. f._1_000.set_x2(n + 8)
  269. f._1_382.set_x2(n + 8)
  270. f.lab1 .set_x (n + 8)
  271. f.lab2 .set_x (n + 8)
  272.  
  273. //-----------------------------------------------------------------------------}
  274. //Plots
  275. //-----------------------------------------------------------------------------{
  276. plotn0_382 = f.n0_382.get_y2()
  277. plot_0_000 = f._0_000.get_y2()
  278. plot_0_236 = f._0_236.get_y2()
  279. plot_0_382 = f._0_382.get_y2()
  280. plot_0_500 = f._0_500.get_y2()
  281. plot_0_618 = f._0_618.get_y2()
  282. plot_0_764 = f._0_764.get_y2()
  283. plot_1_000 = f._1_000.get_y2()
  284. plot_1_382 = f._1_382.get_y2()
  285.  
  286. ch = ta.change(plot_0_000)
  287.  
  288. p1 = plot(ch ? na : plotn0_382 , color=color.new(color.blue, 100), style=plot.style_steplinebr)
  289. p2 = plot(ch ? na : plot_0_000 , color=color.new(color.blue, 100), style=plot.style_steplinebr)
  290. p3 = plot(ch ? na : plot_0_236 , color=color.new(color.blue, 100), style=plot.style_steplinebr)
  291. p4 = plot(ch ? na : plot_0_382 , color=color.new(color.blue, 100), style=plot.style_steplinebr)
  292. p5 = plot(ch ? na : plot_0_500 , color=color.new(color.blue, 100), style=plot.style_steplinebr)
  293. p6 = plot(ch ? na : plot_0_618 , color=color.new(color.blue, 100), style=plot.style_steplinebr)
  294. p7 = plot(ch ? na : plot_0_764 , color=color.new(color.blue, 100), style=plot.style_steplinebr)
  295. p8 = plot(ch ? na : plot_1_000 , color=color.new(color.blue, 100), style=plot.style_steplinebr)
  296. p9 = plot(ch ? na : plot_1_382 , color=color.new(color.blue, 100), style=plot.style_steplinebr)
  297.  
  298. fill(p1, p2, ch or not fl ? na : plot_0_000 , math.avg(plotn0_382, plot_0_000) , F0 , color.new(chart.bg_color, 100) )
  299. fill(p1, p2, ch or not fl ? na : plotn0_382 , math.avg(plotn0_382, plot_0_000) , F0 , color.new(chart.bg_color, 100) )
  300. fill(p3, p4, ch or not fl ? na : plot_0_236 , math.avg(plot_0_382, math.avg(plot_0_382, plot_0_236)), F_ , color.new(chart.bg_color, 100) )
  301. fill(p3, p4, ch or not fl ? na : math.avg(plot_0_236, math.avg(plot_0_382, plot_0_236)), plot_0_382 , color.new(chart.bg_color, 100), F_ )
  302. fill(p4, p5, ch or not fl ? na : plot_0_382 , plot_0_500 , color.new(chart.bg_color, 100), Fm )
  303. fill(p5, p6, ch or not fl ? na : plot_0_500 , plot_0_618 , Fm , color.new(chart.bg_color, 100) )
  304. fill(p6, p7, ch or not fl ? na : plot_0_764 , math.avg(plot_0_618, math.avg(plot_0_618, plot_0_764)), Fp , color.new(chart.bg_color, 100) )
  305. fill(p6, p7, ch or not fl ? na : math.avg(plot_0_764, math.avg(plot_0_618, plot_0_764)), plot_0_618 , color.new(chart.bg_color, 100), Fp )
  306. fill(p8, p9, ch or not fl ? na : plot_1_000 , math.avg(plot_1_382, plot_1_000) , F1 , color.new(chart.bg_color, 100) )
  307. fill(p8, p9, ch or not fl ? na : plot_1_382 , math.avg(plot_1_382, plot_1_000) , F1 , color.new(chart.bg_color, 100) )
  308.  
  309. plotshape(sb and brokeU and f.active[1] ? high : na, location=location.abovebar, style=shape.labeldown, size=size.tiny, color=#089981)
  310. plotshape(sb and brokeD and f.active[1] ? low : na, location=location.belowbar, style=shape.labelup , size=size.tiny, color=#f23645)
  311.  
  312. //-----------------------------------------------------------------------------}
  313. //Alerts
  314. //-----------------------------------------------------------------------------{
  315. alertcondition(brokeU and f.active[1], 'break Up' , 'break Up' )
  316. alertcondition(brokeD and f.active[1], 'break Down', 'break Donw')
  317.  
  318. //-----------------------------------------------------------------------------}
Advertisement
Add Comment
Please, Sign In to add comment