Moving Average - 365/2/2/2/2 - @DaviG117
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DaviG

//@version=5
indicator("MA - 365/2/2/2/2", overlay=true, timeframe="")

ma1Input     =   input.string(title="MA Type 1 - 365", defval="EMA",options=["EMA","SMA","RMA","WMA","VWMA","HMA","KAMA","P-KAMA"], tooltip="365")
ma2Input     =   input.string(title="MA Type 2 - 182.42" , defval="EMA",options=["EMA","SMA","RMA","WMA","VWMA","HMA","KAMA","P-KAMA"], tooltip="365/2")
ma3Input     =   input.string(title="MA Type 3 - 91.25", defval="EMA",options=["EMA","SMA","RMA","WMA","VWMA","HMA","KAMA","P-KAMA"], tooltip="365/2/2")
ma4Input     =   input.string(title="MA Type 4 - 45.625", defval="EMA",options=["EMA","SMA","RMA","WMA","VWMA","HMA","KAMA","P-KAMA"], tooltip="365/2/2/2")
ma5Input     =   input.string(title="MA Type 5 - 22.8125", defval="EMA",options=["EMA","SMA","RMA","WMA","VWMA","HMA","KAMA","P-KAMA"], tooltip="365/2/2/2/2")

fillSwitch   =   input.bool(title="Fill between lines?", defval=true)

//KAMA Function
kama(_src,_len) =>
    xPrice = _src
    xvnoise = math.abs(xPrice - xPrice[1])
    nAMA = 0.0
    nfastend = 0.666
    nslowend = 0.0645
    nsignal = math.abs(xPrice - xPrice[_len])
    nnoise = math.sum(xvnoise, _len)
    nefratio = nnoise != 0 ? nsignal / nnoise : 0
    nsmooth = math.pow(nefratio * (nfastend - nslowend) + nslowend, 2) 
    nAMA := nz(nAMA[1]) + nsmooth * (xPrice - nz(nAMA[1]))
//P-Kama

pKama(_src,_len) =>
    // length = input(100),factor = input(3.),src = input(close),sp = input(false,title="Self Powered")
    //----
    factor = 3
    er = math.abs(ta.change(_src,_len))/math.sum(math.abs(ta.change(_src)),_len)
    // pow = sp ? 1/er : factor
    per = math.pow(math.abs(ta.change(_src,_len))/math.sum(math.abs(ta.change(_src)),_len),factor)
    //----
    a = 0.0
    a := per * _src + (1 - per) * nz(a[1],_src)
    //----
    // plot(a,title="P-KAMA",color=#f57f17,linewidth=2,transp=0)
    
    
len1 = 365
len2 = 365 / 2
len3 = 365 / 2 / 2
len4 = 365 / 2 / 2 / 2
len5 = 365 / 2 / 2 / 2 / 2

maFunction(_len,_type)  =>
    switch _type
        "EMA"   => ta.ema(close,    _len)
        "SMA"   => ta.sma(close,    _len)
        "RMA"   => ta.rma(close,    _len)
        "WMA"   => ta.wma(close,    _len)
        "VWMA"  => ta.vwma(close,   _len)
        "HMA"   => ta.hma(close,    _len)
        "KAMA"  => kama(close,      _len)
        "P-KAMA"=> pKama(close,     _len)
        =>
            runtime.error("No matching MA type found!")
            na
            
ma1     = maFunction(len1,ma1Input)
ma2     = maFunction(len2,ma2Input)
ma3     = maFunction(len3,ma3Input)
ma4     = maFunction(len4,ma4Input)
ma5     = maFunction(len5,ma5Input)

p1 = plot(ma1,color=color.green, linewidth=2, title="MA Type 1 - 365")
p2 = plot(ma2,color=color.rgb(255,244,157), linewidth=2, title="MA Type 2 - 182.42")
p3 = plot(ma3,color=color.rgb(255,183,77), linewidth=2, title="MA Type 3 - 91.25")
p4 = plot(ma4,color=color.rgb(247,81,95), linewidth=2, title="MA Type 4 - 45.625")
p5 = plot(ma5,color=color.rgb(242,53,69), linewidth=2, title="MA Type 5 - 22.8125")

fill(p1,p2,color= fillSwitch ? color.new(color.green,80) : na)
fill(p2,p3,color= fillSwitch ? color.rgb(255,244,157,80) : na)
fill(p3,p4,color= fillSwitch ? color.rgb(255,183,77,80) : na)
fill(p4,p5,color= fillSwitch ? color.rgb(242,53,69,80) : na)






