Fibonacci MAs
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © QuantNomad
 
//@version=5
indicator("Fibonacci MAs", overlay = true)

// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// |                                   INPUT                                    |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|

maType    = input.string( "SMA", title = "Fibonacci Moving Average Type", options = ["SMA", "EMA", "WMA", "HMA", "VWMA", "SMMA", "DEMA"])

maSrce    = input.source(close, title = "Moving Average Source")
showOnly  = input.int(17, "Enable The First MA Lines Only", minval = 0, maxval = 17)

// Moving Averages 
ma2Val    = input.int(2,    "", minval = 1,    inline = "1", group = "Moving Averages")
ma2Col    = input.color(#fafa6e, "",         inline = "1", group = "Moving Averages")
ma2Shw    = input.bool(true, " ",              inline = "1", group = "Moving Averages")

ma3Val    = input.int(3,    "", minval = 1,    inline = "1", group = "Moving Averages")
ma3Col    = input.color(#dcf768, "",         inline = "1", group = "Moving Averages")
ma3Shw    = input.bool(true, "",               inline = "1", group = "Moving Averages",
             tooltip = "The Check Mark to Show/Hide Line, and it's Still Included in Calulation for Average Fibonacci of MAs Line")

ma5Val    = input.int(5,    "", minval = 1,    inline = "2", group = "Moving Averages")
ma5Col    = input.color(#bef363, "",         inline = "2", group = "Moving Averages")
ma5Shw    = input.bool(true, " ",              inline = "2", group = "Moving Averages")

ma8Val    = input.int(8,    "", minval = 1,    inline = "2", group = "Moving Averages")
ma8Col    = input.color(#9ef05d, "",         inline = "2", group = "Moving Averages")
ma8Shw    = input.bool(true, "",               inline = "2", group = "Moving Averages")

ma13Val   = input.int(13,    "", minval = 1,   inline = "3", group = "Moving Averages")
ma13Col   = input.color(#7eeb58, "",         inline = "3", group = "Moving Averages")
ma13Shw   = input.bool(true, " ",              inline = "3", group = "Moving Averages")

ma21Val   = input.int(21,    "", minval = 1,   inline = "3", group = "Moving Averages")
ma21Col   = input.color(#5ee754, "",         inline = "3", group = "Moving Averages")
ma21Shw   = input.bool(true, "",               inline = "3", group = "Moving Averages")

ma34Val   = input.int(34,    "", minval = 1,   inline = "4", group = "Moving Averages")
ma34Col   = input.color(#4fe260, "",         inline = "4", group = "Moving Averages")
ma34Shw   = input.bool(true, " ",              inline = "4", group = "Moving Averages")

ma55Val   = input.int(55,    "", minval = 1,   inline = "4", group = "Moving Averages")
ma55Col   = input.color(#4cdd77, "",         inline = "4", group = "Moving Averages")
ma55Shw   = input.bool(true, "",               inline = "4", group = "Moving Averages")

ma89Val   = input.int(89,    "", minval = 1,   inline = "5", group = "Moving Averages")
ma89Col   = input.color(#48d88e, "",         inline = "5", group = "Moving Averages")
ma89Shw   = input.bool(true, " ",              inline = "5", group = "Moving Averages")

ma144Val  = input.int(144,    "", minval = 1,  inline = "5", group = "Moving Averages")
ma144Col  = input.color(#45d2a4, "",         inline = "5", group = "Moving Averages")
ma144Shw  = input.bool(true, "",               inline = "5", group = "Moving Averages")

ma233Val  = input.int(233,    "", minval = 1,  inline = "6", group = "Moving Averages")
ma233Col  = input.color(#42ccb8, "",         inline = "6", group = "Moving Averages")
ma233Shw  = input.bool(true, " ",              inline = "6", group = "Moving Averages")

ma377Val  = input.int(377,    "", minval = 1,  inline = "6", group = "Moving Averages")
ma377Col  = input.color(#3fbfc5, "",         inline = "6", group = "Moving Averages")
ma377Shw  = input.bool(true, "",               inline = "6", group = "Moving Averages")

ma610Val  = input.int(610,    "", minval = 1,  inline = "7", group = "Moving Averages")
ma610Col  = input.color(#3fa0bd, "",         inline = "7", group = "Moving Averages")
ma610Shw  = input.bool(true, " ",              inline = "7", group = "Moving Averages")

ma987Val  = input.int(987,    "", minval = 1,  inline = "7", group = "Moving Averages")
ma987Col  = input.color(#4183b2, "",         inline = "7", group = "Moving Averages")
ma987Shw  = input.bool(true, "",               inline = "7", group = "Moving Averages")

ma1597Val = input.int(1597,    "", minval = 1, inline = "8", group = "Moving Averages")
ma1597Col = input.color(#426aa7, "",         inline = "8", group = "Moving Averages")
ma1597Shw = input.bool(true, " ",              inline = "8", group = "Moving Averages")

ma2584Val = input.int(2584,    "", minval = 1, inline = "8", group = "Moving Averages")
ma2584Col = input.color(#44579c, "",         inline = "8", group = "Moving Averages")
ma2584Shw = input.bool(true, "",               inline = "8", group = "Moving Averages")

ma4181Val = input.int(4181,    "", minval = 1, inline = "9", group = "Moving Averages")
ma4181Col = input.color(#454792, "",         inline = "9", group = "Moving Averages")
ma4181Shw = input.bool(true, " ",              inline = "9", group = "Moving Averages")

 
aveShw    = input.bool(true, "Show Average Fibonacci of MAs Line", 
                                                      inline = "0", group = "Moving Averages")
aveCol    = input.color(color.rgb(248, 61, 15), "", inline = "0", group = "Moving Averages")


// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
// |                                CALCULATION                                 |
// |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|

maType(len) => 
    switch maType
        "SMA"   =>  ta.sma (maSrce, len)
        "EMA"   =>  ta.ema (maSrce, len)
        "WMA"   =>  ta.wma (maSrce, len)
        "HMA"   =>  ta.hma (maSrce, len)
        "VWMA"  =>  ta.vwma(maSrce, len)
        "SMMA"  =>  
            smma = 0.0, sma = ta.sma(maSrce, len)
            smma := na(smma[1]) ? sma : (smma[1] * (len - 1) + maSrce) / len
        "DEMA"  =>  2 * ta.ema(maSrce, len) - ta.ema(ta.ema(maSrce, len),  len)
        => na


fibMA(num, len) =>
    ma = float(na)
    if num <= showOnly 
        ma := maType(len)
    
    ma


// Plot MAs lines
plot(fibMA( 1, ma2Val   ), title = "MA №01", color = ma2Shw    ? ma2Col    : color.new(ma2Col,    100))
plot(fibMA( 2, ma3Val   ), title = "MA №02", color = ma3Shw    ? ma3Col    : color.new(ma3Col,    100))
plot(fibMA( 3, ma5Val   ), title = "MA №03", color = ma5Shw    ? ma5Col    : color.new(ma5Col,    100))
plot(fibMA( 4, ma8Val   ), title = "MA №04", color = ma8Shw    ? ma8Col    : color.new(ma8Col,    100))
plot(fibMA( 5, ma13Val  ), title = "MA №05", color = ma13Shw   ? ma13Col   : color.new(ma13Col,   100))
plot(fibMA( 6, ma21Val  ), title = "MA №06", color = ma21Shw   ? ma21Col   : color.new(ma21Col,   100))
plot(fibMA( 7, ma34Val  ), title = "MA №07", color = ma34Shw   ? ma34Col   : color.new(ma34Col,   100))
plot(fibMA( 8, ma55Val  ), title = "MA №08", color = ma55Shw   ? ma55Col   : color.new(ma55Col,   100))
plot(fibMA( 9, ma89Val  ), title = "MA №09", color = ma89Shw   ? ma89Col   : color.new(ma89Col,   100))
plot(fibMA(10, ma144Val ), title = "MA №10", color = ma144Shw  ? ma144Col  : color.new(ma144Col,  100))
plot(fibMA(11, ma233Val ), title = "MA №11", color = ma233Shw  ? ma233Col  : color.new(ma233Col,  100))
plot(fibMA(12, ma377Val ), title = "MA №12", color = ma377Shw  ? ma377Col  : color.new(ma377Col,  100))
plot(fibMA(13, ma610Val ), title = "MA №13", color = ma610Shw  ? ma610Col  : color.new(ma610Col,  100))
plot(fibMA(14, ma987Val ), title = "MA №14", color = ma987Shw  ? ma987Col  : color.new(ma987Col,  100))
plot(fibMA(15, ma1597Val), title = "MA №15", color = ma1597Shw ? ma1597Col : color.new(ma1597Col, 100))
plot(fibMA(16, ma2584Val), title = "MA №16", color = ma2584Shw ? ma2584Col : color.new(ma2584Col, 100))
plot(fibMA(17, ma4181Val), title = "MA №17", color = ma4181Shw ? ma4181Col : color.new(ma4181Col, 100))



// Calculate The Average Fibonacci of MAs Lines
avgFibMA() =>
    avgMA = float(na)
    if aveShw and showOnly > 0
        maArray = array.from(fibMA( 1, ma2Val   ), fibMA( 2, ma3Val   ),
                             fibMA( 3, ma5Val   ), fibMA( 4, ma8Val   ),
                             fibMA( 5, ma13Val  ), fibMA( 6, ma21Val  ),
                             fibMA( 7, ma34Val  ), fibMA( 8, ma55Val  ),
                             fibMA( 9, ma89Val  ), fibMA(10, ma144Val ),
                             fibMA(11, ma233Val ), fibMA(12, ma377Val ),
                             fibMA(13, ma610Val ), fibMA(14, ma987Val ),
                             fibMA(15, ma1597Val), fibMA(16, ma2584Val),
                             fibMA(17, ma4181Val))

        k = 0
        if array.size(maArray) > 0
            while k != array.size(maArray)
                if na(array.get(maArray, k))
                    array.remove(maArray, k)
                else 
                    k += 1
        
        avgMA := array.sum(maArray)/array.size(maArray)
    
    avgMA


plot(avgFibMA(), title = "Average Fibonacci Lines", color = aveCol, linewidth = 3)
