Colorful Moving Averages
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © faytterro

//@version=5
indicator("Colorful Moving Averages", overlay = true)

////////////
a1 = input(0.7, "T3 Volume Factor", group = "Tillson T3 Settings ")

T3(source,length)=>
    e1 = ta.ema((high + low + 2 * close) / 4, length)
    e2 = ta.ema(e1, length)
    e3 = ta.ema(e2, length)
    e4 = ta.ema(e3, length)
    e5 = ta.ema(e4, length)
    e6 = ta.ema(e5, length)
    c1 = -a1 * a1 * a1
    c2 = 3 * a1 * a1 + 3 * a1 * a1 * a1
    c3 = -6 * a1 * a1 - 3 * a1 - 3 * a1 * a1 * a1
    c4 = 1 + 3 * a1 + a1 * a1 * a1 + 3 * a1 * a1
    T3 = c1 * e6 + c2 * e5 + c3 * e4 + c4 * e3

/////////////////
ma(source, length, type) =>
    switch type
        "T3" => T3(source, length)
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
        "LSMA" => ta.linreg(source,length,0)
        "HLM" => ta.hma(source, length)
maTypeInput = input.string("T3", title="Moving Average Type", options=["T3","SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA","LSMA", "HLM"], group = "general settings for all indicators.")
LengthInput = input.int(8, minval=1, title="Length", group = "general settings for all indicators.")
SourceInput = input.source(close, "Source", group = "general settings for all indicators.")
w = input.int(2, "width", group = "general settings for all indicators.")
MA = ma(SourceInput, LengthInput, maTypeInput)
d = ta.change(MA)
g = ta.change(d)
ad = ta.cum(math.abs(d))/bar_index
ag = ta.cum(math.abs(g))/bar_index
while ad<1
    ad:= ad*10
    d:= d*10
while ag<1
    ag:= ag*10
    g:= g*10
ck = 1//input.float(1,"Contrast", minval = 1, step = 0.5)
fd(x,y) =>
    100-200/(math.pow(1+ck/y,x)+1)
delta = fd(d,ad)
gamma = fd(g,ag)
//plot(delta)
//plot(gamma, color = color.white)
f = 2.55*delta/2+50*2.55
plot(MA, "CMA", color=color.rgb(math.min(510-2*f,255),math.min(2*f,255),0), linewidth = w)
var tbl = table.new(position.top_right, 2, 2, color.rgb(120, 123, 134, 100), color.rgb(0,0,0,100), 1, color.rgb(255, 255, 255,100), 5)
if barstate.islast
    table.cell(tbl, 1, 0, str.tostring(math.round(delta*100)/100), height = 6, width = 9,
     text_color = d<0? color.rgb(255, 255, 255,0) : color.rgb(0,0,0,0), bgcolor = d>0? color.rgb(50, 255, 50,50) : color.rgb(255, 0, 0,50), text_size = size.large) //table_id = testTable, column = 0, row = 0, text = "Open is " + str.tostring(open))
    table.cell(tbl, 1, 1, str.tostring(math.round(gamma*100)/100), height = 6,  width = 9,
     text_color = g<0? color.rgb(255, 255, 255,0) : color.rgb(0,0,0,0), bgcolor =  g>0? color.rgb(50, 255, 50,50) : color.rgb(255, 0, 0,50), text_size = size.large)
    table.cell(tbl, 0, 0, "Δ", height = 6,
     text_color = d<0? color.rgb(255, 255, 255,0) : color.rgb(0,0,0,0), bgcolor = d>0? color.rgb(50, 255, 50,50) : color.rgb(255, 0, 0,50), text_size = size.huge) //table_id = testTable, column = 0, row = 0, text = "Open is " + str.tostring(open))
    table.cell(tbl, 0, 1,"Γ", height = 6,
     text_color = g<0? color.rgb(255, 255, 255,0) : color.rgb(0,0,0,0), bgcolor = g>0? color.rgb(50, 255, 50,50) : color.rgb(255, 0, 0,50), text_size = size.huge)

buy = ta.crossover(d,0)
sell = ta.crossunder(d,0)
plotshape(buy, "local minimum", shape.diamond, location.belowbar, color.green, 0, "local minimum", color.gray, true, size = size.small)
plotshape(sell, "local maximum", shape.diamond, location.abovebar, color.red, 0, "local maximum", color.gray, true, size = size.small)
alertcondition(buy, "local minimum")
alertcondition(sell, "local maximum")