Democratic Fibonacci Moving Averages
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LeafAlgo

//@version=5
indicator("Democratic Fibonacci MAs", overlay=true)

src = input.source(ohlc4, 'Source')

// Fib MA
cfib_ma_3 = ta.ema(src, 3)
plot(cfib_ma_3, title='Fib. MA (3)', color=color.white, linewidth=2)

cfib_ma_5 = ta.ema(src, 5)
plot(cfib_ma_5, title='Fib. MA (5)')

cfib_ma_8 = ta.ema(src, 8)
plot(cfib_ma_8, title='Fib. MA (8)')

cfib_ma_13 = ta.ema(src, 13)
plot(cfib_ma_13, title='Fib. MA (13)')

cfib_ma_21 = ta.ema(src, 21)
plot(cfib_ma_21, title='Fib. MA (21)')

cfib_ma_34 = ta.ema(src, 34)
plot(cfib_ma_34, title='Fib. MA (34)')

cfib_ma_55 = ta.ema(src, 55)
plot(cfib_ma_55, title='Fib. MA (55)')

cfib_ma_89 = ta.ema(src, 89)
plot(cfib_ma_89, title='Fib. MA (89)')

cfib_ma_144 = ta.ema(src, 144)
plot(cfib_ma_144, title='Fib. MA (144)')

cfib_ma_233 = ta.ema(src, 233)
plot(cfib_ma_233, title='Fib. MA (233)', color=color.white, linewidth=2)


fib_ohlc4 = (cfib_ma_3 + cfib_ma_5 + cfib_ma_8 + cfib_ma_13 + cfib_ma_21 + cfib_ma_34 + cfib_ma_55 + cfib_ma_89 + cfib_ma_144 + cfib_ma_233) / 10
plot(fib_ohlc4, 'DFMA', color=(fib_ohlc4 > cfib_ma_233) ? color.lime : color.red, linewidth=4)


var string GP2 = 'Display'
string  tableYposInput = input.string("bottom", "Panel position", inline = "11", options = ["top", "middle", "bottom"], group = GP2)
string  tableXposInput = input.string("right", "", inline = "11", options = ["left", "center", "right"], group = GP2)
color   bullColorInput = input.color(color.new(color.green, 20), "Bull", inline = "12", group = GP2)
color   bearColorInput = input.color(color.new(color.red, 20), "Bear", inline = "12", group = GP2)
color   neutColorInput = input.color(color.new(color.gray, 20), "Neutral", inline = "12", group = GP2)

var table panel = table.new(tableYposInput + "_" + tableXposInput, 2, 12)
if barstate.islast
    table.cell(panel, 0, 0, 'Fib. Number', bgcolor = neutColorInput, text_color = color.black)
    table.cell(panel, 1, 0, 'MA Value', bgcolor = neutColorInput, text_color = color.black)

    bgc1 = close > cfib_ma_3 ? open < cfib_ma_3 ? neutColorInput : bullColorInput : open > cfib_ma_3 ? neutColorInput : bearColorInput
    table.cell(panel, 0, 1, str.tostring(3), bgcolor = bgc1, text_color = color.black)
    table.cell(panel, 1, 1, str.tostring(cfib_ma_3, format.mintick), bgcolor = bgc1, text_color = color.black)

    bgc2 = close > cfib_ma_5 ? open < cfib_ma_5 ? neutColorInput : bullColorInput : open > cfib_ma_5 ? neutColorInput : bearColorInput
    table.cell(panel, 0, 2, str.tostring(5), bgcolor = bgc2, text_color = color.black)
    table.cell(panel, 1, 2, str.tostring(cfib_ma_5, format.mintick), bgcolor = bgc2, text_color = color.black)

    bgc3 = close > cfib_ma_8 ? open < cfib_ma_8 ? neutColorInput : bullColorInput : open > cfib_ma_8 ? neutColorInput : bearColorInput
    table.cell(panel, 0, 3, str.tostring(8), bgcolor = bgc3, text_color = color.black)
    table.cell(panel, 1, 3, str.tostring(cfib_ma_8, format.mintick), bgcolor = bgc3, text_color = color.black)

    bgc4 = close > cfib_ma_13 ? open < cfib_ma_13 ? neutColorInput : bullColorInput : open > cfib_ma_13 ? neutColorInput : bearColorInput
    table.cell(panel, 0, 4, str.tostring(13), bgcolor = bgc4, text_color = color.black)
    table.cell(panel, 1, 4, str.tostring(cfib_ma_13, format.mintick), bgcolor = bgc4, text_color = color.black)

    bgc5 = close > cfib_ma_21 ? open < cfib_ma_21 ? neutColorInput : bullColorInput : open > cfib_ma_21 ? neutColorInput : bearColorInput
    table.cell(panel, 0, 5, str.tostring(21), bgcolor = bgc5, text_color = color.black)
    table.cell(panel, 1, 5, str.tostring(cfib_ma_21, format.mintick), bgcolor = bgc5, text_color = color.black)

    bgc6 = close > cfib_ma_34 ? open < cfib_ma_34 ? neutColorInput : bullColorInput : open > cfib_ma_34 ? neutColorInput : bearColorInput
    table.cell(panel, 0, 6, str.tostring(34), bgcolor = bgc6, text_color = color.black)
    table.cell(panel, 1, 6, str.tostring(cfib_ma_34, format.mintick), bgcolor = bgc6, text_color = color.black)

    bgc7 = close > cfib_ma_55 ? open < cfib_ma_55 ? neutColorInput : bullColorInput : open > cfib_ma_55 ? neutColorInput : bearColorInput
    table.cell(panel, 0, 7, str.tostring(55), bgcolor = bgc7, text_color = color.black)
    table.cell(panel, 1, 7, str.tostring(cfib_ma_55, format.mintick), bgcolor = bgc7, text_color = color.black)

    bgc8 = close > cfib_ma_89 ? open < cfib_ma_89 ? neutColorInput : bullColorInput : open > cfib_ma_89 ? neutColorInput : bearColorInput
    table.cell(panel, 0, 8, str.tostring(89), bgcolor = bgc8, text_color = color.black)
    table.cell(panel, 1, 8, str.tostring(cfib_ma_89, format.mintick), bgcolor = bgc8, text_color = color.black)

    bgc9 = close > cfib_ma_144 ? open < cfib_ma_144 ? neutColorInput : bullColorInput : open > cfib_ma_144 ? neutColorInput : bearColorInput
    table.cell(panel, 0, 9, str.tostring(144), bgcolor = bgc9, text_color = color.black)
    table.cell(panel, 1, 9, str.tostring(cfib_ma_144, format.mintick), bgcolor = bgc9, text_color = color.black)

    bgc10 = close > cfib_ma_233 ? open < cfib_ma_233 ? neutColorInput : bullColorInput : open > cfib_ma_233 ? neutColorInput : bearColorInput
    table.cell(panel, 0, 10, str.tostring(233), bgcolor = bgc10, text_color = color.black)
    table.cell(panel, 1, 10, str.tostring(cfib_ma_233, format.mintick), bgcolor = bgc10, text_color = color.black)

    bgc11 = close > fib_ohlc4 ? open < fib_ohlc4 ? neutColorInput : bullColorInput : open > fib_ohlc4 ? neutColorInput : bearColorInput
    table.cell(panel, 0, 11, 'DFMA', bgcolor = bgc11, text_color = color.black)
    table.cell(panel, 1, 11, str.tostring(fib_ohlc4, format.mintick), bgcolor = bgc11, text_color = color.black)