RedK Ribbon v2 Extended by Loxxxa
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RedKTrader original script
// Adapted and extended by Loxxxa

//@version=4
study("RedK Ribbon Extended by Loxxxa", shorttitle = "RedKRibbon Loxxxa", precision=2, overlay=true)
length = input(title="Length", type=input.integer, defval=7, minval=1, step=1)
ribf   = input(title="Ribbon Size", defval=1.6, minval=1, step=0.2)
smooth = input(title="Smoothing", type=input.integer, defval=3, minval=1, step=1)

// Optional plots - will be hidden by default
Short_EMA = input(title="Short EMA", defval=30, minval=1, step=1)
Long_EMA  = input(title="Long EMA", defval = 50, minval=1, step=1)

// Calculations
SEMA    = ema(close, Short_EMA)
LEMA    = ema(close, Long_EMA)

CloseMA = wma(wma(close, length),smooth)
ZLMA    = (2 * CloseMA) - wma(CloseMA, length) 

length2 = int(length*ribf)    //have to make length2 an integer or the wma call fails
AvgMA   = wma(wma(hl2,length2),smooth)

// some other variables
myred   = #ff0000, mygreen = #00ff00
up      = change(ZLMA) > 0 , dn = change(ZLMA) < 0 
zup     = #2196f3, zdown = #f57f17

// Optional Plots
plot(SEMA,  title="Short EMA", color = color.yellow)
plot(LEMA,  title="Long EMA",  color = color.fuchsia)

// Main Plot
p1=plot(CloseMA, title="Bulls", color = color.green, linewidth=2, transp=50)
p2=plot(AvgMA, title="Bears", color=color.red, linewidth=2, transp=50)
fill(p1,p2,color = CloseMA > AvgMA ? mygreen : myred, transp=70)
p3=plot(ZLMA, title="Zero Lag MA", color=up ? zup : zdown, linewidth=4)

// Multiple EMAs
//shortest = ema(close, 15)
//short = ema(close, 30)
longer = ema(close, 90)
longest = wma(close, 140)

//plot(shortest, color = color.red)
//plot(short, color = color.orange)
plot(longer, title="xLong EMA (90)", color = color.white)
plot(longest, title="xLong WMA (120)", color = color.blue)


// BB
bblength = input(20, minval=1)
src = input(close, title="Source")
mult = input(2.0, minval=0.001, maxval=50, title="StdDev")
basis = sma(src, bblength)
dev = mult * stdev(src, bblength)
upper = basis + dev
lower = basis - dev
offset = input(0, "BB Offset", type = input.integer, minval = -500, maxval = 500)
plot(basis, "BB Basis", color=color.yellow, style = plot.style_circles, offset = offset)
bbp1 = plot(upper, "BB Upper", color=color.teal, offset = offset)
bbp2 = plot(lower, "BB Lower", color=color.fuchsia, offset = offset)
fill(bbp1, bbp2, title = "BB Background", color=color.rgb(33, 150, 243, 95))


// Alerts on ZLMA changing direction up or down
// TurnUp = up and dn[1], TurnDn = dn and up[1]
// alertcondition(TurnUp, title="ZLMA Up!",   message="Ribbon | Price Turning up! - {{exchange}}:{{ticker}} at {{close}}")
// alertcondition(TurnDn, title="ZLMA Down!", message="Ribbon | Price Turning Down! - {{exchange}}:{{ticker}} at {{close}}")