Advertisement
xmd79

Fibonacci Trailing Stop [LuxAlgo]

Sep 12th, 2023
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.43 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 Trailing Stop [LuxAlgo]", "LuxAlgo - Fibonacci Trailing Stop", max_lines_count=500, max_labels_count=500, overlay=true)
  6.  
  7. //------------------------------------------------------------------------------
  8. //Settings
  9. //-----------------------------------------------------------------------------{
  10. L = input.int ( 20 , 'L' , group= 'swings' , minval= 1 )
  11. R = input.int ( 1 , 'R' , group= 'swings' , minval= 1 )
  12. lb= input.bool ( false , '            Swings labels', group= 'swings' )
  13. F = input.float ( -0.382 , 'Level       ' , inline='_', group='Fibonacci Trailing Stop', options= [-0.5, -0.382, -0.236 , 0, 0.236, 0.382, 0.5, 0.618] )
  14. i = input.bool ( false , '' , inline='_', group='Fibonacci Trailing Stop' )
  15. f = input.float ( 1.618 , '' , inline='_', group='Fibonacci Trailing Stop', step = 0.001 )
  16. c = input.string( 'close' , 'Trigger' , group='Fibonacci Trailing Stop', options= ['close', 'wick'] )
  17. cU= input.color ( #089981 , '                  ' , inline='c', group='Fibonacci Trailing Stop' )
  18. cD= input.color ( #f23645 , '' , inline='c', group='Fibonacci Trailing Stop' )
  19. sF= input.bool ( true , '         Latest Fibonacci', group= 'Fibonacci' )
  20. F0= input.color (#08998164 , '           ' , inline='f', group= 'Fibonacci' )
  21. Fm= input.color (#1e42b070 , '' , inline='f', group= 'Fibonacci' )
  22. F1= input.color (#f2364564 , '' , inline='f', group= 'Fibonacci' )
  23. fl= input.bool ( true , '              Shadows' )
  24.  
  25. Ls= math.max(1, math.ceil(L / 2))
  26. Rs= math.max(1, math.ceil(R / 2))
  27.  
  28. n = bar_index
  29.  
  30. iF=i ? f : F
  31.  
  32. //-----------------------------------------------------------------------------}
  33. //User Defined Types
  34. //-----------------------------------------------------------------------------{
  35. type piv
  36. int b
  37. float p
  38. int d
  39. label l
  40.  
  41. type fib
  42. linefill lf_0
  43. linefill lfMd
  44. linefill lf_1
  45. line ln_0_5
  46. line diagon
  47.  
  48. //-----------------------------------------------------------------------------}
  49. //Variables
  50. //-----------------------------------------------------------------------------{
  51. var int dir = 0
  52. var float st = close
  53. var float max = high
  54. var float min = low
  55. var float dif = na
  56. var float other_side = na
  57.  
  58. var piv[] pivs = array.new<piv>()
  59.  
  60. var fib fib = fib.new(
  61. lf_0 = linefill.new(
  62. line .new(na, na, na, na, color=sF ? F0 : color.new(color.blue, 100))
  63. , line .new(na, na, na, na, color=sF ? F0 : color.new(color.blue, 100))
  64. , color .new(F0 , fl ? 100: 85)
  65. ),
  66. lfMd = linefill.new(
  67. line .new(na, na, na, na, color=sF ? Fm : color.new(color.blue, 100))
  68. , line .new(na, na, na, na, color=sF ? Fm : color.new(color.blue, 100))
  69. , color .new(Fm , fl ? 100: 85)
  70. ),
  71. lf_1 = linefill.new(
  72. line .new(na, na, na, na, color=sF ? F1 : color.new(color.blue, 100))
  73. , line .new(na, na, na, na, color=sF ? F1 : color.new(color.blue, 100))
  74. , color .new(F1 , fl ? 100: 85)
  75. ),
  76. ln_0_5 = line.new(na, na, na, na, color=sF ? color.blue : color.new(color.blue, 100)),
  77. diagon = line.new(na, na, na, na, color=sF ? color.silver : color.new(color.blue, 100)
  78. , style= line.style_dashed ))
  79.  
  80. var line_0 = line.new(na, na, na, na)
  81. var line_1 = line.new(na, na, na, na)
  82.  
  83. //-----------------------------------------------------------------------------}
  84. //Function
  85. //-----------------------------------------------------------------------------{
  86. flab(i, x, y) => label.new(x, y, style=i == 1 ? label.style_label_down : label.style_label_up, color=color.gray)
  87.  
  88. //-----------------------------------------------------------------------------}
  89. //Execution
  90. //-----------------------------------------------------------------------------{
  91. ph = ta.pivothigh(L , R )
  92. pl = ta.pivotlow (L , R )
  93.  
  94. ph_= ta.pivothigh(Ls, Rs) // used for fill ~ ST
  95. pl_= ta.pivotlow (Ls, Rs) // used for fill ~ ST
  96.  
  97. if ph
  98. if pivs.size() > 0
  99. get = pivs.first()
  100. if get.d > 0 and ph > get.p
  101. get.b := n -R
  102. get.p := high[R]
  103. get.l.set_xy(n -R, high[R])
  104. if get.d < 0 and ph > get.p
  105. pivs.unshift(piv.new(n -R, high[R] , 1, lb ? flab( 1, n -R, high[R]) : na))
  106. else
  107. pivs .unshift(piv.new(n -R, high[R] , 1, lb ? flab( 1, n -R, high[R]) : na))
  108.  
  109. if pl
  110. if pivs.size() > 0
  111. get = pivs.first()
  112. if get.d < 0 and pl < get.p
  113. get.b := n -R
  114. get.p := low [R]
  115. get.l.set_xy(n -R, low [R])
  116. if get.d > 0 and pl < get.p
  117. pivs.unshift(piv.new(n -R, low [R] ,-1, lb ? flab(-1, n -R, low [R]) : na))
  118. else
  119. pivs .unshift(piv.new(n -R, low [R] ,-1, lb ? flab(-1, n -R, low [R]) : na))
  120.  
  121. if pivs.size() >= 2
  122. max := math.max(pivs.first().p, pivs.get(1).p)
  123. min := math.min(pivs.first().p, pivs.get(1).p)
  124. if pivs.size() == 2
  125. st := math.avg(max, min)
  126. other_side := math.avg(max, min)
  127. dif := max - min
  128. max += dif * iF
  129. min -= dif * iF
  130.  
  131. _0i = pivs.first( ).b, _0p = pivs.first( ).p
  132. _1i = pivs.get (1).b, _1p = pivs.get (1).p
  133. d = _0p < _1p ? -dif : dif
  134.  
  135. l0_1 = fib. lf_0.get_line1(), l0_2 = fib. lf_0.get_line2()
  136. lMd1 = fib. lfMd.get_line1(), lMd2 = fib. lfMd.get_line2()
  137. l1_1 = fib. lf_1.get_line1(), l1_2 = fib. lf_1.get_line2()
  138.  
  139. if (ph or pl) and (sF or fl)
  140. l0_1 .set_xy1(_0i, _0p ), l0_1 .set_xy2( n + 3 , _0p )
  141. l0_2 .set_xy1(_0i, _0p - (d * 0.236)), l0_2 .set_xy2( n + 3 , _0p - (d * 0.236))
  142. lMd1 .set_xy1(_0i, _0p - (d * 0.382)), lMd1 .set_xy2( n + 3 , _0p - (d * 0.382))
  143. lMd2 .set_xy1(_0i, _0p - (d * 0.618)), lMd2 .set_xy2( n + 3 , _0p - (d * 0.618))
  144. l1_1 .set_xy1(_0i, _0p - (d * 0.786)), l1_1 .set_xy2( n + 3 , _0p - (d * 0.786))
  145. l1_2 .set_xy1(_0i, _1p ), l1_2 .set_xy2( n + 3 , _1p )
  146. fib.ln_0_5.set_xy1(_0i, _0p - (d * 0.5 )), fib.ln_0_5.set_xy2( n + 3 , _0p - (d * 0.5 ))
  147. fib.diagon.set_xy1(_0i, _0p ), fib.diagon.set_xy2(_1i , _1p )
  148. else
  149. l0_1 .set_x2 ( n + 3 )
  150. l0_2 .set_x2 ( n + 3 )
  151. lMd1 .set_x2 ( n + 3 )
  152. lMd2 .set_x2 ( n + 3 )
  153. l1_1 .set_x2 ( n + 3 )
  154. l1_2 .set_x2 ( n + 3 )
  155. fib.ln_0_5.set_x2 ( n + 3 )
  156.  
  157. price = c == 'close' ? close : dir < 1 ? high : low
  158.  
  159. if dir < 1
  160. if price > st
  161. st := min
  162. dir := 1
  163. else
  164. st := math.min(st, max)
  165.  
  166. if dir > -1
  167. if price < st
  168. st := max
  169. dir := -1
  170. else
  171. st := math.max(st, min)
  172.  
  173. col = dir < 1 ? cD : cU
  174.  
  175. other_side := switch
  176. dir < 1 and ph_ => math.min(other_side, ph_, st)
  177. dir < 1 => math.min(other_side , st)
  178. dir > -1 and pl_ => math.max(other_side, pl_, st)
  179. dir > -1 => math.max(other_side , st)
  180. => other_side
  181.  
  182. //-----------------------------------------------------------------------------}
  183. //Plots
  184. //-----------------------------------------------------------------------------{
  185. plot0 = fib.lf_0.get_line1().get_y2()
  186. plot_236 = fib.lf_0.get_line2().get_y2()
  187. plot_382 = fib.lfMd.get_line1().get_y2()
  188. plot_500 = fib.ln_0_5 .get_y2()
  189. plot_618 = fib.lfMd.get_line2().get_y2()
  190. plot_786 = fib.lf_1.get_line1().get_y2()
  191. plot1 = fib.lf_1.get_line2().get_y2()
  192.  
  193. ch = ta.change(plot0) or ta.change(plot1) // when lines change
  194. p1 = plot(not ch ? plot0 : na, color=color.new(color.blue, 100), style=plot.style_circles, display=display.none)
  195. p2 = plot(not ch ? plot_236 : na, color=color.new(color.blue, 100), style=plot.style_circles, display=display.none)
  196. p3 = plot(not ch ? plot_382 : na, color=color.new(color.blue, 100), style=plot.style_circles, display=display.none)
  197. p4 = plot(not ch ? plot_618 : na, color=color.new(color.blue, 100), style=plot.style_circles, display=display.none)
  198. p5 = plot(not ch ? plot_786 : na, color=color.new(color.blue, 100), style=plot.style_circles, display=display.none)
  199. p6 = plot(not ch ? plot1 : na, color=color.new(color.blue, 100), style=plot.style_circles, display=display.none)
  200.  
  201. fill(p1, p2, ch or not fl ? na : math.avg(plot0, plot_236), plot_236, color.new(chart.bg_color, 100), F0)
  202. fill(p3, p4, ch or not fl ? na : plot_382 , plot_500, Fm, color.new(chart.bg_color, 100) )
  203. fill(p3, p4, ch or not fl ? na : plot_500 , plot_618, color.new(chart.bg_color, 100), Fm)
  204. fill(p5, p6, ch or not fl ? na : math.avg(plot1, plot_786), plot_786, color.new(chart.bg_color, 100), F1)
  205.  
  206. s = plot( st , 'Fib Trailing Stop', color= col , display=display.pane + display.data_window)
  207. o = plot(fl ? na : other_side, 'other side' , color=color.new(col , 75), display=display.pane + display.data_window)
  208. fill ( o , s , color=color.new(col, 90))
  209.  
  210. //-----------------------------------------------------------------------------}
  211. //Alerts
  212. //-----------------------------------------------------------------------------{
  213. alertcondition(ta.change(dir) and dir == 1, 'Uptrend' , 'Uptrend' )
  214. alertcondition(ta.change(dir) and dir == -1, 'Downtrend' , 'Downtrend' )
  215. alertcondition(ta.change(dir) , 'Trend change', 'Trend change')
  216.  
  217. //-----------------------------------------------------------------------------}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement