Strat Dashboard [TFO]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tradeforopp

//@version=5
indicator("Strat Dashboard [TFO]", "Strat Dashboard [TFO]", true)


// -------------------- Inputs --------------------
var g_symbols = "Symbols" 
last_x_candles = input.int(3, minval = 1, maxval = 10, title = "Show Last X Candles", group = g_symbols)
table_position = input.string('Top Right', "Table Position", options = ['Bottom Center', 'Bottom Left', 'Bottom Right', 'Middle Center', 'Middle Left', 'Middle Right', 'Top Center', 'Top Left', 'Top Right'], group = g_symbols)

enable_s1 = input.bool(true, "", inline = "S1", group = g_symbols)
s1 = input.symbol("SPY", "Symbol 1", inline = "S1", group = g_symbols)

enable_s2 = input.bool(true, "", inline = "S2", group = g_symbols)
s2 = input.symbol("TSLA", "Symbol 2", inline = "S2", group = g_symbols)

enable_s3 = input.bool(true, "", inline = "S3", group = g_symbols)
s3 = input.symbol("AAPL", "Symbol 3", inline = "S3", group = g_symbols)

enable_s4 = input.bool(true, "", inline = "S4", group = g_symbols)
s4 = input.symbol("QQQ", "Symbol 4", inline = "S4", group = g_symbols)

enable_s5 = input.bool(true, "", inline = "S5", group = g_symbols)
s5 = input.symbol("AMZN", "Symbol 5", inline = "S5", group = g_symbols)

enable_s6 = input.bool(true, "", inline = "S6", group = g_symbols)
s6 = input.symbol("NVDA", "Symbol 6", inline = "S6", group = g_symbols)

enable_s7 = input.bool(true, "", inline = "S7", group = g_symbols)
s7 = input.symbol("MSFT", "Symbol 7", inline = "S7", group = g_symbols)

enable_s8 = input.bool(true, "", inline = "S8", group = g_symbols)
s8 = input.symbol("META", "Symbol 8", inline = "S8", group = g_symbols)

enable_s9 = input.bool(true, "", inline = "S9", group = g_symbols)
s9 = input.symbol("AMD", "Symbol 9", inline = "S9", group = g_symbols)

enable_s10 = input.bool(true, "", inline = "S10", group = g_symbols)
s10 = input.symbol("GME", "Symbol 10", inline = "S10", group = g_symbols)

var g_table = "Table"
col_sss = input.bool(true, "SSS 50% Rule", group = g_table)
col_tfc = input.bool(true, "Timeframe Continuity", group = g_table)
col_last_rev = input.bool(true, "Last Strat Reversal", group = g_table)
col_ma1 = input.bool(true, "Moving Average 1", group = g_table)
col_ma2 = input.bool(true, "Moving Average 2", group = g_table)
col_vwap = input.bool(true, "VWAP", group = g_table)

var g_tfc = "Timeframe Continuity"
y1 = input.bool(true, "", inline = "1", group = g_tfc)
tf1 = input.timeframe("D", "Timeframe 1", inline = "1", group = g_tfc)
y2 = input.bool(true, "", inline = "2", group = g_tfc)
tf2 = input.timeframe("60", "Timeframe 2", inline = "2", group = g_tfc)
y3 = input.bool(true, "", inline = "3", group = g_tfc)
tf3 = input.timeframe("15", "Timeframe 3", inline = "3", group = g_tfc)

var g_rev = "Strat Reversals"
rev_22 = input.bool(true, "2-2", group = g_rev)
rev_122 = input.bool(true, "1-2-2", group = g_rev)
rev_212 = input.bool(true, "2-1-2", group = g_rev)
rev_312 = input.bool(true, "3-1-2", group = g_rev)
rev_322 = input.bool(true, "3-2-2", group = g_rev)

var g_ma = "Additional Information"
plot_vwap = input.bool(true, "Plot VWAP", inline = "PVWAP", group = g_ma)
vwap_color = input.color(color.blue, "", inline = "PVWAP", group = g_ma)
plot_fast_ma = input.bool(true, "Plot Fast EMA", inline = "PFAST", group = g_ma)
fast_color = input.color(color.green, "", inline = "PFAST", group = g_ma)
plot_slow_ma = input.bool(true, "Plot Slow EMA", inline = "PSLOW", group = g_ma)
slow_color = input.color(color.red, "", inline = "PSLOW", group = g_ma)
fast_ma = input.int(8, "Fast EMA Length", group = g_ma)
slow_ma = input.int(21, "Slow EMA Length", group = g_ma)
line_width = input.int(1, "Line Width", 1, group = g_ma)

var g_style = "Style"
up_color = input.color(#70ccbd, "In Force - Long", group = g_style)
dn_color = input.color(#f77c80, "In Force - Short", group = g_style)
table_text = input.color(color.black, "Text", group = g_style)
table_bg = input.color(color.white, "Table Background", group = g_style)
table_header = input.color(color.silver, "Table Header", group = g_style)
table_frame = input.color(color.gray, "Table Frame", group = g_style)
table_border = input.color(color.gray, "Table Border", inline = "TBG", group = g_style)
table_border_width = input.int(1, "", inline = "TBG", group = g_style)

var g_alert = "Alerts"
alert_confirmation = input.bool(false, "Wait for Confirmation", tooltip = "When true, will alert after the current bar closes and provides a valid strat reversal. Otherwise will alert immediately", group = g_alert)

// -------------------- Inputs --------------------


// -------------------- Initiate --------------------
table_position := switch table_position
    "Bottom Center" => position.bottom_center
    "Bottom Left" => position.bottom_left
    "Bottom Right" => position.bottom_right
    "Middle Center" => position.middle_center
    "Middle Left" => position.middle_left
    "Middle Right" => position.middle_right
    "Top Center" => position.top_center
    "Top Left" => position.top_left
    "Top Right" => position.top_right

[s1_o, s1_h, s1_l, s1_c, s1_4] = request.security(s1, timeframe.period, [open, high, low, close, ohlc4], barmerge.gaps_off, barmerge.lookahead_on)
[s2_o, s2_h, s2_l, s2_c, s2_4] = request.security(s2, timeframe.period, [open, high, low, close, ohlc4], barmerge.gaps_off, barmerge.lookahead_on)
[s3_o, s3_h, s3_l, s3_c, s3_4] = request.security(s3, timeframe.period, [open, high, low, close, ohlc4], barmerge.gaps_off, barmerge.lookahead_on)
[s4_o, s4_h, s4_l, s4_c, s4_4] = request.security(s4, timeframe.period, [open, high, low, close, ohlc4], barmerge.gaps_off, barmerge.lookahead_on)
[s5_o, s5_h, s5_l, s5_c, s5_4] = request.security(s5, timeframe.period, [open, high, low, close, ohlc4], barmerge.gaps_off, barmerge.lookahead_on)
[s6_o, s6_h, s6_l, s6_c, s6_4] = request.security(s6, timeframe.period, [open, high, low, close, ohlc4], barmerge.gaps_off, barmerge.lookahead_on)
[s7_o, s7_h, s7_l, s7_c, s7_4] = request.security(s7, timeframe.period, [open, high, low, close, ohlc4], barmerge.gaps_off, barmerge.lookahead_on)
[s8_o, s8_h, s8_l, s8_c, s8_4] = request.security(s8, timeframe.period, [open, high, low, close, ohlc4], barmerge.gaps_off, barmerge.lookahead_on)
[s9_o, s9_h, s9_l, s9_c, s9_4] = request.security(s9, timeframe.period, [open, high, low, close, ohlc4], barmerge.gaps_off, barmerge.lookahead_on)
[s10_o, s10_h, s10_l, s10_c, s10_4] = request.security(s10, timeframe.period, [open, high, low, close, ohlc4], barmerge.gaps_off, barmerge.lookahead_on)

var s1_name = array.get(str.split(str.tostring(s1), ":"), 1) + " "
var s2_name = array.get(str.split(str.tostring(s2), ":"), 1) + " "
var s3_name = array.get(str.split(str.tostring(s3), ":"), 1) + " "
var s4_name = array.get(str.split(str.tostring(s4), ":"), 1) + " "
var s5_name = array.get(str.split(str.tostring(s5), ":"), 1) + " "
var s6_name = array.get(str.split(str.tostring(s6), ":"), 1) + " "
var s7_name = array.get(str.split(str.tostring(s7), ":"), 1) + " "
var s8_name = array.get(str.split(str.tostring(s8), ":"), 1) + " "
var s9_name = array.get(str.split(str.tostring(s9), ":"), 1) + " "
var s10_name = array.get(str.split(str.tostring(s10), ":"), 1) + " "

var table stats = table.new(table_position, 20, 20, bgcolor = table_bg, frame_color = table_frame, border_color = table_border, border_width = table_border_width)
// -------------------- Initiate --------------------


// -------------------- Functions --------------------
strat_candle(h, l, i) =>
    result = ""
    if h[i] > h[i + 1] and l[i] < l[i + 1]
        result := "3"
    else if h[i] <= h[i + 1] and l[i] < l[i + 1]
        result := "2D"
    else if h[i] > h[i + 1] and l[i] >= l[i + 1]
        result := "2U"
    else if h[i] <= h[i + 1] and l[i] >= l[i + 1]
        result := "1"    
    result

compound_candles(c, h, l) =>
    string result = ""
    color current_color = na
    for j = last_x_candles - 1 to 0
        if j != last_x_candles - 1
            result += " - "
        result += strat_candle(h[j], l[j], j)
    if c > h[1] 
        current_color := up_color
    else if c < l[1]
        current_color := dn_color
    [result, current_color]

sss50(c, h, l) =>
    string result = "NO"
    color sss_color = na
    midline = math.avg(h[1], l[1])
    if h > h[1] and l > l[1] and c < midline
        result := "YES"
        sss_color := dn_color
    else if h < h[1] and l < l[1] and c > midline
        result := "YES"
        sss_color := up_color
    [result, sss_color]

strat_pattern(str, enable, ftfc_up, ftfc_dn) =>
    c22 = false
    c122 = false
    c212 = false
    c312 = false
    c322 = false
    if enable
        split_arr = str.split(str, " - ")
        size = array.size(split_arr)
        if size >= 2
            // 2 - 2
            if rev_22 and ((array.get(split_arr, size - 2) == "2D" and array.get(split_arr, size - 1) == "2U") and ftfc_up or (array.get(split_arr, size - 2) == "2U" and array.get(split_arr, size - 1) == "2D") and ftfc_dn)
                c22 := true
        if size >= 3
            // 1 - 2 - 2
            if rev_122 and ((array.get(split_arr, size - 3) == "1" and array.get(split_arr, size - 2) == "2D" and array.get(split_arr, size - 1) == "2U") and ftfc_up or (array.get(split_arr, size - 3) == "1" and array.get(split_arr, size - 2) == "2U" and array.get(split_arr, size - 1) == "2D") and ftfc_dn)
                c122 := true
            // 2 - 1 - 2
            if rev_212 and ((array.get(split_arr, size - 3) == "2D" and array.get(split_arr, size - 2) == "1" and array.get(split_arr, size - 1) == "2U") and ftfc_up or (array.get(split_arr, size - 3) == "2U" and array.get(split_arr, size - 2) == "1" and array.get(split_arr, size - 1) == "2D") and ftfc_dn)
                c212 := true
            // 3 - 1 - 2
            if rev_312 and ((array.get(split_arr, size - 3) == "3" and array.get(split_arr, size - 2) == "1" and array.get(split_arr, size - 1) == "2U") and ftfc_up or (array.get(split_arr, size - 3) == "3" and array.get(split_arr, size - 2) == "1" and array.get(split_arr, size - 1) == "2D") and ftfc_dn)
                c312 := true
            // 3 - 2 - 2
            if rev_322 and ((array.get(split_arr, size - 3) == "3" and array.get(split_arr, size - 2) == "2D" and array.get(split_arr, size - 1) == "2U") and ftfc_up or (array.get(split_arr, size - 3) == "3" and array.get(split_arr, size - 2) == "2U" and array.get(split_arr, size - 1) == "2D") and ftfc_dn)
                c322 := true
    [c22, c122, c212, c312, c322]

tfc(e1, e2, e3, o1, o2, o3, c) =>
    ftfc_up = true
    ftfc_dn = true
    if e1
        if c >= o1
            ftfc_dn := false
        else if c <= o1
            ftfc_up := false
    if e2
        if c >= o2
            ftfc_dn := false
        else if c <= o2
            ftfc_up := false
    if e3
        if c >= o3
            ftfc_dn := false
        else if c <= o3
            ftfc_up := false
    [ftfc_up, ftfc_dn]

get_ma(price) =>
    fast_val = ta.ema(price, fast_ma)
    slow_val = ta.ema(price, slow_ma)

    ma1_color = price > fast_val ? up_color : dn_color
    ma1_text = price > fast_val ? "Above" : "Below"
    
    ma2_color = price > slow_val ? up_color : dn_color
    ma2_text = price > slow_val ? "Above" : "Below"
    
    [ma1_text, ma1_color, ma2_text, ma2_color]
    
get_vwap(c, o4) =>
    value = ta.vwap(o4)
    table_color = c > value ? up_color : dn_color
    vwap_text = c > value ? "Above" : "Below"
    [vwap_text, table_color]

get_all_info(c, h, l, o4) =>
    [last_x, last_color] = compound_candles(c, h, l)
    [sss50, sss50_color] = sss50(c, h, l)
    [ma1_text, ma1_color, ma2_text, ma2_color] = get_ma(c)
    [vwap_text, vwap_color] = get_vwap(c, o4)
    [last_x, last_color, sss50, sss50_color, ma1_text, ma1_color, ma2_text, ma2_color, vwap_text, vwap_color]
// -------------------- Functions --------------------


// -------------------- TFC --------------------
// Timeframe 1
s1_1o = request.security(s1, tf1, open, barmerge.gaps_off, barmerge.lookahead_on)
s2_1o = request.security(s2, tf1, open, barmerge.gaps_off, barmerge.lookahead_on)
s3_1o = request.security(s3, tf1, open, barmerge.gaps_off, barmerge.lookahead_on)
s4_1o = request.security(s4, tf1, open, barmerge.gaps_off, barmerge.lookahead_on)
s5_1o = request.security(s5, tf1, open, barmerge.gaps_off, barmerge.lookahead_on)
s6_1o = request.security(s6, tf1, open, barmerge.gaps_off, barmerge.lookahead_on)
s7_1o = request.security(s7, tf1, open, barmerge.gaps_off, barmerge.lookahead_on)
s8_1o = request.security(s8, tf1, open, barmerge.gaps_off, barmerge.lookahead_on)
s9_1o = request.security(s9, tf1, open, barmerge.gaps_off, barmerge.lookahead_on)
s10_1o = request.security(s10, tf1, open, barmerge.gaps_off, barmerge.lookahead_on)

// Timeframe 2
s1_2o = request.security(s1, tf2, open, barmerge.gaps_off, barmerge.lookahead_on)
s2_2o = request.security(s2, tf2, open, barmerge.gaps_off, barmerge.lookahead_on)
s3_2o = request.security(s3, tf2, open, barmerge.gaps_off, barmerge.lookahead_on)
s4_2o = request.security(s4, tf2, open, barmerge.gaps_off, barmerge.lookahead_on)
s5_2o = request.security(s5, tf2, open, barmerge.gaps_off, barmerge.lookahead_on)
s6_2o = request.security(s6, tf2, open, barmerge.gaps_off, barmerge.lookahead_on)
s7_2o = request.security(s7, tf2, open, barmerge.gaps_off, barmerge.lookahead_on)
s8_2o = request.security(s8, tf2, open, barmerge.gaps_off, barmerge.lookahead_on)
s9_2o = request.security(s9, tf2, open, barmerge.gaps_off, barmerge.lookahead_on)
s10_2o = request.security(s10, tf2, open, barmerge.gaps_off, barmerge.lookahead_on)

// Timeframe 2
s1_3o = request.security(s1, tf3, open, barmerge.gaps_off, barmerge.lookahead_on)
s2_3o = request.security(s2, tf3, open, barmerge.gaps_off, barmerge.lookahead_on)
s3_3o = request.security(s3, tf3, open, barmerge.gaps_off, barmerge.lookahead_on)
s4_3o = request.security(s4, tf3, open, barmerge.gaps_off, barmerge.lookahead_on)
s5_3o = request.security(s5, tf3, open, barmerge.gaps_off, barmerge.lookahead_on)
s6_3o = request.security(s6, tf3, open, barmerge.gaps_off, barmerge.lookahead_on)
s7_3o = request.security(s7, tf3, open, barmerge.gaps_off, barmerge.lookahead_on)
s8_3o = request.security(s8, tf3, open, barmerge.gaps_off, barmerge.lookahead_on)
s9_3o = request.security(s9, tf3, open, barmerge.gaps_off, barmerge.lookahead_on)
s10_3o = request.security(s10, tf3, open, barmerge.gaps_off, barmerge.lookahead_on)

// Get TFC
[s1_ftfc_up, s1_ftfc_dn] = tfc(y1, y2, y3, s1_1o, s1_2o, s1_3o, s1_c)
[s2_ftfc_up, s2_ftfc_dn] = tfc(y1, y2, y3, s2_1o, s2_2o, s2_3o, s2_c)
[s3_ftfc_up, s3_ftfc_dn] = tfc(y1, y2, y3, s3_1o, s3_2o, s3_3o, s3_c)
[s4_ftfc_up, s4_ftfc_dn] = tfc(y1, y2, y3, s4_1o, s4_2o, s4_3o, s4_c)
[s5_ftfc_up, s5_ftfc_dn] = tfc(y1, y2, y3, s5_1o, s5_2o, s5_3o, s5_c)
[s6_ftfc_up, s6_ftfc_dn] = tfc(y1, y2, y3, s6_1o, s6_2o, s6_3o, s6_c)
[s7_ftfc_up, s7_ftfc_dn] = tfc(y1, y2, y3, s7_1o, s7_2o, s7_3o, s7_c)
[s8_ftfc_up, s8_ftfc_dn] = tfc(y1, y2, y3, s8_1o, s8_2o, s8_3o, s8_c)
[s9_ftfc_up, s9_ftfc_dn] = tfc(y1, y2, y3, s9_1o, s9_2o, s9_3o, s9_c)
[s10_ftfc_up, s10_ftfc_dn] = tfc(y1, y2, y3, s10_1o, s10_2o, s10_3o, s10_c)
// -------------------- TFC --------------------


// -------------------- Patterns --------------------
plot(plot_vwap ? ta.vwap(ohlc4) : na, "VWAP", vwap_color, linewidth = line_width)
plot(plot_fast_ma ? ta.ema(close, fast_ma) : na, "Fast EMA", fast_color, linewidth = line_width)
plot(plot_slow_ma ? ta.ema(close, slow_ma) : na, "Slow EMA", slow_color, linewidth = line_width)

[s1_last_x, s1_last_color, s1_sss50, s1_sss50_color, s1_ma1_text, s1_ma1_color, s1_ma2_text, s1_ma2_color, s1_vwap_text, s1_vwap_color] = get_all_info(s1_c, s1_h, s1_l, s1_4)
[s2_last_x, s2_last_color, s2_sss50, s2_sss50_color, s2_ma1_text, s2_ma1_color, s2_ma2_text, s2_ma2_color, s2_vwap_text, s2_vwap_color] = get_all_info(s2_c, s2_h, s2_l, s2_4)
[s3_last_x, s3_last_color, s3_sss50, s3_sss50_color, s3_ma1_text, s3_ma1_color, s3_ma2_text, s3_ma2_color, s3_vwap_text, s3_vwap_color] = get_all_info(s3_c, s3_h, s3_l, s3_4)
[s4_last_x, s4_last_color, s4_sss50, s4_sss50_color, s4_ma1_text, s4_ma1_color, s4_ma2_text, s4_ma2_color, s4_vwap_text, s4_vwap_color] = get_all_info(s4_c, s4_h, s4_l, s4_4)
[s5_last_x, s5_last_color, s5_sss50, s5_sss50_color, s5_ma1_text, s5_ma1_color, s5_ma2_text, s5_ma2_color, s5_vwap_text, s5_vwap_color] = get_all_info(s5_c, s5_h, s5_l, s5_4)
[s6_last_x, s6_last_color, s6_sss50, s6_sss50_color, s6_ma1_text, s6_ma1_color, s6_ma2_text, s6_ma2_color, s6_vwap_text, s6_vwap_color] = get_all_info(s6_c, s6_h, s6_l, s6_4)
[s7_last_x, s7_last_color, s7_sss50, s7_sss50_color, s7_ma1_text, s7_ma1_color, s7_ma2_text, s7_ma2_color, s7_vwap_text, s7_vwap_color] = get_all_info(s7_c, s7_h, s7_l, s7_4)
[s8_last_x, s8_last_color, s8_sss50, s8_sss50_color, s8_ma1_text, s8_ma1_color, s8_ma2_text, s8_ma2_color, s8_vwap_text, s8_vwap_color] = get_all_info(s8_c, s8_h, s8_l, s8_4)
[s9_last_x, s9_last_color, s9_sss50, s9_sss50_color, s9_ma1_text, s9_ma1_color, s9_ma2_text, s9_ma2_color, s9_vwap_text, s9_vwap_color] = get_all_info(s9_c, s9_h, s9_l, s9_4)
[s10_last_x, s10_last_color, s10_sss50, s10_sss50_color, s10_ma1_text, s10_ma1_color, s10_ma2_text, s10_ma2_color, s10_vwap_text, s10_vwap_color] = get_all_info(s10_c, s10_h, s10_l, s10_4)

[s1_22, s1_122, s1_212, s1_312, s1_322] = strat_pattern(s1_last_x, enable_s1, s1_ftfc_up, s1_ftfc_dn)
[s2_22, s2_122, s2_212, s2_312, s2_322] = strat_pattern(s2_last_x, enable_s2, s2_ftfc_up, s2_ftfc_dn)
[s3_22, s3_122, s3_212, s3_312, s3_322] = strat_pattern(s3_last_x, enable_s3, s3_ftfc_up, s3_ftfc_dn)
[s4_22, s4_122, s4_212, s4_312, s4_322] = strat_pattern(s4_last_x, enable_s4, s4_ftfc_up, s4_ftfc_dn)
[s5_22, s5_122, s5_212, s5_312, s5_322] = strat_pattern(s5_last_x, enable_s5, s5_ftfc_up, s5_ftfc_dn)
[s6_22, s6_122, s6_212, s6_312, s6_322] = strat_pattern(s6_last_x, enable_s6, s6_ftfc_up, s6_ftfc_dn)
[s7_22, s7_122, s7_212, s7_312, s7_322] = strat_pattern(s7_last_x, enable_s7, s7_ftfc_up, s7_ftfc_dn)
[s8_22, s8_122, s8_212, s8_312, s8_322] = strat_pattern(s8_last_x, enable_s8, s8_ftfc_up, s8_ftfc_dn)
[s9_22, s9_122, s9_212, s9_312, s9_322] = strat_pattern(s9_last_x, enable_s9, s9_ftfc_up, s9_ftfc_dn)
[s10_22, s10_122, s10_212, s10_312, s10_322] = strat_pattern(s10_last_x, enable_s10, s10_ftfc_up, s10_ftfc_dn)

any_22 = s1_22 or s2_22 or s3_22 or s4_22 or s5_22 or s6_22 or s7_22 or s8_22 or s9_22 or s10_22
any_122 = s1_122 or s2_122 or s3_122 or s4_122 or s5_122 or s6_122 or s7_122 or s8_122 or s9_122 or s10_122
any_212 = s1_212 or s2_212 or s3_212 or s4_212 or s5_212 or s6_212 or s7_212 or s8_212 or s9_212 or s10_212
any_312 = s1_312 or s2_312 or s3_312 or s4_312 or s5_312 or s6_312 or s7_312 or s8_312 or s9_312 or s10_312
any_322 = s1_322 or s2_322 or s3_322 or s4_322 or s5_322 or s6_322 or s7_322 or s8_322 or s9_322 or s10_322

any_s1 = s1_22 or s1_122 or s1_212 or s1_312 or s1_322
any_s2 = s2_22 or s2_122 or s2_212 or s2_312 or s2_322
any_s3 = s3_22 or s3_122 or s3_212 or s3_312 or s3_322
any_s4 = s4_22 or s4_122 or s4_212 or s4_312 or s4_322
any_s5 = s5_22 or s5_122 or s5_212 or s5_312 or s5_322
any_s6 = s6_22 or s6_122 or s6_212 or s6_312 or s6_322
any_s7 = s7_22 or s7_122 or s7_212 or s7_312 or s7_322
any_s8 = s8_22 or s8_122 or s8_212 or s8_312 or s8_322
any_s9 = s9_22 or s9_122 or s9_212 or s9_312 or s9_322
any_s10 = s10_22 or s10_122 or s10_212 or s10_312 or s10_322

any_s1_text = s1_122 ? "1 - 2 - 2" : s1_212 ? "2 - 1 - 2" : s1_312 ? "3 - 1 - 2" : s1_322 ? "3 - 2 - 2" : s1_22 ? "2 - 2" : "-"
any_s2_text = s2_122 ? "1 - 2 - 2" : s2_212 ? "2 - 1 - 2" : s2_312 ? "3 - 1 - 2" : s2_322 ? "3 - 2 - 2" : s2_22 ? "2 - 2" : "-"
any_s3_text = s3_122 ? "1 - 2 - 2" : s3_212 ? "2 - 1 - 2" : s3_312 ? "3 - 1 - 2" : s3_322 ? "3 - 2 - 2" : s3_22 ? "2 - 2" : "-"
any_s4_text = s4_122 ? "1 - 2 - 2" : s4_212 ? "2 - 1 - 2" : s4_312 ? "3 - 1 - 2" : s4_322 ? "3 - 2 - 2" : s4_22 ? "2 - 2" : "-"
any_s5_text = s5_122 ? "1 - 2 - 2" : s5_212 ? "2 - 1 - 2" : s5_312 ? "3 - 1 - 2" : s5_322 ? "3 - 2 - 2" : s5_22 ? "2 - 2" : "-"
any_s6_text = s6_122 ? "1 - 2 - 2" : s6_212 ? "2 - 1 - 2" : s6_312 ? "3 - 1 - 2" : s6_322 ? "3 - 2 - 2" : s6_22 ? "2 - 2" : "-"
any_s7_text = s7_122 ? "1 - 2 - 2" : s7_212 ? "2 - 1 - 2" : s7_312 ? "3 - 1 - 2" : s7_322 ? "3 - 2 - 2" : s7_22 ? "2 - 2" : "-"
any_s8_text = s8_122 ? "1 - 2 - 2" : s8_212 ? "2 - 1 - 2" : s8_312 ? "3 - 1 - 2" : s8_322 ? "3 - 2 - 2" : s8_22 ? "2 - 2" : "-"
any_s9_text = s9_122 ? "1 - 2 - 2" : s9_212 ? "2 - 1 - 2" : s9_312 ? "3 - 1 - 2" : s9_322 ? "3 - 2 - 2" : s9_22 ? "2 - 2" : "-"
any_s10_text = s10_122 ? "1 - 2 - 2" : s10_212 ? "2 - 1 - 2" : s10_312 ? "3 - 1 - 2" : s10_322 ? "3 - 2 - 2" : s10_22 ? "2 - 2" : "-"
// -------------------- Patterns --------------------


// -------------------- Populate Cells --------------------
table.cell(stats, 0, 0, "TF = " + str.tostring(timeframe.period), bgcolor = table_header)
table.cell(stats, 1, 0, "Last " + str.tostring(last_x_candles) + " Candles", bgcolor = table_header)
if col_sss
    table.cell(stats, 2, 0, "SSS 50%", bgcolor = table_header)
if col_tfc
    table.cell(stats, 3, 0, "TFC", bgcolor = table_header)
if col_last_rev
    table.cell(stats, 4, 0, "Last", bgcolor = table_header)
if col_ma1
    table.cell(stats, 5, 0, str.tostring(fast_ma) + " EMA", bgcolor = table_header)
if col_ma2
    table.cell(stats, 6, 0, str.tostring(slow_ma) + " EMA", bgcolor = table_header)
if col_vwap
    table.cell(stats, 7, 0, "VWAP", bgcolor = table_header)

// Symbol 1
if enable_s1
    table.cell(stats, 0, 1, str.tostring(array.get(str.split(s1, ":"), 1)), text_color = table_text)
    table.cell(stats, 1, 1, s1_last_x, bgcolor = s1_last_color, text_color = table_text)
    if col_sss
        table.cell(stats, 2, 1, s1_sss50, bgcolor = s1_sss50_color, text_color = table_text)
    if col_tfc
        table.cell(stats, 3, 1, s1_ftfc_up or s1_ftfc_dn ? "YES" : "NO", bgcolor = s1_ftfc_up ? up_color : s1_ftfc_dn ? dn_color : na, text_color = table_text)
    if col_last_rev
        table.cell(stats, 4, 1, any_s1[1] ? any_s1_text[1] : "-", bgcolor = any_s1[1] ? s1_last_color[1] : na, text_color = table_text)
    if col_ma1
        table.cell(stats, 5, 1, s1_ma1_text, bgcolor = s1_ma1_color, text_color = table_text)
    if col_ma2
        table.cell(stats, 6, 1, s1_ma2_text, bgcolor = s1_ma2_color, text_color = table_text)
    if col_vwap
        table.cell(stats, 7, 1, s1_vwap_text, bgcolor = s1_vwap_color, text_color = table_text)


// Symbol 2
if enable_s2
    table.cell(stats, 0, 2, str.tostring(array.get(str.split(s2, ":"), 1)), text_color = table_text)
    table.cell(stats, 1, 2, s2_last_x, bgcolor = s2_last_color, text_color = table_text)
    if col_sss
        table.cell(stats, 2, 2, s2_sss50, bgcolor = s2_sss50_color, text_color = table_text)
    if col_tfc
        table.cell(stats, 3, 2, s2_ftfc_up or s2_ftfc_dn ? "YES" : "NO", bgcolor = s2_ftfc_up ? up_color : s2_ftfc_dn ? dn_color : na, text_color = table_text)
    if col_last_rev
        table.cell(stats, 4, 2, any_s2[1] ? any_s2_text[1] : "-", bgcolor = any_s2[1] ? s2_last_color[1] : na, text_color = table_text)
    if col_ma1
        table.cell(stats, 5, 2, s2_ma1_text, bgcolor = s2_ma1_color, text_color = table_text)
    if col_ma2
        table.cell(stats, 6, 2, s2_ma2_text, bgcolor = s2_ma2_color, text_color = table_text)
    if col_vwap
        table.cell(stats, 7, 2, s2_vwap_text, bgcolor = s2_vwap_color, text_color = table_text)

// Symbol 3
if enable_s3
    table.cell(stats, 0, 3, str.tostring(array.get(str.split(s3, ":"), 1)), text_color = table_text)
    table.cell(stats, 1, 3, s3_last_x, bgcolor = s3_last_color, text_color = table_text)
    if col_sss
        table.cell(stats, 2, 3, s3_sss50, bgcolor = s3_sss50_color, text_color = table_text)
    if col_tfc
        table.cell(stats, 3, 3, s3_ftfc_up or s3_ftfc_dn ? "YES" : "NO", bgcolor = s3_ftfc_up ? up_color : s3_ftfc_dn ? dn_color : na, text_color = table_text)
    if col_last_rev
        table.cell(stats, 4, 3, any_s3[1] ? any_s3_text[1] : "-", bgcolor = any_s3[1] ? s3_last_color[1] : na, text_color = table_text)
    if col_ma1
        table.cell(stats, 5, 3, s3_ma1_text, bgcolor = s3_ma1_color, text_color = table_text)
    if col_ma2
        table.cell(stats, 6, 3, s3_ma2_text, bgcolor = s3_ma2_color, text_color = table_text)
    if col_vwap
        table.cell(stats, 7, 3, s3_vwap_text, bgcolor = s3_vwap_color, text_color = table_text)

// Symbol 4
if enable_s4
    table.cell(stats, 0, 4, str.tostring(array.get(str.split(s4, ":"), 1)), text_color = table_text)
    table.cell(stats, 1, 4, s4_last_x, bgcolor = s4_last_color, text_color = table_text)
    if col_sss
        table.cell(stats, 2, 4, s4_sss50, bgcolor = s4_sss50_color, text_color = table_text)
    if col_tfc
        table.cell(stats, 3, 4, s4_ftfc_up or s4_ftfc_dn ? "YES" : "NO", bgcolor = s4_ftfc_up ? up_color : s4_ftfc_dn ? dn_color : na, text_color = table_text)
    if col_last_rev
        table.cell(stats, 4, 4, any_s4[1] ? any_s4_text[1] : "-", bgcolor = any_s4[1] ? s4_last_color[1] : na, text_color = table_text)
    if col_ma1
        table.cell(stats, 5, 4, s4_ma1_text, bgcolor = s4_ma1_color, text_color = table_text)
    if col_ma2
        table.cell(stats, 6, 4, s4_ma2_text, bgcolor = s4_ma2_color, text_color = table_text)
    if col_vwap
        table.cell(stats, 7, 4, s4_vwap_text, bgcolor = s4_vwap_color, text_color = table_text)

// Symbol 5
if enable_s5
    table.cell(stats, 0, 5, str.tostring(array.get(str.split(s5, ":"), 1)), text_color = table_text)
    table.cell(stats, 1, 5, s5_last_x, bgcolor = s5_last_color, text_color = table_text)
    if col_sss
        table.cell(stats, 2, 5, s5_sss50, bgcolor = s5_sss50_color, text_color = table_text)
    if col_tfc
        table.cell(stats, 3, 5, s5_ftfc_up or s5_ftfc_dn ? "YES" : "NO", bgcolor = s5_ftfc_up ? up_color : s5_ftfc_dn ? dn_color : na, text_color = table_text)
    if col_last_rev
        table.cell(stats, 4, 5, any_s5[1] ? any_s5_text[1] : "-", bgcolor = any_s5[1] ? s5_last_color[1] : na, text_color = table_text)
    if col_ma1
        table.cell(stats, 5, 5, s5_ma1_text, bgcolor = s5_ma1_color, text_color = table_text)
    if col_ma2
        table.cell(stats, 6, 5, s5_ma2_text, bgcolor = s5_ma2_color, text_color = table_text)
    if col_vwap
        table.cell(stats, 7, 5, s5_vwap_text, bgcolor = s5_vwap_color, text_color = table_text)

// Symbol 6
if enable_s6
    table.cell(stats, 0, 6, str.tostring(array.get(str.split(s6, ":"), 1)), text_color = table_text)
    table.cell(stats, 1, 6, s6_last_x, bgcolor = s6_last_color, text_color = table_text)
    if col_sss
        table.cell(stats, 2, 6, s6_sss50, bgcolor = s6_sss50_color, text_color = table_text)
    if col_tfc
        table.cell(stats, 3, 6, s6_ftfc_up or s6_ftfc_dn ? "YES" : "NO", bgcolor = s6_ftfc_up ? up_color : s6_ftfc_dn ? dn_color : na, text_color = table_text)
    if col_last_rev
        table.cell(stats, 4, 6, any_s6[1] ? any_s6_text[1] : "-", bgcolor = any_s6[1] ? s6_last_color[1] : na, text_color = table_text)
    if col_ma1
        table.cell(stats, 5, 6, s6_ma1_text, bgcolor = s6_ma1_color, text_color = table_text)
    if col_ma2
        table.cell(stats, 6, 6, s6_ma2_text, bgcolor = s6_ma2_color, text_color = table_text)
    if col_vwap
        table.cell(stats, 7, 6, s6_vwap_text, bgcolor = s6_vwap_color, text_color = table_text)

// Symbol 7
if enable_s7
    table.cell(stats, 0, 7, str.tostring(array.get(str.split(s7, ":"), 1)), text_color = table_text)
    table.cell(stats, 1, 7, s7_last_x, bgcolor = s7_last_color, text_color = table_text)
    if col_sss
        table.cell(stats, 2, 7, s7_sss50, bgcolor = s7_sss50_color, text_color = table_text)
    if col_tfc
        table.cell(stats, 3, 7, s7_ftfc_up or s7_ftfc_dn ? "YES" : "NO", bgcolor = s7_ftfc_up ? up_color : s7_ftfc_dn ? dn_color : na, text_color = table_text)
    if col_last_rev
        table.cell(stats, 4, 7, any_s7[1] ? any_s7_text[1] : "-", bgcolor = any_s7[1] ? s7_last_color[1] : na, text_color = table_text)
    if col_ma1
        table.cell(stats, 5, 7, s7_ma1_text, bgcolor = s7_ma1_color, text_color = table_text)
    if col_ma2
        table.cell(stats, 6, 7, s7_ma2_text, bgcolor = s7_ma2_color, text_color = table_text)
    if col_vwap
        table.cell(stats, 7, 7, s7_vwap_text, bgcolor = s7_vwap_color, text_color = table_text)

// Symbol 8
if enable_s8
    table.cell(stats, 0, 8, str.tostring(array.get(str.split(s8, ":"), 1)), text_color = table_text)
    table.cell(stats, 1, 8, s8_last_x, bgcolor = s8_last_color, text_color = table_text)
    if col_sss
        table.cell(stats, 2, 8, s8_sss50, bgcolor = s8_sss50_color, text_color = table_text)
    if col_tfc
        table.cell(stats, 3, 8, s8_ftfc_up or s8_ftfc_dn ? "YES" : "NO", bgcolor = s8_ftfc_up ? up_color : s8_ftfc_dn ? dn_color : na, text_color = table_text)
    if col_last_rev
        table.cell(stats, 4, 8, any_s8[1] ? any_s8_text[1] : "-", bgcolor = any_s8[1] ? s8_last_color[1] : na, text_color = table_text)
    if col_ma1
        table.cell(stats, 5, 8, s8_ma1_text, bgcolor = s8_ma1_color, text_color = table_text)
    if col_ma2
        table.cell(stats, 6, 8, s8_ma2_text, bgcolor = s8_ma2_color, text_color = table_text)
    if col_vwap
        table.cell(stats, 7, 8, s8_vwap_text, bgcolor = s8_vwap_color, text_color = table_text)

// Symbol 9
if enable_s9
    table.cell(stats, 0, 9, str.tostring(array.get(str.split(s9, ":"), 1)), text_color = table_text)
    table.cell(stats, 1, 9, s9_last_x, bgcolor = s9_last_color, text_color = table_text)
    if col_sss
        table.cell(stats, 2, 9, s9_sss50, bgcolor = s9_sss50_color, text_color = table_text)
    if col_tfc
        table.cell(stats, 3, 9, s9_ftfc_up or s9_ftfc_dn ? "YES" : "NO", bgcolor = s9_ftfc_up ? up_color : s9_ftfc_dn ? dn_color : na, text_color = table_text)
    if col_last_rev
        table.cell(stats, 4, 9, any_s9[1] ? any_s9_text[1] : "-", bgcolor = any_s9[1] ? s9_last_color[1] : na, text_color = table_text)
    if col_ma1
        table.cell(stats, 5, 9, s9_ma1_text, bgcolor = s9_ma1_color, text_color = table_text)
    if col_ma2
        table.cell(stats, 6, 9, s9_ma2_text, bgcolor = s9_ma2_color, text_color = table_text)
    if col_vwap
        table.cell(stats, 7, 9, s9_vwap_text, bgcolor = s9_vwap_color, text_color = table_text)

// Symbol 10
if enable_s10
    table.cell(stats, 0, 10, str.tostring(array.get(str.split(s10, ":"), 1)), text_color = table_text)
    table.cell(stats, 1, 10, s10_last_x, bgcolor = s10_last_color, text_color = table_text)
    if col_sss
        table.cell(stats, 2, 10, s10_sss50, bgcolor = s10_sss50_color, text_color = table_text)
    if col_tfc
        table.cell(stats, 3, 10, s10_ftfc_up or s10_ftfc_dn ? "YES" : "NO", bgcolor = s10_ftfc_up ? up_color : s10_ftfc_dn ? dn_color : na, text_color = table_text)
    if col_last_rev
        table.cell(stats, 4, 10, any_s10[1] ? any_s10_text[1] : "-", bgcolor = any_s10[1] ? s10_last_color[1] : na, text_color = table_text)
    if col_ma1
        table.cell(stats, 5, 10, s10_ma1_text, bgcolor = s10_ma1_color, text_color = table_text)
    if col_ma2
        table.cell(stats, 6, 10, s10_ma2_text, bgcolor = s10_ma2_color, text_color = table_text)
    if col_vwap
        table.cell(stats, 7, 10, s10_vwap_text, bgcolor = s10_vwap_color, text_color = table_text)
// -------------------- Populate Cells --------------------


// -------------------- Alerts --------------------
alert_index = alert_confirmation ? 1 : 0
alertcondition(any_s1[alert_index], "Strat Reversal into TFC - Symbol 1")
alertcondition(any_s2[alert_index], "Strat Reversal into TFC - Symbol 2")
alertcondition(any_s3[alert_index], "Strat Reversal into TFC - Symbol 3")
alertcondition(any_s4[alert_index], "Strat Reversal into TFC - Symbol 4")
alertcondition(any_s5[alert_index], "Strat Reversal into TFC - Symbol 5")
alertcondition(any_s6[alert_index], "Strat Reversal into TFC - Symbol 6")
alertcondition(any_s7[alert_index], "Strat Reversal into TFC - Symbol 7")
alertcondition(any_s8[alert_index], "Strat Reversal into TFC - Symbol 8")
alertcondition(any_s9[alert_index], "Strat Reversal into TFC - Symbol 9")
alertcondition(any_s10[alert_index], "Strat Reversal into TFC - Symbol 10")
alertcondition(not na(s1_sss50_color[alert_index]) or not na(s2_sss50_color[alert_index]) or not na(s3_sss50_color[alert_index]) or not na(s4_sss50_color[alert_index]) or not na(s5_sss50_color[alert_index]) or not na(s6_sss50_color[alert_index]) or not na(s7_sss50_color[alert_index]) or not na(s8_sss50_color[alert_index]) or not na(s9_sss50_color[alert_index]) or not na(s10_sss50_color[alert_index]), "SSS 50% Rule - Any Symbol")
alertcondition(not na(s1_sss50_color[alert_index]), "SSS 50% Rule - Symbol 1")
alertcondition(not na(s2_sss50_color[alert_index]), "SSS 50% Rule - Symbol 2")
alertcondition(not na(s3_sss50_color[alert_index]), "SSS 50% Rule - Symbol 3")
alertcondition(not na(s4_sss50_color[alert_index]), "SSS 50% Rule - Symbol 4")
alertcondition(not na(s5_sss50_color[alert_index]), "SSS 50% Rule - Symbol 5")
alertcondition(not na(s6_sss50_color[alert_index]), "SSS 50% Rule - Symbol 6")
alertcondition(not na(s7_sss50_color[alert_index]), "SSS 50% Rule - Symbol 7")
alertcondition(not na(s8_sss50_color[alert_index]), "SSS 50% Rule - Symbol 8")
alertcondition(not na(s9_sss50_color[alert_index]), "SSS 50% Rule - Symbol 9")
alertcondition(not na(s10_sss50_color[alert_index]), "SSS 50% Rule - Symbol 10")
// -------------------- Alerts --------------------
