Function Geometric Moving Average

Using a formula that is generally used for calculating investment over time to check gains on a commodity.

Geometric mean as described here: www.investopedia.com...06/geometricmean.asp

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.


The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?
study(title='Function Geometric Moving Average', overlay=true)
//  ||-----------------------------------------------------------------------------------------||
//  ||---   As described here:
//  ||      http://www.investopedia.com/ask/answers/06/geometricmean.asp
//  ||-----------------------------------------------------------------------------------------||

src = input(close)
length = input(10)

f_gma(_src, _length)=>
    _length_adjusted = _length < 1 ? 0 : _length
    _perc_change = change(_src) / _src
    _sum = 1
    for _i = 0 to (_length_adjusted)
        _sum := _sum * (1 + _perc_change[_i])
    _return = nz(_return[1], _src) + _src * (pow(_sum, (1 / _length_adjusted)) - 1)

//plot(ema(src, length), color=color(gray, 0), linewidth=5)
plot(f_gma(src, length), color=color(blue, 0), linewidth=1)