Intraday Super Sectors
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © stuarthenryjames
// @version=5

indicator("Intraday Super Sectors", overlay = false)

// VERSION 2.0 CHANGELOG

// NEW FEATURES
// User can choose whether a sector is 'Cyclical' or 'Defensive'
// User can display spaghetti, if they really want
// BUG FIXES
// Small (display-related) bug fix


/////////////////////////////////////////////////
// LET'S GET THIS PARTY STARTED
/////////////////////////////////////////////////


// Input Variables

var oGroup1 = "Line                                   Color     Width"

oColorCyclical = input.color(color.rgb(0, 192, 0, 50), "Cyclical Super Sector           ", inline = "11", group = oGroup1)
oLineWidthCyclical  = input.int(5, "", options = [1, 2, 3, 4, 5], inline = "11", group = oGroup1)

oColorDefensive = input.color(color.rgb(192, 0, 0, 50), "Defensive Super Sector         ", inline = "12", group = oGroup1)
oLineWidthDefensive  = input.int(4, "", options = [1, 2, 3, 4, 5], inline = "12", group = oGroup1)

oMidTick = input.string("SPX", "Midline ticker?", inline = "13", group = oGroup1)
oColorSpx = input.color(color.rgb(255, 255, 255, 50), "", inline = "13", group = oGroup1)
oLineWidthSpx = input.int(3, "", options = [1, 2, 3, 4, 5], inline = "13", group = oGroup1)


var oGroup2 = "If you're just using SPX / Cash Session, no need to touch this. Unfortunately, TradingView doesn't know if the clocks are forwards or backwards!"

// If I'm wrong about this, please let me know in the comments :) ... (and yes, I know there is a workaround, but I'm not sure it'd be robust)

oSessionInfo = input.string("Regular", "What sessions are you displaying?", options = ["Regular", "Extended"], inline = "21", group = oGroup2)
oClocks = input.string("Summer Time", "What are the clocks doing?      ", options = ["Summer Time", "Winter Time"], inline = "22", group = oGroup2)
oSummerTime = oClocks == "Summer Time" ? true : false


var oGroup4 = "Soft Background Clouds"

oSoftGreen = input.color(color.rgb(0, 255, 0, 97), "Cyclical > Defensive?", inline = "41", group = oGroup4)
oSoftRed = input.color(color.rgb(255, 0, 0, 95), "Defensive > Cyclical?", inline = "41", group = oGroup4)


var oGroup5 = "Random Stuff"

oEma = input.int(5, "EMA Smoothing", options = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], inline = "51", group = oGroup5)

oTextColor = input.color(color.rgb(128, 128, 128, 0), "Title Text Color", inline = "52", group = oGroup5)
oTextGreen = input.color(color.rgb(64, 192, 64, 0), "Up Text Color  ", inline = "53", group = oGroup5)
oTextRed   = input.color(color.rgb(255, 0, 0, 0), "Down Text Color", inline = "53", group = oGroup5)


var oGroup6 = "Sector Settings"

oWeightXLB = input.float(2.6, "XLB Weight / Sector ", minval = 0, inline = "61", group = oGroup6)
oSectorXLB = input.string("Cyclical", "", options = ["Cyclical", "Defensive"], inline = "61", group = oGroup6)

oWeightXLC = input.float(10.8, "XLC Weight / Sector ", minval = 0, inline = "62", group = oGroup6)
oSectorXLC = input.string("Cyclical", "", options = ["Cyclical", "Defensive"], inline = "62", group = oGroup6)

oWeightXLE = input.float(2.3, "XLE Weight / Sector ", minval = 0, inline = "63", group = oGroup6)
oSectorXLE = input.string("Defensive", "", options = ["Cyclical", "Defensive"], inline = "63", group = oGroup6)

oWeightXLF = input.float(10.4, "XLF Weight / Sector ", minval = 0, inline = "64", group = oGroup6)
oSectorXLF = input.string("Cyclical", "", options = ["Cyclical", "Defensive"], inline = "64", group = oGroup6)

oWeightXLI = input.float(8.4, "XLI Weight / Sector ", minval = 0, inline = "65", group = oGroup6)
oSectorXLI = input.string("Cyclical", "", options = ["Cyclical", "Defensive"], inline = "65", group = oGroup6)

oWeightXLK = input.float(27.6, "XLK Weight / Sector ", minval = 0, inline = "66", group = oGroup6)
oSectorXLK = input.string("Cyclical", "", options = ["Cyclical", "Defensive"], inline = "66", group = oGroup6)

oWeightXLP = input.float(6.5, "XLP Weight / Sector ", minval = 0, inline = "67", group = oGroup6)
oSectorXLP = input.string("Defensive", "", options = ["Cyclical", "Defensive"], inline = "67", group = oGroup6)

oWeightXLRE = input.float(2.4, "XLRE Weight / Sector", minval = 0, inline = "68", group = oGroup6)
oSectorXLRE = input.string("Cyclical", "", options = ["Cyclical", "Defensive"], inline = "68", group = oGroup6)

oWeightXLU = input.float(2.8, "XLU Weight / Sector ", minval = 0, inline = "69", group = oGroup6)
oSectorXLU = input.string("Defensive", "", options = ["Cyclical", "Defensive"], inline = "69", group = oGroup6)

oWeightXLV = input.float(13.5, "XLV Weight / Sector ", minval = 0, inline = "6A", group = oGroup6)
oSectorXLV = input.string("Defensive", "", options = ["Cyclical", "Defensive"], inline = "6A", group = oGroup6)

oWeightXLY = input.float(12.7, "XLY Weight / Sector ", minval = 0, inline = "6B", group = oGroup6)
oSectorXLY = input.string("Cyclical", "", options = ["Cyclical", "Defensive"], inline = "6B", group = oGroup6)


var oGroup7 = "Would you like a side of spaghetti with your order?"

oSpaghetti = input.string("Nope, keep it clean!", "Side dish of spaghetti?", options = ["Nope, keep it clean!", "Go for it!"], inline = "71", group = oGroup7)

oSpagCyclical  = input.color(color.rgb(0, 192, 0, 70), "Cyclical Spaghetti Color", inline = "72", group = oGroup7)
oSpagDefensive = input.color(color.rgb(192, 0, 0, 60), "Defensive Spaghetti Color", inline = "73", group = oGroup7)
oSpagWidth     = input.int(1, "Spaghetti Width", options = [1, 2, 3, 4, 5], inline = "74", group = oGroup7)

oSpagFlag = oSpaghetti == "Nope, keep it clean!" ? false : true



// Initialize / Global Variables

oColorNone = color.rgb(0, 0, 0, 255)
oColorGray = color.rgb(128, 128, 128, 50)

var oOpenSpx = 0.0
var oOpenCyclical = 0.0
var oOpenDefensive = 0.0

var oOpenXLB = 0.0
var oOpenXLC = 0.0
var oOpenXLE = 0.0
var oOpenXLF = 0.0
var oOpenXLI = 0.0
var oOpenXLK = 0.0
var oOpenXLP = 0.0
var oOpenXLRE = 0.0
var oOpenXLU = 0.0
var oOpenXLV = 0.0
var oOpenXLY = 0.0

var oRTH = true
var oNewSession = false


/////////////////////////////////////////////////
// DO SOME CALCULATIONS
/////////////////////////////////////////////////


// Intialize for a new session

if oSessionInfo == "Regular"
    oNewSession := dayofweek != dayofweek[1] ? true : false

if oSessionInfo == "Extended"
    oRTH := time(timeframe.period, oSummerTime ? "0830-1500" : "0930-1600") != 0 ? true : false
    oNewSession := oRTH and (oRTH[1] == false) ? true : false



oOpenSpx := oNewSession ? request.security(oMidTick, timeframe.period, open) : oOpenSpx

oOpenXLB := oNewSession ? request.security("XLB", timeframe.period, open) : oOpenXLB
oOpenXLC := oNewSession ? request.security("XLC", timeframe.period, open) : oOpenXLC
oOpenXLE := oNewSession ? request.security("XLE", timeframe.period, open) : oOpenXLE
oOpenXLF := oNewSession ? request.security("XLF", timeframe.period, open) : oOpenXLF
oOpenXLI := oNewSession ? request.security("XLI", timeframe.period, open) : oOpenXLI
oOpenXLK := oNewSession ? request.security("XLK", timeframe.period, open) : oOpenXLK
oOpenXLP := oNewSession ? request.security("XLP", timeframe.period, open) : oOpenXLP
oOpenXLRE := oNewSession ? request.security("XLRE", timeframe.period, open) : oOpenXLRE
oOpenXLU := oNewSession ? request.security("XLU", timeframe.period, open) : oOpenXLU
oOpenXLV := oNewSession ? request.security("XLV", timeframe.period, open) : oOpenXLV
oOpenXLY := oNewSession ? request.security("XLY", timeframe.period, open) : oOpenXLY


if oNewSession
    oOpenCyclical :=     (oSectorXLB == "Cyclical" ? oOpenXLB * oWeightXLB : 0) +
                         (oSectorXLC == "Cyclical" ? oOpenXLC * oWeightXLC : 0) +
                         (oSectorXLE == "Cyclical" ? oOpenXLE * oWeightXLE : 0) +
                         (oSectorXLF == "Cyclical" ? oOpenXLF * oWeightXLF : 0) +
                         (oSectorXLI == "Cyclical" ? oOpenXLI * oWeightXLI : 0) +
                         (oSectorXLK == "Cyclical" ? oOpenXLK * oWeightXLK : 0) +
                         (oSectorXLP == "Cyclical" ? oOpenXLP * oWeightXLP : 0) +
                         (oSectorXLRE == "Cyclical" ? oOpenXLRE * oWeightXLRE : 0) +
                         (oSectorXLU == "Cyclical" ? oOpenXLU * oWeightXLU : 0) +
                         (oSectorXLV == "Cyclical" ? oOpenXLV * oWeightXLV : 0) +
                         (oSectorXLY == "Cyclical" ? oOpenXLY * oWeightXLY : 0)                   
                     
    oOpenDefensive :=    (oSectorXLB == "Defensive" ? oOpenXLB * oWeightXLB : 0) +
                         (oSectorXLC == "Defensive" ? oOpenXLC * oWeightXLC : 0) +
                         (oSectorXLE == "Defensive" ? oOpenXLE * oWeightXLE : 0) +
                         (oSectorXLF == "Defensive" ? oOpenXLF * oWeightXLF : 0) +
                         (oSectorXLI == "Defensive" ? oOpenXLI * oWeightXLI : 0) +
                         (oSectorXLK == "Defensive" ? oOpenXLK * oWeightXLK : 0) +
                         (oSectorXLP == "Defensive" ? oOpenXLP * oWeightXLP : 0) +
                         (oSectorXLRE == "Defensive" ? oOpenXLRE * oWeightXLRE : 0) +
                         (oSectorXLU == "Defensive" ? oOpenXLU * oWeightXLU : 0) +
                         (oSectorXLV == "Defensive" ? oOpenXLV * oWeightXLV : 0) +
                         (oSectorXLY == "Defensive" ? oOpenXLY * oWeightXLY : 0) 

// yes, I know we should do '1 - Cyclical' to get the Defensive value, but for development purposes this (slightly duplicate) approach is easier

// also, if/when this script is ever 'final final' I will convert everything to arrays / loops


// So where are we now?

oCurrentSpx = request.security(oMidTick, timeframe.period, close)

oCurrentXLB = request.security("XLB", timeframe.period, close)
oCurrentXLC = request.security("XLC", timeframe.period, close)
oCurrentXLE = request.security("XLE", timeframe.period, close)
oCurrentXLF = request.security("XLF", timeframe.period, close)
oCurrentXLI = request.security("XLI", timeframe.period, close)
oCurrentXLK = request.security("XLK", timeframe.period, close)
oCurrentXLP = request.security("XLP", timeframe.period, close)
oCurrentXLRE = request.security("XLRE", timeframe.period, close)
oCurrentXLU = request.security("XLU", timeframe.period, close)
oCurrentXLV = request.security("XLV", timeframe.period, close)
oCurrentXLY = request.security("XLY", timeframe.period, close)


oCurrentCyclical =   (oSectorXLB == "Cyclical" ? oCurrentXLB * oWeightXLB : 0) +
                     (oSectorXLC == "Cyclical" ? oCurrentXLC * oWeightXLC : 0) +
                     (oSectorXLE == "Cyclical" ? oCurrentXLE * oWeightXLE : 0) +
                     (oSectorXLF == "Cyclical" ? oCurrentXLF * oWeightXLF : 0) +
                     (oSectorXLI == "Cyclical" ? oCurrentXLI * oWeightXLI : 0) +
                     (oSectorXLK == "Cyclical" ? oCurrentXLK * oWeightXLK : 0) +
                     (oSectorXLP == "Cyclical" ? oCurrentXLP * oWeightXLP : 0) +
                     (oSectorXLRE == "Cyclical" ? oCurrentXLRE * oWeightXLRE : 0) +
                     (oSectorXLU == "Cyclical" ? oCurrentXLU * oWeightXLU : 0) +
                     (oSectorXLV == "Cyclical" ? oCurrentXLV * oWeightXLV : 0) +
                     (oSectorXLY == "Cyclical" ? oCurrentXLY * oWeightXLY : 0)               
                 
oCurrentDefensive =  (oSectorXLB == "Defensive" ? oCurrentXLB * oWeightXLB : 0) +
                     (oSectorXLC == "Defensive" ? oCurrentXLC * oWeightXLC : 0) +
                     (oSectorXLE == "Defensive" ? oCurrentXLE * oWeightXLE : 0) +
                     (oSectorXLF == "Defensive" ? oCurrentXLF * oWeightXLF : 0) +
                     (oSectorXLI == "Defensive" ? oCurrentXLI * oWeightXLI : 0) +
                     (oSectorXLK == "Defensive" ? oCurrentXLK * oWeightXLK : 0) +
                     (oSectorXLP == "Defensive" ? oCurrentXLP * oWeightXLP : 0) +
                     (oSectorXLRE == "Defensive" ? oCurrentXLRE * oWeightXLRE : 0) +
                     (oSectorXLU == "Defensive" ? oCurrentXLU * oWeightXLU : 0) +
                     (oSectorXLV == "Defensive" ? oCurrentXLV * oWeightXLV : 0) +
                     (oSectorXLY == "Defensive" ? oCurrentXLY * oWeightXLY : 0)


// Calculate the changes

oSpx = 100 * (oCurrentSpx / oOpenSpx - 1)
oCyclical = 100 * (oCurrentCyclical / oOpenCyclical - 1)
oDefensive = 100 * (oCurrentDefensive / oOpenDefensive - 1)


// Calculate the plot values

oSpxPlot = ta.ema(oSpx, oEma)
oCyclicalPlot = ta.ema(oCyclical, oEma)
oDefensivePlot = ta.ema(oDefensive, oEma)


/////////////////////////////////////////////////
//  DISPLAY STUFF
/////////////////////////////////////////////////

// Spaghetti

oXLB = 100 * (oCurrentXLB / oOpenXLB - 1)
oXLC = 100 * (oCurrentXLC / oOpenXLC - 1)
oXLE = 100 * (oCurrentXLE / oOpenXLE - 1)
oXLF = 100 * (oCurrentXLF / oOpenXLF - 1)
oXLI = 100 * (oCurrentXLI / oOpenXLI - 1)
oXLK = 100 * (oCurrentXLK / oOpenXLK - 1)
oXLP = 100 * (oCurrentXLP / oOpenXLP - 1)
oXLRE = 100 * (oCurrentXLRE / oOpenXLRE - 1)
oXLU = 100 * (oCurrentXLU / oOpenXLU - 1)
oXLV = 100 * (oCurrentXLV / oOpenXLV - 1)
oXLY = 100 * (oCurrentXLY / oOpenXLY - 1)

oXLBPlot = ta.ema(oXLB, oEma)
oXLCPlot = ta.ema(oXLC, oEma)
oXLEPlot = ta.ema(oXLE, oEma)
oXLFPlot = ta.ema(oXLF, oEma)
oXLIPlot = ta.ema(oXLI, oEma)
oXLKPlot = ta.ema(oXLK, oEma)
oXLPPlot = ta.ema(oXLP, oEma)
oXLREPlot = ta.ema(oXLRE, oEma)
oXLUPlot = ta.ema(oXLU, oEma)
oXLVPlot = ta.ema(oXLV, oEma)
oXLYPlot = ta.ema(oXLY, oEma)

plot(oXLBPlot, linewidth = oSpagWidth, color = oSpagFlag and oRTH ? oSectorXLB == "Cyclical" ? oSpagCyclical : oSpagDefensive : oColorNone)
plot(oXLCPlot, linewidth = oSpagWidth, color = oSpagFlag and oRTH ? oSectorXLC == "Cyclical" ? oSpagCyclical : oSpagDefensive : oColorNone)
plot(oXLEPlot, linewidth = oSpagWidth, color = oSpagFlag and oRTH ? oSectorXLE == "Cyclical" ? oSpagCyclical : oSpagDefensive : oColorNone)
plot(oXLFPlot, linewidth = oSpagWidth, color = oSpagFlag and oRTH ? oSectorXLF == "Cyclical" ? oSpagCyclical : oSpagDefensive : oColorNone)
plot(oXLIPlot, linewidth = oSpagWidth, color = oSpagFlag and oRTH ? oSectorXLI == "Cyclical" ? oSpagCyclical : oSpagDefensive : oColorNone)
plot(oXLKPlot, linewidth = oSpagWidth, color = oSpagFlag and oRTH ? oSectorXLK == "Cyclical" ? oSpagCyclical : oSpagDefensive : oColorNone)
plot(oXLPPlot, linewidth = oSpagWidth, color = oSpagFlag and oRTH ? oSectorXLP == "Cyclical" ? oSpagCyclical : oSpagDefensive : oColorNone)
plot(oXLREPlot, linewidth = oSpagWidth, color = oSpagFlag and oRTH ? oSectorXLRE == "Cyclical" ? oSpagCyclical : oSpagDefensive : oColorNone)
plot(oXLUPlot, linewidth = oSpagWidth, color = oSpagFlag and oRTH ? oSectorXLU == "Cyclical" ? oSpagCyclical : oSpagDefensive : oColorNone)
plot(oXLVPlot, linewidth = oSpagWidth, color = oSpagFlag and oRTH ? oSectorXLV == "Cyclical" ? oSpagCyclical : oSpagDefensive : oColorNone)
plot(oXLYPlot, linewidth = oSpagWidth, color = oSpagFlag and oRTH ? oSectorXLY == "Cyclical" ? oSpagCyclical : oSpagDefensive : oColorNone)



// Plot the lines and infill

plot(0, color = oColorGray)

oPlotSpx = plot(oSpxPlot, linewidth = oLineWidthSpx, color = oNewSession or not(oRTH) ? oColorNone : oColorSpx)
oPlotCyclical = plot(oCyclicalPlot, linewidth = oLineWidthCyclical, color = oNewSession or not(oRTH) ? oColorNone : oColorCyclical)
oPlotDefensive = plot(oDefensivePlot, linewidth = oLineWidthDefensive, color = oNewSession or not(oRTH) ? oColorNone : oColorDefensive)

fill(oPlotCyclical, oPlotDefensive, color = oNewSession or not(oRTH) ? oColorNone : oCyclicalPlot > oDefensivePlot ? oSoftGreen : oSoftRed)


// Plot the table


var table oTable1 = table.new(position.bottom_left, 13, 6)

var oCount = 9

if barstate.isfirst

    table.cell(oTable1, 1, 0, syminfo.ticker, text_color = oTextColor, text_halign = text.align_left)

    table.cell(oTable1, 0, 2, " ", bgcolor = not(oRTH) ? oColorNone : oColorDefensive)
    table.cell(oTable1, 0, 3, " ", bgcolor = not(oRTH) ? oColorNone : oColorDefensive)
    table.cell(oTable1, 1, 2, "Defensive", text_color = oTextColor, text_halign = text.align_left)

    table.cell(oTable1, 0, 4, " ", bgcolor = not(oRTH) ? oColorNone : oColorCyclical)
    table.cell(oTable1, 0, 5, " ", bgcolor = not(oRTH) ? oColorNone : oColorCyclical)
    table.cell(oTable1, 1, 4, "Cyclical", text_color = oTextColor, text_halign = text.align_left)

    oCount := 2
    
    if oSectorXLB == "Defensive"
        table.cell(oTable1, oCount, 2, "XLB", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
    
    if oSectorXLC == "Defensive"
        table.cell(oTable1, oCount, 2, "XLC", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
    
    if oSectorXLE == "Defensive"
        table.cell(oTable1, oCount, 2, "XLE", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
    
    if oSectorXLF == "Defensive"
        table.cell(oTable1, oCount, 2, "XLF", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
        
    if oSectorXLI == "Defensive"
        table.cell(oTable1, oCount, 2, "XLI", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
    
    if oSectorXLK == "Defensive"
        table.cell(oTable1, oCount, 2, "XLK", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
    
    if oSectorXLP == "Defensive"
        table.cell(oTable1, oCount, 2, "XLP", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
    
    if oSectorXLRE == "Defensive"
        table.cell(oTable1, oCount, 2, "XLRE", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
    
    if oSectorXLU == "Defensive"
        table.cell(oTable1, oCount, 2, "XLU", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
        
    if oSectorXLV == "Defensive"
        table.cell(oTable1, oCount, 2, "XLV", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
        
    if oSectorXLY == "Defensive"
        table.cell(oTable1, oCount, 2, "XLY", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
    
    oCount := 2
    
    if oSectorXLB == "Cyclical"
        table.cell(oTable1, oCount, 4, "XLB", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
    
    if oSectorXLC == "Cyclical"
        table.cell(oTable1, oCount, 4, "XLC", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
    
    if oSectorXLE == "Cyclical"
        table.cell(oTable1, oCount, 4, "XLE", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
    
    if oSectorXLF == "Cyclical"
        table.cell(oTable1, oCount, 4, "XLF", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
        
    if oSectorXLI == "Cyclical"
        table.cell(oTable1, oCount, 4, "XLI", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
    
    if oSectorXLK == "Cyclical"
        table.cell(oTable1, oCount, 4, "XLK", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
    
    if oSectorXLP == "Cyclical"
        table.cell(oTable1, oCount, 4, "XLP", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
    
    if oSectorXLRE == "Cyclical"
        table.cell(oTable1, oCount, 4, "XLRE", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
    
    if oSectorXLU == "Cyclical"
        table.cell(oTable1, oCount, 4, "XLU", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
        
    if oSectorXLV == "Cyclical"
        table.cell(oTable1, oCount, 4, "XLV", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1
        
    if oSectorXLY == "Cyclical"
        table.cell(oTable1, oCount, 4, "XLY", text_color = oTextColor, text_halign = text.align_center)
        oCount := oCount + 1



table.cell(oTable1, 1, 1, str.format("{0, number, #.##}", math.abs(oSpx)), text_color = not(oRTH) ? oColorNone : oSpx > 0 ? oTextGreen : oTextRed, text_halign = text.align_left)
table.cell(oTable1, 1, 3, str.format("{0, number, #.##}", math.abs(oDefensive)), text_color = not(oRTH) ? oColorNone : oDefensive > 0 ? oTextGreen : oTextRed, text_halign = text.align_left)
table.cell(oTable1, 1, 5, str.format("{0, number, #.##}", math.abs(oCyclical)), text_color = not(oRTH) ? oColorNone : oCyclical > 0 ? oTextGreen : oTextRed, text_halign = text.align_left)


oCount := 2

if oSectorXLB == "Defensive"
    table.cell(oTable1, oCount, 3, str.format("{0, number, #.##}", math.abs(oXLB)), text_color = not(oRTH) ? oColorNone : oXLB > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1

if oSectorXLC == "Defensive"
    table.cell(oTable1, oCount, 3, str.format("{0, number, #.##}", math.abs(oXLC)), text_color = not(oRTH) ? oColorNone : oXLC > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1

if oSectorXLE == "Defensive"
    table.cell(oTable1, oCount, 3, str.format("{0, number, #.##}", math.abs(oXLE)), text_color = not(oRTH) ? oColorNone : oXLE > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1

if oSectorXLF == "Defensive"
    table.cell(oTable1, oCount, 3, str.format("{0, number, #.##}", math.abs(oXLF)), text_color = not(oRTH) ? oColorNone : oXLF > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1
    
if oSectorXLI == "Defensive"
    table.cell(oTable1, oCount, 3, str.format("{0, number, #.##}", math.abs(oXLI)), text_color = not(oRTH) ? oColorNone : oXLI > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1

if oSectorXLK == "Defensive"
    table.cell(oTable1, oCount, 3, str.format("{0, number, #.##}", math.abs(oXLK)), text_color = not(oRTH) ? oColorNone : oXLK > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1

if oSectorXLP == "Defensive"
    table.cell(oTable1, oCount, 3, str.format("{0, number, #.##}", math.abs(oXLP)), text_color = not(oRTH) ? oColorNone : oXLP > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1

if oSectorXLRE == "Defensive"
    table.cell(oTable1, oCount, 3, str.format("{0, number, #.##}", math.abs(oXLRE)), text_color = not(oRTH) ? oColorNone : oXLRE > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1

if oSectorXLU == "Defensive"
    table.cell(oTable1, oCount, 3, str.format("{0, number, #.##}", math.abs(oXLU)), text_color = not(oRTH) ? oColorNone : oXLU > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1
    
if oSectorXLV == "Defensive"
    table.cell(oTable1, oCount, 3, str.format("{0, number, #.##}", math.abs(oXLV)), text_color = not(oRTH) ? oColorNone : oXLV > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1
    
if oSectorXLY == "Defensive"
    table.cell(oTable1, oCount, 3, str.format("{0, number, #.##}", math.abs(oXLY)), text_color = not(oRTH) ? oColorNone : oXLY > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1

oCount := 2

if oSectorXLB == "Cyclical"
    table.cell(oTable1, oCount, 5, str.format("{0, number, #.##}", math.abs(oXLB)), text_color = not(oRTH) ? oColorNone : oXLB > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1

if oSectorXLC == "Cyclical"
    table.cell(oTable1, oCount, 5, str.format("{0, number, #.##}", math.abs(oXLC)), text_color = not(oRTH) ? oColorNone : oXLC > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1

if oSectorXLE == "Cyclical"
    table.cell(oTable1, oCount, 5, str.format("{0, number, #.##}", math.abs(oXLE)), text_color = not(oRTH) ? oColorNone : oXLE > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1

if oSectorXLF == "Cyclical"
    table.cell(oTable1, oCount, 5, str.format("{0, number, #.##}", math.abs(oXLF)), text_color = not(oRTH) ? oColorNone : oXLF > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1
    
if oSectorXLI == "Cyclical"
    table.cell(oTable1, oCount, 5, str.format("{0, number, #.##}", math.abs(oXLI)), text_color = not(oRTH) ? oColorNone : oXLI > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1

if oSectorXLK == "Cyclical"
    table.cell(oTable1, oCount, 5, str.format("{0, number, #.##}", math.abs(oXLK)), text_color = not(oRTH) ? oColorNone : oXLK > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1

if oSectorXLP == "Cyclical"
    table.cell(oTable1, oCount, 5, str.format("{0, number, #.##}", math.abs(oXLP)), text_color = not(oRTH) ? oColorNone : oXLP > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1

if oSectorXLRE == "Cyclical"
    table.cell(oTable1, oCount, 5, str.format("{0, number, #.##}", math.abs(oXLRE)), text_color = not(oRTH) ? oColorNone : oXLRE > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1

if oSectorXLU == "Cyclical"
    table.cell(oTable1, oCount, 5, str.format("{0, number, #.##}", math.abs(oXLU)), text_color = not(oRTH) ? oColorNone : oXLU > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1
    
if oSectorXLV == "Cyclical"
    table.cell(oTable1, oCount, 5, str.format("{0, number, #.##}", math.abs(oXLV)), text_color = not(oRTH) ? oColorNone : oXLV > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1
    
if oSectorXLY == "Cyclical"
    table.cell(oTable1, oCount, 5, str.format("{0, number, #.##}", math.abs(oXLY)), text_color = not(oRTH) ? oColorNone : oXLY > 0 ? oTextGreen : oTextRed, text_halign = text.align_center)
    oCount := oCount + 1




// That's all folks









