SML Suite
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kikfraben
// Updated last on 25. Oct, 2023
//@version=5

indicator("SML Suite", overlay = false)

// Define Moving Average Lengths
len_ma1 = input(9, "Moving Average 1", group = "User Inputs")
len_ma2 = input(22, "Moving Average 2", group = "User Inputs")
len_ma3 = input(52, "Moving Average 3", group = "User Inputs")

// Source
src = input(close, "Moving Average Source", group = "User Inputs")

// Define Standard Deviation Multiplier
mult = input.float(1.0, "Standard Deviation Multiplier", group = "User Inputs")

// Get Moving Averages
val_ma1 = ta.sma(src, len_ma1)
val_ma2 = ta.sma(src, len_ma2)
val_ma3 = ta.sma(src, len_ma3)

// Get Standard Deviations
stdev_ma1 = ta.stdev(src, len_ma1)
stdev_ma2 = ta.stdev(src, len_ma2)
stdev_ma3 = ta.stdev(src, len_ma3)

// Get Upper and Lower Bands
u1 = val_ma1 + mult * stdev_ma1
l1 = val_ma1 - mult * stdev_ma1
u2 = val_ma2 + mult * stdev_ma2
l2 = val_ma2 - mult * stdev_ma2
u3 = val_ma3 + mult * stdev_ma3
l3 = val_ma3 - mult * stdev_ma3

// Get Binary Results
b1 = (close > u1) ? 1 : (close < l1) ? -1 : 1
b2 = (close > u2) ? 1 : (close < l2) ? -1 : 1
b3 = (close > u3) ? 1 : (close < l3) ? -1 : 1

// SML Calculation
sml = math.avg(b1, b2, b3)

// Color Inputs
col_up1 = #3fa8c9
col_up2 = color.new(#3fa8c9, 55)
col_down1 = #c93f3f
col_down2 = color.new(#c93f3f, 55)

// Determine the Color of the Bars
hist_col = sml >= 0 ? (sml > sml[1] ? col_up1 : col_up2) : (sml < sml[1] ? col_down1 : col_down2)

// Plot SML as Histogram
plot(sml, style = plot.style_histogram, color = hist_col, linewidth = 3)

// Table with Binary Results
var bTable = table.new(position = position.top_right, columns = 5, rows = 5, bgcolor = color.new(color.white, 95), border_width = 1, frame_color = color.new(color.white, 50), frame_width = 1, border_color = color.new(color.white, 70))

table.merge_cells(bTable, 0, 0, 1, 0)

table.cell(bTable, 0, 0, "SML Suite", text_color = color.new(color.white, 50))
table.cell(bTable, 0, 1, "S", text_color = color.new(color.white, 50))
table.cell(bTable, 0, 2, "M", text_color = color.new(color.white, 50))
table.cell(bTable, 0, 3, "L", text_color = color.new(color.white, 50))
table.cell(bTable, 0, 4, "SML", text_color = color.new(color.white, 50))
table.cell(bTable, 1, 1, str.tostring(b1), text_color = b1 >= 0 ? col_up1 : col_down1)
table.cell(bTable, 1, 2, str.tostring(b2), text_color = b2 >= 0 ? col_up1 : col_down1)
table.cell(bTable, 1, 3, str.tostring(b3), text_color = b3 >= 0 ? col_up1 : col_down1)
table.cell(bTable, 1, 4, str.tostring(sml), text_color = sml >= 0 ? col_up1 : col_down1)