All for One Moving Average
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © darkbrewery
// @version=5
// Thank-you: SGJoe and DTKing for their help with the switch! kurtisbu for the hand-holding! Franklin Moormann (cheatcountry) for the Ehlers & Kaufman MA

indicator("All for One Moving Average", "A41MA", true)

///// Inputs /////
MA_TF    = input.timeframe  ('','TimeFrame', inline='L1')
Repaint  = input            (false,'Repainting', inline='L1')
Use_HA   = input            (false,'Heikin Ashi',  inline='L1')
MA_Type  = input.string     ("EMA", 'Type', ["SMA","EMA","DEMA","TEMA","QEMA","WMA","VWMA","HMA","SWMA","ALMA","LMA","RMA","WWMA","TMA","EKMA"], inline="L2")
MA_Input = input.source     (close, 'Src', inline='L2')
MA_L1    = input.int        (20, 'Len', 1, 200, inline='L2')
MA_Off1  = input.float      (0.85, 'Offset', 0, 3, 0.01, inline='L3')
MA_Sig1  = input.int        (6, 'Σ', 0, 10, tooltip="Offset & Σ only apply to ALMA", inline='L3')
MA_S1    = request.security (Use_HA?ticker.heikinashi(syminfo.tickerid):syminfo.tickerid, MA_TF, MA_Input[Repaint?0:barstate.isrealtime?1:0])[Repaint?0:barstate.isrealtime?0:1]

///// Calculate Moving Average /////
MA_1 = switch MA_Type
    "SMA"  => ta.sma    (MA_S1,MA_L1)
    "EMA"  => ta.ema    (MA_S1,MA_L1)
    "DEMA" => 2* ta.ema (MA_S1, MA_L1) - ta.ema(ta.ema(MA_S1, MA_L1),  MA_L1)
    "TEMA" => 3*(ta.ema (MA_S1, MA_L1) - ta.ema(ta.ema(MA_S1, MA_L1), MA_L1)) + ta.ema(ta.ema(ta.ema(MA_S1, MA_L1), MA_L1), MA_L1)
    "QEMA" => 5* ta.ema (MA_S1,MA_L1) - 10 * ta.ema(ta.ema(MA_S1, MA_L1), MA_L1) + 10 * ta.ema(ta.ema(ta.ema(MA_S1, MA_L1), MA_L1), MA_L1) - 5 * ta.ema(ta.ema(ta.ema(ta.ema(MA_S1, MA_L1), MA_L1), MA_L1), MA_L1) + ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(MA_S1, MA_L1), MA_L1), MA_L1), MA_L1), MA_L1)
    "WMA"  => ta.wma    (MA_S1,MA_L1)
    "VWMA" => ta.vwma   (MA_S1,MA_L1)
    "HMA"  => ta.hma    (MA_S1,MA_L1)
    "SWMA" => ta.swma   (MA_S1)
    "ALMA" => ta.alma   (MA_S1, MA_L1, MA_Off1, MA_Sig1)
    "LMA"  => ta.linreg (MA_S1, MA_L1, 0)
    "RMA"  => ta.rma    (MA_S1,MA_L1)
    "WWMA" =>
        Wilder_MA1 = .0
        Wilder_MA1 := 1 / MA_L1 * MA_S1 + (1 - 1 / MA_L1) * nz(Wilder_MA1[1])
    "TMA"  => ta.sma(ta.sma(MA_S1,MA_L1),MA_L1)
    "EKMA" =>
        KA_D1 = .0
        for int i = 0 to MA_L1 - 1 by 1
            KA_D1 += math.abs(nz(MA_S1[i]) - nz(MA_S1[i + 1]))
        KA_EF1 = KA_D1 != 0 ? math.min(math.abs(MA_S1 - nz(MA_S1[MA_L1 - 1])) / KA_D1, 1) : 0
        KAMA1 = .0
        KAMA1 := (math.pow((0.6667 * KA_EF1) + 0.0645, 2) * MA_S1) + ((1 - math.pow((0.6667 * KA_EF1) + 0.0645, 2)) * nz(KAMA1[1]))

///// Draw it up /////
plot(MA_1, title='A41MA', color=color.yellow)
