[MX]Moving Average - Library
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Marqx

//@version=5
library("MovingAverage_library", overlay = true)

Source = input(close,"Source")
Length = input(12,"Length")

//@function Calculates different types of moving average
//@param dema Double Exponential Moving Average
//@param tema Triple Exponential Moving Average
//@param wwma Welles Wilder Moving Average
//@param gma  Geometric Moving Average
//@returns Different Moving Average types.

export tema(float price, simple int length) =>
    tema_source1 = ta.ema(price,length)
    tema_source2 = ta.ema(tema_source1,length)
    tema_source3 = ta.ema(tema_source2,length)
    3 * (tema_source1 - tema_source2) + tema_source3

export dema(float price, simple int length) =>
    dema_source1 = ta.ema(price,length)
    dema_source2 = ta.ema(dema_source1,length)
    2 * dema_source1 - dema_source2
    
export wwma(float price, simple int length) =>
    WWMA = 0.0
    WWMA := (nz(WWMA[1]) * (length - 1) + price) / length

export gma(float price, simple int length) =>
    sum = price
    for i = 1 to length-1
        sum := sum * price[i]
    math.pow(sum,(1/length))

DEMA = dema(Source,Length)
TEMA = tema(Source,Length)
WWMA = wwma(Source,Length)
GMA = gma(Source,Length)

plot(DEMA,"Dema Moving Average",color.white,4)
plot(TEMA,"Tema Moving Average",color.white,4)
plot(WWMA,"Welles Wilder Moving Average",color.white,4)
plot(GMA,"Geometric Moving Average",color.white,4)
