xmd79

Volume Support and Resistance

May 27th, 2023
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.59 KB | None | 0 0
  1. //@version=5
  2. indicator('Volume Support and Resistance', shorttitle='VSR', overlay=true, max_bars_back=500)
  3. from = input.int(0, minval=0, maxval=400, step=1, tooltip = "start candle, maxval=400")
  4. loockback = input.int(100, minval=10, maxval=500, tooltip="The number of candles selected for the calculation to go back from the starting candle. Selecting the number of candles greater than 200 can cause errors in the time required for processing. minval=10, maxval=500")
  5. resist_high_slice = input.int(30, "Resist high slice",minval=11, maxval=60, tooltip="Aim for the highest green-to-red volume ratio by setting the maximum number of slices from Highest momentum price to close[]of start candle. minval=11, maxval=60")
  6. supp_high_slice = input.int(30, "supp high slice",minval=11, maxval=60, tooltip="Aim for the highest red-to-green volume ratio by setting the maximum number of slices from Lowest momentum price to close[]of start candle. minval=11, maxval=60")
  7. var nvl = matrix.new<float>(2,8,0) //Best layers storage table
  8. var nvl_s = matrix.new<string>(2,2,na)
  9. var L = array.new_label(loockback,na)
  10. rl = 0 //row of labels table
  11.  
  12. start = from
  13. end = from + loockback
  14. var float hst = na
  15. var int hstcount = na
  16. var float lst = na
  17. var int lstcount = na
  18. float mhigh = na //The top border of the layer
  19. float mlow = na //The bottom border of the layer
  20. float green_vol = 0.0
  21. float red_vol = 0.0
  22. int place = na //place == -1 for supports and +1 for resistances
  23. var float pr = na //layer height
  24. bool savedata = false
  25. int green_candles = 0 //Candle counter
  26. int red_candles = 0
  27. if barstate.islast
  28. max_bars_back(time,500)
  29. matrix.fill(nvl, 0, 0, 2, 0, 7)
  30. matrix.fill(nvl_s, na, 0, 2, 0, 2)
  31. for j = 0 to loockback-1
  32. lb = array.get(L, j)
  33. label.delete(lb)
  34. hst := na
  35. lst := na
  36. hstcount := na
  37. lstcount := na
  38. for i = start to end
  39. if close[i]>open[i] and ((high[i]-close[i])+(open[i]-low[i]))<(close[i]-open[i]) //Green momentum candle
  40. if na(hst) or high[i]>hst
  41. hst := high[i]
  42. hstcount := i
  43. if close[i]<open[i] and ((high[i]-open[i])+(close[i]-low[i]))<(open[i]-close[i]) //Red momentum candle
  44. if na(lst) or low[i]<lst
  45. lst := low[i]
  46. lstcount := i
  47. array.set(L,rl,label.new(bar_index-hstcount, high[hstcount], "Highest momentum price", color = color.new(color.red,60), textalign = text.align_center,style = label.style_label_lower_right))
  48. rl += 1
  49. array.set(L,rl,label.new(bar_index-lstcount, low[lstcount], "Lowest momentum price", color = color.new(color.green,60), textalign = text.align_center,style = label.style_label_upper_right))
  50. rl += 1
  51. //CALCULATING VOLUME IN LAYERS
  52. for k = resist_high_slice to 1
  53. if close[from]>=low[hstcount]
  54. pr := (hst - hlc3[hstcount]) / k
  55. else
  56. pr := (hst - close[from]) / k
  57. place := 1
  58. for j = 0 to k-1 //Resistance
  59. mhigh := hst - (j*pr)
  60. mlow := hst - ((j+1)*pr)
  61. green_vol := 0.0
  62. red_vol := 0.0
  63. savedata := false
  64. green_candles := 0
  65. red_candles := 0
  66. for i = start to end //candles
  67. if high[i]<=mhigh and low[i]>=mlow //1
  68. if close[i]>open[i]
  69. green_vol += volume[i]
  70. green_candles += 1
  71. else
  72. red_vol += volume[i]
  73. red_candles += 1
  74. if high[i]<=mhigh and high[i]>mlow and low[i]<mlow //2
  75. if close[i]>open[i]
  76. green_vol += (math.abs(high[i]-mlow) / math.abs(high[i]-low[i]))*volume[i]
  77. green_candles += 1
  78. else
  79. red_vol += (math.abs(high[i]-mlow) / math.abs(high[i]-low[i]))*volume[i]
  80. red_candles += 1
  81. if high[i]>mhigh and low[i]<mhigh and low[i]>=mlow //3
  82. if close[i]>open[i]
  83. green_vol += (math.abs(mhigh-low[i]) / math.abs(high[i]-low[i]))*volume[i]
  84. green_candles += 1
  85. else
  86. red_vol += (math.abs(mhigh-low[i]) / math.abs(high[i]-low[i]))*volume[i]
  87. red_candles += 1
  88. if high[i]>mhigh and low[i]<mlow //4
  89. if close[i]>open[i]
  90. green_vol += (math.abs(mhigh-mlow) / math.abs(high[i]-low[i]))*volume[i]
  91. green_candles += 1
  92. else
  93. red_vol += (math.abs(mhigh-mlow) / math.abs(high[i]-low[i]))*volume[i]
  94. red_candles += 1
  95. if green_vol>0 and red_vol==0
  96. if matrix.get(nvl, 0, 0)==0
  97. savedata := true
  98. if k<matrix.get(nvl, 0, 0)
  99. savedata := true
  100. if green_vol>0 and red_vol>0
  101. if matrix.get(nvl, 0, 0)==0
  102. savedata := true
  103. if ((green_vol/red_vol)>(matrix.get(nvl, 0, 3)/matrix.get(nvl, 0, 4))) and green_candles>=matrix.get(nvl, 0, 6) and red_candles<=matrix.get(nvl, 0, 7)
  104. savedata := true
  105. if (green_vol/red_vol)==(matrix.get(nvl, 0, 3)/matrix.get(nvl, 0, 4)) and pr>matrix.get(nvl, 0, 5)
  106. savedata := true
  107. if savedata==true
  108. matrix.set(nvl, 0, 0, k)
  109. matrix.set(nvl, 0, 1, mlow)
  110. matrix.set(nvl, 0, 2, mhigh)
  111. matrix.set(nvl, 0, 3, green_vol)
  112. matrix.set(nvl, 0, 4, red_vol)
  113. matrix.set(nvl, 0, 5, pr)
  114. matrix.set(nvl, 0, 6, green_candles)
  115. matrix.set(nvl, 0, 7, red_candles)
  116. for k = supp_high_slice to 1
  117. if close[from]<=high[lstcount]
  118. pr := (hlc3[lstcount] - lst) / k
  119. else
  120. pr := (close[from] - lst) / k
  121. place := -1
  122. for j = 0 to k-1 //Resistance
  123. mhigh := lst + ((j+1)*pr)
  124. mlow := lst + (j*pr)
  125. green_vol := 0.0
  126. red_vol := 0.0
  127. savedata := false
  128. green_candles := 0
  129. red_candles := 0
  130. for i = start to end //candles
  131. if high[i]<=mhigh and low[i]>=mlow //1
  132. if close[i]>open[i]
  133. green_vol += volume[i]
  134. green_candles += 1
  135. else
  136. red_vol += volume[i]
  137. red_candles += 1
  138. if high[i]<=mhigh and high[i]>mlow and low[i]<mlow //2
  139. if close[i]>open[i]
  140. green_vol += (math.abs(high[i]-mlow) / math.abs(high[i]-low[i]))*volume[i]
  141. green_candles += 1
  142. else
  143. red_vol += (math.abs(high[i]-mlow) / math.abs(high[i]-low[i]))*volume[i]
  144. red_candles += 1
  145. if high[i]>mhigh and low[i]<mhigh and low[i]>=mlow //3
  146. if close[i]>open[i]
  147. green_vol += (math.abs(mhigh-low[i]) / math.abs(high[i]-low[i]))*volume[i]
  148. green_candles += 1
  149. else
  150. red_vol += (math.abs(mhigh-low[i]) / math.abs(high[i]-low[i]))*volume[i]
  151. red_candles += 1
  152. if high[i]>mhigh and low[i]<mlow //4
  153. if close[i]>open[i]
  154. green_vol += (math.abs(mhigh-mlow) / math.abs(high[i]-low[i]))*volume[i]
  155. green_candles += 1
  156. else
  157. red_vol += (math.abs(mhigh-mlow) / math.abs(high[i]-low[i]))*volume[i]
  158. red_candles += 1
  159. if red_vol>0 and green_vol==0
  160. if matrix.get(nvl, 1, 0)==0
  161. savedata := true
  162. if k<matrix.get(nvl, 1, 0)
  163. savedata := true
  164. if red_vol>0 and green_vol>0
  165. if matrix.get(nvl, 1, 0)==0
  166. savedata := true
  167. if (red_vol/green_vol)>(matrix.get(nvl, 1, 4)/matrix.get(nvl, 1, 3)) and green_candles<=matrix.get(nvl, 1, 6) and red_candles>=matrix.get(nvl, 1, 7)
  168. savedata := true
  169. if (red_vol/green_vol)==(matrix.get(nvl, 1, 4)/matrix.get(nvl, 1, 3)) and pr>matrix.get(nvl, 1, 5)
  170. savedata := true
  171. if savedata==true
  172. matrix.set(nvl, 1, 0, k)
  173. matrix.set(nvl, 1, 1, mlow)
  174. matrix.set(nvl, 1, 2, mhigh)
  175. matrix.set(nvl, 1, 3, green_vol)
  176. matrix.set(nvl, 1, 4, red_vol)
  177. matrix.set(nvl, 1, 5, pr)
  178. matrix.set(nvl, 1, 6, green_candles)
  179. matrix.set(nvl, 1, 7, red_candles)
  180. //PLOTING LINE AND BOXES
  181. var line baseLine = na
  182. if na(baseLine)
  183. baseLine:= line.new(bar_index - lstcount, lst, bar_index - hstcount, hst, width=1)
  184. else
  185. line.set_xy1(baseLine, bar_index - lstcount, lst)
  186. line.set_xy2(baseLine, bar_index - hstcount, hst)
  187. var box resist_box = na
  188. if na(resist_box)
  189. resist_box := box.new(bar_index-from-loockback, matrix.get(nvl,0,2), bar_index-from, matrix.get(nvl,0,1),bgcolor=color.new(color.red,60), border_color=color.new(color.red,100))
  190. else
  191. box.set_lefttop(resist_box, bar_index-from-loockback, matrix.get(nvl,0,2))
  192. box.set_rightbottom(resist_box, bar_index-from ,matrix.get(nvl,0,1))
  193. var box supp_box = na
  194. if na(supp_box)
  195. supp_box := box.new(bar_index-from-loockback, matrix.get(nvl,1,2), bar_index-from, matrix.get(nvl,1,1),bgcolor=color.new(color.green,60), border_color=color.new(color.green,100))
  196. else
  197. box.set_lefttop(supp_box, bar_index-from-loockback, matrix.get(nvl,1,2))
  198. box.set_rightbottom(supp_box, bar_index-from ,matrix.get(nvl,1,1))
  199. //FORMATING VOLUMES
  200. if barstate.islast
  201. for j = 0 to 1
  202. string formatPrecision = "#" + "0.000000"
  203. for i = 3 to 4 //The density of green and red volumes are stored in indexes 3 and 4 of the table, respectively.
  204. float gvol_digitsAmt = math.log10(math.abs(matrix.get(nvl,j,i)))
  205. string sgvd = if gvol_digitsAmt > 12
  206. str.tostring(matrix.get(nvl,j,i) / 1e12, formatPrecision + " T")
  207. else if gvol_digitsAmt > 9
  208. str.tostring(matrix.get(nvl,j,i) / 1e9, formatPrecision + " B")
  209. else if gvol_digitsAmt > 6
  210. str.tostring(matrix.get(nvl,j,i) / 1e6, formatPrecision + " M")
  211. else if gvol_digitsAmt > 3
  212. str.tostring(matrix.get(nvl,j,i) / 1e3, formatPrecision + " K")
  213. else
  214. str.tostring(matrix.get(nvl,j,i), "#" + formatPrecision)
  215. matrix.set(nvl_s, j, i-3, sgvd)
  216. //PLOTING LABELS
  217. string strLL = na
  218. strLL := str.format("cutting = {0,number,#} Layers",matrix.get(nvl,0,0))
  219. strLL := strLL + str.format("\nlayer height = {0,number,#.########}",matrix.get(nvl,0,5))
  220. strLL := strLL + str.format("\n{0,number,#.#########}",matrix.get(nvl,0,1))
  221. strLL := strLL + str.format(" >>> {0,number,#.########}",matrix.get(nvl,0,2))
  222. strLL := strLL +"\ngreen_vol = "+str.tostring(matrix.get(nvl_s,0,0))
  223. strLL := strLL +"\nred_vol = "+str.tostring(matrix.get(nvl_s,0,1))
  224. if matrix.get(nvl,0,4)>0
  225. strLL := strLL +"\n(green_vol / red_vol) = "+str.tostring((matrix.get(nvl,0,3)/matrix.get(nvl,0,4)))
  226. else if matrix.get(nvl,0,4)==0
  227. strLL := strLL + "\n(green_vol / red_vol) = Unlimited"
  228. array.set(L,rl,label.new(bar_index-from, matrix.get(nvl,0,2), strLL, textalign = text.align_center,style = label.style_label_lower_left,color =color.new(color.red,80)))
  229. rl += 1
  230. strLL := str.format("cutting = {0,number,#} Layers",matrix.get(nvl,1,0))
  231. strLL := strLL + str.format("\nlayer height = {0,number,#.########}",matrix.get(nvl,1,5))
  232. strLL := strLL + str.format("\n{0,number,#.#########}",matrix.get(nvl,1,1))
  233. strLL := strLL + str.format(" >>> {0,number,#.########}",matrix.get(nvl,1,2))
  234. strLL := strLL +"\ngreen_vol = "+str.tostring(matrix.get(nvl_s,1,0))
  235. strLL := strLL +"\nred_vol = "+str.tostring(matrix.get(nvl_s,1,1))
  236. if matrix.get(nvl,1,3)>0
  237. strLL := strLL +"\n(red_vol / green_vol) = "+str.tostring((matrix.get(nvl,1,4)/matrix.get(nvl,1,3)))
  238. else
  239. strLL := strLL + "\n(red_vol / green_vol) = Unlimited"
  240. array.set(L,rl,label.new(bar_index-from, matrix.get(nvl,1,1), strLL, textalign = text.align_center,style = label.style_label_upper_left,color =color.new(color.green,80)))
  241. rl += 1
Advertisement
Add Comment
Please, Sign In to add comment