Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- indicator('Volume Support and Resistance', shorttitle='VSR', overlay=true, max_bars_back=500)
- from = input.int(0, minval=0, maxval=400, step=1, tooltip = "start candle, maxval=400")
- 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")
- 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")
- 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")
- var nvl = matrix.new<float>(2,8,0) //Best layers storage table
- var nvl_s = matrix.new<string>(2,2,na)
- var L = array.new_label(loockback,na)
- rl = 0 //row of labels table
- start = from
- end = from + loockback
- var float hst = na
- var int hstcount = na
- var float lst = na
- var int lstcount = na
- float mhigh = na //The top border of the layer
- float mlow = na //The bottom border of the layer
- float green_vol = 0.0
- float red_vol = 0.0
- int place = na //place == -1 for supports and +1 for resistances
- var float pr = na //layer height
- bool savedata = false
- int green_candles = 0 //Candle counter
- int red_candles = 0
- if barstate.islast
- max_bars_back(time,500)
- matrix.fill(nvl, 0, 0, 2, 0, 7)
- matrix.fill(nvl_s, na, 0, 2, 0, 2)
- for j = 0 to loockback-1
- lb = array.get(L, j)
- label.delete(lb)
- hst := na
- lst := na
- hstcount := na
- lstcount := na
- for i = start to end
- if close[i]>open[i] and ((high[i]-close[i])+(open[i]-low[i]))<(close[i]-open[i]) //Green momentum candle
- if na(hst) or high[i]>hst
- hst := high[i]
- hstcount := i
- if close[i]<open[i] and ((high[i]-open[i])+(close[i]-low[i]))<(open[i]-close[i]) //Red momentum candle
- if na(lst) or low[i]<lst
- lst := low[i]
- lstcount := i
- 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))
- rl += 1
- 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))
- rl += 1
- //CALCULATING VOLUME IN LAYERS
- for k = resist_high_slice to 1
- if close[from]>=low[hstcount]
- pr := (hst - hlc3[hstcount]) / k
- else
- pr := (hst - close[from]) / k
- place := 1
- for j = 0 to k-1 //Resistance
- mhigh := hst - (j*pr)
- mlow := hst - ((j+1)*pr)
- green_vol := 0.0
- red_vol := 0.0
- savedata := false
- green_candles := 0
- red_candles := 0
- for i = start to end //candles
- if high[i]<=mhigh and low[i]>=mlow //1
- if close[i]>open[i]
- green_vol += volume[i]
- green_candles += 1
- else
- red_vol += volume[i]
- red_candles += 1
- if high[i]<=mhigh and high[i]>mlow and low[i]<mlow //2
- if close[i]>open[i]
- green_vol += (math.abs(high[i]-mlow) / math.abs(high[i]-low[i]))*volume[i]
- green_candles += 1
- else
- red_vol += (math.abs(high[i]-mlow) / math.abs(high[i]-low[i]))*volume[i]
- red_candles += 1
- if high[i]>mhigh and low[i]<mhigh and low[i]>=mlow //3
- if close[i]>open[i]
- green_vol += (math.abs(mhigh-low[i]) / math.abs(high[i]-low[i]))*volume[i]
- green_candles += 1
- else
- red_vol += (math.abs(mhigh-low[i]) / math.abs(high[i]-low[i]))*volume[i]
- red_candles += 1
- if high[i]>mhigh and low[i]<mlow //4
- if close[i]>open[i]
- green_vol += (math.abs(mhigh-mlow) / math.abs(high[i]-low[i]))*volume[i]
- green_candles += 1
- else
- red_vol += (math.abs(mhigh-mlow) / math.abs(high[i]-low[i]))*volume[i]
- red_candles += 1
- if green_vol>0 and red_vol==0
- if matrix.get(nvl, 0, 0)==0
- savedata := true
- if k<matrix.get(nvl, 0, 0)
- savedata := true
- if green_vol>0 and red_vol>0
- if matrix.get(nvl, 0, 0)==0
- savedata := true
- 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)
- savedata := true
- if (green_vol/red_vol)==(matrix.get(nvl, 0, 3)/matrix.get(nvl, 0, 4)) and pr>matrix.get(nvl, 0, 5)
- savedata := true
- if savedata==true
- matrix.set(nvl, 0, 0, k)
- matrix.set(nvl, 0, 1, mlow)
- matrix.set(nvl, 0, 2, mhigh)
- matrix.set(nvl, 0, 3, green_vol)
- matrix.set(nvl, 0, 4, red_vol)
- matrix.set(nvl, 0, 5, pr)
- matrix.set(nvl, 0, 6, green_candles)
- matrix.set(nvl, 0, 7, red_candles)
- for k = supp_high_slice to 1
- if close[from]<=high[lstcount]
- pr := (hlc3[lstcount] - lst) / k
- else
- pr := (close[from] - lst) / k
- place := -1
- for j = 0 to k-1 //Resistance
- mhigh := lst + ((j+1)*pr)
- mlow := lst + (j*pr)
- green_vol := 0.0
- red_vol := 0.0
- savedata := false
- green_candles := 0
- red_candles := 0
- for i = start to end //candles
- if high[i]<=mhigh and low[i]>=mlow //1
- if close[i]>open[i]
- green_vol += volume[i]
- green_candles += 1
- else
- red_vol += volume[i]
- red_candles += 1
- if high[i]<=mhigh and high[i]>mlow and low[i]<mlow //2
- if close[i]>open[i]
- green_vol += (math.abs(high[i]-mlow) / math.abs(high[i]-low[i]))*volume[i]
- green_candles += 1
- else
- red_vol += (math.abs(high[i]-mlow) / math.abs(high[i]-low[i]))*volume[i]
- red_candles += 1
- if high[i]>mhigh and low[i]<mhigh and low[i]>=mlow //3
- if close[i]>open[i]
- green_vol += (math.abs(mhigh-low[i]) / math.abs(high[i]-low[i]))*volume[i]
- green_candles += 1
- else
- red_vol += (math.abs(mhigh-low[i]) / math.abs(high[i]-low[i]))*volume[i]
- red_candles += 1
- if high[i]>mhigh and low[i]<mlow //4
- if close[i]>open[i]
- green_vol += (math.abs(mhigh-mlow) / math.abs(high[i]-low[i]))*volume[i]
- green_candles += 1
- else
- red_vol += (math.abs(mhigh-mlow) / math.abs(high[i]-low[i]))*volume[i]
- red_candles += 1
- if red_vol>0 and green_vol==0
- if matrix.get(nvl, 1, 0)==0
- savedata := true
- if k<matrix.get(nvl, 1, 0)
- savedata := true
- if red_vol>0 and green_vol>0
- if matrix.get(nvl, 1, 0)==0
- savedata := true
- 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)
- savedata := true
- if (red_vol/green_vol)==(matrix.get(nvl, 1, 4)/matrix.get(nvl, 1, 3)) and pr>matrix.get(nvl, 1, 5)
- savedata := true
- if savedata==true
- matrix.set(nvl, 1, 0, k)
- matrix.set(nvl, 1, 1, mlow)
- matrix.set(nvl, 1, 2, mhigh)
- matrix.set(nvl, 1, 3, green_vol)
- matrix.set(nvl, 1, 4, red_vol)
- matrix.set(nvl, 1, 5, pr)
- matrix.set(nvl, 1, 6, green_candles)
- matrix.set(nvl, 1, 7, red_candles)
- //PLOTING LINE AND BOXES
- var line baseLine = na
- if na(baseLine)
- baseLine:= line.new(bar_index - lstcount, lst, bar_index - hstcount, hst, width=1)
- else
- line.set_xy1(baseLine, bar_index - lstcount, lst)
- line.set_xy2(baseLine, bar_index - hstcount, hst)
- var box resist_box = na
- if na(resist_box)
- 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))
- else
- box.set_lefttop(resist_box, bar_index-from-loockback, matrix.get(nvl,0,2))
- box.set_rightbottom(resist_box, bar_index-from ,matrix.get(nvl,0,1))
- var box supp_box = na
- if na(supp_box)
- 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))
- else
- box.set_lefttop(supp_box, bar_index-from-loockback, matrix.get(nvl,1,2))
- box.set_rightbottom(supp_box, bar_index-from ,matrix.get(nvl,1,1))
- //FORMATING VOLUMES
- if barstate.islast
- for j = 0 to 1
- string formatPrecision = "#" + "0.000000"
- for i = 3 to 4 //The density of green and red volumes are stored in indexes 3 and 4 of the table, respectively.
- float gvol_digitsAmt = math.log10(math.abs(matrix.get(nvl,j,i)))
- string sgvd = if gvol_digitsAmt > 12
- str.tostring(matrix.get(nvl,j,i) / 1e12, formatPrecision + " T")
- else if gvol_digitsAmt > 9
- str.tostring(matrix.get(nvl,j,i) / 1e9, formatPrecision + " B")
- else if gvol_digitsAmt > 6
- str.tostring(matrix.get(nvl,j,i) / 1e6, formatPrecision + " M")
- else if gvol_digitsAmt > 3
- str.tostring(matrix.get(nvl,j,i) / 1e3, formatPrecision + " K")
- else
- str.tostring(matrix.get(nvl,j,i), "#" + formatPrecision)
- matrix.set(nvl_s, j, i-3, sgvd)
- //PLOTING LABELS
- string strLL = na
- strLL := str.format("cutting = {0,number,#} Layers",matrix.get(nvl,0,0))
- strLL := strLL + str.format("\nlayer height = {0,number,#.########}",matrix.get(nvl,0,5))
- strLL := strLL + str.format("\n{0,number,#.#########}",matrix.get(nvl,0,1))
- strLL := strLL + str.format(" >>> {0,number,#.########}",matrix.get(nvl,0,2))
- strLL := strLL +"\ngreen_vol = "+str.tostring(matrix.get(nvl_s,0,0))
- strLL := strLL +"\nred_vol = "+str.tostring(matrix.get(nvl_s,0,1))
- if matrix.get(nvl,0,4)>0
- strLL := strLL +"\n(green_vol / red_vol) = "+str.tostring((matrix.get(nvl,0,3)/matrix.get(nvl,0,4)))
- else if matrix.get(nvl,0,4)==0
- strLL := strLL + "\n(green_vol / red_vol) = Unlimited"
- 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)))
- rl += 1
- strLL := str.format("cutting = {0,number,#} Layers",matrix.get(nvl,1,0))
- strLL := strLL + str.format("\nlayer height = {0,number,#.########}",matrix.get(nvl,1,5))
- strLL := strLL + str.format("\n{0,number,#.#########}",matrix.get(nvl,1,1))
- strLL := strLL + str.format(" >>> {0,number,#.########}",matrix.get(nvl,1,2))
- strLL := strLL +"\ngreen_vol = "+str.tostring(matrix.get(nvl_s,1,0))
- strLL := strLL +"\nred_vol = "+str.tostring(matrix.get(nvl_s,1,1))
- if matrix.get(nvl,1,3)>0
- strLL := strLL +"\n(red_vol / green_vol) = "+str.tostring((matrix.get(nvl,1,4)/matrix.get(nvl,1,3)))
- else
- strLL := strLL + "\n(red_vol / green_vol) = Unlimited"
- 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)))
- rl += 1
Advertisement
Add Comment
Please, Sign In to add comment