MA Band Distance Monitor
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HasanRifat

//@version=5
indicator("MA Band Distance Monitor", overlay = true, max_labels_count = 500)
// array for calculations
var slowMAdis = array.new_float()
var fastMAdis = array.new_float()
var maDisBtin = array.new_float()
arraySource   = close
// MA input
slowlen = input.int(200, title="Slow MA Length", minval=1, tooltip = "Slow lenght input must be greater than fast length input, otherwise indicator will produce faulty results")
fastlen = input.int(50, title="Fast MA Length", minval=1, tooltip  = "Fast lenght input must be less than slow length input, otherwise indicator will produce faulty results")
slowsrc = input(close, title="Slow MA Source")
fastsrc = input(close, title="Fast MA Source")
slowMAtype = input.string(defval = "SMA", title = "Slow MA Method", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
fastMAtype = input.string(defval = "SMA", title = "Fast MA Method", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
// table input
showTable     = input.bool(true, "Show Table")
tablePosInput = input.string(title="Position", defval="Top Right", options=["Bottom Left", "Bottom Right", "Top Left", "Top Right"], tooltip="Select where you want the table to draw.")
var tablePos  = tablePosInput == "Bottom Left" ? position.bottom_left : tablePosInput == "Bottom Right" ? position.bottom_right : tablePosInput == "Top Left" ? position.top_left : tablePosInput == "Top Right" ? position.top_right : na
//color
color1 = input.color(color.new(color.yellow    , 35), 'color 1')
color2 = input.color(color.new(color.lime    , 35), 'color 2')
color3 = input.color(color.new(color.red     , 35), 'color 3')
maBandFill  = input.bool (true,'fill')
// MA function
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
// array function
f_array(source, speedMA, arrayC) => 
    if source > speedMA
        array.push(arrayC, source - speedMA)
    if source < speedMA
        array.push(arrayC, speedMA - source)
    arrayC

slowMA = ma(slowsrc, slowlen, slowMAtype)
fastMA = ma(fastsrc, fastlen, fastMAtype)

slowMAarray = f_array(arraySource, slowMA, slowMAdis)
fastArray   = f_array(arraySource, fastMA, fastMAdis)
array.push(maDisBtin, slowMA > fastMA ? slowMA - fastMA : fastMA - slowMA)
// calculation
slowMAavgDis = array.avg(slowMAdis)
fastMAavgDis = array.avg(fastMAdis)
maAvgDisBtin = array.avg(maDisBtin)
slowMACrrDis = close > slowMA ? math.round_to_mintick(close - slowMA) : math.round_to_mintick(slowMA - close)
fastMACrrDis = close > fastMA ? math.round_to_mintick(close - fastMA) : math.round_to_mintick(fastMA - close)
maCrrDis     = slowMA > fastMA ? math.round_to_mintick(slowMA - fastMA) : math.round_to_mintick(fastMA - slowMA)

slowPlt = plot(slowMA, "Slow MA", color = na)
fastPlt = plot(fastMA, "Fast MA", color = na)

topVal = slowMA > fastMA ? math.min(slowMA, fastMA) : math.max(slowMA, fastMA)
botVal = slowMA > fastMA ? math.max(slowMA, fastMA) : math.min(slowMA, fastMA)
topClr  = maBandFill      ? slowMA > fastMA ? color1 : color2 : na
botClr  = maBandFill      ? slowMA > fastMA ? color3 : color1 : na
fill(slowPlt, fastPlt, topVal, botVal, topClr, botClr)
// table color
strongRed   = color.rgb(255, 25, 25, 25)
weakRed     = color.rgb(255, 82, 82, 25)
strongGreen = color.rgb(19, 200, 25, 25)
weakGreen   = color.rgb(76, 175, 80, 25)
blue        = color.rgb(20, 123, 134, 25)
//
var dataTable = table.new(tablePos, columns = 4, rows = 5, border_color = color.rgb(40, 40, 40, 25), border_width = 2)
if showTable
    table.cell(dataTable, 1, 1, text = "Difference:   ", bgcolor = blue, text_color = color.white)
    table.cell(dataTable, 1, 2, text = "Slow MA"+"("+str.tostring(slowlen)+")", bgcolor = blue, text_color = color.white)
    table.cell(dataTable, 1, 3, text = "Fast MA"+"("+str.tostring(fastlen)+")  ", bgcolor = blue, text_color = color.white)
    table.cell(dataTable, 1, 4, text = "Between MA ", bgcolor = blue, text_color = color.white)
    table.cell(dataTable, 2, 1, text = "Average  ", bgcolor = blue, text_color = color.white)
    table.cell(dataTable, 3, 1, text = "Current  ", bgcolor = blue, text_color = color.white)
    table.cell(dataTable, 2, 2, text = str.tostring(math.round_to_mintick(slowMAavgDis)), bgcolor = close > slowMA ? weakGreen : weakRed, text_color = color.white)
    table.cell(dataTable, 3, 2, text = str.tostring(slowMACrrDis), bgcolor = close > slowMA ? weakGreen : weakRed, text_color = color.white)
    table.cell(dataTable, 2, 3, text = str.tostring(math.round_to_mintick(fastMAavgDis)), bgcolor = close > fastMA ? weakGreen : weakRed, text_color = color.white)
    table.cell(dataTable, 3, 3, text = str.tostring(fastMACrrDis), bgcolor = close > fastMA ? weakGreen : weakRed, text_color = color.white)
    table.cell(dataTable, 2, 4, text = str.tostring(math.round_to_mintick(fastMACrrDis)), bgcolor = fastMA > slowMA ? weakGreen : weakRed, text_color = color.white)
    table.cell(dataTable, 3, 4, text = str.tostring(maCrrDis), bgcolor = fastMA > slowMA ? weakGreen : weakRed, text_color = color.white)

// alert
crossOver  = ta.crossover(fastMA, slowMA)
crossUnder = ta.crossunder(fastMA, slowMA)
alertcondition(crossOver, "Bullish Crossover", "Bullish Crossover")
alertcondition(crossUnder, "Bearish CrossUnder", "Bearish Crossunder")
