Democratic Fibonacci McGinley Dynamics
// 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 McGinley Dynamics", 'DFMG', overlay=true)

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


mg3 = 0.0
mg5 = 0.0
mg8 = 0.0
mg13 = 0.0
mg21 = 0.0
mg34 = 0.0
mg55 = 0.0
mg89 = 0.0
mg144 = 0.0
mg233 = 0.0

mg3 := na(mg3[1]) ? ta.ema(src, 3) : mg3[1] + (src - mg3[1]) / (3 * math.pow(src/mg3[1], 4))
plot(mg3, title='Fib. MG (3)', color=color.white, linewidth=2)

mg5 := na(mg5[1]) ? ta.ema(src, 5) : mg5[1] + (src - mg5[1]) / (5 * math.pow(src/mg5[1], 4))
plot(mg5, title='Fib. MG (5)')

mg8 := na(mg8[1]) ? ta.ema(src, 8) : mg8[1] + (src - mg8[1]) / (8 * math.pow(src/mg8[1], 4))
plot(mg8, title='Fib. MG (8)')

mg13 := na(mg13[1]) ? ta.ema(src, 13) : mg13[1] + (src - mg13[1]) / (13 * math.pow(src/mg13[1], 4))
plot(mg13, title='Fib. MG (13)')

mg21 := na(mg21[1]) ? ta.ema(src, 21) : mg21[1] + (src - mg21[1]) / (21 * math.pow(src/mg21[1], 4))
plot(mg21, title='Fib. MG (21)')

mg34 := na(mg34[1]) ? ta.ema(src, 34) : mg34[1] + (src - mg34[1]) / (34 * math.pow(src/mg34[1], 4))
plot(mg34, title='Fib. MG (34)')

mg55 := na(mg55[1]) ? ta.ema(src, 55) : mg55[1] + (src - mg55[1]) / (55 * math.pow(src/mg55[1], 4))
plot(mg55, title='Fib. MG (55)')

mg89 := na(mg89[1]) ? ta.ema(src, 89) : mg89[1] + (src - mg89[1]) / (89 * math.pow(src/mg89[1], 4))
plot(mg89, title='Fib. MG (89)')

mg144 := na(mg144[1]) ? ta.ema(src, 144) : mg144[1] + (src - mg144[1]) / (144 * math.pow(src/mg144[1], 4))
plot(mg144, title='Fib. MG (144)')

mg233 := na(mg233[1]) ? ta.ema(src, 233) : mg233[1] + (src - mg233[1]) / (233 * math.pow(src/mg233[1], 4))
plot(mg233, title='Fib. MG (233)',color=color.white, linewidth = 2)


dfmg = (mg3 + mg5 + mg8 + mg13 + mg21 + mg34 + mg55 + mg89 + mg144 + mg233) / 10
plot(dfmg, 'DFMG', color=(dfmg > mg233) ? 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, 'MG Value', bgcolor = neutColorInput, text_color = color.black)

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

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

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

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

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

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

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

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

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

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

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