Advertisement
xmd79

Liquidity Swings [LuxAlgo]

Feb 1st, 2023
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 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("Liquidity Swings [LuxAlgo]"
  6. , overlay = true
  7. , max_lines_count = 500
  8. , max_labels_count = 500
  9. , max_boxes_count = 500)
  10. //------------------------------------------------------------------------------
  11. //Settings
  12. //-----------------------------------------------------------------------------{
  13. length = input(14, 'Pivot Lookback')
  14.  
  15. area = input.string('Wick Extremity', 'Swing Area', options = ['Wick Extremity', 'Full Range'])
  16.  
  17. intraPrecision = input(false, 'Intrabar Precision', inline = 'intrabar')
  18. intrabarTf = input.timeframe('1', '' , inline = 'intrabar')
  19.  
  20. filterOptions = input.string('Count', 'Filter Areas By', options = ['Count', 'Volume'], inline = 'filter')
  21. filterValue = input.float(0, '' , inline = 'filter')
  22.  
  23. //Style
  24. showTop = input(true, 'Swing High' , inline = 'top', group = 'Style')
  25. topCss = input(color.red, '' , inline = 'top', group = 'Style')
  26. topAreaCss = input(color.new(color.red, 50), 'Area', inline = 'top', group = 'Style')
  27.  
  28. showBtm = input(true, 'Swing Low' , inline = 'btm', group = 'Style')
  29. btmCss = input(color.teal, '' , inline = 'btm', group = 'Style')
  30. btmAreaCss = input(color.new(color.teal, 50), 'Area', inline = 'btm', group = 'Style')
  31.  
  32. labelSize = input.string('Tiny', 'Labels Size', options = ['Tiny', 'Small', 'Normal'], group = 'Style')
  33.  
  34. //-----------------------------------------------------------------------------}
  35. //Functions
  36. //-----------------------------------------------------------------------------{
  37. n = bar_index
  38.  
  39. get_data()=> [high, low, volume]
  40.  
  41. [h, l, v] = request.security_lower_tf(syminfo.tickerid, intrabarTf, get_data())
  42.  
  43. get_counts(condition, top, btm)=>
  44. var count = 0
  45. var vol = 0.
  46.  
  47. if condition
  48. count := 0
  49. vol := 0.
  50. else
  51. if intraPrecision
  52. if n > length
  53. if array.size(v[length]) > 0
  54. for [index, element] in v[length]
  55. vol += array.get(l[length], index) < top and array.get(h[length], index) > btm ? element : 0
  56. else
  57. vol += low[length] < top and high[length] > btm ? volume[length] : 0
  58.  
  59. count += low[length] < top and high[length] > btm ? 1 : 0
  60.  
  61. [count, vol]
  62.  
  63. set_label(count, vol, x, y, css, lbl_style)=>
  64. var label lbl = na
  65. var label_size = switch labelSize
  66. 'Tiny' => size.tiny
  67. 'Small' => size.small
  68. 'Normal' => size.normal
  69.  
  70. target = switch filterOptions
  71. 'Count' => count
  72. 'Volume' => vol
  73.  
  74. if ta.crossover(target, filterValue)
  75. lbl := label.new(x, y, str.tostring(vol, format.volume)
  76. , style = lbl_style
  77. , size = label_size
  78. , color = #00000000
  79. , textcolor = css)
  80.  
  81. if target > filterValue
  82. label.set_text(lbl, str.tostring(vol, format.volume))
  83.  
  84. set_level(condition, crossed, value, count, vol, css)=>
  85. var line lvl = na
  86.  
  87. target = switch filterOptions
  88. 'Count' => count
  89. 'Volume' => vol
  90.  
  91. if condition
  92. if target[1] < filterValue[1]
  93. line.delete(lvl[1])
  94. else if not crossed[1]
  95. line.set_x2(lvl, n - length)
  96.  
  97. lvl := line.new(n - length, value, n, value
  98. , color = na)
  99.  
  100. if not crossed[1]
  101. line.set_x2(lvl, n+3)
  102.  
  103. if crossed and not crossed[1]
  104. line.set_x2(lvl, n)
  105. line.set_style(lvl, line.style_dashed)
  106.  
  107. if target > filterValue
  108. line.set_color(lvl, css)
  109.  
  110. set_zone(condition, x, top, btm, count, vol, css)=>
  111. var box bx = na
  112.  
  113. target = switch filterOptions
  114. 'Count' => count
  115. 'Volume' => vol
  116.  
  117. if ta.crossover(target, filterValue)
  118. bx := box.new(x, top, x + count, btm
  119. , border_color = na
  120. , bgcolor = css)
  121.  
  122. if target > filterValue
  123. box.set_right(bx, x + count)
  124.  
  125. //-----------------------------------------------------------------------------}
  126. //Global variables
  127. //-----------------------------------------------------------------------------{
  128. //Pivot high
  129. var float ph_top = na
  130. var float ph_btm = na
  131. var bool ph_crossed = na
  132. var ph_x1 = 0
  133. var box ph_bx = box.new(na,na,na,na
  134. , bgcolor = color.new(topAreaCss, 80)
  135. , border_color = na)
  136.  
  137. //Pivot low
  138. var float pl_top = na
  139. var float pl_btm = na
  140. var bool pl_crossed = na
  141. var pl_x1 = 0
  142. var box pl_bx = box.new(na,na,na,na
  143. , bgcolor = color.new(btmAreaCss, 80)
  144. , border_color = na)
  145.  
  146. //-----------------------------------------------------------------------------}
  147. //Display pivot high levels/blocks
  148. //-----------------------------------------------------------------------------{
  149. ph = ta.pivothigh(length, length)
  150.  
  151. //Get ph counts
  152. [ph_count, ph_vol] = get_counts(ph, ph_top, ph_btm)
  153.  
  154. //Set ph area and level
  155. if ph and showTop
  156. ph_top := high[length]
  157. ph_btm := switch area
  158. 'Wick Extremity' => math.max(close[length], open[length])
  159. 'Full Range' => low[length]
  160.  
  161. ph_x1 := n - length
  162. ph_crossed := false
  163.  
  164. box.set_lefttop(ph_bx, ph_x1, ph_top)
  165. box.set_rightbottom(ph_bx, ph_x1, ph_btm)
  166. else
  167. ph_crossed := close > ph_top ? true : ph_crossed
  168.  
  169. if ph_crossed
  170. box.set_right(ph_bx, ph_x1)
  171. else
  172. box.set_right(ph_bx, n+3)
  173.  
  174. if showTop
  175. //Set ph zone
  176. set_zone(ph, ph_x1, ph_top, ph_btm, ph_count, ph_vol, topAreaCss)
  177.  
  178. //Set ph level
  179. set_level(ph, ph_crossed, ph_top, ph_count, ph_vol, topCss)
  180.  
  181. //Set ph label
  182. set_label(ph_count, ph_vol, ph_x1, ph_top, topCss, label.style_label_down)
  183.  
  184. //-----------------------------------------------------------------------------}
  185. //Display pivot low levels/blocks
  186. //-----------------------------------------------------------------------------{
  187. pl = ta.pivotlow(length, length)
  188.  
  189. //Get pl counts
  190. [pl_count, pl_vol] = get_counts(pl, pl_top, pl_btm)
  191.  
  192. //Set pl area and level
  193. if pl and showBtm
  194. pl_top := switch area
  195. 'Wick Extremity' => math.min(close[length], open[length])
  196. 'Full Range' => high[length]
  197. pl_btm := low[length]
  198.  
  199. pl_x1 := n - length
  200. pl_crossed := false
  201.  
  202. box.set_lefttop(pl_bx, pl_x1, pl_top)
  203. box.set_rightbottom(pl_bx, pl_x1, pl_btm)
  204. else
  205. pl_crossed := close < pl_btm ? true : pl_crossed
  206.  
  207. if pl_crossed
  208. box.set_right(pl_bx, pl_x1)
  209. else
  210. box.set_right(pl_bx, n+3)
  211.  
  212. if showBtm
  213. //Set pl zone
  214. set_zone(pl, pl_x1, pl_top, pl_btm, pl_count, pl_vol, btmAreaCss)
  215.  
  216. //Set pl level
  217. set_level(pl, pl_crossed, pl_btm, pl_count, pl_vol, btmCss)
  218.  
  219. //Set pl labels
  220. set_label(pl_count, pl_vol, pl_x1, pl_btm, btmCss, label.style_label_up)
  221.  
  222. //-----------------------------------------------------------------------------}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement