republish US sector rrg (Relative Rotation Graph)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © hkorange2007

//@version=4
study("My Script")
period = input(14)
getStatus(ticker) =>
    securityClose = security(ticker, timeframe.period, close)
    spx = security("SPX", timeframe.period, close)
    rs = 100 * (securityClose / spx)
    rsr = 101 + ((rs - sma(rs, period)) / stdev(rs, period))
    rsrRoc = 100 * ((rsr / rsr[1]) - 1)
    rsm = 101 + ((rsrRoc - sma(rsrRoc, period)) / stdev(rsrRoc, period))
    status = "null"
    bgcolor = color.white
    if rsr < 100 and rsm > 100
        status := "improving"
        bgcolor := color.blue
    else if rsr > 100 and rsm > 100
        status := "leading"
        bgcolor := color.green
    else if rsr > 100 and rsm < 100
        status := "weakening"
        bgcolor := color.yellow
    else if rsr < 100 and rsm < 100
        status := "lagging"
        bgcolor := color.red
    status
    // [rsrRoc, rsr, rsm, status, bgcolor]

var table rrgTtable = table.new(position.bottom_center, 2, 9, frame_color = color.white, bgcolor = color.white, border_width = 1, frame_width = 1, border_color = color.black)
table.cell(rrgTtable, 0, 0, "Energy")
table.cell(rrgTtable, 1, 0, getStatus("XLE"))
table.cell(rrgTtable, 0, 1, "Utilities")
table.cell(rrgTtable, 1, 1, getStatus("XLU"))
table.cell(rrgTtable, 0, 2, "Industrial")
table.cell(rrgTtable, 1, 2, getStatus("XLI"))
table.cell(rrgTtable, 0, 3, "Consumer Staples")
table.cell(rrgTtable, 1, 3, getStatus("XLP"))
table.cell(rrgTtable, 0, 4, "Consumer Discretionary")
table.cell(rrgTtable, 1, 4, getStatus("XLY"))
table.cell(rrgTtable, 0, 5, "Material")
table.cell(rrgTtable, 1, 5, getStatus("XLB"))
table.cell(rrgTtable, 0, 6, "Financial")
table.cell(rrgTtable, 1, 6, getStatus("XLF"))
table.cell(rrgTtable, 0, 7, "Health Care")
table.cell(rrgTtable, 1, 7, getStatus("XLV"))
table.cell(rrgTtable, 0, 8, "Technology")
table.cell(rrgTtable, 1, 8, getStatus("XLK"))
