Untitled

 avatar
unknown
plain_text
2 months ago
7.3 kB
3
Indexable
//@version=4
study("Support Resistance Channels", "SRchannel", overlay = true, max_bars_back = 501)

// Timeframe settings
timeframe = input("D", title="Timeframe", type=input.resolution)

prd = input(defval = 10, title="Pivot Period", minval = 4, maxval = 30)
ppsrc = input(defval = 'High/Low', title="Source", options = ['High/Low', 'Close/Open'])
ChannelW = input(defval = 5, title = "Maximum Channel Width %", minval = 1, maxval = 8)
minstrength = input(defval = 1, title = "Minimum Strength", minval = 1)
maxnumsr = input(defval = 6, title = "Maximum Number of S/R", minval = 1, maxval = 10) - 1
loopback = input(defval = 290, title = "Loopback Period", minval = 100, maxval = 400)
res_col = input(defval = color.new(color.red, 75), title = "Resistance Color")
sup_col = input(defval = color.new(color.lime, 75), title = "Support Color")
inch_col = input(defval = color.new(color.gray, 75), title = "Color When Price in Channel")
showpp = input(defval = false, title = "Show Pivot Points")
showsrbroken = input(defval = false, title = "Show Broken Support/Resistance")

// Get higher timeframe data
high_htf = security(syminfo.tickerid, timeframe, high)
low_htf = security(syminfo.tickerid, timeframe, low)
close_htf = security(syminfo.tickerid, timeframe, close)
open_htf = security(syminfo.tickerid, timeframe, open)

// Source selection based on higher timeframe
float src1 = ppsrc == 'High/Low' ? high_htf : max(close_htf, open_htf)
float src2 = ppsrc == 'High/Low' ? low_htf : min(close_htf, open_htf)

// Pivot points calculation
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 channel width
prdhighest = highest(high_htf, 300)
prdlowest = lowest(low_htf, 300)
cwidth = (prdhighest - prdlowest) * ChannelW / 100

// Arrays for pivot values
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
            array.pop(pivotvals)
            array.pop(pivotlocs)
            continue
        break

// Function to get SR values
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
            if cpp <= hi
                lo := min(lo, cpp)
            else
                hi := max(hi, cpp)
            numpp := numpp + 20
    [hi, lo, numpp]

// Support resistance array
var suportresistance = array.new_float(20, 0)

// Function to change array values
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)
    stren = array.new_float(10, 0)
    
    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)
    
    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_htf[y] <= h and high_htf[y] >= l) or (low_htf[y] <= h and low_htf[y] >= l)
                s := s + 1
        array.set(supres, x * 3, array.get(supres, x * 3) + s)
    
    array.fill(suportresistance, 0)
    src = 0
    
    for x = 0 to array.size(pivotvals) - 1
        stv = -1.
        stl = -1
        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
            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))
            
            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

// Get level function
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 function
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

// Draw SR channels
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))

// Check for broken levels
resistancebroken = false
supportbroken = false
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 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

// Plot alerts
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)
Leave a Comment