Untitled

 avatar
unknown
plain_text
2 years ago
19 kB
112
Indexable
//@version=5
indicator(title="MA Ribbon R5.2 + Support Resistance", overlay=true)

// --- Script 1: MA Ribbon R5.2 by JustUncleL ---
// Use Alternate Anchor TF for MAs 
anchor = input.int(0, minval=0, title='Set to an Anchor TimeFrame in mins (0=none, 1D=1440, 1W=7200)')
showRibbon = input(false, title='Show Ribbon If Chart TF Greater Than Anchor TF')

// Fast MA - type, length
fastType = input.string(defval='EMA', title='Fast MA Type: ', options=['SMA', 'EMA', 'WMA', 'VWMA', 'SMMA', 'DEMA', 'TEMA', 'LAGMA', 'LINREG', 'HullMA', 'ZEMA', 'TMA', 'SSMA'])
fastLen = input.int(defval=5, title='Fast MA - Length', minval=1)
fastGamma = input(defval=0.33, title='Fast MA - Gamma for LAGMA')
// Medium MA - type, length
mediumType = input.string(defval='EMA', title='Medium MA Type: ', options=['SMA', 'EMA', 'WMA', 'VWMA', 'SMMA', 'DEMA', 'TEMA', 'LAGMA', 'LINREG', 'HullMA', 'ZEMA', 'TMA', 'SSMA'])
mediumLen = input.int(defval=1, title='Medium MA - Length (1=disabled)', minval=1)
mediumGamma = input(defval=0.55, title='Medium MA - Gamma for LAGMA')
// Slow MA - type, length
slowType = input.string(defval='EMA', title='Slow MA Type: ', options=['SMA', 'EMA', 'WMA', 'VWMA', 'SMMA', 'DEMA', 'TEMA', 'LAGMA', 'LINREG', 'HullMA', 'ZEMA', 'TMA', 'SSMA'])
slowLen = input.int(defval=22, title='Slow MA - Length', minval=2)
slowGamma = input(defval=0.77, title='Slow MA - Gamma for LAGMA')
//
ma_src = input(close, title='MA Source')
sBars = input(false, title='Show Coloured Bars')
uGrabClr = input(false, title='Use Grab Bar 6-tone Colours, instead of Standard 3-tone')
uOpen = input(false, title='Candles must open and close outside ribbon Colouring')
//
ShowMACross = input(false, title='Show MA Cross Alerts')
//
ShowSwing = input(false, title='Show Swing Alerts')
rFilter = input(false, title='Filter Swing Alerts to Ribbon Colour')
dFilter = input(false, title='Filter Swing Alerts to Fast MA Directional Slope')
//
// - INPUTS END

// Constants colours that include fully non-transparent option.
green100 = #008000FF
lime100 = #00FF00FF
red100 = #FF0000FF
blue100 = #0000FFFF
aqua100 = #00FFFFFF
darkred100 = #8B0000FF
gray100 = #808080FF

// - FUNCTIONS

// - variant(type, src, len, gamma)
// Returns MA input selection variant, default to SMA if blank or typo.

// SuperSmoother filter
// © 2013  John F. Ehlers
variant_supersmoother(src, len) =>
    a1 = math.exp(-1.414 * 3.14159 / len)
    b1 = 2 * a1 * math.cos(1.414 * 3.14159 / len)
    c2 = b1
    c3 = -a1 * a1
    c1 = 1 - c2 - c3
    v9 = 0.0
    v9 := c1 * (src + nz(src[1])) / 2 + c2 * nz(v9[1]) + c3 * nz(v9[2])
    v9

variant_smoothed(src, len) =>
    v5 = 0.0
    sma_1 = ta.sma(src, len)
    v5 := na(v5[1]) ? sma_1 : (v5[1] * (len - 1) + src) / len
    v5

variant_zerolagema(src, len) =>
    xLag = (len - 1) / 2
    xEMA = src + src - src[xLag]
    v10 = ta.ema(xEMA, len)
    v10

variant_doubleema(src, len) =>
    v2 = ta.ema(src, len)
    v6 = 2 * v2 - ta.ema(v2, len)
    v6

variant_tripleema(src, len) =>
    v2 = ta.ema(src, len)
    v7 = 3 * (v2 - ta.ema(v2, len)) + ta.ema(ta.ema(v2, len), len)  // Triple Exponential
    v7

//calc Laguerre
variant_lag(p, g) =>
    L0 = 0.0
    L1 = 0.0
    L2 = 0.0
    L3 = 0.0
    L0 := (1 - g) * p + g * nz(L0[1])
    L1 := -g * L0 + nz(L0[1]) + g * nz(L1[1])
    L2 := -g * L1 + nz(L1[1]) + g * nz(L2[1])
    L3 := -g * L2 + nz(L2[1]) + g * nz(L3[1])
    f = (L0 + 2 * L1 + 2 * L2 + L3) / 6
    f

// return variant, defaults to SMA 
variant(type, src, len, g) =>
    result = src
    if len > 1
        ema_1 = ta.ema(src, len)
        wma_1 = ta.wma(src, len)
        vwma_1 = ta.vwma(src, len)
        variant_smoothed__1 = variant_smoothed(src, len)
        variant_doubleema__1 = variant_doubleema(src, len)
        variant_tripleema__1 = variant_tripleema(src, len)
        variant_lag__1 = variant_lag(src, g)
        linreg_1 = ta.linreg(src, len, 0)
        wma_2 = ta.wma(src, len / 2)
        wma_3 = ta.wma(src, len)
        wma_4 = ta.wma(2 * wma_2 - wma_3, math.round(math.sqrt(len)))
        variant_supersmoother__1 = variant_supersmoother(src, len)
        variant_zerolagema__1 = variant_zerolagema(src, len)
        sma_1 = ta.sma(src, len)
        sma_2 = ta.sma(sma_1, len)
        sma_3 = ta.sma(src, len)
        result := type == 'EMA' ? ema_1 : type == 'WMA' ? wma_1 : type == 'VWMA' ? vwma_1 : type == 'SMMA' ? variant_smoothed__1 : type == 'DEMA' ? variant_doubleema__1 : type == 'TEMA' ? variant_tripleema__1 : type == 'LAGMA' ? variant_lag__1 : type == 'LINREG' ? linreg_1 : type == 'HullMA' ? wma_4 : type == 'SSMA' ? variant_supersmoother__1 : type == 'ZEMA' ? variant_zerolagema__1 : type == 'TMA' ? sma_2 : sma_3
        result
    //end if
    result

// - /variant 

// - FUNCTIONS END

// Make sure we have minimum channel spread.
LengthSlow_ = slowLen  //fastType==slowType?(slowLen-slowLen)<1?slowLen+1:slowLen : slowLen


//what is the 1st Anchor (normally current chart TF)
currentTFmins = timeframe.isintraday ? timeframe.multiplier : timeframe.isdaily ? timeframe.multiplier * 1440 : timeframe.isweekly ? timeframe.multiplier * 7200 : timeframe.ismonthly ? timeframe.multiplier * 30240 : timeframe.multiplier

// function to caculate multiplier from anchor time frame (TF is in mins)
multAnchor(anchor) =>
    mult = 1
    if timeframe.isintraday
        mult := anchor > 0 ? timeframe.multiplier <= 0 ? 1 : timeframe.multiplier >= anchor ? 1 : math.round(anchor / timeframe.multiplier) : 1
        mult
    else
        mult := anchor > 0 ? timeframe.isdaily ? anchor <= 1440 ? 1 : math.round(anchor / 1440) : timeframe.isweekly ? anchor <= 7200 ? 1 : math.round(anchor / 7200) : timeframe.ismonthly ? anchor <= 30240 ? 1 : math.round(anchor / 30240) : 1 : 1
        mult
    //end if
    mult


// get multipliers for each time frame
mult = multAnchor(anchor)

// Adjust MA lengths with Anchor multiplier
LengthFast = mult == 1 ? fastLen : fastLen * mult
LengthMedium = mult == 1 ? mediumLen : mediumLen * mult
LengthSlow = mult == 1 ? LengthSlow_ : LengthSlow_ * mult
//plotshape(interval,location=location.bottom)

// Get the two MAs
fastMA = variant(fastType, ma_src, LengthFast, fastGamma)
slowMA = variant(slowType, ma_src, LengthSlow, slowGamma)
variant__1 = variant(mediumType, ma_src, LengthMedium, mediumGamma)
mediumMA = mediumLen == 1 ? na : variant__1

showRibbonFlag = showRibbon or anchor == 0 or currentTFmins <= anchor

fDirection = 0
sDirection = 0
//mDirection = 0

fDirection := hlc3 > fastMA ? 1 : hlc3 < fastMA ? -1 : nz(fDirection[1], 1)
sDirection := hlc3 > slowMA ? 1 : hlc3 < slowMA ? -1 : nz(sDirection[1], 1)
//mDirection := LengthMedium==1 ? na : hlc3 > mediumMA ? 1 : hlc3  < mediumMA ? -1 : nz(mDirection[1],1)

//Plot the Ribbon
fastMA_ = plot(showRibbonFlag ? fastMA : na, color=fDirection == 1 ? color.green : color.red, style=plot.style_line, join=true, linewidth=1, title='Fast MA', transp=20)
slowMA_ = plot(showRibbonFlag ? slowMA : na, color=sDirection == 1 ? color.green : color.red, style=plot.style_line, join=true, linewidth=1, title='Slow MA', transp=20)
plot(showRibbonFlag ? mediumMA : na, color=sDirection == 1 ? color.green : color.red, style=plot.style_circles, join=true, linewidth=1, title='Medium MA', transp=20)
fcolor = fastMA > slowMA ? color.green : color.red
fill(fastMA_, slowMA_, color=fcolor, title='Ribbon Fill', transp=80)

// Colour bars according to the close position relative to the MA selected
// Or Grab candle colour code bars according to the close position relative to the MA selected
grabcol = uGrabClr ? close >= open ? hlc3 > fastMA and hlc3 > slowMA and (not uOpen or open > fastMA and open > slowMA) ? lime100 : hlc3 < fastMA and hlc3 < slowMA and (not uOpen or open < fastMA and open < slowMA) ? red100 : aqua100 : hlc3 > fastMA and hlc3 > slowMA and (not uOpen or open > fastMA and open > slowMA) ? green100 : hlc3 < fastMA and hlc3 < slowMA and (not uOpen or open < fastMA and open < slowMA) ? darkred100 : blue100 : na
grabcol := uGrabClr ? grabcol : hlc3 > fastMA and hlc3 > slowMA and (not uOpen or open > fastMA and open > slowMA) ? lime100 : hlc3 < fastMA and hlc3 < slowMA and (not uOpen or open < fastMA and open < slowMA) ? red100 : gray100

barcolor(showRibbonFlag and sBars and not ShowMACross ? grabcol : na, title='Bar Colours')

// Generate Alert Arrows
//
buy = 0
sell = 0
buyT = 0
sellT = 0
// Generate signal by Candle Colour
buy := grabcol == lime100 or grabcol == green100 ? nz(buy[1]) + 1 : 0
sell := grabcol == red100 or grabcol == darkred100 ? nz(sell[1]) + 1 : 0

// Trend Filter
falling_1 = ta.falling(fastMA, 2)
buyT := buy == 0 ? 0 : rFilter and fastMA < slowMA or dFilter and falling_1 ? 0 : nz(buyT[1]) + 1
rising_1 = ta.rising(fastMA, 2)
sellT := sell == 0 ? 0 : rFilter and fastMA > slowMA or dFilter and rising_1 ? 0 : nz(sellT[1]) + 1

// Exit conditions
exitbuy = nz(buyT[1]) > 0 and buyT == 0
exitsell = nz(sellT[1]) > 0 and sellT == 0

//
plotarrow(showRibbonFlag and ShowSwing and buyT == 1 ? 1 : na, title='BUY Swing Arrow', colorup=color.new(color.lime, 20), maxheight=60, minheight=50)
plotarrow(showRibbonFlag and ShowSwing and sellT == 1 ? -1 : na, title='SELL Swing Arrow', colordown=color.new(color.red, 20), maxheight=60, minheight=50)
//
plotshape(showRibbonFlag and ShowSwing and exitbuy, title='BUY Exit', style=shape.xcross, location=location.belowbar, color=color.new(color.gray, 0), text='Exit\nBuy', offset=0)
plotshape(showRibbonFlag and ShowSwing and exitsell, title='Sell Exit', style=shape.xcross, location=location.abovebar, color=color.new(color.gray, 0), text='Exit\nSell', offset=0)

// MA trend bar color
TrendingUp = fastMA > slowMA
TrendingDown = fastMA < slowMA
barcolor(showRibbonFlag and sBars and ShowMACross ? TrendingUp ? color.green : TrendingDown ? color.red : color.blue : na)

// MA cross alert
MAcrossing = ta.cross(fastMA, slowMA) ? fastMA : na
plot(showRibbonFlag and ShowMACross ? MAcrossing : na, style=plot.style_cross, linewidth=4, color=color.new(color.black, 0))

// MA cross background color alert
Uptrend = TrendingUp and TrendingDown[1]
Downtrend = TrendingDown and TrendingUp[1]
bgcolor(showRibbonFlag and ShowMACross ? Uptrend ? color.green : Downtrend ? color.red : na : na, transp=50)

// Buy and sell alert
XBuy = 0
XBuy := nz(XBuy[1])
XSell = 0
XSell := nz(XSell[1])
XBuy := TrendingUp and close > close[1] ? XBuy + 1 : TrendingDown or XBuy == 0 ? 0 : XBuy + 1
XSell := TrendingDown and close < close[1] ? XSell + 1 : TrendingUp or XSell == 0 ? 0 : XSell + 1

plotshape(showRibbonFlag and ShowMACross and XBuy == 1 ? close : na, color=color.new(color.black, 0), style=shape.triangleup, text='XBuy', location=location.bottom, size=size.small)
plotshape(showRibbonFlag and ShowMACross and XSell == 1 ? close : na, color=color.new(color.black, 0), style=shape.triangledown, text='XSell', location=location.top, size=size.small)
//
//plotshape(showRibbonFlag and ShowMACross and exitbuy, title='BUY Exit', style=shape.xcross, location=location.belowbar, color=gray, text="Exit\nBuy", offset=0,transp=0)
//plotshape(showRibbonFlag and ShowMACross and exitsell, title='Sell Exit', style=shape.xcross, location=location.abovebar, color=gray, text="Exit\nSell", offset=0,transp=0)

// Generate Alarms
alertcondition(showRibbonFlag and buyT == 1, title='BUY Alert', message='BUY')
alertcondition(showRibbonFlag and sellT == 1, title='SELL Alert', message='SELL')
alertcondition(showRibbonFlag and XBuy == 1, title='X BUY Alert', message='X BUY')
alertcondition(showRibbonFlag and XSell == 1, title='X SELL Alert', message='X SELL')
alertcondition(showRibbonFlag and exitbuy, title='BUY Exit Alert', message='ExitBuy')
alertcondition(showRibbonFlag and exitsell, title='SELL Exit Alert', message='ExitSell')

//eof

// --- Script 2: Support Reistance - Dynamic v2 ---
prd = input.int(defval=10, title='Pivot Period', minval=4, maxval=30, group='Setup')
ppsrc = input.string(defval='High/Low', title='Source', options=['High/Low', 'Close/Open'], group='Setup')
maxnumpp = input.int(defval=20, title=' Maximum Number of Pivot', minval=5, maxval=100, group='Setup')
ChannelW = input.int(defval=10, title='Maximum Channel Width %', minval=1, group='Setup')
maxnumsr = input.int(defval=5, title=' Maximum Number of S/R', minval=1, maxval=10, group='Setup')
min_strength = input.int(defval=2, title=' Minimum Strength', minval=1, maxval=10, group='Setup')
labelloc = input.int(defval=20, title='Label Location', group='Colors', tooltip='Positive numbers reference future bars, negative numbers reference histical bars')
linestyle = input.string(defval='Dashed', title='Line Style', options=['Solid', 'Dotted', 'Dashed'], group='Colors')
linewidth = input.int(defval=2, title='Line Width', minval=1, maxval=4, group='Colors')
resistancecolor = input.color(defval=color.red, title='Resistance Color', group='Colors')
supportcolor = input.color(defval=color.lime, title='Support Color', group='Colors')
showpp = input(false, title='Show Point Points')

float src1 = ppsrc == 'High/Low' ? high : math.max(close, open)
float src2 = ppsrc == 'High/Low' ? low : math.min(close, open)
float ph = ta.pivothigh(src1, prd, prd)
float pl = ta.pivotlow(src2, prd, prd)

plotshape(ph and showpp, text='H', style=shape.labeldown, color=na, textcolor=color.new(color.red, 0), location=location.abovebar, offset=-prd)
plotshape(pl and showpp, text='L', style=shape.labelup, color=na, textcolor=color.new(color.lime, 0), location=location.belowbar, offset=-prd)

Lstyle = linestyle == 'Dashed' ? line.style_dashed : linestyle == 'Solid' ? line.style_solid : line.style_dotted

//calculate maximum S/R channel zone width
prdhighest = ta.highest(300)
prdlowest = ta.lowest(300)
cwidth = (prdhighest - prdlowest) * ChannelW / 100

var pivotvals = array.new_float(0)

if ph or pl
    array.unshift(pivotvals, ph ? ph : pl)
    if array.size(pivotvals) > maxnumpp  // limit the array size
        array.pop(pivotvals)

get_sr_vals(ind) =>
    float lo = array.get(pivotvals, ind)
    float hi = lo
    int numpp = 0
    for y = 0 to array.size(pivotvals) - 1 by 1
        float cpp = array.get(pivotvals, y)
        float wdth = cpp <= lo ? hi - cpp : cpp - lo
        if wdth <= cwidth  // fits the max channel width?
            if cpp <= hi
                lo := math.min(lo, cpp)
            else
                hi := math.max(hi, cpp)

            numpp += 1
            numpp
    [hi, lo, numpp]

var sr_up_level = array.new_float(0)
var sr_dn_level = array.new_float(0)
sr_strength = array.new_float(0)

find_loc(strength) =>
    ret = array.size(sr_strength)
    for i = ret > 0 ? array.size(sr_strength) - 1 : na to 0 by 1
        if strength <= array.get(sr_strength, i)
            break
        ret := i
        ret
    ret

check_sr(hi, lo, strength) =>
    ret = true
    for i = 0 to array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na by 1
        //included?
        if array.get(sr_up_level, i) >= lo and array.get(sr_up_level, i) <= hi or array.get(sr_dn_level, i) >= lo and array.get(sr_dn_level, i) <= hi
            if strength >= array.get(sr_strength, i)
                array.remove(sr_strength, i)
                array.remove(sr_up_level, i)
                array.remove(sr_dn_level, i)
                ret
            else
                ret := false
                ret
            break
    ret

var sr_lines = array.new_line(11, na)
var sr_labels = array.new_label(11, na)

for x = 1 to 10 by 1
    rate = 100 * (label.get_y(array.get(sr_labels, x)) - close) / close
    label.set_text(array.get(sr_labels, x), text=str.tostring(label.get_y(array.get(sr_labels, x))) + '(' + str.tostring(rate, '#.##') + '%)')
    label.set_x(array.get(sr_labels, x), x=bar_index + labelloc)
    label.set_color(array.get(sr_labels, x), color=label.get_y(array.get(sr_labels, x)) >= close ? color.red : color.lime)
    label.set_textcolor(array.get(sr_labels, x), textcolor=label.get_y(array.get(sr_labels, x)) >= close ? color.white : color.black)
    label.set_style(array.get(sr_labels, x), style=label.get_y(array.get(sr_labels, x)) >= close ? label.style_label_down : label.style_label_up)
    line.set_color(array.get(sr_lines, x), color=line.get_y1(array.get(sr_lines, x)) >= close ? resistancecolor : supportcolor)

if ph or pl
    //because of new calculation, remove old S/R levels
    array.clear(sr_up_level)
    array.clear(sr_dn_level)
    array.clear(sr_strength)
    //find S/R zones
    for x = 0 to array.size(pivotvals) - 1 by 1
        [hi, lo, strength] = get_sr_vals(x)
        if check_sr(hi, lo, strength)
            loc = find_loc(strength)
            // if strength is in first maxnumsr sr then insert it to the arrays 
            if loc < maxnumsr and strength >= min_strength
                array.insert(sr_strength, loc, strength)
                array.insert(sr_up_level, loc, hi)
                array.insert(sr_dn_level, loc, lo)
                // keep size of the arrays = 5
                if array.size(sr_strength) > maxnumsr
                    array.pop(sr_strength)
                    array.pop(sr_up_level)
                    array.pop(sr_dn_level)

    for x = 1 to 10 by 1
        line.delete(array.get(sr_lines, x))
        label.delete(array.get(sr_labels, x))

    for x = 0 to array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na by 1
        float mid = math.round_to_mintick((array.get(sr_up_level, x) + array.get(sr_dn_level, x)) / 2)
        rate = 100 * (mid - close) / close
        array.set(sr_labels, x + 1, label.new(x=bar_index + labelloc, y=mid, text=str.tostring(mid) + '(' + str.tostring(rate, '#.##') + '%)', color=mid >= close ? color.red : color.lime, textcolor=mid >= close ? color.white : color.black, style=mid >= close ? label.style_label_down : label.style_label_up))

        array.set(sr_lines, x + 1, line.new(x1=bar_index, y1=mid, x2=bar_index - 1, y2=mid, extend=extend.both, color=mid >= close ? resistancecolor : supportcolor, style=Lstyle, width=linewidth))

f_crossed_over() =>
    ret = false
    for x = 0 to array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na by 1
        float mid = math.round_to_mintick((array.get(sr_up_level, x) + array.get(sr_dn_level, x)) / 2)
        if close[1] <= mid and close > mid
            ret := true
            ret
    ret

f_crossed_under() =>
    ret = false
    for x = 0 to array.size(sr_up_level) > 0 ? array.size(sr_up_level) - 1 : na by 1
        float mid = math.round_to_mintick((array.get(sr_up_level, x) + array.get(sr_dn_level, x)) / 2)
        if close[1] >= mid and close < mid
            ret := true
            ret
    ret

alertcondition(f_crossed_over(), title='Resistance Broken', message='Resistance Broken')
alertcondition(f_crossed_under(), title='Support Broken', message='Support Broken')

// End of the combined script
Editor is loading...
Leave a Comment