Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // WARNING THIS ARE A MIX OF INDICATORS AND I DO NOT OWN ANY OF IT.
- // THANKS TO: HEAVY91,LONSOMETHEBLUE,JULIANZACO
- //@version=4
- study("BUY-SELL ZONES", "BUY-SELL ZONES", overlay = true, max_bars_back = 501)
- var bool is_first_candle= true
- var diff = 0.0
- float maxV = 0.0
- int maxV_index = 0
- int last_time = 0
- margin=0.0
- ys=0.0
- int bar_offset=0
- var is_first_inRange=false
- period_time = input(defval = 10,
- title = "Number of days (Min: 3, Max: 500)",
- type = input.integer,
- minval = 3,
- maxval = 500)
- resolution = input(defval = 500,
- title = "calculation resolution (Min: 400, Max: 700)",
- type = input.integer,
- minval = 400,
- maxval = 700)
- show_from = input(defval = 3,
- title = "show support/resistance since (Min: 2, Max: 45)",
- type = input.integer,
- minval = 2,
- maxval = 45)
- show_volume_bars = input(defval = true,
- title = "Show volume bars (If selected, maximum days are 17)",
- type = input.bool
- )
- show_vpoc_line = input(defval = true,
- title = "Show VPOC line",
- type = input.bool
- )
- show_support_line = input(defval = true,
- title = "Show support line",
- type = input.bool
- )
- show_res_line = input(defval = true,
- title = "Show resistance line",
- type = input.bool
- )
- show_H_line = input(defval = true,
- title = "Show high line",
- type = input.bool
- )
- show_L_line = input(defval = true,
- title = "Show low line",
- type = input.bool
- )
- show_labels = input(defval = true,
- title = "Show from days label",
- type = input.bool
- )
- vpoc_color = input(defval = color.new(color.yellow, 10),
- title = "VPOC color",
- type = input.color
- )
- high_low_color = input(defval = color.new(color.blue, 50),
- title = "High-low lines color",
- type = input.color
- )
- volume_bars_color = input(defval = color.new(color.green, 70),
- title = "Volume bars color",
- type = input.color
- )
- sup_color = input(defval = color.new(color.red, 10),
- title = "Support line color",
- type = input.color
- )
- res_color = input(defval = color.new(color.green, 10),
- title = "Resistance line color",
- type = input.color
- )
- range_prices = array.new_float((resolution + 1), 0.0)
- partial_vol = array.new_float(resolution, 0.0)
- total_vol = array.new_float(resolution, 0.0)
- var all_ys = array.new_float(show_from, 0.0)
- var ys_time = array.new_float(show_from, 0.0)
- var is_support = array.new_bool(show_from, true)
- t = time("1440", session.regular)
- var int startTime =time
- int ncandles =0
- if barstate.isfirst
- startTime := timenow - period_time*86400000
- bar_offset:= barssince(is_first_inRange)
- is_first = na(t[1]) and not na(t) or t[1] < t
- is_first_inRange:=is_first and t>startTime
- high_price=0.0
- low_price=0.0
- price_level=0.0
- if is_first_inRange or barstate.islast
- last_time:= t[1]
- ncandles := bar_offset
- current_low_price=0.0
- for i = 1 to (ncandles)
- current_low_price:=low[i]
- if current_low_price < low_price or low_price==0.0
- low_price:=current_low_price
- current_high_price=0.0
- for i = 1 to (ncandles)
- current_high_price:=high[i]
- if current_high_price >high_price or high_price==0.0
- high_price:=current_high_price
- diff := (high_price-low_price)/resolution
- for j = 0 to (resolution)
- array.set(range_prices, (j), (low_price + (diff * (j+1))))
- for i = 0 to (ncandles)
- int w_candle = 0
- array.fill(partial_vol, 0.0)
- for j=0 to (resolution-1)
- float current_price = array.get(range_prices, j)
- if high[i] > current_price and low[i] < current_price
- float j_partial_vol = array.get(partial_vol, j)
- float sum_vol = j_partial_vol + nz(volume[i])
- array.set(partial_vol, j, sum_vol)
- w_candle := w_candle + 1
- for j = 0 to (resolution - 1)
- float j_total_vol = array.get(total_vol, j)
- float j_partial_vol = array.get(partial_vol, j)
- float sum_partial_total=0.0
- if w_candle > 0
- sum_partial_total := j_total_vol + j_partial_vol/w_candle
- else
- sum_partial_total := j_total_vol
- array.set(total_vol, j, sum_partial_total)
- maxV := array.max(total_vol)
- maxV_index := array.indexof(total_vol, maxV)
- if is_first_inRange
- price_level:=diff*maxV_index+low_price
- if high_price - price_level > price_level - low_price
- margin:=high_price - price_level
- ys:=price_level - margin
- else
- margin:=price_level - low_price
- ys:=price_level + margin
- if ys > high_price
- array.shift(is_support)
- array.push(is_support, false)
- else
- array.shift(is_support)
- array.push(is_support, true)
- array.shift(all_ys)
- array.push(all_ys, ys)
- array.shift(ys_time)
- array.push(ys_time, t)
- if barstate.islast
- for i=0 to (show_from - 1)
- ys := array.get(all_ys, i)
- time_back = array.get(ys_time, i)
- if array.get(is_support, i)
- if show_support_line
- 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)
- else
- if show_res_line
- 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)
- if show_labels
- label.new(int(time + 86400000), ys, text ="From days: " + tostring(show_from - i), xloc=xloc.bar_time)
- dif_time=0.0
- if is_first_inRange or barstate.islast
- last_time:= int(t[1])
- dif_time:= (time-last_time)
- if dif_time/86400000 > 1.5
- dif_time:=(dif_time)/9
- else
- dif_time:=(dif_time)/3
- diff_t=0
- x1=0.0
- norm_vol=0.0
- proportion=resolution/40
- for i = 0 to (resolution - 1)
- y=array.get(range_prices,i)
- current_vol= array.get(total_vol,i)
- norm_vol :=current_vol/maxV
- x1 :=dif_time*norm_vol
- diff_t := last_time + int(x1)
- if (i % int(proportion) == 0 or int(proportion) == 0) and show_volume_bars
- 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)
- if show_L_line
- line.new(x1=t[1], y1=low_price, x2=time, y2=low_price,xloc= xloc.bar_time, color=high_low_color)
- if show_vpoc_line
- line.new(x1=t[1], y1=diff*maxV_index+low_price, x2=time, y2=diff*maxV_index+low_price, color=vpoc_color, width=2,xloc= xloc.bar_time)
- if show_H_line
- line.new(x1=t[1], y1=high_price, x2=time, y2=high_price,xloc= xloc.bar_time, color=high_low_color)
- prd = input(defval = 10, title="Pivot Period", minval = 4, maxval = 30, group = "Settings", tooltip="Used while calculating Pivot Points, checks left&right bars")
- ppsrc = input(defval = 'High/Low', title="Source", options = ['High/Low', 'Close/Open'], group = "Settings ", tooltip="Source for Pivot Points")
- ChannelW = input(defval = 5, title = "Maximum Channel Width %", minval = 1, maxval = 8, group = "Settings ", tooltip="Calculated using Highest/Lowest levels in 300 bars")
- minstrength = input(defval = 1, title = "Minimum Strength", minval = 1, group = "Settings ", tooltip = "Channel must contain at least 2 Pivot Points")
- maxnumsr = input(defval = 6, title = "Maximum Number of S/R", minval = 1, maxval = 10, group = "Settings ", tooltip = "Maximum number of Support/Resistance Channels to Show") - 1
- loopback = input(defval = 290, title = "Loopback Period", minval = 100, maxval = 400, group = "Settings ", tooltip="While calculating S/R levels it checks Pivots in Loopback Period")
- res_col = input(defval = color.new(color.red, 75), title = "Resistance Color", group = "Colors")
- sup_col = input(defval = color.new(color.lime, 75), title = "Support Color", group = "Colors ")
- inch_col = input(defval = color.new(color.gray, 75), title = "Color When Price in Channel", group = "Colors")
- showpp = input(defval = false, title = "Show Pivot Points", group = "Extras ⏶⏷")
- showsrbroken = input(defval = false, title = "Show Broken Support/Resistance", group = "Extras ⏶⏷")
- showthema1en = input(defval = false, title = "MA 1", inline = "ma1")
- showthema1len = input(defval = 50, title = "", inline = "ma1")
- showthema1type = input(defval = "SMA", title = "", options = ["SMA", "EMA"], inline = "ma1")
- showthema2en = input(defval = false, title = "MA 2", inline = "ma2")
- showthema2len = input(defval = 200, title = "", inline = "ma2")
- showthema2type = input(defval = "SMA", title = "", options = ["SMA", "EMA"], inline = "ma2")
- ma1 = showthema1en ? (showthema1type == "SMA" ? sma(close, showthema1len) : ema(close, showthema1len)) : na
- ma2 = showthema2en ? (showthema2type == "SMA" ? sma(close, showthema2len) : ema(close, showthema2len)) : na
- plot(ma1, color = not na(ma1) ? color.blue : na)
- plot(ma2, color = not na(ma2) ? color.red : na)
- // get Pivot High/low
- float src1 = ppsrc == 'High/Low' ? high : max(close, open)
- float src2 = ppsrc == 'High/Low' ? low: min(close, open)
- float ph = pivothigh(src1, prd, prd)
- float pl = pivotlow(src2, prd, prd)
- // draw Pivot points
- plotshape(ph and showpp, text = "H", style = shape.labeldown, color = na, textcolor = color.red, location = location.abovebar, offset = -prd)
- plotshape(pl and showpp, text = "L", style = shape.labelup, color = na, textcolor = color.lime, location = location.belowbar, offset = -prd)
- //calculate maximum S/R channel width
- prdhighest = highest(300)
- prdlowest = lowest(300)
- cwidth = (prdhighest - prdlowest) * ChannelW / 100
- // get/keep Pivot levels
- var pivotvals= array.new_float(0)
- var pivotlocs= array.new_float(0)
- if ph or pl
- array.unshift(pivotvals, ph ? ph : pl)
- array.unshift(pivotlocs, bar_index)
- for x = array.size(pivotvals) - 1 to 0
- if bar_index - array.get(pivotlocs, x) > loopback // remove old pivot points
- array.pop(pivotvals)
- array.pop(pivotlocs)
- continue
- break
- //find/create SR channel of a pivot point
- get_sr_vals(ind)=>
- float lo = array.get(pivotvals, ind)
- float hi = lo
- int numpp = 0
- for y = 0 to array.size(pivotvals) - 1
- float cpp = array.get(pivotvals, y)
- float wdth = cpp <= hi ? hi - cpp : cpp - lo
- if wdth <= cwidth // fits the max channel width?
- if cpp <= hi
- lo := min(lo, cpp)
- else
- hi := max(hi, cpp)
- numpp := numpp + 20 // each pivot point added as 20
- [hi, lo, numpp]
- // keep old SR channels and calculate/sort new channels if we met new pivot point
- var suportresistance = array.new_float(20, 0) // min/max levels
- changeit(x, y)=>
- tmp = array.get(suportresistance, y * 2)
- array.set(suportresistance, y * 2, array.get(suportresistance, x * 2))
- array.set(suportresistance, x * 2, tmp)
- tmp := array.get(suportresistance, y * 2 + 1)
- array.set(suportresistance, y * 2 + 1, array.get(suportresistance, x * 2 + 1))
- array.set(suportresistance, x * 2 + 1, tmp)
- if ph or pl
- supres = array.new_float(0) // number of pivot, strength, min/max levels
- stren = array.new_float(10, 0)
- // get levels and strengs
- for x = 0 to array.size(pivotvals) - 1
- [hi, lo, strength] = get_sr_vals(x)
- array.push(supres, strength)
- array.push(supres, hi)
- array.push(supres, lo)
- // add each HL to strengh
- for x = 0 to array.size(pivotvals) - 1
- h = array.get(supres, x * 3 + 1)
- l = array.get(supres, x * 3 + 2)
- s = 0
- for y = 0 to loopback
- if (high[y] <= h and high[y] >= l) or
- (low[y] <= h and low[y] >= l)
- s := s + 1
- array.set(supres, x * 3, array.get(supres, x * 3) + s)
- //reset SR levels
- array.fill(suportresistance, 0)
- // get strongest SRs
- src = 0
- for x = 0 to array.size(pivotvals) - 1
- stv = -1. // value
- stl = -1 // location
- for y = 0 to array.size(pivotvals) - 1
- if array.get(supres, y * 3) > stv and array.get(supres, y * 3) >= minstrength * 20
- stv := array.get(supres, y * 3)
- stl := y
- if stl >= 0
- //get sr level
- hh = array.get(supres, stl * 3 + 1)
- ll = array.get(supres, stl * 3 + 2)
- array.set(suportresistance, src * 2, hh)
- array.set(suportresistance, src * 2 + 1, ll)
- array.set(stren, src, array.get(supres, stl * 3))
- // make included pivot points' strength zero
- for y = 0 to array.size(pivotvals) - 1
- if (array.get(supres, y * 3 + 1) <= hh and array.get(supres, y * 3 + 1) >= ll) or
- (array.get(supres, y * 3 + 2) <= hh and array.get(supres, y * 3 + 2) >= ll)
- array.set(supres, y * 3, -1)
- src += 1
- if src >= 10
- break
- for x = 0 to 8
- for y = x + 1 to 9
- if array.get(stren, y) > array.get(stren, x)
- tmp = array.get(stren, y)
- array.set(stren, y, array.get(stren, x))
- changeit(x, y)
- get_level(ind)=>
- float ret = na
- if ind < array.size(suportresistance)
- if array.get(suportresistance, ind) != 0
- ret := array.get(suportresistance, ind)
- ret
- get_color(ind)=>
- color ret = na
- if ind < array.size(suportresistance)
- if array.get(suportresistance, ind) != 0
- ret := array.get(suportresistance, ind) > close and array.get(suportresistance, ind + 1) > close ? res_col :
- array.get(suportresistance, ind) < close and array.get(suportresistance, ind + 1) < close ? sup_col :
- inch_col
- ret
- var srchannels = array.new_box(10)
- for x = 0 to min(9, maxnumsr)
- box.delete(array.get(srchannels, x))
- srcol = get_color(x * 2)
- if not na(srcol)
- array.set(srchannels, x,
- box.new(left = bar_index, top = get_level(x * 2), right = bar_index + 1, bottom = get_level(x * 2 + 1),
- border_color = srcol,
- border_width = 1,
- extend = extend.both,
- bgcolor = srcol))
- resistancebroken = false
- supportbroken = false
- lengthc = input(20, "Upper Length", inline = "length")
- length2 = input(20, "Lower Length", inline = "length")
- astartc = input(2,"Draw Upper Line From Pivot #", inline = "pivot")
- aend = input(0,"To Pivot #", inline = "pivot")
- bstart = input(3,"Draw Lower Line From Pivot #", inline = "pivot2")
- bend = input(0,"To Pivot #", inline = "pivot2")
- csrc = input(false,"Use Custom Source?", inline = "custom")
- src = input(close,"Select:", inline = "custom")
- color = input("Red/Green", "Line Color:", options=["Red/Green", "White", "Yellow", "Blue"], inline = "custom2")
- lwidth = input("1", "Line Width:", options=["1", "2", "3", "4"], inline = "custom2")
- //Conditions
- up = pivothigh(iff(csrc,src,high),lengthc,lengthc)
- dn = pivotlow(iff(csrc,src,low),length2,length2)
- n = bar_index
- a1 = valuewhen(not na(up),n,astartc)
- b1 = valuewhen(not na(dn),n,bstart)
- a2 = valuewhen(not na(up),n,aend)
- b2 = valuewhen(not na(dn),n,bend)
- //Colors
- color1 = color.red
- color2 = color.green
- if color == "White"
- color1 := color.white
- color2 := color.white
- if color == "Yellow"
- color1 := color.yellow
- color2 := color.yellow
- if color == "Blue"
- color1 := color.blue
- color2 := color.blue
- if color == "Red/Green"
- color1 := color.red
- color2 := color.green
- //Width
- width1 = 1
- if lwidth == "1"
- width1 := 1
- if lwidth == "2"
- width1 := 2
- if lwidth == "3"
- width1 := 3
- if lwidth == "4"
- width1 := 4
- //Plots
- line upper = line.new(n[n - a1 + lengthc],up[n - a1],n[n - a2 + lengthc],up[n - a2],
- extend=extend.right,color=color1,width=width1)
- line lower = line.new(n[n - b1 + length2],dn[n - b1],n[n - b2 + length2],dn[n - b2],
- extend=extend.right,color=color2,width=width1)
- line.delete(upper[1])
- line.delete(lower[1])
- // check if it's not in a channel
- not_in_a_channel = true
- for x = 0 to min(9, maxnumsr)
- if close <= array.get(suportresistance, x * 2) and close >= array.get(suportresistance, x * 2 + 1)
- not_in_a_channel := false
- // if price is not in a channel then check broken ones
- if not_in_a_channel
- for x = 0 to min(9, maxnumsr)
- if close[1] <= array.get(suportresistance, x * 2) and close > array.get(suportresistance, x * 2)
- resistancebroken := true
- if close[1] >= array.get(suportresistance, x * 2 + 1) and close < array.get(suportresistance, x * 2 + 1)
- supportbroken := true
- alertcondition(resistancebroken, title = "Resistance Broken", message = "Resistance Broken")
- alertcondition(supportbroken, title = "Support Broken", message = "Support Broken")
- plotshape(showsrbroken and resistancebroken, style = shape.triangleup, location = location.belowbar, color = color.new(color.lime, 0), size = size.tiny)
- plotshape(showsrbroken and supportbroken, style = shape.triangledown, location = location.abovebar, color = color.new(color.red, 0), size = size.tiny)
Advertisement
Add Comment
Please, Sign In to add comment