Multiple Moving Average Toolkit
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © traderview2

//03/23/23 - added ribbon functionality along with a value table 
//04/02/23 - cleaned up user inputs and added interative MA length to table
//04/06/23 - added on/off functionality for each MA & user changing table location 

//@version=5
indicator("Moving Average Explorer", overlay = true)

// Get User Inputs
g_a  = "Accessibility"
useRibbon = input.bool(defval = true, title = "Turn into Color Ribbon?", group = g_a)
plotCross = input.bool(defval = false, title = "Plot Crosses?", group = g_a)
drawTable = input.bool(defval = true, title = "Draw MA Value Table?", group = g_a)
ribbTrans = input.int(defval = 88, maxval = 98, minval = 55, title = "Transparency of Ribbon", group = g_a)
tabLoca   = input.string(title = "Location of Table", defval = "Top Right", options = ["Top Right", "Top Left", "Bottom Left", "Bottom Right"], group = g_a)

//Get User Input MA Lengths
g_l  = "MA Settings"
allmat = input.string(title = "Set all MA types", defval = "EMA", options = ["Disabled","EMA", "SMA", "HMA", "WMA", "DEMA", "VWMA","VWAP"], group = g_l)
maOn1  = input.bool(true, inline = "one", group = g_l, title = "")
malen1 = input.int(title ="#1 : Length", defval = 20,  minval = 1, step = 10, inline = "one", group = g_l)
mat1   = input.string(title = "Type",   defval = "EMA",      options = ["EMA", "SMA", "HMA", "WMA", "DEMA", "VWMA","VWAP"], inline = "one", group = g_l)
maOn2  = input.bool(true, inline = "two", group = g_l, title = "")
malen2 = input.int(title ="#2 : Length", defval = 50, minval = 0, step = 10, inline = "two", group = g_l)
mat2   = input.string(title = "Type",   defval = "EMA",      options = ["EMA", "SMA", "HMA", "WMA", "DEMA", "VWMA","VWAP"], inline = "two", group = g_l)
maOn3  = input.bool(true, inline = "three", group = g_l, title = "")
malen3 = input.int(title ="#3 : Length", defval = 100, minval = 0, step = 10, inline = "three", group = g_l)
mat3   = input.string(title = "Type",   defval = "EMA",      options = ["EMA", "SMA", "HMA", "WMA", "DEMA", "VWMA","VWAP"], inline = "three", group = g_l)
maOn4 = input.bool(true, inline = "four", group = g_l, title = "")
malen4 = input.int(title ="#4 : Length", defval = 200,   minval = 0, step = 10, inline = "four", group = g_l)
mat4   = input.string(title = "Type",   defval = "EMA",      options = ["EMA", "SMA", "HMA", "WMA", "DEMA", "VWMA","VWAP"], inline = "four", group = g_l)
maOn5  = input.bool(true, inline = "five", group = g_l, title = "")
malen5 = input.int(title ="#5 : Length", defval = 300,   minval = 0, step = 10, inline = "five", group = g_l)
mat5   = input.string(title = "Type",   defval = "EMA",      options = ["EMA", "SMA", "HMA", "WMA", "DEMA", "VWMA","VWAP"], inline = "five", group = g_l)

//Get user inputed MA
getMA(_matype, _malen) => 
    if _malen == 0
        na
    else 
        switch allmat == "Disabled" ? _matype : allmat
            "SMA" => ta.sma(close,_malen)
            "HMA" => ta.hma(close,_malen)
            "WMA" => ta.wma(close,_malen)
            "VWMA" => ta.vwma(close,_malen)
            "VWAP" => ta.vwap
            "DEMA" => 
                e1 = ta.ema(close, _malen)
                e2 = ta.ema(e1, _malen)
                2 * e1 - e2
            => ta.ema(close,_malen)

//Get MAs
ma1 = getMA(mat1, malen1)
ma2 = getMA(mat2, malen2)
ma3 = getMA(mat3, malen3)
ma4 = getMA(mat4, malen4)
ma5 = getMA(mat5, malen5)
atr = ta.atr(14)

//Set color filter
bullCross = ma1 > ma5 
bearCross = ma5 > ma1
col = bullCross ? color.new(color.lime, ribbTrans) : bearCross ? color.new(color.red, ribbTrans) : na
colb = useRibbon and bullCross ? color.new(color.lime, 50) : bearCross ? color.new(color.red, 50) : na


// Define trend
longBullx  = ma1 > ma5 and not (ma1 > ma5)[1]
shortBearx = ma5 > ma1 and not (ma5 > ma1)[1]

//Enter postions
if longBullx
    alert("Bullish Cross Detected", freq = alert.freq_once_per_bar_close)
if shortBearx
    alert("Bearish Cross Detected", freq = alert.freq_once_per_bar_close)

//Draw MAs
one   = plot(maOn1 ? ma1 : na, color = useRibbon ? col : color.red, linewidth = 1, title = "MA #1", display = display.all - display.status_line)
two   = plot(maOn2 ? ma2 : na, color = useRibbon ? col :color.orange, linewidth = 1, title = "MA #2", display = display.all - display.status_line)
three = plot(maOn3 ? ma3 : na, color = useRibbon ? col :color.yellow, linewidth = 2, title = "MA #3", display = display.all - display.status_line)
four  = plot(maOn4 ? ma4 : na, color = useRibbon ? col :color.green, linewidth = 2, title = "MA #4", display = display.all - display.status_line)
five  = plot(maOn5 ? ma5 : na, color = useRibbon ? col :color.blue, linewidth = 3, title = "MA #5", display = display.all - display.status_line)

// Fill MAs
fill(one, two, color = useRibbon ? col : na)
fill(two, three, color = useRibbon ? col : na)
fill(three, four, color = useRibbon ? col : na)
fill(four, five, color = useRibbon ? col : na)

// Plot Crosses
plotshape(plotCross ? longBullx : na, title = "Long", style = shape.xcross, location = location.bottom, size = size.normal, color= color.lime)
plotshape(plotCross ? shortBearx : na, title = "Short", style = shape.xcross, location = location.top, size = size.normal, color= color.red)

// Create Bull / Bear Text
string buorbe = bullCross ? "Bullish Trend" : bearCross ? "Bearish Trend" : na

// Create Location selections
location = tabLoca == "Top Right" ? position.top_right : tabLoca == "Top Left" ? position.top_left : tabLoca == "Bottom Left" ? position.bottom_left : tabLoca == "Bottom Right" ? position.bottom_right : na

// Draw Value Table
if drawTable
    maValues = table.new(location, 1, 7, bgcolor = colb, border_color = color.white, frame_color = color.white, frame_width = 2, border_width = 1)
    table.cell(maValues, 0, 0, text = buorbe + " ", text_color = color.white, text_size = size.large)
    table.cell(maValues, 0, 1, text = str.tostring(malen1) + " " + allmat + ": " + str.tostring(ma1, format = format.mintick) + " ", text_color = useRibbon ? color.white : color.rgb(0, 0, 0), text_halign = text.align_left, bgcolor = useRibbon ? colb : color.red)
    table.cell(maValues, 0, 2, text = str.tostring(malen2) + "-" + allmat + ": " + str.tostring(ma2, format = format.mintick) + " ", text_color = useRibbon ? color.white : color.rgb(0, 0, 0), text_halign = text.align_left, bgcolor = useRibbon ? colb : color.orange)
    table.cell(maValues, 0, 3, text = str.tostring(malen3) + "-" + allmat + ": " + str.tostring(ma3, format = format.mintick) + " ", text_color = useRibbon ? color.white : color.rgb(0, 0, 0), text_halign = text.align_left, bgcolor = useRibbon ? colb : color.yellow)
    table.cell(maValues, 0, 4, text = str.tostring(malen4) + "-" + allmat + ": " + str.tostring(ma4, format = format.mintick) + " ", text_color = useRibbon ? color.white : color.rgb(0, 0, 0), text_halign = text.align_left, bgcolor = useRibbon ? colb : color.green)
    table.cell(maValues, 0, 5, text = str.tostring(malen5) + "-" + allmat + ": " + str.tostring(ma5, format = format.mintick) + " ", text_color = useRibbon ? color.white : color.rgb(0, 0, 0), text_halign = text.align_left, bgcolor = useRibbon ? colb : color.blue)
    table.cell(maValues, 0, 6, text = " ATR: " + str.tostring(atr, format = format.mintick) + " ", text_color = color.white, bgcolor = color.rgb(0, 0, 0))