FULL MA Optimization Script
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © KioseffTrading

// -----------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------
// Shoutout to Duyck (JD) for instructing how to sort the elements in one array based on the values of customSource separate array!!!
// https://www.tradingview.com/script/Mzc4dmq7-ma-sorter-sort-by-array-example-JD/  <----- (Duyck's original array script)
// -----------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------

//@version=5
indicator("Find Best Performing MA For Price Crossover", overlay = true)

crossUnderOver = input.string(defval = "Crossover", group = "Necessary Parameters", title = "Crossover : Crossunder", options = ["Crossover", "Crossunder"], tooltip = "Select Whether to Measure the Performance of MA/Price Corssovers or MA/Price Crossunders")
dataType = input.string(defval = "Custom", title = "MA Data Type", options = ["Benchmark", "Custom"], tooltip = "Select Whether to Test customSource Benchmark MA, or customSource Custom MA. Both Options Allow For Personal Modifications; However, Setting the Parameters for Benchmark MA and Custom MA Allows for Quick Comparison Between Two Moving Average Lengths and Data Sources. Both Work the Same - The Titles Are Different")
instructions = input.string(defval = "Off", title = "Indicator Instructions", options = ["Off", "On"], tooltip = "Additional Indicator Instructions")

benchmarkSource = input.source(close, title = "Benchmark MA Source", group = "Benchmark Parameters", tooltip = "Adjust Benchmark MA Data Source")
benchmarkLength = input.int(50, title = "Benchmark MA Length", minval = 2, tooltip = "Adjust Benchmark MA Length")
customSource = input.source(close, title = "Custom MA Source", group = "Custom MA Parameters", tooltip = "Adjust Custom MA Data Source")
customLength = input.int(50, title = "Custom MA Length", minval = 2, tooltip = "Adjust Custom MA Length")
MA = input.string(defval = "Best Performing", title = "Plot MA?", tooltip = "Modify How Averages Are Plotted, and What Averages Are Plotted", group = "MA Plot" , options = ["Best Performing", "SMA", "EMA", "ALMA", "HMA", "WMA", "VWMA", "LSMA", "DEMA", "TEMA", "T3", "OFF"])
show = input.string(title = "Show Other MAs ? (If Best Performing Is Selected)", tooltip = 'Additional Plot Modificants for When "Best Performing" Is Selected', defval = "Gradient", options = ["Light", "Full", "Off", "Gradient"], group = "Best Performing MA Customization")
seriesPlot = input.string(title = "Series Plot", defval = "Highest Average Gain", options = ["Highest Average Gain", "Cumulative Gain"], tooltip = 'When "Best Performing MA" Is Selected, Choose Whether to Plot the MA That Achieves the Highest Average Gain, or the MA Which Achieves the Highest Cumulative Gain')

smaBenchmark = ta.sma(benchmarkSource, benchmarkLength)
emaBenchmark = ta.ema(benchmarkSource, benchmarkLength)
almaBenchmark = ta.alma(benchmarkSource, benchmarkLength, 0.85, 6)
hmaBenchmark = ta.hma(benchmarkSource, benchmarkLength)
wmaBenchmark = ta.wma(benchmarkSource, benchmarkLength)
vwmaBenchmark = ta.vwma(benchmarkSource, benchmarkLength)
lsmaBenchmark = ta.linreg(benchmarkSource, benchmarkLength, 0)
demaBenchmark = (2 * (emaBenchmark)) - (ta.ema(emaBenchmark, benchmarkLength))
tem(x) =>
    j = ta.ema(benchmarkSource, x)
    j1 = ta.ema(j, x)
    j2 = 3 * (j - j1) + ta.ema(j1, x)
    j2
temaBenchmark = tem(benchmarkLength) 

t(x) =>
    y1 = ta.ema(benchmarkSource,x)
    y2 = ta.ema(y1, x)
    y3 = ta.ema(y2, x)
    y4 = ta.ema(y3, x)
    y5 = ta.ema(y4, x)
    y6 = ta.ema(y5, x)
    v = -0.7 * math.pow(0.7,2)
    v2 = 3 * math.pow(0.7,2) + 3 * math.pow(0.7,3)
    v3 = -6 * math.pow(0.7, 2) - 3 * 0.7 - 3 * math.pow(0.7, 3)
    v4 = 1 + 3 * 0.7 + 0.7 * math.pow(0.7, 2) + 3 * math.pow(0.7, 2)
    v1 = v * y6 + v2 * y5 + v3 * y4 + v4 * y3
    v1
Tilson3Benchmark = t(benchmarkLength)

smaCustom = ta.sma(customSource, customLength)
emaCustom = ta.ema(customSource, customLength)
almaCustom = ta.alma(customSource, customLength, 0.85, 6)
hmaCustom = ta.hma(customSource, customLength)
wmaCustom = ta.wma(customSource, customLength)
vwmaCustom = ta.vwma(customSource, customLength)
rmaCustom = ta.rma(customSource, customLength)
lsmaCustom = ta.linreg(customSource, customLength, 0)
demaCustom = (2 * (emaCustom)) - (ta.ema(emaCustom, customLength))
temaCustom = tem(customLength)
Tilson3Average = t(customLength)

// 
// SMA
var int smaCounter = 0
var int smaCounter1 = 0
var float smaPerformance = 0.0
var float smaPerformance1 = 0.0
var int smaBarCounter = 0
var int smaBarCounter1 = 0

if ta.crossover(benchmarkSource, smaBenchmark) and smaCounter == 0 and crossUnderOver == "Crossover"
    smaPerformance := close
    smaCounter := 1
    smaBarCounter := bar_index
if ta.crossunder(benchmarkSource, smaBenchmark) and smaCounter == 1 and crossUnderOver == "Crossover"
    smaCounter := 0
    smaPerformance1 := ((close / smaPerformance) - 1) * 100
    smaPerformance1 := smaPerformance1[1] + smaPerformance1
    smaCounter1 += 1
    smaBarCounter1 := bar_index - smaBarCounter
    smaBarCounter1 := smaBarCounter1[1] + smaBarCounter1

smaCalculation = smaPerformance1 / smaCounter1
smaCalculationBar = smaBarCounter1 / smaCounter1

// EMA
var int emaCounter = 0
var int emaCounter1 = 0
var float emaPerformance = 0.0
var float emaPerformance1 = 0.0
var int emaBarCounter = 0
var int emaBarCounter1 = 0

if ta.crossover(benchmarkSource, emaBenchmark) and emaCounter == 0 and crossUnderOver == "Crossover"
    emaPerformance := close
    emaCounter := 1
    emaBarCounter := bar_index
if ta.crossunder(benchmarkSource, emaBenchmark) and emaCounter == 1 and crossUnderOver == "Crossover"
    emaCounter := 0
    emaPerformance1 := ((close / emaPerformance) - 1) * 100
    emaPerformance1 := emaPerformance1[1] + emaPerformance1
    emaCounter1 += 1
    emaBarCounter1 := bar_index - emaBarCounter
    emaBarCounter1 := emaBarCounter1[1] + emaBarCounter1

emaCalculation = emaPerformance1 / emaCounter1
emaCalculationBar = emaBarCounter1 / emaCounter1

// ALMA
var int almaCounter = 0
var int almaCounter1 = 0
var float almaPerformance = 0.0
var float almaPerformance1 = 0.0
var int almaBarCounter = 0
var int almaBarCounter1 = 0

if ta.crossover(benchmarkSource, almaBenchmark) and almaCounter == 0 and crossUnderOver == "Crossover"
    almaPerformance := close
    almaCounter := 1
    almaBarCounter := bar_index
if ta.crossunder(benchmarkSource, almaBenchmark) and almaCounter == 1 and crossUnderOver == "Crossover"
    almaCounter := 0
    almaPerformance1 := ((close / almaPerformance) - 1) * 100
    almaPerformance1 := almaPerformance1[1] + almaPerformance1
    almaCounter1 += 1
    almaBarCounter1 := bar_index - almaBarCounter
    almaBarCounter1 := almaBarCounter1[1] + almaBarCounter1

almaCalculation = almaPerformance1 / almaCounter1
almaCalculationBar = almaBarCounter1 / almaCounter1

// HMA
var int hmaCounter = 0
var int hmaCounter1 = 0
var float hmaPerformance = 0.0
var float hmaPerformance1 = 0.0
var int hmaBarCounter = 0
var int hmaBarCounter1 = 0
if ta.crossover(benchmarkSource, hmaBenchmark) and hmaCounter == 0 and crossUnderOver == "Crossover"
    hmaPerformance := close
    hmaCounter := 1
    hmaBarCounter := bar_index
if ta.crossunder(benchmarkSource, hmaBenchmark) and hmaCounter == 1 and crossUnderOver == "Crossover"
    hmaCounter := 0
    hmaPerformance1 := ((close / hmaPerformance) - 1) * 100
    hmaPerformance1 := hmaPerformance1[1] + hmaPerformance1
    hmaCounter1 += 1
    hmaBarCounter1 := bar_index - hmaBarCounter
    hmaBarCounter1 := hmaBarCounter1[1] + hmaBarCounter1

hmaCalculation = hmaPerformance1 / hmaCounter1
hmaCalculationBar = hmaBarCounter1 / hmaCounter1

// WMA
var int wmaCounter = 0
var int wmaCounter1 = 0
var float wmaPerformance = 0.0
var float wmaPerformance1 = 0.0
var int wmaBarCounter = 0
var int wmaBarCounter1 = 0
if ta.crossover(benchmarkSource, wmaBenchmark) and wmaCounter == 0 and crossUnderOver == "Crossover"
    wmaPerformance := close
    wmaCounter := 1
    wmaBarCounter := bar_index
if ta.crossunder(benchmarkSource, wmaBenchmark) and wmaCounter == 1 and crossUnderOver == "Crossover"
    wmaCounter := 0
    wmaPerformance1 := ((close / wmaPerformance) - 1) * 100
    wmaPerformance1 := wmaPerformance1[1] + wmaPerformance1
    wmaCounter1 += 1
    wmaBarCounter1 := bar_index - wmaBarCounter
    wmaBarCounter1 := wmaBarCounter1[1] + wmaBarCounter1

wmaCalculation = wmaPerformance1 / wmaCounter1
wmaCalculationBar = wmaBarCounter1 / wmaCounter1

// VWMA
var int vwmaCounter = 0
var int vwmaCounter1 = 0
var float vwmaPerformance = 0.0
var float vwmaPerformance1 = 0.0
var int vwmaBarCounter = 0
var int vwmaBarCounter1 = 0
if ta.crossover(benchmarkSource, vwmaBenchmark) and vwmaCounter == 0 and crossUnderOver == "Crossover"
    vwmaPerformance := close
    vwmaCounter := 1
    vwmaBarCounter := bar_index
if ta.crossunder(benchmarkSource, vwmaBenchmark) and vwmaCounter == 1 and crossUnderOver == "Crossover"
    vwmaCounter := 0
    vwmaPerformance1 := ((close / vwmaPerformance) - 1) * 100
    vwmaPerformance1 := vwmaPerformance1[1] + vwmaPerformance1
    vwmaCounter1 += 1
    vwmaBarCounter1 := bar_index - vwmaBarCounter
    vwmaBarCounter1 := vwmaBarCounter1[1] + vwmaBarCounter1

vwmaCalculation = vwmaPerformance1 / vwmaCounter1
vwmaCalculationBar = vwmaBarCounter1 / vwmaCounter1

// ls
var int lsmaCounter = 0
var int lsmaCounter1 = 0
var float lsmaPerformance = 0.0
var float lsmaPerformance1 = 0.0
var int lsmaBarCounter = 0
var int lsmaBarCounter1 = 0
if ta.crossover(benchmarkSource, lsmaBenchmark) and lsmaCounter == 0 and crossUnderOver == "Crossover"
    lsmaPerformance := close
    lsmaCounter := 1
    lsmaBarCounter := bar_index
if ta.crossunder(benchmarkSource, lsmaBenchmark) and lsmaCounter == 1 and crossUnderOver == "Crossover"
    lsmaCounter := 0
    lsmaPerformance1 := ((close / lsmaPerformance) - 1) * 100
    lsmaPerformance1 := lsmaPerformance1[1] + lsmaPerformance1
    lsmaCounter1 += 1
    lsmaBarCounter1 := bar_index - lsmaBarCounter
    lsmaBarCounter1 := lsmaBarCounter1[1] + lsmaBarCounter1

lsmaCalculation = lsmaPerformance1 / lsmaCounter1
lsmaCalculationBar = lsmaBarCounter1 / lsmaCounter1

// DEMA
var int demaCounter = 0
var int demaCounter1 = 0
var float demaPerformance = 0.0
var float demaPerformance1 = 0.0
var int demaBarCounter = 0
var int demaBarCounter1 = 0
if ta.crossover(benchmarkSource, demaBenchmark) and demaCounter == 0 and crossUnderOver == "Crossover"
    demaPerformance := close
    demaCounter := 1
    demaBarCounter := bar_index
if ta.crossunder(benchmarkSource, demaBenchmark) and demaCounter == 1 and crossUnderOver == "Crossover"
    demaCounter := 0
    demaPerformance1 := ((close / demaPerformance) - 1) * 100
    demaPerformance1 := demaPerformance1[1] + demaPerformance1
    demaCounter1 += 1
    demaBarCounter1 := bar_index - demaBarCounter
    demaBarCounter1 := demaBarCounter1[1] + demaBarCounter1

demaCalculation = demaPerformance1 / demaCounter1
demaCalculationBar = demaBarCounter1 / demaCounter1

// TEMA
var int temaCounter = 0
var int temaCounter1 = 0
var float temaPerformance = 0.0
var float temaPerformance1 = 0.0
var int temaBarCounter = 0
var int temaBarCounter1 = 0
if ta.crossover(benchmarkSource, temaBenchmark) and temaCounter == 0 and crossUnderOver == "Crossover"
    temaPerformance := close
    temaCounter := 1
    temaBarCounter := bar_index
if ta.crossunder(benchmarkSource, temaBenchmark) and temaCounter == 1 and crossUnderOver == "Crossover"
    temaCounter := 0
    temaPerformance1 := ((close / temaPerformance) - 1) * 100
    temaPerformance1 := temaPerformance1[1] + temaPerformance1
    temaCounter1 += 1
    temaBarCounter1 := bar_index - temaBarCounter
    temaBarCounter1 := temaBarCounter1[1] + temaBarCounter1

temaCalculation = temaPerformance1 / temaCounter1
temaCalculationBar = temaBarCounter1 / temaCounter1

// T3
var int Tilson3Counter = 0
var int Tilson3Counter1 = 0
var float Tilson3Performance = 0.0
var float Tilson3Performance1 = 0.0
var int Tislon3BarCounter = 0
var int Tislon3BarCounter1 = 0
if ta.crossover(benchmarkSource, Tilson3Benchmark) and Tilson3Counter == 0 and crossUnderOver == "Crossover"
    Tilson3Performance := close
    Tilson3Counter := 1
    Tislon3BarCounter := bar_index
if ta.crossunder(benchmarkSource, Tilson3Benchmark) and Tilson3Counter == 1 and crossUnderOver == "Crossover"
    Tilson3Counter := 0
    Tilson3Performance1 := ((close / Tilson3Performance) - 1) * 100
    Tilson3Performance1 := Tilson3Performance1[1] + Tilson3Performance1
    Tilson3Counter1 += 1
    Tislon3BarCounter1 := bar_index - Tislon3BarCounter
    Tislon3BarCounter1 := Tislon3BarCounter1[1] + Tislon3BarCounter1

Tilson3Calculation = Tilson3Performance1 / Tilson3Counter1
Tilson3CalculationBar = Tislon3BarCounter1 / Tilson3Counter1

// SMA
var int smaCounterx = 0
var int smaCounter2x = 0
var float smaPerformanceX = 0.0
var float smaPerformance1X = 0.0
var int smaCounterBarx = 0
var int smaCounterBarx1 = 0

if ta.crossover(customSource, smaCustom) and smaCounterx == 0 and crossUnderOver == "Crossover"
    smaPerformanceX := close
    smaCounterx := 1
    smaCounterBarx := bar_index
if ta.crossunder(customSource, smaCustom) and smaCounterx == 1 and crossUnderOver == "Crossover"
    smaCounterx := 0
    smaPerformance1X := ((close / smaPerformanceX) - 1) * 100
    smaPerformance1X := smaPerformance1X[1] + smaPerformance1X
    smaCounter2x += 1
    smaCounterBarx1 := bar_index - smaCounterBarx
    smaCounterBarx1 := smaCounterBarx1[1] + smaCounterBarx1 

smaCalculationx = smaPerformance1X / smaCounter2x
smaCalculationBarx = smaCounterBarx1 / smaCounter2x

// EMA
var int emaCounterx = 0
var int emaCounter2x = 0
var float emaPerformanceX = 0.0
var float emaPerformance1X = 0.0
var int emaBarCounterx = 0 
var int emaBarCounterx1 = 0
if ta.crossover(customSource, emaCustom) and emaCounterx == 0 and crossUnderOver == "Crossover"
    emaPerformanceX := close
    emaCounterx := 1
    emaBarCounterx := bar_index
if ta.crossunder(customSource, emaCustom) and emaCounterx == 1 and crossUnderOver == "Crossover"
    emaCounterx := 0
    emaPerformance1X := ((close / emaPerformanceX) - 1) * 100
    emaPerformance1X := emaPerformance1X[1] + emaPerformance1X
    emaCounter2x += 1
    emaBarCounterx1 := bar_index - emaBarCounterx
    emaBarCounterx1 := emaBarCounterx1[1] + emaBarCounterx1

emaCaculationx = emaPerformance1X / emaCounter2x
emaCaculationBarx = emaBarCounterx1 / emaCounter2x

// ALMA
var int almaCounterx = 0
var int almaCounter2x = 0
var float almaPerformanceX = 0.0
var float almaPerformance1X = 0.0
var int almaBarCounterx = 0
var int almaBarCounterx1 = 0
if ta.crossover(customSource, almaCustom) and almaCounterx == 0 and crossUnderOver == "Crossover"
    almaPerformanceX := close
    almaCounterx := 1
    almaBarCounterx := bar_index
if ta.crossunder(customSource, almaCustom) and almaCounterx == 1 and crossUnderOver == "Crossover"
    almaCounterx := 0
    almaPerformance1X := ((close / almaPerformanceX) - 1) * 100
    almaPerformance1X := almaPerformance1X[1] + almaPerformance1X
    almaCounter2x += 1
    almaBarCounterx1 := bar_index - almaBarCounterx
    almaBarCounterx1 := almaBarCounterx1[1] + almaBarCounterx1

almaCalculationx = almaPerformance1X / almaCounter2x
almaBarCalculationx = almaBarCounterx1 / almaCounter2x

// HMA
var int hmaCounterx = 0
var int hmaCounter2x = 0
var float hmaPerformanceX = 0.0
var float hmaPerformance1X = 0.0
var int hmaBarCounterx = 0
var int hmaBarCounterx1 = 0
if ta.crossover(customSource, hmaCustom) and hmaCounterx == 0 and crossUnderOver == "Crossover"
    hmaPerformanceX := close
    hmaCounterx := 1
    hmaBarCounterx := bar_index
if ta.crossunder(customSource, hmaCustom) and hmaCounterx == 1 and crossUnderOver == "Crossover"
    hmaCounterx := 0
    hmaPerformance1X := ((close / hmaPerformanceX) - 1) * 100
    hmaPerformance1X := hmaPerformance1X[1] + hmaPerformance1X
    hmaCounter2x += 1
    hmaBarCounterx1 := bar_index - hmaBarCounterx
    hmaBarCounterx1 := hmaBarCounterx1[1] + hmaBarCounterx1

hmaCalculationx = (hmaPerformance1X / hmaCounter2x)
hmaCalculationBarx = hmaBarCounterx1 / hmaCounter2x

// WMA
var int wmaCounterx = 0
var int wmaCounter2x = 0
var float wmaPerformanceX = 0.0
var float wmaPerformance1X = 0.0
var int wmaBarCounterx = 0
var int wmaBarCounterx1 = 0 

if ta.crossover(customSource, wmaCustom) and wmaCounterx == 0 and crossUnderOver == "Crossover"
    wmaPerformanceX := close
    wmaCounterx := 1
    wmaBarCounterx := bar_index
if ta.crossunder(customSource, wmaCustom) and wmaCounterx == 1 and crossUnderOver == "Crossover"
    wmaCounterx := 0
    wmaPerformance1X := ((close / wmaPerformanceX) - 1) * 100
    wmaPerformance1X := wmaPerformance1X[1] + wmaPerformance1X
    wmaCounter2x += 1
    wmaBarCounterx1 := bar_index - wmaBarCounterx
    wmaBarCounterx1 := wmaBarCounterx1[1] + wmaBarCounterx1

wmaCalculationx = wmaPerformance1X / wmaCounter2x
wmaCalculationBarx = wmaBarCounterx1 / wmaCounter2x

// VWMA
var int vwmaCounterx = 0
var int vwmaCounter2x = 0
var float vwmaPerformanceX = 0.0
var float vwmaPerformance1X = 0.0
var int vwmaBarCounterx = 0
var int vwmaBarCounterx1 = 0

if ta.crossover(customSource, vwmaCustom) and vwmaCounterx == 0 and crossUnderOver == "Crossover"
    vwmaPerformanceX := close
    vwmaCounterx := 1
    vwmaBarCounterx := bar_index
if ta.crossunder(customSource, vwmaCustom) and vwmaCounterx == 1 and crossUnderOver == "Crossover"
    vwmaCounterx := 0
    vwmaPerformance1X := ((close / vwmaPerformanceX) - 1) * 100
    vwmaPerformance1X := vwmaPerformance1X[1] + vwmaPerformance1X
    vwmaCounter2x += 1
    vwmaBarCounterx1 := bar_index - vwmaBarCounterx
    vwmaBarCounterx1 := vwmaBarCounterx1[1] + vwmaBarCounterx1

vwmaCalculationx = vwmaPerformance1X / vwmaCounter2x
vwmaCalculationBarx = vwmaBarCounterx1 / vwmaCounter2x

// ls
var int lsCounterx = 0
var int lsCounter2x = 0
var float lsPerformanceX = 0.0
var float lsPerformance1X = 0.0
var int lsmaBarCounterx = 0
var int lsmaBarCounterx1 = 0
if ta.crossover(customSource, lsmaCustom) and lsCounterx == 0 and crossUnderOver == "Crossover"
    lsPerformanceX := close
    lsCounterx := 1
    lsmaBarCounterx := bar_index
if ta.crossunder(customSource, lsmaCustom) and lsCounterx == 1 and crossUnderOver == "Crossover"
    lsCounterx := 0
    lsPerformance1X := ((close / lsPerformanceX) - 1) * 100
    lsPerformance1X := lsPerformance1X[1] + lsPerformance1X
    lsCounter2x += 1
    lsmaBarCounterx1 := bar_index - lsmaBarCounterx
    lsmaBarCounterx1 := lsmaBarCounterx1[1] + lsmaBarCounterx1

lsmaCalculationx = lsPerformance1X / lsCounter2x
lsmaCalculationBarx = lsmaBarCounterx1 / lsCounter2x

// DEMA
var int dCounterx = 0
var int dCounter2x = 0
var float dPerformanceX = 0.0
var float dPerformance1X = 0.0
var int demaBarCounterx = 0
var int demaBarCounterx1 = 0
if ta.crossover(customSource, demaCustom) and dCounterx == 0 and crossUnderOver == "Crossover"
    dPerformanceX := close
    dCounterx := 1
    demaBarCounterx := bar_index
if ta.crossunder(customSource, demaCustom) and dCounterx == 1 and crossUnderOver == "Crossover"
    dCounterx := 0
    dPerformance1X := ((close / dPerformanceX) - 1) * 100
    dPerformance1X := dPerformance1X[1] + dPerformance1X
    dCounter2x += 1
    demaBarCounterx1 := bar_index - demaBarCounterx
    demaBarCounterx1 := demaBarCounterx1[1] + demaBarCounterx1

demaCalculationx = dPerformance1X / dCounter2x
demaCalculationBarx = demaBarCounterx1 / dCounter2x

// TEMA
var int tCounterx = 0
var int tCounter2x = 0
var float tPerformanceX = 0.0
var float tPerformance1X = 0.0
var int temaBarCounterx = 0
var int temaBarCounterx1 = 0
if ta.crossover(customSource, temaCustom) and tCounterx == 0 and crossUnderOver == "Crossover"
    tPerformanceX := close
    tCounterx := 1
    temaBarCounterx := bar_index
if ta.crossunder(customSource, temaCustom) and tCounterx == 1 and crossUnderOver == "Crossover"
    tCounterx := 0
    tPerformance1X := ((close / tPerformanceX) - 1) * 100
    tPerformance1X := tPerformance1X[1] + tPerformance1X
    tCounter2x += 1
    temaBarCounterx1 := bar_index - temaBarCounterx
    temaBarCounterx1 := temaBarCounterx1[1] + temaBarCounterx1

temaCalculationx = tPerformance1X / tCounter2x
temaCalculationBarx = temaBarCounterx1 / tCounter2x

// T3
var int t5Counterx = 0
var int t5Counter2x = 0
var float t5PerformanceX = 0.0
var float t5Performance1X = 0.0
var int Tilson3BarCounterx = 0
var int Tilson3BarCounterx1 = 0
if ta.crossover(customSource, Tilson3Average) and t5Counterx == 0 and crossUnderOver == "Crossover"
    t5PerformanceX := close
    t5Counterx := 1
    Tilson3BarCounterx := bar_index
if ta.crossunder(customSource, Tilson3Average) and t5Counterx == 1 and crossUnderOver == "Crossover"
    t5Counterx := 0
    t5Performance1X := ((close / t5PerformanceX) - 1) * 100
    t5Performance1X := t5Performance1X[1] + t5Performance1X
    t5Counter2x += 1
    Tilson3BarCounterx1 := bar_index - Tilson3BarCounterx
    Tilson3BarCounterx1 := Tilson3BarCounterx1[1] + Tilson3BarCounterx1

calculationTilson3x = t5Performance1X / t5Counter2x
calculationTilson3Barx = Tilson3BarCounterx1 / t5Counter2x

// Crossunders -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// SMA
if ta.crossunder(benchmarkSource, smaBenchmark) and smaCounter == 0 and crossUnderOver == "Crossunder"
    smaPerformance := close
    smaCounter := 1
    smaBarCounter := bar_index
if ta.crossover(benchmarkSource, smaBenchmark) and smaCounter == 1 and crossUnderOver == "Crossunder"
    smaCounter := 0
    smaPerformance1 := ((close / smaPerformance) - 1) * 100
    smaPerformance1 := smaPerformance1[1] + smaPerformance1
    smaCounter1 += 1
    smaBarCounter1 := bar_index - smaBarCounter 
    smaBarCounter1 := smaBarCounter1[1] + smaBarCounter1

finsmapU = smaPerformance1 / smaCounter1
fincountsU = smaBarCounter1 / smaCounter1

// EMA
if ta.crossunder(benchmarkSource, emaBenchmark) and emaCounter == 0 and crossUnderOver == "Crossunder"
    emaPerformance := close
    emaCounter := 1
    emaBarCounter := bar_index
if ta.crossover(benchmarkSource, emaBenchmark) and emaCounter == 1 and crossUnderOver == "Crossunder"
    emaCounter := 0
    emaPerformance1 := ((close / emaPerformance) - 1) * 100
    emaPerformance1 := emaPerformance1[1] + emaPerformance1
    emaCounter1 += 1
    emaBarCounter1 := bar_index - emaBarCounter
    emaBarCounter1 := emaBarCounter1[1] + emaBarCounter1

finemapU = emaPerformance1 / emaCounter1
fincounteU = emaBarCounter1 / emaCounter1

// ALMA
if ta.crossunder(benchmarkSource, almaBenchmark) and almaCounter == 0 and crossUnderOver == "Crossunder"
    almaPerformance := close
    almaCounter := 1
    almaBarCounter := bar_index
if ta.crossover(benchmarkSource, almaBenchmark) and almaCounter == 1 and crossUnderOver == "Crossunder"
    almaCounter := 0
    almaPerformance1 := ((close / almaPerformance) - 1) * 100
    almaPerformance1 := almaPerformance1[1] + almaPerformance1
    almaCounter1 += 1
    almaBarCounter1 := bar_index - almaBarCounter
    almaBarCounter1 := almaBarCounter1[1] + almaBarCounter1

finalmapU = (almaPerformance1 / almaCounter1)
fincountaU = almaBarCounter1 / almaCounter1

// HMA
if ta.crossunder(benchmarkSource, hmaBenchmark) and hmaCounter == 0 and crossUnderOver == "Crossunder"
    hmaPerformance := close
    hmaCounter := 1
    hmaBarCounter := bar_index
if ta.crossover(benchmarkSource, hmaBenchmark) and hmaCounter == 1 and crossUnderOver == "Crossunder"
    hmaCounter := 0
    hmaPerformance1 := ((close / hmaPerformance) - 1) * 100
    hmaPerformance1 := hmaPerformance1[1] + hmaPerformance1
    hmaCounter1 += 1
    hmaBarCounter1 := bar_index - hmaBarCounter
    hmaBarCounter1 := hmaBarCounter1[1] + hmaBarCounter1

finhmapU = (hmaPerformance1 / hmaCounter1)
fincounthU = hmaBarCounter1 / hmaCounter1


// WMA
if ta.crossunder(benchmarkSource, wmaBenchmark) and wmaCounter == 0 and crossUnderOver == "Crossunder"
    wmaPerformance := close
    wmaCounter := 1
    wmaBarCounter := bar_index
if ta.crossover(benchmarkSource, wmaBenchmark) and wmaCounter == 1 and crossUnderOver == "Crossunder"
    wmaCounter := 0
    wmaPerformance1 := ((close / wmaPerformance) - 1) * 100
    wmaPerformance1 := wmaPerformance1[1] + wmaPerformance1
    wmaCounter1 += 1
    wmaBarCounter1 := bar_index - wmaBarCounter
    wmaBarCounter1 := wmaBarCounter1[1] + wmaBarCounter1

finwmapU = (wmaPerformance1 / wmaCounter1)
fincountwU = wmaBarCounter1 / wmaCounter1


// VWMA
if ta.crossunder(benchmarkSource, vwmaBenchmark) and vwmaCounter == 0 and crossUnderOver == "Crossunder"
    vwmaPerformance := close
    vwmaCounter := 1
    vwmaBarCounter := bar_index
if ta.crossover(benchmarkSource, vwmaBenchmark) and vwmaCounter == 1 and crossUnderOver == "Crossunder"
    vwmaCounter := 0
    vwmaPerformance1 := ((close / vwmaPerformance) - 1) * 100
    vwmaPerformance1 := vwmaPerformance1[1] + vwmaPerformance1
    vwmaCounter1 += 1
    vwmaBarCounter1 := bar_index - vwmaBarCounter
    vwmaBarCounter1 := vwmaBarCounter1[1] + vwmaBarCounter1

finvwmapU = (vwmaPerformance1 / vwmaCounter1)
fincountvwU = vwmaBarCounter1 / vwmaCounter1

// ls
if ta.crossunder(benchmarkSource, lsmaBenchmark) and lsmaCounter == 0 and crossUnderOver == "Crossunder"
    lsmaPerformance := close
    lsmaCounter := 1
    lsmaBarCounter := bar_index
if ta.crossover(benchmarkSource, lsmaBenchmark) and lsmaCounter == 1 and crossUnderOver == "Crossunder"
    lsmaCounter := 0
    lsmaPerformance1 := ((close / lsmaPerformance) - 1) * 100
    lsmaPerformance1 := lsmaPerformance1[1] + lsmaPerformance1
    lsmaCounter1 += 1
    lsmaBarCounter1 := bar_index - lsmaBarCounter
    lsmaBarCounter1 := lsmaBarCounter1[1] + lsmaBarCounter1

finlspU = (lsmaPerformance1 / lsmaCounter1)
fincountlsU = lsmaBarCounter1 / lsmaCounter1

// DEMA 
if ta.crossunder(benchmarkSource, demaBenchmark) and demaCounter == 0 and crossUnderOver == "Crossunder"
    demaPerformance := close
    demaCounter := 1
    demaBarCounter := bar_index
if ta.crossover(benchmarkSource, demaBenchmark) and demaCounter == 1 and crossUnderOver == "Crossunder"
    demaCounter := 0
    demaPerformance1 := ((close / demaPerformance) - 1) * 100
    demaPerformance1 := demaPerformance1[1] + demaPerformance1
    demaCounter1 += 1
    demaBarCounter1 := bar_index - demaBarCounter
    demaBarCounter1 := demaBarCounter1[1] + demaBarCounter1

findpU = (demaPerformance1 / demaCounter1)
fincountdU = demaBarCounter1 / demaCounter1

// TEMA
if ta.crossunder(benchmarkSource, temaBenchmark) and temaCounter == 0 and crossUnderOver == "Crossunder"
    temaPerformance := close
    temaCounter := 1
    temaBarCounter := bar_index
if ta.crossover(benchmarkSource, temaBenchmark) and temaCounter == 1 and crossUnderOver == "Crossunder"
    temaCounter := 0
    temaPerformance1 := ((close / temaPerformance) - 1) * 100
    temaPerformance1 := temaPerformance1[1] + temaPerformance1
    temaCounter1 += 1
    temaBarCounter1 := bar_index - temaBarCounter
    temaBarCounter1 := temaBarCounter1[1] + temaBarCounter1

fintpU = (temaPerformance1 / temaCounter1)
fincounttU = temaBarCounter1 / temaCounter1

// T3
if ta.crossunder(benchmarkSource, Tilson3Benchmark) and Tilson3Counter == 0 and crossUnderOver == "Crossunder"
    Tilson3Performance := close
    Tilson3Counter := 1
    Tislon3BarCounter := bar_index
if ta.crossover(benchmarkSource, Tilson3Benchmark) and Tilson3Counter == 1 and crossUnderOver == "Crossunder"
    Tilson3Counter := 0
    Tilson3Performance1 := ((close / Tilson3Performance) - 1) * 100
    Tilson3Performance1 := Tilson3Performance1[1] + Tilson3Performance1
    Tilson3Counter1 += 1
    Tislon3BarCounter1 := bar_index - Tislon3BarCounter
    Tislon3BarCounter1 := Tislon3BarCounter1[1] + Tislon3BarCounter1

fint5pU = (Tilson3Performance1 / Tilson3Counter1)
fincountt5U = Tislon3BarCounter1 / Tilson3Counter1

// SMA
if ta.crossunder(customSource, smaCustom) and smaCounterx == 0 and crossUnderOver == "Crossunder"
    smaPerformanceX := close
    smaCounterx := 1
    smaCounterBarx := bar_index
if ta.crossover(customSource, smaCustom) and smaCounterx == 1 and crossUnderOver == "Crossunder"
    smaCounterx := 0
    smaPerformance1X := ((close / smaPerformanceX) - 1) * 100
    smaPerformance1X := smaPerformance1X[1] + smaPerformance1X
    smaCounter2x += 1
    smaCounterBarx1 := bar_index - smaCounterBarx
    smaCounterBarx1 := smaCounterBarx1[1] + smaCounterBarx1 

smaCalculationxU = (smaPerformance1X / smaCounter2x)
fincountsxU = smaCounterBarx1 / smaCounter2x

// EMA
if ta.crossunder(customSource, emaCustom) and emaCounterx == 0 and crossUnderOver == "Crossunder"
    emaPerformanceX := close
    emaCounterx := 1
    emaBarCounterx := bar_index
if ta.crossover(customSource, emaCustom) and emaCounterx == 1 and crossUnderOver == "Crossunder"
    emaCounterx := 0
    emaPerformance1X := ((close / emaPerformanceX) - 1) * 100
    emaPerformance1X := emaPerformance1X[1] + emaPerformance1X
    emaCounter2x += 1
    emaBarCounterx1 := bar_index - emaBarCounterx
    emaBarCounterx1 := emaBarCounterx1[1] + emaBarCounterx1

emaCaculationxU = (emaPerformance1X / emaCounter2x)
finemaBarCounterxU = emaBarCounterx1 / emaCounter2x
// ALMA

if ta.crossunder(customSource, almaCustom) and almaCounterx == 0 and crossUnderOver == "Crossunder"
    almaPerformanceX := close
    almaCounterx := 1
    almaBarCounterx := bar_index
if ta.crossover(customSource, almaCustom) and almaCounterx == 1 and crossUnderOver == "Crossunder"
    almaCounterx := 0
    almaPerformance1X := ((close / almaPerformanceX) - 1) * 100
    almaPerformance1X := almaPerformance1X[1] + almaPerformance1X
    almaCounter2x += 1
    almaBarCounterx1 := bar_index - almaBarCounterx
    almaBarCounterx1 := almaBarCounterx1[1] + almaBarCounterx1

finalmaPerformanceXU = (almaPerformance1X / almaCounter2x)
fincountaxU = almaBarCounterx1 / almaCounter2x

// HMA
if ta.crossunder(customSource, hmaCustom) and hmaCounterx == 0 and crossUnderOver == "Crossunder"
    hmaPerformanceX := close
    hmaCounterx := 1
    hmaBarCounterx := bar_index
if ta.crossover(customSource, hmaCustom) and hmaCounterx == 1 and crossUnderOver == "Crossunder"
    hmaCounterx := 0
    hmaPerformance1X := ((close / hmaPerformanceX) - 1) * 100
    hmaPerformance1X := hmaPerformance1X[1] + hmaPerformance1X
    hmaCounter2x += 1
    hmaBarCounterx1 := bar_index - hmaBarCounterx
    hmaBarCounterx1 := hmaBarCounterx1[1] + hmaBarCounterx1

finhmaPerformanceXU = (hmaPerformance1X / hmaCounter2x)
fincounthxU = hmaBarCounterx1 / hmaCounter2x

// WMA
if ta.crossunder(customSource, wmaCustom) and wmaCounterx == 0 and crossUnderOver == "Crossunder"
    wmaPerformanceX := close
    wmaCounterx := 1
    wmaBarCounterx := bar_index
if ta.crossover(customSource, wmaCustom) and wmaCounterx == 1 and crossUnderOver == "Crossunder"
    wmaCounterx := 0
    wmaPerformance1X := ((close / wmaPerformanceX) - 1) * 100
    wmaPerformance1X := wmaPerformance1X[1] + wmaPerformance1X
    wmaCounter2x += 1
    wmaBarCounterx1 := bar_index - wmaBarCounterx
    wmaBarCounterx1 := wmaBarCounterx1[1] + wmaBarCounterx1

finwmaPerformanceXU = (wmaPerformance1X / wmaCounter2x)
fincountwxU = wmaBarCounterx1 / wmaCounter2x

// VWMA
if ta.crossunder(customSource, vwmaCustom) and vwmaCounterx == 0 and crossUnderOver == "Crossunder"
    vwmaPerformanceX := close
    vwmaCounterx := 1
    vwmaBarCounterx := bar_index
if ta.crossover(customSource, vwmaCustom) and vwmaCounterx == 1 and crossUnderOver == "Crossunder"
    vwmaCounterx := 0
    vwmaPerformance1X := ((close / vwmaPerformanceX) - 1) * 100
    vwmaPerformance1X := vwmaPerformance1X[1] + vwmaPerformance1X
    vwmaCounter2x += 1
    vwmaBarCounterx1 := bar_index - vwmaBarCounterx
    vwmaBarCounterx1 := vwmaBarCounterx1[1] + vwmaBarCounterx1

finvwmaPerformanceXU = (vwmaPerformance1X / vwmaCounter2x)
fincountvwxU = vwmaBarCounterx1 / vwmaCounter2x

// ls
if ta.crossunder(customSource, lsmaCustom) and lsCounterx == 0 and crossUnderOver == "Crossunder"
    lsPerformanceX := close
    lsCounterx := 1
    lsmaBarCounterx := bar_index
if ta.crossover(customSource, lsmaCustom) and lsCounterx == 1 and crossUnderOver == "Crossunder"
    lsCounterx := 0
    lsPerformance1X := ((close / lsPerformanceX) - 1) * 100
    lsPerformance1X := lsPerformance1X[1] + lsPerformance1X
    lsCounter2x += 1
    lsmaBarCounterx1 := bar_index - lsmaBarCounterx
    lsmaBarCounterx1 := lsmaBarCounterx1[1] + lsmaBarCounterx1

finlsPerformanceXU = (lsPerformance1X / lsCounter2x)
fincountlsxU = lsmaBarCounterx1 / lsCounter2x

// DEMA
if ta.crossunder(customSource, demaCustom) and dCounterx == 0 and crossUnderOver == "Crossunder"
    dPerformanceX := close
    dCounterx := 1
    demaBarCounterx := bar_index
if ta.crossover(customSource, demaCustom) and dCounterx == 1 and crossUnderOver == "Crossunder"
    dCounterx := 0
    dPerformance1X := ((close / dPerformanceX) - 1) * 100
    dPerformance1X := dPerformance1X[1] + dPerformance1X
    dCounter2x += 1
    demaBarCounterx1 := bar_index - demaBarCounterx
    demaBarCounterx1 := demaBarCounterx1[1] + demaBarCounterx1

findPerformanceXU = (dPerformance1X / dCounter2x)
fincountdxU = demaBarCounterx1 / dCounter2x

// TEMA
if ta.crossunder(customSource, temaCustom) and tCounterx == 0 and crossUnderOver == "Crossunder"
    tPerformanceX := close
    tCounterx := 1
    temaBarCounterx := bar_index
if ta.crossover(customSource, temaCustom) and tCounterx == 1 and crossUnderOver == "Crossunder"
    tCounterx := 0
    tPerformance1X := ((close / tPerformanceX) - 1) * 100
    tPerformance1X := tPerformance1X[1] + tPerformance1X
    tCounter2x += 1
    temaBarCounterx1 := bar_index - temaBarCounterx
    temaBarCounterx1 := temaBarCounterx1[1] + temaBarCounterx1

fintPerformanceXU = (tPerformance1X / tCounter2x)
fincounttxU = temaBarCounterx1 / tCounter2x

// T3
if ta.crossunder(customSource, Tilson3Average) and t5Counterx == 0 and crossUnderOver == "Crossunder"
    t5PerformanceX := close
    t5Counterx := 1
    Tilson3BarCounterx := bar_index
if ta.crossover(customSource, Tilson3Average) and t5Counterx == 1 and crossUnderOver == "Crossunder"
    t5Counterx := 0
    t5Performance1X := ((close / t5PerformanceX) - 1) * 100
    t5Performance1X := t5Performance1X[1] + t5Performance1X
    t5Counter2x += 1
    Tilson3BarCounterx1 := bar_index - Tilson3BarCounterx
    Tilson3BarCounterx1 := Tilson3BarCounterx1[1] + Tilson3BarCounterx1

fint5PerformanceXU = (t5Performance1X / t5Counter2x)
fincountt5xU = Tilson3BarCounterx1 / t5Counter2x

// FixNan
if na(smaCalculation)
    smaCalculation := 0.0
if na(emaCalculation) 
    emaCalculation := 0.0
if na(almaCalculation) 
    almaCalculation := 0.0
if na(hmaCalculation) 
    hmaCalculation := 0.0
if na(wmaCalculation)
    wmaCalculation := 0.0
if na(vwmaCalculation)
    wmaCalculation := 0.0
if na(lsmaCalculation)
    lsmaCalculation := 0.0
if na(demaCalculation)
    demaCalculation := 0.0
if na(temaCalculation)
    temaCalculation := 0.0
if na(Tilson3Calculation)
    Tilson3Calculation := 0.0

if na(smaCalculationx)
    smaCalculationx := 0.0
if na(emaCaculationx) 
    emaCaculationx := 0.0
if na(almaCalculationx) 
    almaCalculationx := 0.0
if na(hmaCalculationx) 
    hmaCalculationx := 0.0
if na(wmaCalculationx)
    wmaCalculationx := 0.0
if na(vwmaCalculationx)
    wmaCalculationx := 0.0
if na(lsmaCalculationx)
    lsmaCalculationx := 0.0
if na(demaCalculationx)
    demaCalculationx := 0.0
if na(temaCalculationx)
    temaCalculationx := 0.0
if na(calculationTilson3x)
    calculationTilson3x := 0.0

if na(finsmapU)
    finsmapU := 0.0
if na(finemapU) 
    finemapU := 0.0
if na(finalmapU) 
    finalmapU := 0.0
if na(finhmapU) 
    finhmapU := 0.0
if na(finwmapU)
    finwmapU := 0.0
if na(finvwmapU)
    finwmapU := 0.0
if na(finlspU)
    finlspU := 0.0
if na(findpU)
    findpU := 0.0
if na(fintpU)
    fintpU := 0.0
if na(fint5pU)
    fint5pU := 0.0

if na(smaCalculationxU)
    smaCalculationxU := 0.0
if na(emaCaculationxU) 
    emaCaculationxU := 0.0
if na(finalmaPerformanceXU) 
    finalmaPerformanceXU := 0.0
if na(finhmaPerformanceXU) 
    finhmaPerformanceXU := 0.0
if na(finwmaPerformanceXU)
    finwmaPerformanceXU := 0.0
if na(finvwmaPerformanceXU)
    finwmaPerformanceXU := 0.0
if na(finlsPerformanceXU)
    finlsPerformanceXU := 0.0
if na(findPerformanceXU)
    findPerformanceXU := 0.0
if na(fintPerformanceXU)
    fintPerformanceXU := 0.0
if na(fint5PerformanceXU)
    fint5PerformanceXU := 0.0

// Array Sortment
f_sort_by_array(_array1, _array2, _array3, _array4) =>
    a1s    = array.size(_array1)
    a1so  = array.new_string(a1s)
    a3so = array.copy(_array3)
    a4so = array.copy(_array4)
    a2so  = array.copy(_array2)
    if a1s == array.size(_array2) and a1s == array.size(_array2)
        array.sort(a2so, order.descending)
        for i = 0 to a1s - 1
            sv    = array.get(a2so, i)
            ui  = math.max(0, array.indexof(_array2, sv))
            uv  = array.get (_array1, ui)
            uv2 = array.get(_array3, ui)
            uv3 = array.get(_array4, ui)
            array.set(a1so, i, uv)
            array.set(a3so, i, uv2)
            array.set(a4so, i, uv3)
    
    [a1so, a2so, a3so, a4so]

f_sort_by_array(_array1x, _array2x) =>
    a1sx    = array.size(_array1x)
    a1sox  = array.new_string(a1sx)
    a2sox = array.copy(_array2x)
    if a1sx == array.size(_array2x) 
        array.sort(a2sox, order.descending)
        for i = 0 to a1sx - 1
            svx    = array.get(a2sox, i)
            uix  = math.max(0, array.indexof(_array2x, svx))
            uvx  = array.get (_array1x, uix)
            array.set(a1sox, i, uvx)

    [a1sox, a2sox]

// Array Set
best = array.new_float(10)
array.set(best, 0, dataType == "Benchmark" ? smaPerformance1 : smaPerformance1X)  
array.set(best, 1, dataType == "Benchmark" ? emaPerformance1 : emaPerformance1X) 
array.set(best, 2,dataType == "Benchmark" ?  almaPerformance1 : almaPerformance1X)
array.set(best, 3, dataType == "Benchmark" ? hmaPerformance1 : hmaPerformance1X) 
array.set(best, 4, dataType == "Benchmark" ? wmaPerformance1 : wmaPerformance1X)
array.set(best, 5, dataType == "Benchmark" ? vwmaPerformance1 : vwmaPerformance1X)
array.set(best, 6, dataType == "Benchmark" ? lsmaPerformance1 : lsPerformance1X)
array.set(best, 7, dataType == "Benchmark" ? demaPerformance1 : dPerformance1X)
array.set(best, 8, dataType == "Benchmark" ? temaPerformance1 : tPerformance1X)
array.set(best, 9, dataType == "Benchmark" ? Tilson3Performance1 : t5Performance1X)
best1 = array.new_string(10)
array.set(best1, 0, "SMA"), 
array.set(best1, 1, "EMA"), 
array.set(best1, 2, "ALMA")
array.set(best1, 3, "HMA"), 
array.set(best1, 4, "WMA"), 
array.set(best1, 5, "VWMA")
array.set(best1, 6, "LSMA")
array.set(best1, 7, "DEMA")
array.set(best1, 8, "TEMA")
array.set(best1, 9, "T3")

[cumulativePerformanceNameSorted, cumulativePerformanceSorted] = f_sort_by_array(best1, best)

array.reverse(cumulativePerformanceSorted)
array.reverse(cumulativePerformanceNameSorted)


// Array Set 2
orgname   = array.new_string(10)
org  = array.new_float(10)
org1 = array.new_float(10)
org2 = array.new_int(10)
if crossUnderOver == "Crossover"
    sma = smaCalculation, array.set(orgname, 0, "SMA"), 
         array.set(org, 0, dataType == "Benchmark" ? smaCalculation : smaCalculationx), 
             array.set(org1, 0, dataType == "Benchmark" ? smaCalculationBar : smaCalculationBarx), 
                 array.set(org2, 0, dataType == "Benchmark" ? smaCounter1 : smaCounter2x)
if crossUnderOver == "Crossunder"
    smaU = finsmapU, array.set(orgname, 0, "SMA"), 
         array.set(org, 0, dataType == "Benchmark" ? finsmapU : smaCalculationxU), 
             array.set(org1, 0, dataType == "Benchmark" ? fincountsU : fincountsxU), 
                 array.set(org2, 0, dataType == "Benchmark" ? smaCounter1 : smaCounter2x)
if crossUnderOver == "Crossover"
    ema = emaCalculation, array.set(orgname, 1, "EMA"), 
         array.set(org, 1, dataType == "Benchmark" ? emaCalculation : emaCaculationx), 
             array.set(org1, 1, dataType == "Benchmark" ? emaCalculationBar : emaCaculationBarx), 
                 array.set(org2, 1, dataType == "Benchmark" ? emaCounter1 : emaCounter2x)
if crossUnderOver == "Crossover"
    alma = almaCalculation, array.set(orgname, 2, "ALMA"), 
         array.set(org, 2, dataType == "Benchmark" ? almaCalculation : almaCalculationx), 
             array.set(org1, 2, dataType == "Benchmark" ? almaCalculationBar : almaBarCalculationx), 
                 array.set(org2, 2, dataType == "Benchmark" ? almaCounter1 : almaCounter2x)
if crossUnderOver == "Crossover"
    hma = hmaCalculation, array.set(orgname, 3, "HMA"), 
         array.set(org, 3, dataType == "Benchmark" ? hmaCalculation : hmaCalculationx), 
             array.set(org1, 3, dataType == "Benchmark" ? hmaCalculationBar : hmaCalculationBarx), 
                 array.set(org2, 3, dataType == "Benchmark" ? hmaCounter1 : hmaCounter2x)
if crossUnderOver == "Crossover" 
    wma = wmaCalculation, array.set(orgname, 4, "WMA"), 
         array.set(org, 4, dataType == "Benchmark" ? wmaCalculation : wmaCalculationx), 
             array.set(org1, 4, dataType == "Benchmark" ? wmaCalculationBar : wmaCalculationBarx), 
                 array.set(org2, 4, dataType == "Benchmark" ? wmaCounter1 : wmaCounter2x)
if crossUnderOver == "Crossover"
    vwma = vwmaCalculation, array.set(orgname, 5, "VWMA"), 
          array.set(org, 5, dataType == "Benchmark" ? vwmaCalculation : vwmaCalculationx), 
             array.set(org1, 5, dataType == "Benchmark" ? vwmaCalculationBar : vwmaCalculationBarx), 
                 array.set(org2, 5, dataType == "Benchmark" ? vwmaCounter1 : vwmaCounter2x)
if crossUnderOver == "Crossover"
    lsma = lsmaCalculation, array.set(orgname, 6, "LSMA"),
          array.set(org, 6,dataType == "Benchmark" ? lsmaCalculation : lsmaCalculationx), 
             array.set(org1, 6, dataType == "Benchmark" ? lsmaCalculationBar : lsmaCalculationBarx), 
                 array.set(org2, 6, dataType == "Benchmark" ? lsmaCounter1 : lsCounter2x)
if crossUnderOver == "Crossover"
    dema = demaCalculation, array.set(orgname, 7, "DEMA"), 
         array.set(org, 7, dataType == "Benchmark" ? demaCalculation : demaCalculationx), 
             array.set(org1, 7, dataType == "Benchmark" ? demaCalculationBar : demaCalculationBarx), 
                 array.set(org2, 7, dataType == "Benchmark" ? demaCounter1 : dCounter2x)
if crossUnderOver == "Crossover"
    tema = temaCalculation, array.set(orgname, 8, "TEMA"), 
         array.set(org, 8, dataType == "Benchmark" ? temaCalculation : temaCalculationx), 
             array.set(org1, 8, dataType == "Benchmark" ? temaCalculationBar : temaCalculationBarx), 
                 array.set(org2, 8, dataType == "Benchmark" ? temaCounter1 : tCounter2x)
if crossUnderOver == "Crossover"
    T3 = Tilson3Calculation, array.set(orgname, 9, "T3"),
         array.set(org, 9,dataType == "Benchmark" ? Tilson3Calculation : calculationTilson3x), 
             array.set(org1, 9, dataType == "Benchmark" ? Tilson3CalculationBar : calculationTilson3Barx), 
                  array.set(org2, 9, dataType == "Benchmark" ? Tilson3Counter1 : t5Counter2x)        

if crossUnderOver == "Crossunder"
    smaU = finsmapU, array.set(orgname, 0, "SMA"), 
         array.set(org, 0, dataType == "Benchmark" ? finsmapU : smaCalculationxU), 
             array.set(org1, 0, dataType == "Benchmark" ? fincountsU : fincountsxU), 
                 array.set(org2, 0, dataType == "Benchmark" ? smaCounter1 : smaCounter2x)
if crossUnderOver == "Crossunder"
    emaU = finemapU, array.set(orgname, 1, "EMA"), 
         array.set(org, 1, dataType == "Benchmark" ? finemapU : emaCaculationxU), 
             array.set(org1, 1, dataType == "Benchmark" ? fincounteU : finemaBarCounterxU), 
                 array.set(org2, 1, dataType == "Benchmark" ? emaCounter1 : emaCounter2x)
if crossUnderOver == "Crossunder"
    almaU = finalmapU, array.set(orgname, 2, "ALMA"), 
         array.set(org, 2, dataType == "Benchmark" ? finalmapU : finalmaPerformanceXU), 
             array.set(org1, 2, dataType == "Benchmark" ? fincountaU : fincountaxU), 
                 array.set(org2, 2, dataType == "Benchmark" ? almaCounter1 : almaCounter2x)
if crossUnderOver == "Crossunder"
    hmaU = finhmapU, array.set(orgname, 3, "HMA"), 
         array.set(org, 3, dataType == "Benchmark" ? finhmapU : finhmaPerformanceXU), 
             array.set(org1, 3, dataType == "Benchmark" ? fincounthU : fincounthxU), 
                 array.set(org2, 3, dataType == "Benchmark" ? hmaCounter1 : hmaCounter2x)
if crossUnderOver == "Crossunder" 
    wmaU = finwmapU, array.set(orgname, 4, "WMA"), 
         array.set(org, 4, dataType == "Benchmark" ? finwmapU : finwmaPerformanceXU), 
             array.set(org1, 4, dataType == "Benchmark" ? fincountwU : fincountwxU), 
                 array.set(org2, 4, dataType == "Benchmark" ? wmaCounter1 : wmaCounter2x)
if crossUnderOver == "Crossunder"
    vwmaU = finvwmapU, array.set(orgname, 5, "VWMA"), 
          array.set(org, 5, dataType == "Benchmark" ? finvwmapU : finvwmaPerformanceXU), 
             array.set(org1, 5, dataType == "Benchmark" ? fincountvwU : fincountvwxU), 
                 array.set(org2, 5, dataType == "Benchmark" ? vwmaCounter1 : vwmaCounter2x)
if crossUnderOver == "Crossunder"
    lsmaU = finlspU, array.set(orgname, 6, "LSMA"),
          array.set(org, 6,dataType == "Benchmark" ? finlspU : finlsPerformanceXU), 
             array.set(org1, 6, dataType == "Benchmark" ? fincountlsU : fincountlsxU), 
                 array.set(org2, 6, dataType == "Benchmark" ? lsmaCounter1 : lsCounter2x)
if crossUnderOver == "Crossunder"
    demaU = findpU, array.set(orgname, 7, "DEMA"), 
         array.set(org, 7, dataType == "Benchmark" ? findpU : findPerformanceXU), 
             array.set(org1, 7, dataType == "Benchmark" ? fincountdU : fincountdxU), 
                 array.set(org2, 7, dataType == "Benchmark" ? demaCounter1 : dCounter2x)
if crossUnderOver == "Crossunder"
    temaU = fintpU, array.set(orgname, 8, "TEMA"), 
         array.set(org, 8, dataType == "Benchmark" ? fintpU : fintPerformanceXU), 
             array.set(org1, 8, dataType == "Benchmark" ? fincounttU : fincounttxU), 
                 array.set(org2, 8, dataType == "Benchmark" ? temaCounter1 : tCounter2x)
if crossUnderOver == "Crossunder"
    T3U = fint5pU, array.set(orgname, 9, "T3"),
         array.set(org, 9,dataType == "Benchmark" ? fint5pU : fint5PerformanceXU), 
             array.set(org1, 9, dataType == "Benchmark" ? fincountt5U : fincountt5xU), 
                  array.set(org2, 9, dataType == "Benchmark" ? Tilson3Counter1 : t5Counter2x)        
                          

[performanceNameSorted, performanceSorted, barsSorted, crossesSorted] = f_sort_by_array(orgname, org, org1, org2)

// Math Assignments
MAmax = math.max(smaCustom, emaCustom, almaCustom, hmaCustom, wmaCustom, vwmaCustom, lsmaCustom, demaCustom, temaCustom, Tilson3Average)
MAmin = math.min(smaCustom, emaCustom, almaCustom, hmaCustom, wmaCustom, vwmaCustom, lsmaCustom, demaCustom, temaCustom, Tilson3Average)
MAmaxBenchmark = math.max(smaBenchmark, emaBenchmark, almaBenchmark, hmaBenchmark, wmaBenchmark, vwmaBenchmark, lsmaBenchmark, demaBenchmark, temaBenchmark, Tilson3Benchmark)
MAminBenchmark = math.min(smaBenchmark, emaBenchmark, almaBenchmark, hmaBenchmark, wmaBenchmark, vwmaBenchmark, lsmaBenchmark, demaBenchmark, temaBenchmark, Tilson3Benchmark)
averageMax = math.max(smaCalculation, emaCalculation, almaCalculation, hmaCalculation, wmaCalculation, vwmaCalculation, lsmaCalculation, demaCalculation, temaCalculation, Tilson3Calculation)
averageMaxx = math.max(smaCalculationx, emaCaculationx, almaCalculationx, hmaCalculationx, wmaCalculationx, vwmaCalculationx, lsmaCalculationx, demaCalculationx, temaCalculationx, calculationTilson3x)
averageMin = math.min(smaCalculation, emaCalculation, almaCalculation, hmaCalculation, wmaCalculation, vwmaCalculation, lsmaCalculation, demaCalculation, temaCalculation, Tilson3Calculation)
averageMinx = math.min(smaCalculationx, emaCaculationx, almaCalculationx, hmaCalculationx, wmaCalculationx, vwmaCalculationx, lsmaCalculationx, demaCalculationx, temaCalculationx, calculationTilson3x)

// MA Plot
xx = plot(MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Custom" ? smaCustom : MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Benchmark" ? smaBenchmark
     : na, 
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.max(best) == array.get(best, 0) ? color.from_gradient(close, smaCustom - 10, smaCustom + 10, #ff0500, #03ff00) : 
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) != array.get(best,0) and show == "Full" ? color.green : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) != array.get(best,0) and show == "Light" ? color.new(color.black, 50) : 
                     MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax == smaCalculation ? color.from_gradient(close, smaCustom - 10, smaCustom + 10, #ff0500, #03ff00) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMax != smaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax != smaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx == smaCalculationx ? color.from_gradient(close, smaCustom - 10, smaCustom + 10, #ff0500, #03ff00) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMaxx != smaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx != smaCalculationx and show == "Light" ? color.new(color.black, 50):
                                             color.new(color.black, 100), 
                                                 linewidth = 2
                 )
                
                
x1 = plot(MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Custom" ? emaCustom : MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Benchmark" ? emaBenchmark
     : na, 
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) == array.get(best, 1) ? color.from_gradient(close, emaCustom - 10, emaCustom + 10, #ff0500, #03ff00) : 
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) != array.get(best,1) and show == "Full" ? color.orange : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) != array.get(best,1) and show == "Light" ? color.new(color.black, 50) :          
                     MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax == emaCalculation ? color.from_gradient(close, emaCustom - 10, emaCustom + 10, #ff0500, #03ff00) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMax != emaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax != emaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx == emaCaculationx ? color.from_gradient(close, emaCustom - 10, emaCustom + 10, #ff0500, #03ff00) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMaxx != emaCaculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx != emaCaculationx and show == "Light" ? color.new(color.black, 50):
                                              color.new(color.black, 100), 
                                                 linewidth = 2
                 )
                
                
x2 = plot(MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Custom" ? almaCustom : MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Benchmark" ? almaBenchmark
     : na, 
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) == array.get(best, 2) ? color.from_gradient(close, almaCustom - 10, almaCustom + 10, #ff0500, #03ff00) : 
             MA == "Best Performing"  and seriesPlot == "Cumulative Gain"   and array.max(best) != array.get(best,2) and show == "Full" ? color.yellow : 
                  MA == "Best Performing"  and seriesPlot == "Cumulative Gain"   and array.max(best) != array.get(best,2) and show == "Light" ? color.new(color.black, 50) : 
                     MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax == almaCalculation ? color.from_gradient(close, almaCustom - 10, almaCustom + 10, #ff0500, #03ff00) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMax != almaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax != almaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx == almaCalculationx ? color.from_gradient(close, almaCustom - 10, almaCustom + 10, #ff0500, #03ff00) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMaxx != almaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx != almaCalculationx and show == "Light" ? color.new(color.black, 50):
                                              color.new(color.black, 100), 
                                                 linewidth = 2
                 )
                
                
x3 = plot(MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Custom" ? hmaCustom : MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Benchmark" ? hmaBenchmark
     : na,
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) == array.get(best, 3) ? color.from_gradient(close, hmaCustom - 10, hmaCustom + 10, #ff0500, #03ff00) :
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.max(best) != array.get(best,3) and show == "Full" ? color.blue : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) != array.get(best,3) and show == "Light" ? color.new(color.black, 50) : 
                   MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax == hmaCalculation ? color.from_gradient(close, hmaCustom - 10, hmaCustom + 10, #ff0500, #03ff00) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMax != hmaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax != hmaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx == hmaCalculationx ? color.from_gradient(close, hmaCustom - 10, hmaCustom + 10, #ff0500, #03ff00) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMaxx != hmaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx != hmaCalculationx and show == "Light" ? color.new(color.black, 50):
                                             color.new(color.black, 100), 
                                                 linewidth = 2
                 )
                
                
x4 = plot(MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Custom" ? wmaCustom : MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Benchmark" ? wmaBenchmark
     : na, 
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) == array.get(best, 4) ? color.from_gradient(close, wmaCustom - 10, wmaCustom + 10, #ff0500, #03ff00) : 
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) != array.get(best,4) and show == "Full" ? color.purple : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) != array.get(best,4) and show == "Light" ? color.new(color.black, 50) : 
                     MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax == wmaCalculation ? color.from_gradient(close, wmaCustom - 10, wmaCustom + 10, #ff0500, #03ff00) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMax != wmaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax != wmaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx == wmaCalculationx ? color.from_gradient(close, wmaCustom - 10, wmaCustom + 10, #ff0500, #03ff00) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMaxx != wmaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx != wmaCalculationx and show == "Light" ? color.new(color.black, 50):
                                             color.new(color.black, 100), linewidth = 2
                 )
                
                
x5 = plot(MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Custom" ? vwmaCustom : MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Benchmark" ? vwmaBenchmark
     : na, 
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) == array.get(best, 5) ? color.from_gradient(close, vwmaCustom - 10, vwmaCustom + 10, #ff0500, #03ff00) : 
             MA == "Best Performing"  and seriesPlot == "Cumulative Gain"   and array.max(best) != array.get(best,5) and show == "Full" ? color.gray : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) != array.get(best,5) and show == "Light" ? color.new(color.black, 50) : 
                      MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax == vwmaCalculation ? color.from_gradient(close, vwmaCustom - 10, vwmaCustom + 10, #ff0500, #03ff00) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMax != vwmaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax != vwmaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx == vwmaCalculationx ? color.from_gradient(close, vwmaCustom - 10, vwmaCustom + 10, #ff0500, #03ff00) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMaxx != vwmaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx != vwmaCalculationx and show == "Light" ? color.new(color.black, 50):
                                                 color.new(color.black, 100), 
                                                     linewidth = 2
                 )
                
                
x6 = plot(MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Custom" ? lsmaCustom : MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Benchmark" ? lsmaBenchmark
     : na,  
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) == array.get(best, 6) ? color.from_gradient(close, lsmaCustom - 10, lsmaCustom + 10, #ff0500, #03ff00) : 
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) != array.get(best,6) and show == "Full" ? color.teal : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.max(best) != array.get(best,6) and show == "Light" ? color.new(color.black, 50) : 
                      MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax == lsmaCalculation ? color.from_gradient(close, lsmaCustom - 10, lsmaCustom + 10, #ff0500, #03ff00) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMax != lsmaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax != lsmaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx == lsmaCalculationx ? color.from_gradient(close, lsmaCustom - 10, lsmaCustom + 10, #ff0500, #03ff00) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMaxx != lsmaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx != lsmaCalculationx and show == "Light" ? color.new(color.black, 50):
                                             color.new(color.black, 100), 
                                                 linewidth = 2
                 )
                 
                 
x7 = plot(MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Custom" ? demaCustom : MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Benchmark" ? demaBenchmark
     : na,  
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.max(best) == array.get(best, 7) ? color.from_gradient(close, demaCustom - 10, demaCustom + 10, #ff0500, #03ff00) : 
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.max(best) != array.get(best,7) and show == "Full" ? color.red : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.max(best) != array.get(best,7) and show == "Light" ? color.new(color.black, 50) : 
                     MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax == demaCalculation ? color.from_gradient(close, demaCustom - 10, demaCustom + 10, #ff0500, #03ff00) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMax != demaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax != demaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx == demaCalculationx ? color.from_gradient(close, demaCustom - 10, demaCustom + 10, #ff0500, #03ff00) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMaxx != demaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx != demaCalculationx and show == "Light" ? color.new(color.black, 50):
                                             color.new(color.black, 100), 
                                                 linewidth = 2
                 )
                 
                 
x8 = plot(MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Custom" ? temaCustom : MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Benchmark" ? temaBenchmark
     : na,  
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.max(best) == array.get(best, 8) ? color.from_gradient(close, temaCustom - 10, temaCustom + 10, #ff0500, #03ff00) : 
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.max(best) != array.get(best,8) and show == "Full" ? color.black : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.max(best) != array.get(best,8) and show == "Light" ? color.new(color.black, 50) : 
                      MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax == temaCalculation ? color.from_gradient(close, temaCustom - 10, temaCustom + 10, #ff0500, #03ff00) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMax != temaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax != temaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx == temaCalculationx ? color.from_gradient(close, temaCustom - 10, temaCustom + 10, #ff0500, #03ff00) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMaxx != temaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx != temaCalculationx and show == "Light" ? color.new(color.black, 50):
                                             color.new(color.black, 100), 
                                                 linewidth = 2
                 )
                 
                 
x9 = plot(MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Custom" ? Tilson3Average : MA == "Best Performing" and crossUnderOver == "Crossover" and dataType == "Benchmark" ? Tilson3Benchmark
     : na,  
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.max(best) == array.get(best, 9) ? color.from_gradient(close, Tilson3Average - 10, Tilson3Average + 10, #ff0500, #03ff00) : 
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.max(best) != array.get(best,9) and show == "Full" ? color.white : 
                  MA == "Best Performing"and seriesPlot == "Cumulative Gain"   and array.max(best) != array.get(best,9) and show == "Light" ? color.new(color.black, 50) :         
                       MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax == Tilson3Calculation ? color.from_gradient(close, Tilson3Average - 10, Tilson3Average + 10, #ff0500, #03ff00) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMax != Tilson3Calculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMax != Tilson3Calculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx == calculationTilson3x ? color.from_gradient(close, Tilson3Average - 10, Tilson3Average + 10, #ff0500, #03ff00) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMaxx != calculationTilson3x and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMaxx != calculationTilson3x and show == "Light" ? color.new(color.black, 50):
                                             color.new(color.black, 100), 
                                                 linewidth = 2
                 )
                 

xx1 = plot(MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Custom" ? smaCustom : MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Benchmark" ? smaBenchmark 
     : na, 
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.min(best) == array.get(best, 0) ? color.from_gradient(close, smaCustom - 10, smaCustom + 10, #03ff00, #ff0500) : 
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) != array.get(best,0) and show == "Full" ? color.green : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) != array.get(best,0) and show == "Light" ? color.new(color.black, 50) : 
                     MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin == smaCalculation ? color.from_gradient(close, smaCustom - 10, smaCustom + 10, #03ff00, #ff0500) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMin != smaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin != smaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx == smaCalculationx ? color.from_gradient(close, smaCustom - 10, smaCustom + 10, #03ff00, #ff0500) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMinx != smaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx != smaCalculationx and show == "Light" ? color.new(color.black, 50):
                             color.new(color.black, 100), 
                                  linewidth = 2
                 )
                
                
x11 = plot(MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Custom" ? emaCustom : MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Benchmark" ? emaBenchmark 
     : na, 
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) == array.get(best, 1) ? color.from_gradient(close, emaCustom - 10, emaCustom + 10, #03ff00, #ff0500) : 
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) != array.get(best,1) and show == "Full" ? color.orange : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) != array.get(best,1) and show == "Light" ? color.new(color.black, 50) :          
                     MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin == emaCalculation ? color.from_gradient(close, emaCustom - 10, emaCustom + 10,#03ff00, #ff0500) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMin != emaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin != emaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx == emaCaculationx ? color.from_gradient(close, emaCustom - 10, emaCustom + 10,#03ff00, #ff0500) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMinx != emaCaculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx != emaCaculationx and show == "Light" ? color.new(color.black, 50):
                                              color.new(color.black, 100), 
                                                 linewidth = 2
                 )
                 
                 
x21 = plot(MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Custom" ? almaCustom : MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Benchmark" ? almaBenchmark 
     : na, 
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) == array.get(best, 2) ? color.from_gradient(close, almaCustom - 10, almaCustom + 10,#03ff00, #ff0500) : 
             MA == "Best Performing"  and seriesPlot == "Cumulative Gain"   and array.min(best) != array.get(best,2) and show == "Full" ? color.yellow : 
                  MA == "Best Performing"  and seriesPlot == "Cumulative Gain"   and array.min(best) != array.get(best,2) and show == "Light" ? color.new(color.black, 50) : 
                     MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin == almaCalculation ? color.from_gradient(close, almaCustom - 10, almaCustom + 10,#03ff00, #ff0500) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMin != almaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin != almaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx == almaCalculationx ? color.from_gradient(close, almaCustom - 10, almaCustom + 10,#03ff00, #ff0500) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMinx != almaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx != almaCalculationx and show == "Light" ? color.new(color.black, 50):
                                              color.new(color.black, 100), 
                                                 linewidth = 2
                 )
                 
                 
x31 = plot(MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Custom" ? hmaCustom : MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Benchmark" ? hmaBenchmark 
     : na,
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) == array.get(best, 3) ? color.from_gradient(close, hmaCustom - 10, hmaCustom + 10,#03ff00, #ff0500) :
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.min(best) != array.get(best,3) and show == "Full" ? color.blue : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) != array.get(best,3) and show == "Light" ? color.new(color.black, 50) : 
                   MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin == hmaCalculation ? color.from_gradient(close, hmaCustom - 10, hmaCustom + 10,#03ff00, #ff0500) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMin != hmaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin != hmaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx == hmaCalculationx ? color.from_gradient(close, hmaCustom - 10, hmaCustom + 10,#03ff00, #ff0500) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMinx != hmaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx != hmaCalculationx and show == "Light" ? color.new(color.black, 50):
                                             color.new(color.black, 100), 
                                                 linewidth = 2
                 )
                
                
x41 = plot(MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Custom" ? wmaCustom : MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Benchmark" ? wmaBenchmark 
     : na, 
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) == array.get(best, 4) ? color.from_gradient(close, wmaCustom - 10, wmaCustom + 10,#03ff00, #ff0500) : 
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) != array.get(best,4) and show == "Full" ? color.purple : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) != array.get(best,4) and show == "Light" ? color.new(color.black, 50) : 
                     MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin == wmaCalculation ? color.from_gradient(close, wmaCustom - 10, wmaCustom + 10,#03ff00, #ff0500) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMin != wmaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin != wmaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx == wmaCalculationx ? color.from_gradient(close, wmaCustom - 10, wmaCustom + 10,#03ff00, #ff0500) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMinx != wmaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx != wmaCalculationx and show == "Light" ? color.new(color.black, 50):
                                             color.new(color.black, 100), linewidth = 2
                 )
                 
                 
x51 = plot(MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Custom" ? vwmaCustom : MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Benchmark" ? vwmaBenchmark 
     : na, 
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) == array.get(best, 5) ? color.from_gradient(close, vwmaCustom - 10, vwmaCustom + 10,#03ff00, #ff0500) : 
             MA == "Best Performing"  and seriesPlot == "Cumulative Gain"   and array.min(best) != array.get(best,5) and show == "Full" ? color.gray : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) != array.get(best,5) and show == "Light" ? color.new(color.black, 50) : 
                      MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin == vwmaCalculation ? color.from_gradient(close, vwmaCustom - 10, vwmaCustom + 10,#03ff00, #ff0500) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMin != vwmaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin != vwmaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx == vwmaCalculationx ? color.from_gradient(close, vwmaCustom - 10, vwmaCustom + 10,#03ff00, #ff0500) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMinx != vwmaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx != vwmaCalculationx and show == "Light" ? color.new(color.black, 50):
                                                 color.new(color.black, 100), 
                                                     linewidth = 2
                 )
                 
                 
x61 = plot(MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Custom" ? lsmaCustom : MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Benchmark" ? lsmaBenchmark 
     : na,  
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) == array.get(best, 6) ? color.from_gradient(close, lsmaCustom - 10, lsmaCustom + 10,#03ff00, #ff0500) : 
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) != array.get(best,6) and show == "Full" ? color.teal : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"   and array.min(best) != array.get(best,6) and show == "Light" ? color.new(color.black, 50) : 
                      MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin == lsmaCalculation ? color.from_gradient(close, lsmaCustom - 10, lsmaCustom + 10,#03ff00, #ff0500) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMin != lsmaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin != lsmaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx == lsmaCalculationx ? color.from_gradient(close, lsmaCustom - 10, lsmaCustom + 10,#03ff00, #ff0500) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMinx != lsmaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx != lsmaCalculationx and show == "Light" ? color.new(color.black, 50):
                                             color.new(color.black, 100), 
                                                 linewidth = 2
                 )


x71 = plot(MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Custom" ? demaCustom : MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Benchmark" ? demaBenchmark 
     : na,  
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.min(best) == array.get(best, 7) ? color.from_gradient(close, demaCustom - 10, demaCustom + 10,#03ff00, #ff0500) : 
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.min(best) != array.get(best,7) and show == "Full" ? color.red : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.min(best) != array.get(best,7) and show == "Light" ? color.new(color.black, 50) : 
                     MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin == demaCalculation ? color.from_gradient(close, demaCustom - 10, demaCustom + 10,#03ff00, #ff0500) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMin != demaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin != demaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx == demaCalculationx ? color.from_gradient(close, demaCustom - 10, demaCustom + 10,#03ff00, #ff0500) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMinx != demaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx != demaCalculationx and show == "Light" ? color.new(color.black, 50):
                                             color.new(color.black, 100), 
                                                 linewidth = 2
                 )


x81 = plot(MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Custom" ? temaCustom : MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Benchmark" ? temaBenchmark 
     : na,  
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.min(best) == array.get(best, 8) ? color.from_gradient(close, temaCustom - 10, temaCustom + 10,#03ff00, #ff0500) : 
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.min(best) != array.get(best,8) and show == "Full" ? color.black : 
                  MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.min(best) != array.get(best,8) and show == "Light" ? color.new(color.black, 50) : 
                      MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin == temaCalculation ? color.from_gradient(close, temaCustom - 10, temaCustom + 10,#03ff00, #ff0500) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMin != temaCalculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin != temaCalculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx == temaCalculationx ? color.from_gradient(close, temaCustom - 10, temaCustom + 10,#03ff00, #ff0500) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMinx != temaCalculationx and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx != temaCalculationx and show == "Light" ? color.new(color.black, 50):
                                             color.new(color.black, 100), 
                                                 linewidth = 2
                 )


x91 = plot(MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Custom" ? Tilson3Average : MA == "Best Performing" and crossUnderOver == "Crossunder" and dataType == "Benchmark" ? Tilson3Benchmark 
     : na,  
         color = MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.min(best) == array.get(best, 9) ? color.from_gradient(close, Tilson3Average - 10, Tilson3Average + 10,#03ff00, #ff0500) : 
             MA == "Best Performing" and seriesPlot == "Cumulative Gain"  and array.min(best) != array.get(best,9) and show == "Full" ? color.white : 
                  MA == "Best Performing"and seriesPlot == "Cumulative Gain"   and array.min(best) != array.get(best,9) and show == "Light" ? color.new(color.black, 50) :         
                       MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin == Tilson3Calculation ? color.from_gradient(close, Tilson3Average - 10, Tilson3Average + 10,#03ff00, #ff0500) :
                         MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Benchmark" and averageMin != Tilson3Calculation and show == "Full" ? color.green : 
                             MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Benchmark" and averageMin != Tilson3Calculation and show == "Light" ? color.new(color.black, 50) :
                                  MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx == calculationTilson3x ? color.from_gradient(close, Tilson3Average - 10, Tilson3Average + 10,#03ff00, #ff0500) :
                                     MA == "Best Performing" and seriesPlot == "Highest Average Gain"  and dataType == "Custom" and averageMinx != calculationTilson3x and show == "Full" ? color.green : 
                                          MA == "Best Performing" and seriesPlot == "Highest Average Gain" and dataType == "Custom" and averageMinx != calculationTilson3x and show == "Light" ? color.new(color.black, 50):
                                             color.new(color.black, 100), 
                                                 linewidth = 2
                                                 
                                                 
                 )
                                                                                           

// Upper Right Table MA Plot
var table t2 = na
if MA == "Best Performing" and barstate.islast
    t2 := table.new(position.top_right, 10, 10, bgcolor = crossUnderOver == "Crossover" and dataType == "Custom" ? color.from_gradient(close, averageMaxx - 10, averageMaxx + 10, 
     color.new(#03ff00, 33), color.new(#03ff00, 33)) : crossUnderOver == "Crossover" and dataType == "Benchmark" ? color.from_gradient(close, averageMax - 10, averageMax + 10, color.new(#03ff00, 33),
         color.new(#ff0500, 33)) : crossUnderOver == "Crossunder" and dataType == "Custom" ? color.from_gradient(close, averageMinx - 10, averageMinx + 10, color.new(#ff0500, 33), color.new(#03ff00, 33)) : 
             crossUnderOver == "Crossunder" and dataType == "Benchmark" ? color.from_gradient(close, averageMin - 10, averageMin + 10, color.new(#ff0500, 33), color.new(#03ff00, 33)) : na

                  
                  )
                 
                                

// Source String
sourceS = customSource == close ? "Close" : 
             customSource == open ? "Open" : 
                 customSource == high ? "High" : 
                     customSource == low ? "Low" :
                         customSource == hl2 ? "HL" : 
                             customSource == hlc3 ? "HLC" : 
                                 customSource == ohlc4 ? "OHLC" :
                                     customSource == hlcc4 ? "HLCC" : 
                                         customSource == ta.obv ? "OBV" 
                                             : " Other"


// Table Header
if MA == "Best Performing" and crossUnderOver == "Crossover" and seriesPlot == "Cumulative Gain" and barstate.islast
    table.cell(t2, 0, 0, text = dataType == "Benchmark" ? "Plotted: " + 
         array.get(cumulativePerformanceNameSorted, 9) + " " + str.tostring(benchmarkLength) 
             + " (" + sourceS + ")" : "Plotted: " + array.get(cumulativePerformanceNameSorted, 9) + " " + str.tostring(customLength)
                 + " (" + sourceS + ")", bgcolor = MA != "Best Performing" 
                 ? color.new(color.white, 100) :  na, text_color = color.white
                             
                             
                             )


else if MA == "Best Performing" and crossUnderOver == "Crossover" and seriesPlot == "Highest Average Gain" and barstate.islast
    table.cell(t2, 0, 0, 
                     text = dataType == "Custom" ? "Plotted: " 
                             + array.get(performanceNameSorted, 0) +  " " + str.tostring(customLength) + 
                                 " (" + sourceS + ")" : "Plotted: " + array.get(performanceNameSorted,0) + " " + str.tostring(benchmarkLength) 
                                     + " (" + sourceS + ")", bgcolor =  na, text_color = color.white

                                 
                                 
                             
                             )
                             

if MA == "Best Performing" and crossUnderOver == "Crossunder" and seriesPlot == "Cumulative Gain" and barstate.islast
    table.cell(t2, 0, 0, text = dataType == "Benchmark" ? "Plotted: " + array.get(cumulativePerformanceNameSorted, 0) + " " + str.tostring(benchmarkLength) + " (" + sourceS + ")" : 
                                              "Plotted: " + array.get(cumulativePerformanceNameSorted, 0) + " " + str.tostring(customLength) + " (" + sourceS + ")",
                                                 bgcolor = MA != "Best Performing" ? color.new(color.white, 100) : na,  text_color = color.white
                                                     
                                 
                                 
                             
                                 )


else if MA == "Best Performing" and crossUnderOver == "Crossunder" and seriesPlot == "Highest Average Gain" and barstate.islast
    table.cell(t2, 0, 0, text = dataType == "Custom" ? "Plotted: " 
         + array.get(performanceNameSorted, 9) + " " + str.tostring(customLength) + 
             " (" + sourceS + ")" : "Plotted: " + array.get(performanceNameSorted, 9) + " " + str.tostring(benchmarkLength) + " (" + sourceS + ")", bgcolor = na, text_color = color.white
                 
                 
                 
                             )

// Main Table Plot
orgText = ""
orgText1 = ""
orgText2 = ""
bestText = ""
worstText = ""
tablePerformance = table.new(position.bottom_right, 10, 10, bgcolor = color.new(color.white, 100))

if barstate.islast and dataType == "Custom" and crossUnderOver == "Crossover"
    table.cell(tablePerformance, 0,0,text = "Price Crossover Performance" + "\n" + "\n" + str.tostring(sourceS) + "/" + str.tostring(customLength) + 
         " (MA)", text_color = color.blue, text_size = size.normal)
    for i = 0 to array.size(org) - 1
        orgText := "Upper-Tier \n \n" + array.get(performanceNameSorted, 0) + ": " + str.tostring(array.get(performanceSorted, 0), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 0), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 0), "#") + " Crosses)" 
             + "\n" +"\n" + array.get(performanceNameSorted, 1) + ": " + str.tostring(array.get(performanceSorted, 1), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 1), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 1), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 2) + ": " + str.tostring(array.get(performanceSorted, 2), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 2), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 2), "#") + " Crosses)"
        orgText1 := "\nMid-Tier \n \n" + array.get(performanceNameSorted, 3) + ": " + str.tostring(array.get(performanceSorted, 3), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 3), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 3), "#") + " Crosses)" 
             + "\n" +"\n" + array.get(performanceNameSorted, 4) + ": " + str.tostring(array.get(performanceSorted, 4), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 4), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 4), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 5) + ": " + str.tostring(array.get(performanceSorted, 5), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 5), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 5), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 6) + ": " + str.tostring(array.get(performanceSorted, 6), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 6), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 6), "#") + " Crosses)"
        orgText2 := "\nLower-Tier \n \n" + array.get(performanceNameSorted, 7) + ": " + str.tostring(array.get(performanceSorted, 7), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 7), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 7), "#") + " Crosses)" 
             + "\n" +"\n" + array.get(performanceNameSorted, 8) + ": " + str.tostring(array.get(performanceSorted, 8), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 8), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 8), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 9) + ": " + str.tostring(array.get(performanceSorted, 9), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 9), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 9), "#") + " Crosses)" + "\n" +"\n" 
    table.cell(tablePerformance, 0,1,text = orgText, text_color = color.white, bgcolor = color.new(color.green, 75))
    table.cell(tablePerformance, 0,2, text = orgText1, text_color = color.white, bgcolor = color.new(color.blue, 75), height = 23)
    table.cell(tablePerformance, 0, 3, text = orgText2, text_color = color.white, bgcolor = color.new(color.red, 75), height = 20)
    for i = 0 to array.size(best) - 1
        bestText := array.get(cumulativePerformanceSorted, 9) >= 0.0 ? "Highest Cumulative Gains: " + str.tostring(array.get(cumulativePerformanceSorted, i), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted,i) + ")" + "\n" + "\n" : "Highest Cumulative Gains: " + str.tostring(array.get(cumulativePerformanceSorted, i), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted,i) + ")" + "\n(Long Position Loss!)" + "\n"
        worstText := array.get(cumulativePerformanceSorted, 0) >= 0.0 ? "Lowest Cumulative Gains: " + str.tostring(array.get(cumulativePerformanceSorted, 0), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted, 0) + ")" :  "Lowest Cumulative Gains: " + str.tostring(array.get(cumulativePerformanceSorted, 0), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted, 0) + ")\n (Long Position Loss!)" 
    table.cell(tablePerformance, 0, 4, text = bestText, text_color = color.green)
    table.cell(tablePerformance, 0, 5, text = worstText, text_color = color.red)
    
    
else if barstate.islast and dataType == "Benchmark" and crossUnderOver == "Crossover"
    table.cell(tablePerformance, 0,0,text = "Price Crossover Performance (Benchmark) \n \n" + str.tostring(sourceS) + "/" + str.tostring(benchmarkLength) +" (MA)", 
         text_color = color.blue, text_size = size.normal)
    for i = 0 to array.size(org) - 1
        orgText := "Upper-Tier \n \n" + array.get(performanceNameSorted, 0) + ": " + str.tostring(array.get(performanceSorted, 0), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 0), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 0), "#") + " Crosses)" 
             + "\n" +"\n" + array.get(performanceNameSorted, 1) + ": " + str.tostring(array.get(performanceSorted, 1), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 1), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 1), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 2) + ": " + str.tostring(array.get(performanceSorted, 2), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 2), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 2), "#") + " Crosses)"
        orgText1 := "\nMid-Tier \n \n" + array.get(performanceNameSorted, 3) + ": " + str.tostring(array.get(performanceSorted, 3), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 3), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 3), "#") + " Crosses)" 
             + "\n" +"\n" + array.get(performanceNameSorted, 4) + ": " + str.tostring(array.get(performanceSorted, 4), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 4), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 4), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 5) + ": " + str.tostring(array.get(performanceSorted, 5), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 5), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 5), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 6) + ": " + str.tostring(array.get(performanceSorted, 6), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 6), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 6), "#") + " Crosses)"
        orgText2 := "\nLower-Tier \n \n" + array.get(performanceNameSorted, 7) + ": " + str.tostring(array.get(performanceSorted, 7), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 7), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 7), "#") + " Crosses)" 
             + "\n" +"\n" + array.get(performanceNameSorted, 8) + ": " + str.tostring(array.get(performanceSorted, 8), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 8), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 8), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 9) + ": " + str.tostring(array.get(performanceSorted, 9), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 9), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 9), "#") + " Crosses)" + "\n" +"\n" 
    table.cell(tablePerformance, 0,1,text = orgText, text_color = color.white, bgcolor = color.new(color.green, 75))
    table.cell(tablePerformance, 0,2, text = orgText1, text_color = color.white, bgcolor = color.new(color.blue, 75), height = 23)
    table.cell(tablePerformance, 0,3, text = orgText2, text_color = color.white, bgcolor = color.new(color.red, 75), height = 20)
    for i = 0 to array.size(best) - 1
        bestText := array.get(cumulativePerformanceSorted, 9) >= 0.0 ? "Highest Cumulative Gains: " + str.tostring(array.get(cumulativePerformanceSorted, i), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted,i) + ")" + "\n" + "\n" : "Highest Cumulative Gains: " + str.tostring(array.get(cumulativePerformanceSorted, i), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted,i) + ")" + "\n(Long Position Loss!)" + "\n"
        worstText := array.get(cumulativePerformanceSorted, 0) >= 0.0 ? "Lowest Cumulative Gains: " + str.tostring(array.get(cumulativePerformanceSorted, 0), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted, 0) + ")" :  "Lowest Cumulative Gains: " + str.tostring(array.get(cumulativePerformanceSorted, 0), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted, 0) + ")\n (Long Position Loss!)"  
    table.cell(tablePerformance, 0, 4, text = bestText, text_color = color.green)
    table.cell(tablePerformance, 0, 5, text = worstText, text_color = color.red)
    
    
else if barstate.islast and dataType == "Custom" and crossUnderOver == "Crossunder"
    table.cell(tablePerformance, 0,0,text = "Price Crossunder Performance" + "\n" + "\n" + str.tostring(sourceS) + "/" + str.tostring(customLength) + 
         " (MA)", text_color = #ff6d00, text_size = size.normal)
    for i = 0 to array.size(org) - 1
        orgText := "Upper-Tier \n \n" + array.get(performanceNameSorted, 9) + ": " + str.tostring(array.get(performanceSorted, 9), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 9), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 9), "#") + " Crosses)" 
             + "\n" +"\n" + array.get(performanceNameSorted, 8) + ": " + str.tostring(array.get(performanceSorted, 8), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 8), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 8), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 7) + ": " + str.tostring(array.get(performanceSorted, 7), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 7), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 7), "#") + " Crosses)"
        orgText1 := "\nMid-Tier \n \n" + array.get(performanceNameSorted, 6) + ": " + str.tostring(array.get(performanceSorted, 6), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 6), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 6), "#") + " Crosses)" 
             + "\n" +"\n" + array.get(performanceNameSorted, 5) + ": " + str.tostring(array.get(performanceSorted, 5), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 5), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 5), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 4) + ": " + str.tostring(array.get(performanceSorted, 4), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 4), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 4), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 3) + ": " + str.tostring(array.get(performanceSorted, 3), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 3), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 3), "#") + " Crosses)"
        orgText2 := "\nLower-Tier \n \n" + array.get(performanceNameSorted, 2) + ": " + str.tostring(array.get(performanceSorted, 2), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 2), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 2), "#") + " Crosses)" 
             + "\n" +"\n" + array.get(performanceNameSorted, 1) + ": " + str.tostring(array.get(performanceSorted, 1), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 1), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 1), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 0) + ": " + str.tostring(array.get(performanceSorted, 0), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 0), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 0), "#") + " Crosses)" + "\n" +"\n" 
    table.cell(tablePerformance, 0,1,text = orgText, text_color = color.white, bgcolor = color.new(color.green, 75))
    table.cell(tablePerformance, 0,2, text = orgText1, text_color = color.white, bgcolor = color.new(color.blue, 75), height = 23)
    table.cell(tablePerformance, 0, 3, text = orgText2, text_color = color.white, bgcolor = color.new(color.red, 75), height = 20)
    for i = 0 to array.size(best) - 1
        bestText := array.get(cumulativePerformanceSorted, 0) <= 0.0 ? "Highest Cumulative Downside Gains: " + str.tostring(array.get(cumulativePerformanceSorted, 0), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted,0) + ")" + "\n" + "\n" : "Highest Cumulative Downside Gains: " + str.tostring(array.get(cumulativePerformanceSorted, 0), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted,0) + ")" + "\n(Short Position Loss!)" + "\n" 
        worstText := array.get(cumulativePerformanceSorted, 9) <= 0.0 ? "Lowest Cumulative Downside Gains: " + str.tostring(array.get(cumulativePerformanceSorted, 9), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted, 9) + ")" : "Lowest Cumulative Downside Gains: " + str.tostring(array.get(cumulativePerformanceSorted, 9), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted, 9) + ")" + "\n(Short Position Loss!)"
    table.cell(tablePerformance, 0, 4, text = bestText, text_color = color.green)
    table.cell(tablePerformance, 0, 5, text = worstText, text_color = color.red)
    
    
else if barstate.islast and dataType == "Benchmark" and crossUnderOver == "Crossunder"
    table.cell(tablePerformance, 0,0,text = "Price Crossunder Performance (Benchmark)" + "\n" + "\n" + str.tostring(sourceS) + "/" + str.tostring(benchmarkLength) + 
         " (MA)", text_color = #ff6d00, text_size = size.normal)
    for i = 0 to array.size(org) - 1
        orgText := "Upper-Tier \n \n" + array.get(performanceNameSorted, 9) + ": " + str.tostring(array.get(performanceSorted, 9), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 9), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 9), "#") + " Crosses)" 
             + "\n" +"\n" + array.get(performanceNameSorted, 8) + ": " + str.tostring(array.get(performanceSorted, 8), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 8), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 8), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 7) + ": " + str.tostring(array.get(performanceSorted, 7), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 7), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 7), "#") + " Crosses)"
        orgText1 := "\nMid-Tier \n \n" + array.get(performanceNameSorted, 6) + ": " + str.tostring(array.get(performanceSorted, 6), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 6), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 6), "#") + " Crosses)" 
             + "\n" +"\n" + array.get(performanceNameSorted, 5) + ": " + str.tostring(array.get(performanceSorted, 5), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 5), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 5), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 4) + ": " + str.tostring(array.get(performanceSorted, 4), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 4), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 4), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 3) + ": " + str.tostring(array.get(performanceSorted, 3), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 3), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 3), "#") + " Crosses)"
        orgText2 := "\nLower-Tier \n \n" + array.get(performanceNameSorted, 2) + ": " + str.tostring(array.get(performanceSorted, 2), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 2), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 2), "#") + " Crosses)" 
             + "\n" +"\n" + array.get(performanceNameSorted, 1) + ": " + str.tostring(array.get(performanceSorted, 1), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 1), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 1), "#") + " Crosses)" + "\n" +"\n" +
             array.get(performanceNameSorted, 0) + ": " + str.tostring(array.get(performanceSorted, 0), format.percent) + 
             " (" +str.tostring(array.get(barsSorted, 0), '#') + " Candles" + ")" + " (" + str.tostring(array.get(crossesSorted, 0), "#") + " Crosses)" + "\n" +"\n"
    table.cell(tablePerformance, 0,1,text = orgText, text_color = color.white, bgcolor = color.new(color.green, 75))
    table.cell(tablePerformance, 0,2, text = orgText1, text_color = color.white, bgcolor = color.new(color.blue, 75), height = 23)
    table.cell(tablePerformance, 0, 3, text = orgText2, text_color = color.white, bgcolor = color.new(color.red, 75), height = 20)
    for i = 0 to array.size(best) - 1
        bestText := array.get(cumulativePerformanceSorted, 0) <= 0.0 ? "Highest Cumulative Downside Gains: " + str.tostring(array.get(cumulativePerformanceSorted, 0), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted,0) + ")" + "\n" + "\n" : "Highest Cumulative Downside Gains: " + str.tostring(array.get(cumulativePerformanceSorted, 0), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted,0) + ")" + "\n(Short Position Loss!)" + "\n" 
        worstText := array.get(cumulativePerformanceSorted, 9) <= 0.0 ? "Lowest Cumulative Downside Gains: " + str.tostring(array.get(cumulativePerformanceSorted, 9), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted, 9) + ")" : "Lowest Cumulative Downside Gains: " + str.tostring(array.get(cumulativePerformanceSorted, 9), format.percent) + " (" 
             + array.get(cumulativePerformanceNameSorted, 9) + ")" + "\n(Short Position Loss!)"
    table.cell(tablePerformance, 0, 4, text = bestText, text_color = color.green)
    table.cell(tablePerformance, 0, 5, text = worstText, text_color = color.red)

// MA Plot Overrides
plot(MA == "SMA" ? smaCustom : MA == "EMA" ? emaCustom : MA == "ALMA" ? almaCustom : MA == "HMA" ? hmaCustom : MA == "WMA" ? wmaCustom : MA == "VWMA" ? vwmaCustom : MA == "LSMA" ? lsmaCustom : MA == "DEMA" ? demaCustom : MA == "TEMA" ? temaCustom : MA == "T3" ? Tilson3Average : na, color = 
     MA == "SMA" ? color.green : MA == "EMA" ? color.orange : MA == "ALMA" ? color.yellow : MA == "HMA" ? color.blue : MA == "WMA" ? color.purple : MA == "VWMA" ? color.gray : MA == "LSMA" ? color.teal : 
         MA == "DEMA" ? color.red : MA == "TEMA" ? color.maroon : MA == "T3" ? color.white : na, 
             linewidth = 2)

// Gradient Plot
upperPlot = plot(MAmax, color = color.new(#222123, 100))
lowerPlot = plot(MAmin, color = color.new(#222123, 100))
upperColor = input.color(color.green, title = "Upper Value Gradient", group = "Gradient Colors", tooltip = 'Modify the Upper Value Gradient Color When "Best Performing Ma” and “Gradient” Are Selected')
lowerColor = input.color(color.red, title = "Lower Value Gradient", tooltip = 'Modify the Lower Value Gradient Color When "Best Performing Ma" and "Gradient" Are Selected')
fill(upperPlot, lowerPlot, color = MA == "Best Performing" and show == "Gradient" and crossUnderOver == "Crossover" and dataType == "Custom" ? color.from_gradient(close, MAmin, MAmax, color.new(lowerColor, 85), color.new(upperColor, 85)) : MA == "Best Performing" and show == "Gradient" and crossUnderOver == "Crossunder" and dataType == "Custom" ? color.from_gradient(close, MAmin, MAmax, color.new(upperColor, 85), color.new(lowerColor, 85)) :  color.new(#222123, 100))
upperPlotx= plot(MAmaxBenchmark, color = color.new(#222123, 100))
lowerPlotx = plot(MAminBenchmark, color = color.new(#222123, 100))
fill(upperPlotx, lowerPlotx, color = MA == "Best Performing" and show == "Gradient" and crossUnderOver == "Crossover" and dataType == "Benchmark" ? color.from_gradient(close, MAminBenchmark, MAmaxBenchmark, color.new(lowerColor, 85), color.new(upperColor, 85)) : MA == "Best Performing" and show == "Gradient" and crossUnderOver == "Crossunder" and dataType == "Benchmark" ? color.from_gradient(close, MAminBenchmark, MAmaxBenchmark, color.new(upperColor, 85), color.new(lowerColor, 85)) :  color.new(#222123, 100))

