Pythagorean Moving Averages (and more)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © OztheWoz

//@version=4
study("Pythagorean Averages", overlay=true, max_bars_back=250)



//////////////////
////  INPUTS  ////
//////////////////



mmaonf = input(true, "Harmonic MA", group="🏮 On/Off 🏮")
gmaonf = input(true, "Geometric MA", group="🏮 On/Off 🏮")
amaonf = input(true, "Arithmetic MA", group="🏮 On/Off 🏮")
qmaonf = input(true, "Quadratic MA", group="🏮 On/Off 🏮")
cmaonf = input(true, "Cubic MA", group="🏮 On/Off 🏮")

mmasrc = input(close, "Harmonic MA Source", group="🌌 Source 🌌")
gmasrc = input(close, "Geometric MA Source", group="🌌 Source 🌌")
amasrc = input(close, "Arithmetic MA Source", group="🌌 Source 🌌")
qmasrc = input(close, "Quadratic MA Source", group="🌌 Source 🌌")
cmasrc = input(close, "Cubic MA Source", group="🌌 Source 🌌")

mmalen = input(200, "Harmonic MA Length", group = "🥮 Length 🥮")
gmalen = input(200, "Geometric MA Length", group = "🥮 Length 🥮")
amalen = input(200, "Arithmetic MA Length", group = "🥮 Length 🥮")
qmalen = input(200, "Quadratic MA Length", group = "🥮 Length 🥮")
cmalen = input(200, "Cubic MA Length", group = "🥮 Length 🥮")


    


/////////////////////////
////  HARMONIC MEAN  ////
/////////////////////////
// p = -1



mma(hsrc, hlen) =>
    hkey = 1/hsrc
    for i = 1 to (hlen - 1)
        hkey := hkey + (1/hsrc)[i]
    avg = hkey/hlen
    hend = 1/avg
    hend
    
    
    
//////////////////////////
//// GEOMETRICAL MEAN ////
//////////////////////////
// p = 0



nroot(nsrc, nlen) =>
    num = nsrc
    sqrt = pow(num, 1/nlen)
    sqrt

gma(gsrc, glen) =>
    gkey = gsrc
    for i = 1 to (glen - 1)
        gkey := gkey * gsrc[i]
    nroot(gkey, glen)
    
    
    
/////////////////////////////
////  ARITHMETICAL MEAN  ////
/////////////////////////////
// p = 1
// NOTE: this is the same as the sma(src, len) function, the purpose
//       of this was to show how this function existed.



ama(asrc, alen) =>
    akey = asrc
    for i = 1 to (alen - 1)
        akey := akey + asrc[i]
    avg = akey/(alen)
    avg
    
    
    
//////////////////////////
////  QUADRATIC MEAN  ////
//////////////////////////
// p = 2



qma(qsrc, qlen) =>
    qkey = pow(qsrc, 2)
    for i = 1 to (qlen - 1)
        qkey := qkey + (pow(qsrc, 2))[i]
    qavg = qkey/qlen
    qend = nroot(qavg, 2)
    qend



//////////////////////
////  CUBIC MEAN  ////
//////////////////////
// p = 2



cma(csrc, clen) =>
    ckey = pow(csrc, 3)
    for i = 1 to (clen - 1)
        ckey := ckey + (pow(csrc, 3))[i]
    cavg = ckey/clen
    cend = nroot(cavg, 3)
    cend
    
    

/////////////////
////  PLOTS  ////
/////////////////



plot(mmaonf ? (mma(mmasrc, mmalen)) : na, color=color.fuchsia)
plot(gmaonf ? (gma(gmasrc, gmalen)) : na, color=color.olive)
plot(amaonf ? (ama(amasrc, amalen)) : na, color=color.aqua)
plot(qmaonf ? (qma(qmasrc, qmalen)) : na, color=color.yellow)
plot(cmaonf ? (cma(cmasrc, cmalen)) : na, color=color.red)



