//@version=5 indicator('Support Resistance - Dynamic', overlay=true, max_bars_back=600) enableSR = input(true, "SR On/Off", group="SR") colorSup = input(#00DBFF, "Support Color", group="SR") colorRes = input(#E91E63, "Resistance Color", group="SR") strengthSR = input.int(2, "S/R Strength", 1, group="SR") lineStyle = input.string("Dotted", "Line Style", ["Solid", "Dotted", "Dashed"], group="SR") lineWidth = input.int(2, "S/R Line Width", 1, group="SR") useZones = input(true, "Zones On/Off", group="SR") useHLZones = input(true, "High Low Zones On/Off", group="SR") zoneWidth = input.float(1.5, "Zone Width %", 0, tooltip="it's calculated using % of the distance between highest/lowest in last 300 bars", group="SR") expandSR = true // input(true, "Expand SR") // Functions percWidth(len, perc) => (ta.highest(len) - ta.lowest(len)) * perc / 100 // Get components rb = 10 prd = 290 ChannelW = 6 label_loc = 55 style = lineStyle == "Solid" ? line.style_solid : lineStyle == "Dotted" ? line.style_dotted : line.style_dashed ph = ta.pivothigh(rb, rb) pl = ta.pivotlow (rb, rb) sr_levels = array.new_float(21, na) prdhighest = ta.highest(prd) prdlowest = ta.lowest(prd) cwidth = percWidth(prd, ChannelW) zonePerc = percWidth(300, zoneWidth) aas = array.new_bool(41, true) u1 = 0.0, u1 := nz(u1[1]) d1 = 0.0, d1 := nz(d1[1]) highestph = 0.0, highestph := highestph[1] lowestpl = 0.0, lowestpl := lowestpl[1] var sr_levs = array.new_float(21, na) label hlabel = na, label.delete(hlabel[1]) label llabel = na, label.delete(llabel[1]) var sr_lines = array.new_line(21, na) var sr_linesH = array.new_line(21, na) var sr_linesL = array.new_line(21, na) var sr_linesF = array.new_linefill(21, na) var sr_labels = array.new_label(21, na) if ph or pl for x = 0 to array.size(sr_levels) - 1 array.set(sr_levels, x, na) highestph := prdlowest lowestpl := prdhighest countpp = 0 for x = 0 to prd if na(close[x]) break if not na(ph[x]) or not na(pl[x]) highestph := math.max(highestph, nz(ph[x], prdlowest), nz(pl[x], prdlowest)) lowestpl := math.min(lowestpl, nz(ph[x], prdhighest), nz(pl[x], prdhighest)) countpp += 1 if countpp > 40 break if array.get(aas, countpp) upl = (ph[x] ? high[x + rb] : low[x + rb]) + cwidth dnl = (ph[x] ? high[x + rb] : low[x + rb]) - cwidth u1 := countpp == 1 ? upl : u1 d1 := countpp == 1 ? dnl : d1 tmp = array.new_bool(41, true) cnt = 0 tpoint = 0 for xx = 0 to prd if na(close[xx]) break if not na(ph[xx]) or not na(pl[xx]) chg = false cnt += 1 if cnt > 40 break if array.get(aas, cnt) if not na(ph[xx]) if high[xx + rb] <= upl and high[xx + rb] >= dnl tpoint += 1 chg := true if not na(pl[xx]) if low[xx + rb] <= upl and low[xx + rb] >= dnl tpoint += 1 chg := true if chg and cnt < 41 array.set(tmp, cnt, false) if tpoint >= strengthSR for g = 0 to 40 by 1 if not array.get(tmp, g) array.set(aas, g, false) if ph[x] and countpp < 21 array.set(sr_levels, countpp, high[x + rb]) if pl[x] and countpp < 21 array.set(sr_levels, countpp, low[x + rb]) // Plot var line highest_ = na, line.delete(highest_) var line lowest_ = na, line.delete(lowest_) var line highest_fill1 = na, line.delete(highest_fill1) var line highest_fill2 = na, line.delete(highest_fill2) var line lowest_fill1 = na, line.delete(lowest_fill1) var line lowest_fill2 = na, line.delete(lowest_fill2) hi_col = close >= highestph ? colorSup : colorRes lo_col = close >= lowestpl ? colorSup : colorRes if enableSR highest_ := line.new(bar_index - 311, highestph, bar_index, highestph, xloc.bar_index, expandSR ? extend.both : extend.right, hi_col, style, lineWidth) lowest_ := line.new(bar_index - 311, lowestpl , bar_index, lowestpl , xloc.bar_index, expandSR ? extend.both : extend.right, lo_col, style, lineWidth) if useHLZones highest_fill1 := line.new(bar_index - 311, highestph + zonePerc, bar_index, highestph + zonePerc, xloc.bar_index, expandSR ? extend.both : extend.right, na) highest_fill2 := line.new(bar_index - 311, highestph - zonePerc, bar_index, highestph - zonePerc, xloc.bar_index, expandSR ? extend.both : extend.right, na) lowest_fill1 := line.new(bar_index - 311, lowestpl + zonePerc , bar_index, lowestpl + zonePerc , xloc.bar_index, expandSR ? extend.both : extend.right, na) lowest_fill2 := line.new(bar_index - 311, lowestpl - zonePerc , bar_index, lowestpl - zonePerc , xloc.bar_index, expandSR ? extend.both : extend.right, na) linefill.new(highest_fill1, highest_fill2, color.new(hi_col, 80)) linefill.new(lowest_fill1 , lowest_fill2 , color.new(lo_col, 80)) if ph or pl for x = 0 to array.size(sr_lines) - 1 array.set(sr_levs, x, array.get(sr_levels, x)) for x = 0 to array.size(sr_lines) - 1 line.delete(array.get(sr_lines, x)) line.delete(array.get(sr_linesH, x)) line.delete(array.get(sr_linesL, x)) linefill.delete(array.get(sr_linesF, x)) if array.get(sr_levs, x) and enableSR line_col = close >= array.get(sr_levs, x) ? colorSup : colorRes array.set(sr_lines, x, line.new(bar_index - 355, array.get(sr_levs, x), bar_index, array.get(sr_levs, x), xloc.bar_index, expandSR ? extend.both : extend.right, line_col, style, lineWidth)) if useZones array.set(sr_linesH, x, line.new(bar_index - 355, array.get(sr_levs, x) + zonePerc, bar_index, array.get(sr_levs, x) + zonePerc, xloc.bar_index, expandSR ? extend.both : extend.right, na)) array.set(sr_linesL, x, line.new(bar_index - 355, array.get(sr_levs, x) - zonePerc, bar_index, array.get(sr_levs, x) - zonePerc, xloc.bar_index, expandSR ? extend.both : extend.right, na)) array.set(sr_linesF, x, linefill.new(array.get(sr_linesH, x), array.get(sr_linesL, x), color.new(line_col, 80))) for x = 0 to array.size(sr_labels) - 1 label.delete(array.get(sr_labels, x)) if array.get(sr_levs, x) and enableSR lab_loc = close >= array.get(sr_levs, x) ? label.style_label_up : label.style_label_down lab_col = close >= array.get(sr_levs, x) ? colorSup : colorRes array.set(sr_labels, x, label.new(bar_index + label_loc, array.get(sr_levs, x), str.tostring(math.round_to_mintick(array.get(sr_levs, x))), color=lab_col , textcolor=#000000, style=lab_loc)) hlabel := enableSR ? label.new(bar_index + label_loc + math.round(math.sign(label_loc)) * 20, highestph, "High Level : " + str.tostring(highestph), color=hi_col, textcolor=#000000, style=label.style_label_down) : na llabel := enableSR ? label.new(bar_index + label_loc + math.round(math.sign(label_loc)) * 20, lowestpl , "Low Level : " + str.tostring(lowestpl) , color=lo_col, textcolor=#000000, style=label.style_label_up ) : na // TIME FRAME 1 enableSRs = input(true, "SR On/Off", group="SR TF") tf = input.timeframe(title="Timeframe", defval="D", group="SR TF") colorSups = input(#00DBFF, "Support Color", group="SR TF") colorRess = input(#E91E63, "Resistance Color", group="SR TF") strengthSRs = request.security(syminfo.tickerid, tf, strengthSR, gaps=barmerge.gaps_on) lineStyles = input.string("Dashed", "Line Style", ["Solid", "Dotted", "Dashed"], group="SR TF") lineWidths = input.int(2, "S/R Line Width", 1, group="SR TF") expandSRs = true //input(true, "Expand SR" , group = "SR TF") // Functions // percWidth(len, perc) => (ta.highest(len) - ta.lowest(len)) * perc / 100 // Get components rbs = request.security(syminfo.tickerid, tf, rb, gaps=barmerge.gaps_on) prds = request.security(syminfo.tickerid, tf, prd, gaps=barmerge.gaps_on) ChannelWs = request.security(syminfo.tickerid, tf, ChannelW, gaps=barmerge.gaps_on) label_locs = request.security(syminfo.tickerid, tf, label_loc, gaps=barmerge.gaps_on) styles = lineStyles == "Solid" ? line.style_solid : lineStyles == "Dotted" ? line.style_dotted : line.style_dashed phs = request.security(syminfo.tickerid, tf, ph, gaps=barmerge.gaps_on) pls = request.security(syminfo.tickerid, tf, pl, gaps=barmerge.gaps_on) sr_levelss = request.security(syminfo.tickerid, tf, sr_levels, gaps=barmerge.gaps_on) prdhighests = request.security(syminfo.tickerid, tf, prdhighest, gaps=barmerge.gaps_on) prdlowests = request.security(syminfo.tickerid, tf, prdlowest, gaps=barmerge.gaps_on) cwidths = request.security(syminfo.tickerid, tf, cwidth, gaps=barmerge.gaps_on) aass = request.security(syminfo.tickerid, tf, aas, gaps=barmerge.gaps_on) u1s = request.security(syminfo.tickerid, tf, u1, gaps=barmerge.gaps_on) d1s = request.security(syminfo.tickerid, tf, d1, gaps=barmerge.gaps_on) highestphs = request.security(syminfo.tickerid, tf, highestph, gaps=barmerge.gaps_on) lowestpls = request.security(syminfo.tickerid, tf, lowestpl, gaps=barmerge.gaps_on) var sr_levss = array.new_float(21, na) label hlabels = na, label.delete(hlabels[1]) label llabels = na, label.delete(llabels[1]) var sr_liness = array.new_line(21, na) var sr_linesHs = array.new_line(21, na) var sr_linesLs = array.new_line(21, na) var sr_linesFs = array.new_linefill(21, na) var sr_labelss = array.new_label(21, na) // Plot var line highest_s = na, line.delete(highest_s) var line lowest_s = na, line.delete(lowest_s) var line highest_fill1s = na, line.delete(highest_fill1s) var line highest_fill2s = na, line.delete(highest_fill2s) var line lowest_fill1s = na, line.delete(lowest_fill1s) var line lowest_fill2s = na, line.delete(lowest_fill2s) hi_cols = close >= highestphs ? colorSups : colorRess lo_cols = close >= lowestpls ? colorSups : colorRess if enableSRs highest_s:= line.new(bar_index - 311, highestphs, bar_index, highestphs, xloc.bar_index, expandSRs ? extend.both : extend.right, hi_cols, styles, lineWidths) lowest_s := line.new(bar_index - 311, lowestpls , bar_index, lowestpls , xloc.bar_index, expandSRs ? extend.both : extend.right, lo_cols, styles, lineWidths) if useHLZones highest_fill1s := line.new(bar_index - 311, highestphs , bar_index, highestphs , xloc.bar_index, expandSRs ? extend.both : extend.right, na) highest_fill2s := line.new(bar_index - 311, highestphs , bar_index, highestphs , xloc.bar_index, expandSRs? extend.both : extend.right, na) lowest_fill1s := line.new(bar_index - 311, lowestpls , bar_index, lowestpls , xloc.bar_index, expandSRs ? extend.both : extend.right, na) lowest_fill2s := line.new(bar_index - 311, lowestpls , bar_index, lowestpls , xloc.bar_index, expandSRs ? extend.both : extend.right, na) if phs or pls for y = 0 to array.size(sr_liness) - 1 array.set(sr_levss, y, array.get(sr_levelss, y)) for y = 0 to array.size(sr_lines) - 1 line.delete(array.get(sr_liness, y)) line.delete(array.get(sr_linesHs, y)) line.delete(array.get(sr_linesLs, y)) if array.get(sr_levss, y) and enableSRs line_cols = close >= array.get(sr_levss, y) ? colorSups : colorRess array.set(sr_liness, y, line.new(bar_index - 355, array.get(sr_levss, y), bar_index, array.get(sr_levss, y), xloc.bar_index, expandSRs ? extend.both : extend.right, line_cols, styles, lineWidths)) for y = 0 to array.size(sr_labelss) - 1 label.delete(array.get(sr_labelss, y)) if array.get(sr_levss, y) and enableSRs lab_locs = close >= array.get(sr_levss, y) ? label.style_label_up : label.style_label_down lab_cols = close >= array.get(sr_levss, y) ? colorSups : colorRess array.set(sr_labelss, y, label.new(bar_index + label_loc, array.get(sr_levss, y), str.tostring(math.round_to_mintick(array.get(sr_levss, y))), color=lab_cols , textcolor=#000000, style=lab_locs)) hlabels := enableSRs ? label.new(bar_index + label_locs + math.round(math.sign(label_locs)) * 20, highestphs, "High Level : " + str.tostring(highestphs), color=hi_cols, textcolor=#000000, style=label.style_label_down) : na llabels := enableSRs ? label.new(bar_index + label_locs + math.round(math.sign(label_locs)) * 20, lowestpls , "Low Level : " + str.tostring(lowestpls) , color=lo_cols, textcolor=#000000, style=label.style_label_up ) : na // TIME FRAME 2 SRs = input(true, "SR On/Off", group="SR tf2") tf2 = input.timeframe(title="Timeframe", defval="D", group="SR tf2") csups = input(#00DBFF, "Support Color", group="SR tf2") cres = input(#E91E63, "Resistance Color", group="SR tf2") srs = request.security(syminfo.tickerid, tf2, strengthSR, gaps=barmerge.gaps_on) lstyle = input.string("Solid", "Line Style", ["Solid", "Dotted", "Dashed"], group="SR tf2") lwidth = input.int(2, "S/R Line Width", 1, group="SR tf2") esrs = true //input(true, "Expand SR" , group = "SR tf2") // Functions // percWidth(len, perc) => (ta.highest(len) - ta.lowest(len)) * perc / 100 // Get components rbs2 = request.security(syminfo.tickerid, tf2, rb, gaps=barmerge.gaps_on) prds2 = request.security(syminfo.tickerid, tf2, prd, gaps=barmerge.gaps_on) cws = request.security(syminfo.tickerid, tf2, ChannelW, gaps=barmerge.gaps_on) llocs = request.security(syminfo.tickerid, tf2, label_loc, gaps=barmerge.gaps_on) stle = lstyle == "Solid" ? line.style_solid : lstyle == "Dotted" ? line.style_dotted : line.style_dashed phs1 = request.security(syminfo.tickerid, tf2, ph, gaps=barmerge.gaps_on) pls2 = request.security(syminfo.tickerid, tf2, pl, gaps=barmerge.gaps_on) srl = request.security(syminfo.tickerid, tf2, sr_levels, gaps=barmerge.gaps_on) prdh = request.security(syminfo.tickerid, tf2, prdhighest, gaps=barmerge.gaps_on) prdl = request.security(syminfo.tickerid, tf2, prdlowest, gaps=barmerge.gaps_on) cwidth2 = request.security(syminfo.tickerid, tf2, cwidth, gaps=barmerge.gaps_on) ass = request.security(syminfo.tickerid, tf2, aas, gaps=barmerge.gaps_on) u2s = request.security(syminfo.tickerid, tf2, u1, gaps=barmerge.gaps_on) d2s = request.security(syminfo.tickerid, tf2, d1, gaps=barmerge.gaps_on) hphs = request.security(syminfo.tickerid, tf2, highestph, gaps=barmerge.gaps_on) lpls = request.security(syminfo.tickerid, tf2, lowestpl, gaps=barmerge.gaps_on) var srlev2 = array.new_float(21, na) label hlab2 = na, label.delete(hlab2[1]) label llab2 = na, label.delete(llab2[1]) var srlins = array.new_line(21, na) var srlinsh = array.new_line(21, na) var srlines1 = array.new_line(21, na) var srlind = array.new_linefill(21, na) var srlab = array.new_label(21, na) // Plot var line hs = na, line.delete(hs) var line ls = na, line.delete(ls) var line hf = na, line.delete(hf) var line hf2 = na, line.delete(hf2) var line lf = na, line.delete(lf) var line lf2 = na, line.delete(lf2) hicol1 = close >= hphs ? csups : cres locol1 = close >= lpls ? csups : cres if SRs hs:= line.new(bar_index - 311, hphs, bar_index, hphs, xloc.bar_index, esrs ? extend.both : extend.right, hicol1, stle, lwidth) ls := line.new(bar_index - 311, lpls , bar_index, lpls , xloc.bar_index, esrs ? extend.both : extend.right, locol1, stle, lwidth) if useHLZones hf := line.new(bar_index - 311, hphs , bar_index, hphs , xloc.bar_index, esrs ? extend.both : extend.right, na) hf2 := line.new(bar_index - 311, hphs , bar_index, hphs , xloc.bar_index, esrs? extend.both : extend.right, na) lf := line.new(bar_index - 311, lpls , bar_index, lpls , xloc.bar_index, esrs ? extend.both : extend.right, na) lf2 := line.new(bar_index - 311, lpls , bar_index, lpls , xloc.bar_index, esrs ? extend.both : extend.right, na) if phs1 or pls2 for y = 0 to array.size(srlins) - 1 array.set(srlev2, y, array.get(srl, y)) for y = 0 to array.size(sr_lines) - 1 line.delete(array.get(srlins, y)) line.delete(array.get(srlinsh, y)) line.delete(array.get(srlines1, y)) if array.get(srlev2, y) and SRs line_cols = close >= array.get(srlev2, y) ? csups : cres array.set(srlins, y, line.new(bar_index - 355, array.get(srlev2, y), bar_index, array.get(srlev2, y), xloc.bar_index, esrs ? extend.both : extend.right, line_cols, stle, lwidth)) for y = 0 to array.size(srlab) - 1 label.delete(array.get(srlab, y)) if array.get(srlev2, y) and SRs lab_locs = close >= array.get(srlev2, y) ? label.style_label_up : label.style_label_down lab_cols = close >= array.get(srlev2, y) ? csups : cres array.set(srlab, y, label.new(bar_index + label_loc, array.get(srlev2, y), str.tostring(math.round_to_mintick(array.get(srlev2, y))), color=lab_cols , textcolor=#000000, style=lab_locs)) hlab2 := SRs ? label.new(bar_index + llocs + math.round(math.sign(llocs)) * 20, hphs, "High Level : " + str.tostring(hphs), color=hicol1, textcolor=#000000, style=label.style_label_down) : na llab2 := SRs ? label.new(bar_index + llocs + math.round(math.sign(llocs)) * 20, lpls , "Low Level : " + str.tostring(lpls) , color=locol1, textcolor=#000000, style=label.style_label_up ) : na ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////