xmd79

Volume Profile Regression Channel [LuxAlgo]

May 17th, 2023
536
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 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("Volume Profile Regression Channel [LuxAlgo]", "LuxAlgo - Volume Profile Regression Channel", overlay = true, max_lines_count = 500)
  6. //------------------------------------------------------------------------------
  7. //Settings
  8. //-----------------------------------------------------------------------------{
  9. sections = input.int(100 , minval = 2)
  10. width = input.float(25, 'Width %' , minval = 0, maxval = 100) / 100
  11. vaPer = input.float(50, 'Volume Area %', minval = 0, maxval = 100) / 100
  12.  
  13. //Style
  14. bull = input(color.teal, 'Bullish' , group = 'Style')
  15. bear = input(color.red, 'Bearish' , group = 'Style')
  16.  
  17. showLoc = input(true, 'Show Loc' , inline = 'loc' , group = 'Style')
  18. locCss = input(#ffeb3b, '' , inline = 'loc' , group = 'Style')
  19.  
  20. showDloc = input(true, 'Show Developing Loc', inline = 'dloc', group = 'Style')
  21. dlocCss = input(#2157f3, '' , inline = 'dloc', group = 'Style')
  22.  
  23. vaTransp = input.int(50, 'VA Transparency' , minval = 0, maxval = 100, group = 'Style')
  24. outVaTransp = input.int(80, 'Outside VA Transparency', minval = 0, maxval = 100, group = 'Style')
  25.  
  26. //Coordinates
  27. userX1 = input.time(0, confirm = true, inline = '1', group = 'Coordinates')
  28. userX2 = input.time(0, confirm = true, inline = '2', group = 'Coordinates')
  29.  
  30. //-----------------------------------------------------------------------------}
  31. //Main
  32. //-----------------------------------------------------------------------------{
  33. var x1 = 0
  34. var x2 = 0
  35. var float mean = na
  36. var float wmean = na
  37. var den = 0
  38.  
  39. //Initial LOC line
  40. var loc_ext = line.new(na,na,na,na
  41. , style = line.style_dashed
  42. , color = locCss
  43. , extend = extend.right)
  44.  
  45. //Initial profile bins array
  46. var lines = array.new<line>(0)
  47.  
  48. if barstate.isfirst
  49. for i = 0 to sections-1 //Populate array
  50. lines.push(line.new(na,na,na,na))
  51.  
  52. //-----------------------------------------------------------------------------}
  53. //Get anchor coordinates and calculate trailing weighted/sum
  54. //-----------------------------------------------------------------------------{
  55. n = bar_index
  56.  
  57. if time == userX1
  58. x1 := n
  59. den := 1
  60. mean := close
  61. wmean := close
  62. else
  63. den += 1
  64. mean += close
  65. wmean += close * den
  66.  
  67. //-----------------------------------------------------------------------------}
  68. //Set regression profile
  69. //-----------------------------------------------------------------------------{
  70. var vol = array.new<float>(sections, 0)
  71.  
  72.  
  73. //Set volume profile
  74. if time == userX2
  75. x2 := n
  76. dist = 0.
  77.  
  78. //Get linreg coordinates & slope
  79. mean := mean / den
  80. wmean := wmean / (den * (den+1) / 2)
  81. lreg_y1 = 4 * mean - 3 * wmean
  82. lreg_y2 = 3 * wmean - 2 * mean
  83.  
  84. slope = (lreg_y2 - lreg_y1)/(x2 - x1)
  85.  
  86. css = slope > 0 ? bull : bear //Get color based on slope sign
  87.  
  88. //Get channel width
  89. max = 0., max_dist = 0.
  90. min = 0., min_dist = 0.
  91.  
  92. for i = 0 to (x2 - x1)
  93. y2 = lreg_y2 + slope * -i
  94.  
  95. max := math.max(high[i] - y2, max, 0)
  96. min := math.min(low[i] - y2, min, 0)
  97.  
  98. lreg_y1 += min
  99. lreg_y2 += min
  100. dist := math.abs(max + -min)
  101.  
  102. //Display profile bins/loc/developing loc
  103. dloc = 0.
  104. float prev_dloc = na
  105.  
  106. max_vol = 0.
  107. //Loop over the user set interval
  108. for i = 0 to (x2 - x1)
  109. h = high[(x2 - x1) - i]
  110. l = low[(x2 - x1) - i]
  111. v = volume[(x2 - x1) - i]
  112. y2 = lreg_y1 + slope * i
  113.  
  114. d = 0.
  115. lvl = y2
  116. prev = y2
  117.  
  118. //Loop over the channel areas
  119. for j = 0 to sections-1
  120. lvl += dist / sections
  121. d += dist / sections
  122.  
  123. //If within area accumulate volume
  124. if h > math.min(lvl, prev) and l < math.max(lvl, prev)
  125. vol.set(j, vol.get(j) + v)
  126.  
  127. //Get developing loc
  128. get_vol = vol.get(j)
  129. max_vol := math.max(get_vol, max_vol)
  130. dloc := get_vol == max_vol ? lvl : dloc
  131. prev := lvl
  132.  
  133. //Get bin length
  134. idx = int(get_vol / max_vol * width * (x2 - x1))
  135.  
  136. //Set bins
  137. get_l = lines.get(j)
  138. get_l.set_xy1(x1, lreg_y1 + d)
  139. get_l.set_xy2(x1 + idx, lreg_y1 + slope * idx + d)
  140.  
  141. //Highlight and set loc extension
  142. if vol.get(j) == max_vol and showLoc
  143. get_l.set_color(color.yellow)
  144. loc_ext.set_xy1(x1 + idx, lreg_y1 + slope * idx + d)
  145. loc_ext.set_xy2(x1 + idx + 1, lreg_y1 + slope * (idx + 1) + d)
  146. else
  147. get_l.set_color(color.new(css, outVaTransp))
  148.  
  149. //Set developing point
  150. if showDloc
  151. line.new(x1 + i - 1, prev_dloc, x1 + i, dloc)
  152.  
  153. prev_dloc := dloc
  154.  
  155. va_idx = 0
  156. vol_sum = vol.sum()
  157. loc_idx = vol.indexof(max_vol)
  158.  
  159. //Set value area
  160. for i = 0 to sections-1
  161. va_idx += 1
  162. gotop = loc_idx + va_idx <= sections-1
  163. gobtm = loc_idx - va_idx >= 0
  164.  
  165. //Loop above LOC
  166. if gotop
  167. max_vol += vol.get(loc_idx + va_idx)
  168. lines.get(loc_idx + va_idx).set_color(color.new(css, vaTransp))
  169.  
  170. //Loop below LOC
  171. if gobtm
  172. max_vol += vol.get(loc_idx - va_idx)
  173. lines.get(loc_idx - va_idx).set_color(color.new(css, vaTransp))
  174.  
  175. if max_vol / vol_sum > vaPer
  176. break
  177.  
  178. //Set Lines
  179. l1 = line.new(userX1, lreg_y1, userX2, lreg_y2, xloc.bar_time, color = css)
  180. l2 = line.new(userX1, lreg_y1 + dist, userX2, lreg_y2 + dist, xloc.bar_time, color = color(na))
  181.  
  182. //Set fill
  183. linefill.new(l1, l2, color.new(color.gray, 90))
  184.  
  185. //-----------------------------------------------------------------------------}
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment