xmd79

Supply and Demand Visible Range [LuxAlgo]

Apr 17th, 2023
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 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("Supply and Demand Visible Range [LuxAlgo]", overlay = true, max_boxes_count = 500, max_bars_back = 500)
  6. //------------------------------------------------------------------------------
  7. //Settings
  8. //-----------------------------------------------------------------------------{
  9. per = input.float(10., 'Threshold %', minval = 0, maxval = 100)
  10. div = input.int(50, 'Resolution' , minval = 2, maxval = 500)
  11. tf = input.timeframe('', 'Intrabar TF')
  12.  
  13. //Colors
  14. showSupply = input(true ,'Supply        ', inline = 'supply', group = 'Style')
  15. supplyCss = input(#2157f3, '' , inline = 'supply', group = 'Style')
  16. supplyArea = input(true ,'Area' , inline = 'supply', group = 'Style')
  17. supplyAvg = input(true ,'Average' , inline = 'supply', group = 'Style')
  18. supplyWavg = input(true ,'Weighted' , inline = 'supply', group = 'Style')
  19.  
  20. showEqui = input(true ,'Equilibrium' , inline = 'equi' , group = 'Style')
  21. equiCss = input(color.gray, '' , inline = 'equi' , group = 'Style')
  22. equiAvg = input(true ,'Average' , inline = 'equi' , group = 'Style')
  23. equiWavg = input(true ,'Weighted' , inline = 'equi' , group = 'Style')
  24.  
  25. showDemand = input(true ,'Demand    ' , inline = 'demand', group = 'Style')
  26. demandCss = input(#ff5d00, '' , inline = 'demand', group = 'Style')
  27. demandArea = input(true ,'Area' , inline = 'demand', group = 'Style')
  28. demandAvg = input(true ,'Average' , inline = 'demand', group = 'Style')
  29. demandWavg = input(true ,'Weighted' , inline = 'demand', group = 'Style')
  30.  
  31. //-----------------------------------------------------------------------------}
  32. //UDT's
  33. //-----------------------------------------------------------------------------{
  34. type bin
  35. float lvl
  36. float prev
  37. float sum
  38. float prev_sum
  39. float csum
  40. float avg
  41. bool isreached
  42.  
  43. type area
  44. box bx
  45. line avg
  46. line wavg
  47.  
  48. //-----------------------------------------------------------------------------}
  49. //Functions
  50. //-----------------------------------------------------------------------------{
  51. n = bar_index
  52.  
  53. get_hlv()=> [high, low, volume]
  54.  
  55. method set_area(area id, x1, top, btm, avg, wavg, showArea, showAvg, showWavg)=>
  56. if showArea
  57. id.bx.set_lefttop(x1, top)
  58. id.bx.set_rightbottom(n, btm)
  59.  
  60. if showAvg
  61. id.avg.set_xy1(x1, avg)
  62. id.avg.set_xy2(n, avg)
  63.  
  64. if showWavg
  65. id.wavg.set_xy1(x1, wavg)
  66. id.wavg.set_xy2(n, wavg)
  67.  
  68. //-----------------------------------------------------------------------------}
  69. //Main variables
  70. //-----------------------------------------------------------------------------{
  71. var max = 0.
  72. var min = 0.
  73. var x1 = 0
  74. var csum = 0.
  75.  
  76. //Intrabar data
  77. [h, l, v] = request.security_lower_tf(syminfo.tickerid, tf, get_hlv())
  78.  
  79. //Init on left bar
  80. if time == chart.left_visible_bar_time
  81. max := high
  82. min := low
  83. csum := volume
  84. x1 := n
  85. else //Accumulate
  86. max := math.max(high, max)
  87. min := math.min(low, min)
  88. csum += volume
  89.  
  90. //-----------------------------------------------------------------------------}
  91. //Set zones
  92. //-----------------------------------------------------------------------------{
  93. var supply_area = area.new(
  94. box.new(na, na, na, na, na, bgcolor = color.new(supplyCss, 80))
  95. , line.new(na, na, na, na, color = supplyCss)
  96. , line.new(na, na, na, na, color = supplyCss, style = line.style_dashed))
  97.  
  98. var demand_area = area.new(
  99. box.new(na, na, na, na, na, bgcolor = color.new(demandCss, 80))
  100. , line.new(na, na, na, na, color = demandCss)
  101. , line.new(na, na, na, na, color = demandCss, style = line.style_dashed))
  102.  
  103. var equi = line.new(na, na, na, na, color = equiCss)
  104. var wequi = line.new(na, na, na, na, color = equiCss, style = line.style_dashed)
  105.  
  106. var float supply_wavg = na
  107. var float demand_wavg = na
  108.  
  109. if time == chart.right_visible_bar_time
  110. r = (max - min) / div
  111. supply = bin.new(max, max, 0, 0, 0, 0, false)
  112. demand = bin.new(min, min, 0, 0, 0, 0, false)
  113.  
  114. //Loop trough intervals
  115. for i = 0 to div-1
  116. supply.lvl -= r
  117. demand.lvl += r
  118.  
  119. //Accumulated volume column
  120. if not supply.isreached and showSupply and supplyArea
  121. box.new(x1, supply.prev, x1 + int(supply.sum / csum * (n - x1)), supply.lvl, na
  122. , bgcolor = color.new(supplyCss, 50))
  123.  
  124. if not demand.isreached and showDemand and demandArea
  125. box.new(x1, demand.lvl, x1 + int(demand.sum / csum * (n - x1)), demand.prev, na
  126. , bgcolor = color.new(demandCss, 50))
  127.  
  128. //Loop trough bars
  129. for j = 0 to (n - x1)-1
  130. //Loop trough intrabars
  131. for k = 0 to (v[j]).size()-1
  132. //Accumulate if within upper internal
  133. supply.sum += (h[j]).get(k) > supply.lvl and (h[j]).get(k) < supply.prev ? (v[j]).get(k) : 0
  134. supply.avg += supply.lvl * (supply.sum - supply.prev_sum)
  135. supply.csum += supply.sum - supply.prev_sum
  136. supply.prev_sum := supply.sum
  137.  
  138. //Accumulate if within lower interval
  139. demand.sum += (l[j]).get(k) < demand.lvl and (l[j]).get(k) > demand.prev ? (v[j]).get(k) : 0
  140. demand.avg += demand.lvl * (demand.sum - demand.prev_sum)
  141. demand.csum += demand.sum - demand.prev_sum
  142. demand.prev_sum := demand.sum
  143.  
  144. //Test if supply accumulated volume exceed threshold and set box
  145. if supply.sum / csum * 100 > per and not supply.isreached
  146. avg = math.avg(max, supply.lvl)
  147. supply_wavg := supply.avg / supply.csum
  148.  
  149. //Set Box/Level coordinates
  150. if showSupply
  151. supply_area.set_area(x1, max, supply.lvl, avg, supply_wavg, supplyArea, supplyAvg, supplyWavg)
  152.  
  153. supply.isreached := true
  154.  
  155. //Test if demand accumulated volume exceed threshold and set box
  156. if demand.sum / csum * 100 > per and not demand.isreached and showDemand
  157. avg = math.avg(min, demand.lvl)
  158. demand_wavg := demand.avg / demand.csum
  159.  
  160. //Set Box/Level coordinates
  161. if showDemand
  162. demand_area.set_area(x1, demand.lvl, min, avg, demand_wavg, demandArea, demandAvg, demandWavg)
  163.  
  164. demand.isreached := true
  165.  
  166. if supply.isreached and demand.isreached
  167. break
  168.  
  169. if supply.isreached and demand.isreached and showEqui
  170. //Set equilibrium
  171. if equiAvg
  172. avg = math.avg(max, min)
  173. equi.set_xy1(x1, avg)
  174. equi.set_xy2(n, avg)
  175.  
  176. //Set weighted equilibrium
  177. if equiWavg
  178. wavg = math.avg(supply_wavg, demand_wavg)
  179. wequi.set_xy1(x1, wavg)
  180. wequi.set_xy2(n, wavg)
  181.  
  182. break
  183.  
  184. supply.prev := supply.lvl
  185. demand.prev := demand.lvl
  186.  
  187. //-----------------------------------------------------------------------------}
Advertisement
Add Comment
Please, Sign In to add comment