xmd79

Order Blocks & Breaker Blocks [LuxAlgo]

Mar 11th, 2023
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.99 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("Order Blocks & Breaker Blocks [LuxAlgo]", overlay = true
  6. , max_lines_count = 500
  7. , max_labels_count = 500
  8. , max_boxes_count = 500)
  9. //------------------------------------------------------------------------------
  10. //Settings
  11. //-----------------------------------------------------------------------------{
  12. length = input.int(10, 'Swing Lookback' , minval = 3)
  13. showBull = input.int(3, 'Show Last Bullish OB', minval = 0)
  14. showBear = input.int(3, 'Show Last Bearish OB', minval = 0)
  15. useBody = input(false, 'Use Candle Body')
  16.  
  17. //Style
  18. bullCss = input(color.new(#2157f3, 80), 'Bullish OB' , inline = 'bullcss', group = 'Style')
  19. bullBreakCss = input(color.new(#ff1100, 80), 'Bullish Break', inline = 'bullcss', group = 'Style')
  20.  
  21. bearCss = input(color.new(#ff5d00, 80), 'Bearish OB' , inline = 'bearcss', group = 'Style')
  22. bearBreakCss = input(color.new(#0cb51a, 80), 'Bearish Break', inline = 'bearcss', group = 'Style')
  23.  
  24. showLabels = input(false, 'Show Historical Polarity Changes')
  25.  
  26. //-----------------------------------------------------------------------------}
  27. //UDT's
  28. //-----------------------------------------------------------------------------{
  29. type ob
  30. float top = na
  31. float btm = na
  32. int loc = bar_index
  33. bool breaker = false
  34. int break_loc = na
  35.  
  36. type swing
  37. float y = na
  38. int x = na
  39. bool crossed = false
  40.  
  41. //-----------------------------------------------------------------------------}
  42. //Functions
  43. //-----------------------------------------------------------------------------{
  44. swings(len)=>
  45. var os = 0
  46. var swing top = swing.new(na, na)
  47. var swing btm = swing.new(na, na)
  48.  
  49. upper = ta.highest(len)
  50. lower = ta.lowest(len)
  51.  
  52. os := high[len] > upper ? 0
  53. : low[len] < lower ? 1 : os
  54.  
  55. if os == 0 and os[1] != 0
  56. top := swing.new(high[length], bar_index[length])
  57.  
  58. if os == 1 and os[1] != 1
  59. btm := swing.new(low[length], bar_index[length])
  60.  
  61. [top, btm]
  62.  
  63. method notransp(color css) => color.rgb(color.r(css), color.g(css), color.b(css))
  64.  
  65. method display(ob id, css, break_css)=>
  66. if id.breaker
  67. box.new(id.loc, id.top, id.break_loc, id.btm, css.notransp()
  68. , bgcolor = css
  69. , xloc = xloc.bar_time)
  70.  
  71. box.new(id.break_loc, id.top, time+1, id.btm, na
  72. , bgcolor = break_css
  73. , extend = extend.right
  74. , xloc = xloc.bar_time)
  75.  
  76. line.new(id.loc, id.top, id.break_loc, id.top, xloc.bar_time, color = css.notransp())
  77. line.new(id.loc, id.btm, id.break_loc, id.btm, xloc.bar_time, color = css.notransp())
  78. line.new(id.break_loc, id.top, time+1, id.top, xloc.bar_time, extend.right, break_css.notransp(), line.style_dashed)
  79. line.new(id.break_loc, id.btm, time+1, id.btm, xloc.bar_time, extend.right, break_css.notransp(), line.style_dashed)
  80. else
  81. box.new(id.loc, id.top, time, id.btm, na
  82. , bgcolor = css
  83. , extend = extend.right
  84. , xloc = xloc.bar_time)
  85.  
  86. line.new(id.loc, id.top, time, id.top, xloc.bar_time, extend.right, css.notransp())
  87. line.new(id.loc, id.btm, time, id.btm, xloc.bar_time, extend.right, css.notransp())
  88.  
  89. //-----------------------------------------------------------------------------}
  90. //Detect Swings
  91. //-----------------------------------------------------------------------------{
  92. n = bar_index
  93.  
  94. [top, btm] = swings(length)
  95. max = useBody ? math.max(close, open) : high
  96. min = useBody ? math.min(close, open) : low
  97.  
  98. //-----------------------------------------------------------------------------}
  99. //Bullish OB
  100. //-----------------------------------------------------------------------------{
  101. var bullish_ob = array.new<ob>(0)
  102. bull_break_conf = 0
  103.  
  104. if close > top.y and not top.crossed
  105. top.crossed := true
  106.  
  107. minima = max[1]
  108. maxima = min[1]
  109. loc = time[1]
  110.  
  111. for i = 1 to (n - top.x)-1
  112. minima := math.min(min[i], minima)
  113. maxima := minima == min[i] ? max[i] : maxima
  114. loc := minima == min[i] ? time[i] : loc
  115.  
  116. bullish_ob.unshift(ob.new(maxima, minima, loc))
  117.  
  118. if bullish_ob.size() > 0
  119. for i = bullish_ob.size()-1 to 0
  120. element = bullish_ob.get(i)
  121.  
  122. if not element.breaker
  123. if math.min(close, open) < element.btm
  124. element.breaker := true
  125. element.break_loc := time
  126. else
  127. if close > element.top
  128. bullish_ob.remove(i)
  129. else if i < showBull and top.y < element.top and top.y > element.btm
  130. bull_break_conf := 1
  131.  
  132. //Set label
  133. if bull_break_conf > bull_break_conf[1] and showLabels
  134. label.new(top.x, top.y, '▼', color = na
  135. , textcolor = bearCss.notransp()
  136. , style = label.style_label_down
  137. , size = size.tiny)
  138.  
  139. //-----------------------------------------------------------------------------}
  140. //Bearish OB
  141. //-----------------------------------------------------------------------------{
  142. var bearish_ob = array.new<ob>(0)
  143. bear_break_conf = 0
  144.  
  145. if close < btm.y and not btm.crossed
  146. btm.crossed := true
  147.  
  148. minima = min[1]
  149. maxima = max[1]
  150. loc = time[1]
  151.  
  152. for i = 1 to (n - btm.x)-1
  153. maxima := math.max(max[i], maxima)
  154. minima := maxima == max[i] ? min[i] : minima
  155. loc := maxima == max[i] ? time[i] : loc
  156.  
  157. bearish_ob.unshift(ob.new(maxima, minima, loc))
  158.  
  159. if bearish_ob.size() > 0
  160. for i = bearish_ob.size()-1 to 0
  161. element = bearish_ob.get(i)
  162.  
  163. if not element.breaker
  164. if math.max(close, open) > element.top
  165. element.breaker := true
  166. element.break_loc := time
  167. else
  168. if close < element.btm
  169. bearish_ob.remove(i)
  170. else if i < showBear and btm.y > element.btm and btm.y < element.top
  171. bear_break_conf := 1
  172.  
  173. //Set label
  174. if bear_break_conf > bear_break_conf[1] and showLabels
  175. label.new(btm.x, btm.y, '▲', color = na
  176. , textcolor = bullCss.notransp()
  177. , style = label.style_label_up
  178. , size = size.tiny)
  179.  
  180. //-----------------------------------------------------------------------------}
  181. //Set Order Blocks
  182. //-----------------------------------------------------------------------------{
  183. for bx in box.all
  184. bx.delete()
  185.  
  186. for l in line.all
  187. l.delete()
  188.  
  189. if barstate.islast
  190. //Bullish
  191. if showBull > 0
  192. for i = 0 to math.min(showBull-1, bullish_ob.size())
  193. get_ob = bullish_ob.get(i)
  194. get_ob.display(bullCss, bullBreakCss)
  195.  
  196. //Bearish
  197. if showBear > 0
  198. for i = 0 to math.min(showBear-1, bearish_ob.size())
  199. get_ob = bearish_ob.get(i)
  200. get_ob.display(bearCss, bearBreakCss)
  201.  
  202. //-----------------------------------------------------------------------------}
Add Comment
Please, Sign In to add comment