xmd79

VP-Period

Sep 30th, 2021
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1.  
  2. // © juliangonzaconde
  3.  
  4. //@version=4
  5. study("VP-Period", overlay=true, max_bars_back = 1000, max_lines_count=500)
  6.  
  7. var bool is_first_candle= true
  8. var diff = 0.0
  9. float maxV = 0.0
  10. int maxV_index = 0
  11. int last_time = 0
  12. margin=0.0
  13. ys=0.0
  14. int bar_offset=0
  15. var is_first_inRange=false
  16.  
  17.  
  18.  
  19. period_time = input(defval = 10,
  20. title = "Number of days (Min: 3, Max: 500)",
  21. type = input.integer,
  22. minval = 3,
  23. maxval = 500)
  24.  
  25. resolution = input(defval = 500,
  26. title = "calculation resolution (Min: 400, Max: 700)",
  27. type = input.integer,
  28. minval = 400,
  29. maxval = 700)
  30.  
  31. show_from = input(defval = 3,
  32. title = "show support/resistance since (Min: 2, Max: 45)",
  33. type = input.integer,
  34. minval = 2,
  35. maxval = 45)
  36.  
  37.  
  38. show_volume_bars = input(defval = true,
  39. title = "Show volume bars (If selected, maximum days are 17)",
  40. type = input.bool
  41. )
  42.  
  43. show_vpoc_line = input(defval = true,
  44. title = "Show VPOC line",
  45. type = input.bool
  46. )
  47.  
  48. show_support_line = input(defval = true,
  49. title = "Show support line",
  50. type = input.bool
  51. )
  52.  
  53. show_res_line = input(defval = true,
  54. title = "Show resistance line",
  55. type = input.bool
  56. )
  57.  
  58. show_H_line = input(defval = true,
  59. title = "Show high line",
  60. type = input.bool
  61. )
  62.  
  63. show_L_line = input(defval = true,
  64. title = "Show low line",
  65. type = input.bool
  66. )
  67.  
  68. show_labels = input(defval = true,
  69. title = "Show from days label",
  70. type = input.bool
  71. )
  72.  
  73. vpoc_color = input(defval = color.new(color.yellow, 10),
  74. title = "VPOC color",
  75. type = input.color
  76. )
  77.  
  78. high_low_color = input(defval = color.new(color.blue, 50),
  79. title = "High-low lines color",
  80. type = input.color
  81. )
  82.  
  83. volume_bars_color = input(defval = color.new(color.green, 70),
  84. title = "Volume bars color",
  85. type = input.color
  86. )
  87.  
  88. sup_color = input(defval = color.new(color.red, 10),
  89. title = "Support line color",
  90. type = input.color
  91. )
  92.  
  93. res_color = input(defval = color.new(color.green, 10),
  94. title = "Resistance line color",
  95. type = input.color
  96. )
  97.  
  98.  
  99. range_prices = array.new_float((resolution + 1), 0.0)
  100. partial_vol = array.new_float(resolution, 0.0)
  101. total_vol = array.new_float(resolution, 0.0)
  102. var all_ys = array.new_float(show_from, 0.0)
  103. var ys_time = array.new_float(show_from, 0.0)
  104. var is_support = array.new_bool(show_from, true)
  105.  
  106. t = time("1440", session.regular)
  107. var int startTime =time
  108. int ncandles =0
  109.  
  110. if barstate.isfirst
  111. startTime := timenow - period_time*86400000
  112.  
  113.  
  114. bar_offset:= barssince(is_first_inRange)
  115. is_first = na(t[1]) and not na(t) or t[1] < t
  116. is_first_inRange:=is_first and t>startTime
  117. high_price=0.0
  118. low_price=0.0
  119. price_level=0.0
  120.  
  121. if is_first_inRange or barstate.islast
  122.  
  123. last_time:= t[1]
  124. ncandles := bar_offset
  125. current_low_price=0.0
  126. for i = 1 to (ncandles)
  127. current_low_price:=low[i]
  128. if current_low_price < low_price or low_price==0.0
  129. low_price:=current_low_price
  130.  
  131. current_high_price=0.0
  132. for i = 1 to (ncandles)
  133. current_high_price:=high[i]
  134. if current_high_price >high_price or high_price==0.0
  135. high_price:=current_high_price
  136. diff := (high_price-low_price)/resolution
  137.  
  138. for j = 0 to (resolution)
  139. array.set(range_prices, (j), (low_price + (diff * (j+1))))
  140.  
  141. for i = 0 to (ncandles)
  142. int w_candle = 0
  143. array.fill(partial_vol, 0.0)
  144.  
  145. for j=0 to (resolution-1)
  146. float current_price = array.get(range_prices, j)
  147. if high[i] > current_price and low[i] < current_price
  148. float j_partial_vol = array.get(partial_vol, j)
  149. float sum_vol = j_partial_vol + nz(volume[i])
  150. array.set(partial_vol, j, sum_vol)
  151. w_candle := w_candle + 1
  152.  
  153. for j = 0 to (resolution - 1)
  154. float j_total_vol = array.get(total_vol, j)
  155. float j_partial_vol = array.get(partial_vol, j)
  156. float sum_partial_total=0.0
  157. if w_candle > 0
  158. sum_partial_total := j_total_vol + j_partial_vol/w_candle
  159. else
  160. sum_partial_total := j_total_vol
  161. array.set(total_vol, j, sum_partial_total)
  162. maxV := array.max(total_vol)
  163. maxV_index := array.indexof(total_vol, maxV)
  164.  
  165. if is_first_inRange
  166. price_level:=diff*maxV_index+low_price
  167. if high_price - price_level > price_level - low_price
  168. margin:=high_price - price_level
  169. ys:=price_level - margin
  170. else
  171. margin:=price_level - low_price
  172. ys:=price_level + margin
  173. if ys > high_price
  174. array.shift(is_support)
  175. array.push(is_support, false)
  176. else
  177. array.shift(is_support)
  178. array.push(is_support, true)
  179. array.shift(all_ys)
  180. array.push(all_ys, ys)
  181. array.shift(ys_time)
  182. array.push(ys_time, t)
  183.  
  184. if barstate.islast
  185. for i=0 to (show_from - 1)
  186. ys := array.get(all_ys, i)
  187. time_back = array.get(ys_time, i)
  188. if array.get(is_support, i)
  189. if show_support_line
  190. line.new(x1=int(time_back), y1=ys, x2=time + 86400000, y2=ys, color=sup_color ,xloc= xloc.bar_time, extend = extend.none, width=2, style=line.style_dashed)
  191. else
  192. if show_res_line
  193. line.new(x1=int(time_back), y1=ys, x2=time + 86400000, y2=ys, color=res_color ,xloc= xloc.bar_time, extend = extend.none, width=2, style=line.style_dashed)
  194.  
  195. if show_labels
  196. label.new(int(time + 86400000), ys, text ="From days: " + tostring(show_from - i), xloc=xloc.bar_time)
  197.  
  198. dif_time=0.0
  199.  
  200.  
  201. if is_first_inRange or barstate.islast
  202.  
  203. last_time:= int(t[1])
  204. dif_time:= (time-last_time)
  205. if dif_time/86400000 > 1.5
  206. dif_time:=(dif_time)/9
  207. else
  208. dif_time:=(dif_time)/3
  209. diff_t=0
  210. x1=0.0
  211. norm_vol=0.0
  212. proportion=resolution/40
  213. for i = 0 to (resolution - 1)
  214. y=array.get(range_prices,i)
  215. current_vol= array.get(total_vol,i)
  216. norm_vol :=current_vol/maxV
  217. x1 :=dif_time*norm_vol
  218. diff_t := last_time + int(x1)
  219. if (i % int(proportion) == 0 or int(proportion) == 0) and show_volume_bars
  220. line.new(x1=t[1], y1=y, x2=diff_t, y2=y, color=volume_bars_color ,xloc= xloc.bar_time, extend = extend.none, width=2)
  221.  
  222. next_day = time + 60000 * 60 * 9
  223.  
  224. if show_L_line
  225. line.new(x1=t[1], y1=low_price, x2=next_day, y2=low_price,xloc= xloc.bar_time, color=high_low_color)
  226. if show_vpoc_line
  227. line.new(x1=t[1], y1=diff*maxV_index+low_price, x2=next_day, y2=diff*maxV_index+low_price, color=vpoc_color, width=2,xloc= xloc.bar_time)
  228. if show_H_line
  229. line.new(x1=t[1], y1=high_price, x2=next_day, y2=high_price,xloc= xloc.bar_time, color=high_low_color)
  230.  
  231.  
Advertisement
Add Comment
Please, Sign In to add comment