本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © thescalpeltrader
//@version=6
indicator("Sector Rotation & Stock Dashboard", shorttitle = "Sector Stocks", overlay = true)
// ─────────────────────────────────────────────────────────────────────────────
// WHAT IT DOES
// One table, two levels:
// 1. Sector board — 12 NSE sector indices ranked against a benchmark (NIFTY 50 by default):
// 1D / 1W / 1M / 3M returns (relative or absolute), rank change vs N sessions ago, rotation phase.
// 2. Stock list — the stocks of one sector, each with its own phase measured against the sector index
// (or the benchmark), returns, and distance from a daily EMA. Filters and sorting in the settings.
// Phase = sign of two relative returns (stock or sector return minus base return):
// medium-term (default 50 sessions) and short-term (default 10 sessions)
// Leading = both ≥ 0 · Weakening = medium ≥ 0, short < 0 · Lagging = both < 0 · Improving = medium < 0, short ≥ 0
// Daily closes only, so the table is identical on every chart timeframe.
// Educational tool — no signals, not a recommendation.
// ─────────────────────────────────────────────────────────────────────────────
const int MAXN = 24
// ── ① Stock list ──
g1 = "① Stock list"
pickMode = input.string("Chosen sector", "Sector for the stock list", options = ["Chosen sector", "Chart symbol's sector"], group = g1, display = display.none, tooltip = "Chart symbol's sector: uses the first sector list (or sector index) that contains the chart's symbol. Falls back to the chosen sector if it isn't found.")
sel = input.string("Bank", "Chosen sector", options = ["Bank", "IT", "Pharma", "Auto", "FMCG", "Metal", "Realty", "Energy", "Fin Services", "PSU Bank", "Infra", "Media"], group = g1, display = display.none)
basis = input.string("Sector index", "Compare stocks against", options = ["Sector index", "Benchmark"], group = g1, display = display.none, tooltip = "Sector index = each stock vs its own sector index. Benchmark = each stock vs the benchmark in ③.")
phShow = input.string("All", "Show phases", options = ["All", "Leading + Improving", "Leading only", "Improving only"], group = g1, display = display.none)
emaOn = input.bool(false, "Only stocks above the EMA", group = g1, display = display.none)
emaLen = input.int(50, "EMA length (daily)", minval = 5, maxval = 200, group = g1, display = display.none)
sortBy = input.string("Phase, then short-term strength", "Sort stocks by", options = ["Phase, then short-term strength", "Medium-term strength", "1M return"], group = g1, display = display.none)
// ── ② Sector board ──
g2 = "② Sector board"
mode = input.string("Relative to benchmark", "Returns shown as", options = ["Relative to benchmark", "Absolute"], group = g2, display = display.none, tooltip = "Relative = sector return minus benchmark return over the same period.")
rankBy = input.string("1M", "Rank sectors by", options = ["1D", "1W", "1M", "3M"], group = g2, display = display.none)
chgLb = input.int(5, "Rank change vs N sessions ago", minval = 1, maxval = 20, group = g2, display = display.none)
showPh = input.bool(true, "Show phase column", group = g2, display = display.none)
// ── ③ Phase settings ──
g3 = "③ Phase and benchmark"
midLb = input.int(50, "Medium-term period (sessions)", minval = 20, maxval = 120, group = g3, display = display.none)
shLb = input.int(10, "Short-term period (sessions)", minval = 3, maxval = 30, group = g3, display = display.none)
bench = input.symbol("NSE:NIFTY", "Benchmark", group = g3, display = display.none)
// ── ④ Sector indices (editable) ──
g4 = "④ Sector indices"
s1 = input.symbol("NSE:BANKNIFTY", "Bank", group = g4, display = display.none)
s2 = input.symbol("NSE:CNXIT", "IT", group = g4, display = display.none)
s3 = input.symbol("NSE:CNXPHARMA", "Pharma", group = g4, display = display.none)
s4 = input.symbol("NSE:CNXAUTO", "Auto", group = g4, display = display.none)
s5 = input.symbol("NSE:CNXFMCG", "FMCG", group = g4, display = display.none)
s6 = input.symbol("NSE:CNXMETAL", "Metal", group = g4, display = display.none)
s7 = input.symbol("NSE:CNXREALTY", "Realty", group = g4, display = display.none)
s8 = input.symbol("NSE:CNXENERGY", "Energy", group = g4, display = display.none)
s9 = input.symbol("NSE:CNXFINANCE", "Fin Services", group = g4, display = display.none)
s10 = input.symbol("NSE:CNXPSUBANK", "PSU Bank", group = g4, display = display.none)
s11 = input.symbol("NSE:CNXINFRA", "Infra", group = g4, display = display.none)
s12 = input.symbol("NSE:CNXMEDIA", "Media", group = g4, display = display.none)
// ── ⑤ Stock lists (editable; "NSE:" is added when no exchange is given) ──
g5 = "⑤ Stock lists (comma separated, up to 24 each)"
l1 = input.string("HDFCBANK,ICICIBANK,SBIN,KOTAKBANK,AXISBANK,INDUSINDBK,BANKBARODA,PNB,FEDERALBNK,IDFCFIRSTB,AUBANK,CANBK", "Bank", group = g5, display = display.none)
l2 = input.string("TCS,INFY,HCLTECH,WIPRO,TECHM,LTIM,PERSISTENT,COFORGE,MPHASIS,OFSS", "IT", group = g5, display = display.none)
l3 = input.string("SUNPHARMA,CIPLA,DRREDDY,DIVISLAB,LUPIN,AUROPHARMA,TORNTPHARM,ZYDUSLIFE,ALKEM,MANKIND,GLENMARK,LAURUSLABS,BIOCON,IPCALAB,GLAND,ABBOTINDIA,NATCOPHARM,AJANTPHARM", "Pharma", group = g5, display = display.none)
l4 = input.string("MARUTI,M_M,TMPV,BAJAJ_AUTO,EICHERMOT,HEROMOTOCO,TVSMOTOR,ASHOKLEY,BOSCHLTD,MOTHERSON,BHARATFORG,MRF,TIINDIA,SONACOMS,BALKRISIND", "Auto", group = g5, display = display.none)
l5 = input.string("HINDUNILVR,ITC,NESTLEIND,BRITANNIA,TATACONSUM,VBL,GODREJCP,DABUR,MARICO,COLPAL,UNITDSPR,PATANJALI,RADICO,EMAMILTD", "FMCG", group = g5, display = display.none)
l6 = input.string("TATASTEEL,JSWSTEEL,HINDALCO,VEDL,JINDALSTEL,SAIL,NMDC,NATIONALUM,APLAPOLLO,HINDZINC,JSL,LLOYDSME,WELCORP", "Metal", group = g5, display = display.none)
l7 = input.string("DLF,GODREJPROP,LODHA,OBEROIRLTY,PRESTIGE,PHOENIXLTD,BRIGADE,SOBHA,ANANTRAJ", "Realty", group = g5, display = display.none)
l8 = input.string("RELIANCE,NTPC,POWERGRID,ONGC,COALINDIA,BPCL,TATAPOWER,ADANIGREEN,ADANIPOWER,IOC,GAIL,OIL", "Energy", group = g5, display = display.none)
l9 = input.string("HDFCBANK,ICICIBANK,BAJFINANCE,BAJAJFINSV,SBIN,KOTAKBANK,AXISBANK,SBILIFE,HDFCLIFE,SHRIRAMFIN,CHOLAFIN,JIOFIN,PFC,RECLTD,MUTHOOTFIN,ICICIGI,HDFCAMC,SBICARD", "Fin Services", group = g5, display = display.none)
l10 = input.string("SBIN,BANKBARODA,PNB,CANBK,UNIONBANK,INDIANB,BANKINDIA,MAHABANK,IOB,UCOBANK,CENTRALBK,PSB", "PSU Bank", group = g5, display = display.none)
l11 = input.string("LT,BHARTIARTL,ULTRACEMCO,ADANIPORTS,NTPC,POWERGRID,GRASIM,INDIGO,SIEMENS,ABB,TATAPOWER,GAIL,CONCOR,INDUSTOWER,DLF", "Infra", group = g5, display = display.none)
l12 = input.string("ZEEL,SUNTV,PVRINOX,NETWORK18,NAZARA,TIPSMUSIC,SAREGAMA,DBCORP,JAGRAN,HATHWAY", "Media", group = g5, display = display.none)
// ── ⑥ Layout and colours ──
g6 = "⑥ Layout and colours"
layout = input.string("Stacked", "Layout", options = ["Stacked", "Side by side", "Sectors only", "Stocks only"], group = g6, display = display.none)
tblPos = input.string("Top right", "Position", options = ["Top right", "Top left", "Bottom right", "Bottom left", "Middle right"], group = g6, display = display.none)
tblSize = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal", "Large"], group = g6, display = display.none)
heat = input.bool(true, "Heat-map return cells", group = g6, display = display.none)
cPos = input.color(#1B8F5A, "Positive", group = g6, inline = "h", display = display.none)
cMid = input.color(#2A2E39, "Zero", group = g6, inline = "h", display = display.none)
cNeg = input.color(#B23A3A, "Negative", group = g6, inline = "h", display = display.none)
cLead = input.color(#4ADE80, "Leading", group = g6, inline = "p1", display = display.none)
cWeak = input.color(#FFD54F, "Weakening", group = g6, inline = "p1", display = display.none)
cLag = input.color(#FF6B6B, "Lagging", group = g6, inline = "p2", display = display.none)
cImp = input.color(#64B5F6, "Improving", group = g6, inline = "p2", display = display.none)
cUp = input.color(#4ADE80, "Rank up / above EMA", group = g6, inline = "rk", display = display.none)
cDn = input.color(#FF6B6B, "Rank down / below EMA", group = g6, inline = "rk", display = display.none)
cSel = input.color(#F2C94C, "Selected sector", group = g6, inline = "sl", display = display.none)
cTBg = input.color(color.new(#131722, 5), "Background", group = g6, inline = "t1", display = display.none)
cTHdBg = input.color(#1E222D, "Header bg", group = g6, inline = "t1", display = display.none)
cTHdTx = input.color(#FFFFFF, "Header text", group = g6, inline = "t2", display = display.none)
cTKey = input.color(#A3A6AF, "Label text", group = g6, inline = "t2", display = display.none)
cTVal = input.color(#E0E3EB, "Value text", group = g6, inline = "t3", display = display.none)
cTChart = input.color(#1F4E79, "Chart symbol row", group = g6, inline = "t4", display = display.none)
cTChTx = input.color(#FFFFFF, "Chart symbol row text", group = g6, inline = "t4", display = display.none)
cTFrame = input.color(color.new(#787B86, 70), "Frame", group = g6, inline = "t5", display = display.none)
cTGrid = input.color(color.new(#787B86, 85), "Grid lines", group = g6, inline = "t5", display = display.none)
// ── Helpers ──
f_size(string s) => s == "Tiny" ? size.tiny : s == "Small" ? size.small : s == "Normal" ? size.normal : size.large
f_ret(float now, float then) => na(now) or na(then) or then == 0 ? na : (now / then - 1) * 100
f_heat(float v, float cap) =>
na(v) or not heat ? cTBg : v >= 0 ? color.from_gradient(v, 0, cap, cMid, cPos) : color.from_gradient(v, -cap, 0, cNeg, cMid)
f_fmt(float v) => na(v) ? "—" : str.tostring(v, "+0.00;-0.00") + "%"
f_short(string sym) =>
parts = str.split(sym, ":")
array.size(parts) > 1 ? array.get(parts, 1) : sym
f_full(string t) => str.contains(t, ":") ? t : "NSE:" + t
f_phase(float mRel, float sRel) => na(mRel) or na(sRel) ? -1 : mRel >= 0 and sRel >= 0 ? 0 : mRel >= 0 ? 1 : sRel < 0 ? 2 : 3
f_phName(int p) => p == 0 ? "Leading" : p == 1 ? "Weakening" : p == 2 ? "Lagging" : p == 3 ? "Improving" : "—"
f_phCol(int p) => p == 0 ? cLead : p == 1 ? cWeak : p == 2 ? cLag : p == 3 ? cImp : cTKey
f_phOrd(int p) => p == 0 ? 0 : p == 3 ? 1 : p == 1 ? 2 : p == 2 ? 3 : 4
f_tokens(string s) => str.split(str.upper(str.replace_all(s, " ", "")), ",")
names = array.from("Bank", "IT", "Pharma", "Auto", "FMCG", "Metal", "Realty", "Energy", "Fin Services", "PSU Bank", "Infra", "Media")
secSyms = array.from(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12)
lists = array.from(l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12)
// ── Pick the sector for the stock list and build its symbol list (once) ──
var array<string> syms = array.new<string>()
var int nSt = 0
var int selI = 0
var bool autoHit = false
if barstate.isfirst
selI := math.max(array.indexof(names, sel), 0)
if pickMode == "Chart symbol's sector"
found = -1
for i = 0 to 11
if found < 0 and (syminfo.tickerid == array.get(secSyms, i) or syminfo.ticker == f_short(array.get(secSyms, i)))
found := i
if found < 0
for i = 0 to 11
if found < 0
for t in f_tokens(array.get(lists, i))
if str.length(t) > 0 and f_short(f_full(t)) == syminfo.ticker
found := i
break
if found >= 0
selI := found
autoHit := true
for t in f_tokens(array.get(lists, selI))
if str.length(t) > 0 and array.size(syms) < MAXN
array.push(syms, f_full(t))
nSt := array.size(syms)
while array.size(syms) < MAXN
array.push(syms, bench)
// ── Daily data ──
// sectors and benchmark: close at lags 0, 1, 5, 21, 63; the same lags shifted by chgLb (past ranking); short and medium look-backs
f_secData() =>
[close, close[1], close[5], close[21], close[63], close[chgLb], close[chgLb + 1], close[chgLb + 5], close[chgLb + 21], close[chgLb + 63], close[shLb], close[midLb]]
f_sec(string sym) =>
request.security(sym, "D", f_secData(), ignore_invalid_symbol = true)
// stocks: close, 1D / 1W / 1M ago, medium and short look-backs, EMA
f_stkData() =>
[close, close[1], close[5], close[21], close[midLb], close[shLb], ta.ema(close, emaLen)]
f_stk(string sym) =>
request.security(sym, "D", f_stkData(), ignore_invalid_symbol = true)
// 1 benchmark + 12 sectors + 24 stock slots = 37 requests
[b0, b1, b5, b21, b63, bp0, bp1, bp5, bp21, bp63, bs, bm] = f_sec(bench)
[a0_1, a1_1, a5_1, a21_1, a63_1, p0_1, p1_1, p5_1, p21_1, p63_1, ss_1, sm_1] = f_sec(s1)
[a0_2, a1_2, a5_2, a21_2, a63_2, p0_2, p1_2, p5_2, p21_2, p63_2, ss_2, sm_2] = f_sec(s2)
[a0_3, a1_3, a5_3, a21_3, a63_3, p0_3, p1_3, p5_3, p21_3, p63_3, ss_3, sm_3] = f_sec(s3)
[a0_4, a1_4, a5_4, a21_4, a63_4, p0_4, p1_4, p5_4, p21_4, p63_4, ss_4, sm_4] = f_sec(s4)
[a0_5, a1_5, a5_5, a21_5, a63_5, p0_5, p1_5, p5_5, p21_5, p63_5, ss_5, sm_5] = f_sec(s5)
[a0_6, a1_6, a5_6, a21_6, a63_6, p0_6, p1_6, p5_6, p21_6, p63_6, ss_6, sm_6] = f_sec(s6)
[a0_7, a1_7, a5_7, a21_7, a63_7, p0_7, p1_7, p5_7, p21_7, p63_7, ss_7, sm_7] = f_sec(s7)
[a0_8, a1_8, a5_8, a21_8, a63_8, p0_8, p1_8, p5_8, p21_8, p63_8, ss_8, sm_8] = f_sec(s8)
[a0_9, a1_9, a5_9, a21_9, a63_9, p0_9, p1_9, p5_9, p21_9, p63_9, ss_9, sm_9] = f_sec(s9)
[a0_10, a1_10, a5_10, a21_10, a63_10, p0_10, p1_10, p5_10, p21_10, p63_10, ss_10, sm_10] = f_sec(s10)
[a0_11, a1_11, a5_11, a21_11, a63_11, p0_11, p1_11, p5_11, p21_11, p63_11, ss_11, sm_11] = f_sec(s11)
[a0_12, a1_12, a5_12, a21_12, a63_12, p0_12, p1_12, p5_12, p21_12, p63_12, ss_12, sm_12] = f_sec(s12)
[c_0, d_0, w_0, m_0, mm_0, sh_0, e_0] = f_stk(array.get(syms, 0))
[c_1, d_1, w_1, m_1, mm_1, sh_1, e_1] = f_stk(array.get(syms, 1))
[c_2, d_2, w_2, m_2, mm_2, sh_2, e_2] = f_stk(array.get(syms, 2))
[c_3, d_3, w_3, m_3, mm_3, sh_3, e_3] = f_stk(array.get(syms, 3))
[c_4, d_4, w_4, m_4, mm_4, sh_4, e_4] = f_stk(array.get(syms, 4))
[c_5, d_5, w_5, m_5, mm_5, sh_5, e_5] = f_stk(array.get(syms, 5))
[c_6, d_6, w_6, m_6, mm_6, sh_6, e_6] = f_stk(array.get(syms, 6))
[c_7, d_7, w_7, m_7, mm_7, sh_7, e_7] = f_stk(array.get(syms, 7))
[c_8, d_8, w_8, m_8, mm_8, sh_8, e_8] = f_stk(array.get(syms, 8))
[c_9, d_9, w_9, m_9, mm_9, sh_9, e_9] = f_stk(array.get(syms, 9))
[c_10, d_10, w_10, m_10, mm_10, sh_10, e_10] = f_stk(array.get(syms, 10))
[c_11, d_11, w_11, m_11, mm_11, sh_11, e_11] = f_stk(array.get(syms, 11))
[c_12, d_12, w_12, m_12, mm_12, sh_12, e_12] = f_stk(array.get(syms, 12))
[c_13, d_13, w_13, m_13, mm_13, sh_13, e_13] = f_stk(array.get(syms, 13))
[c_14, d_14, w_14, m_14, mm_14, sh_14, e_14] = f_stk(array.get(syms, 14))
[c_15, d_15, w_15, m_15, mm_15, sh_15, e_15] = f_stk(array.get(syms, 15))
[c_16, d_16, w_16, m_16, mm_16, sh_16, e_16] = f_stk(array.get(syms, 16))
[c_17, d_17, w_17, m_17, mm_17, sh_17, e_17] = f_stk(array.get(syms, 17))
[c_18, d_18, w_18, m_18, mm_18, sh_18, e_18] = f_stk(array.get(syms, 18))
[c_19, d_19, w_19, m_19, mm_19, sh_19, e_19] = f_stk(array.get(syms, 19))
[c_20, d_20, w_20, m_20, mm_20, sh_20, e_20] = f_stk(array.get(syms, 20))
[c_21, d_21, w_21, m_21, mm_21, sh_21, e_21] = f_stk(array.get(syms, 21))
[c_22, d_22, w_22, m_22, mm_22, sh_22, e_22] = f_stk(array.get(syms, 22))
[c_23, d_23, w_23, m_23, mm_23, sh_23, e_23] = f_stk(array.get(syms, 23))
plot(na, "hidden", display = display.none)
// ── Table ──
rel = mode == "Relative to benchmark"
f_val(float sNow, float sThen, float bNow, float bThen) =>
r = f_ret(sNow, sThen)
rel ? r - f_ret(bNow, bThen) : r
pos = tblPos == "Top right" ? position.top_right : tblPos == "Top left" ? position.top_left : tblPos == "Bottom right" ? position.bottom_right : tblPos == "Bottom left" ? position.bottom_left : position.middle_right
tsz = f_size(tblSize)
var table tb = table.new(pos, 18, 52, border_width = 1, frame_width = 1, frame_color = cTFrame, border_color = cTGrid)
f_cell(int c, int r, string t, color tx, color bg) =>
table.cell(tb, c, r, t, text_color = tx, bgcolor = bg, text_size = tsz)
f_cellL(int c, int r, string t, color tx, color bg, string tip) =>
table.cell(tb, c, r, t, text_color = tx, bgcolor = bg, text_size = tsz, text_halign = text.align_left, tooltip = tip)
showBoard = layout != "Stocks only"
showStocks = layout != "Sectors only"
sideBySide = layout == "Side by side"
bCols = showPh ? 8 : 7
if barstate.islast
table.clear(tb, 0, 0, 17, 51)
// ── sector arrays ──
A0 = array.from(a0_1, a0_2, a0_3, a0_4, a0_5, a0_6, a0_7, a0_8, a0_9, a0_10, a0_11, a0_12)
A1 = array.from(a1_1, a1_2, a1_3, a1_4, a1_5, a1_6, a1_7, a1_8, a1_9, a1_10, a1_11, a1_12)
A5 = array.from(a5_1, a5_2, a5_3, a5_4, a5_5, a5_6, a5_7, a5_8, a5_9, a5_10, a5_11, a5_12)
A21 = array.from(a21_1, a21_2, a21_3, a21_4, a21_5, a21_6, a21_7, a21_8, a21_9, a21_10, a21_11, a21_12)
A63 = array.from(a63_1, a63_2, a63_3, a63_4, a63_5, a63_6, a63_7, a63_8, a63_9, a63_10, a63_11, a63_12)
P0 = array.from(p0_1, p0_2, p0_3, p0_4, p0_5, p0_6, p0_7, p0_8, p0_9, p0_10, p0_11, p0_12)
P1 = array.from(p1_1, p1_2, p1_3, p1_4, p1_5, p1_6, p1_7, p1_8, p1_9, p1_10, p1_11, p1_12)
P5 = array.from(p5_1, p5_2, p5_3, p5_4, p5_5, p5_6, p5_7, p5_8, p5_9, p5_10, p5_11, p5_12)
P21 = array.from(p21_1, p21_2, p21_3, p21_4, p21_5, p21_6, p21_7, p21_8, p21_9, p21_10, p21_11, p21_12)
P63 = array.from(p63_1, p63_2, p63_3, p63_4, p63_5, p63_6, p63_7, p63_8, p63_9, p63_10, p63_11, p63_12)
SS = array.from(ss_1, ss_2, ss_3, ss_4, ss_5, ss_6, ss_7, ss_8, ss_9, ss_10, ss_11, ss_12)
SM = array.from(sm_1, sm_2, sm_3, sm_4, sm_5, sm_6, sm_7, sm_8, sm_9, sm_10, sm_11, sm_12)
secPhase = array.new<int>(12, -1)
idx = array.new<int>()
r1 = array.new<float>()
r5 = array.new<float>()
r21 = array.new<float>()
r63 = array.new<float>()
sc = array.new<float>()
scP = array.new<float>()
for i = 0 to 11
c0 = array.get(A0, i)
if not na(c0)
v1 = f_val(c0, array.get(A1, i), b0, b1)
v5 = f_val(c0, array.get(A5, i), b0, b5)
v21 = f_val(c0, array.get(A21, i), b0, b21)
v63 = f_val(c0, array.get(A63, i), b0, b63)
pv = rankBy == "1D" ? f_val(array.get(P0, i), array.get(P1, i), bp0, bp1) : rankBy == "1W" ? f_val(array.get(P0, i), array.get(P5, i), bp0, bp5) : rankBy == "1M" ? f_val(array.get(P0, i), array.get(P21, i), bp0, bp21) : f_val(array.get(P0, i), array.get(P63, i), bp0, bp63)
cur = rankBy == "1D" ? v1 : rankBy == "1W" ? v5 : rankBy == "1M" ? v21 : v63
array.set(secPhase, i, f_phase(f_ret(c0, array.get(SM, i)) - f_ret(b0, bm), f_ret(c0, array.get(SS, i)) - f_ret(b0, bs)))
array.push(idx, i)
array.push(r1, v1)
array.push(r5, v5)
array.push(r21, v21)
array.push(r63, v63)
array.push(sc, nz(cur, -1e9))
array.push(scP, nz(pv, -1e9))
m = array.size(idx)
boardRows = m == 0 ? 3 : m + 4
// ── block origins ──
sCol0 = showBoard and sideBySide ? bCols + 1 : 0
sRow0 = showBoard and not sideBySide ? boardRows + 1 : 0
// ── 1. Sector board ──
if showBoard
f_cellL(0, 0, "SECTOR ROTATION · ranked by " + rankBy + (rel ? " vs " + f_short(bench) : " return"), cTHdTx, cTHdBg, "")
table.merge_cells(tb, 0, 0, bCols - 1, 0)
hdr = array.from("#", "Sector", "Δ" + str.tostring(chgLb) + "d", "1D", "1W", "1M", "3M", "Phase")
for c = 0 to bCols - 1
f_cell(c, 1, array.get(hdr, c), cTKey, cTHdBg)
if m == 0
f_cellL(0, 2, "No sector data. Check the symbols in ④.", cTVal, cTBg, "")
table.merge_cells(tb, 0, 2, bCols - 1, 2)
else
ordNow = array.sort_indices(sc, order.descending)
ordPast = array.sort_indices(scP, order.descending)
pastRank = array.new<int>(m, 0)
for k = 0 to m - 1
array.set(pastRank, array.get(ordPast, k), k + 1)
for k = 0 to m - 1
j = array.get(ordNow, k)
i = array.get(idx, j)
rw = k + 2
sSym = array.get(secSyms, i)
isChart = syminfo.tickerid == sSym or syminfo.ticker == f_short(sSym)
isSel = showStocks and i == selI
bg = isChart ? cTChart : cTBg
tx = isChart ? cTChTx : isSel ? cSel : cTVal
dr = array.get(pastRank, j) - (k + 1)
p = array.get(secPhase, i)
f_cell(0, rw, str.tostring(k + 1), tx, bg)
f_cellL(1, rw, (isChart ? "► " : isSel ? "◆ " : "") + array.get(names, i), tx, bg, sSym)
f_cell(2, rw, dr > 0 ? "▲" + str.tostring(dr) : dr < 0 ? "▼" + str.tostring(-dr) : "–", dr > 0 ? cUp : dr < 0 ? cDn : cTKey, bg)
v1 = array.get(r1, j)
v5 = array.get(r5, j)
v21 = array.get(r21, j)
v63 = array.get(r63, j)
f_cell(3, rw, f_fmt(v1), cTVal, f_heat(v1, 1.5))
f_cell(4, rw, f_fmt(v5), cTVal, f_heat(v5, 3.0))
f_cell(5, rw, f_fmt(v21), cTVal, f_heat(v21, 6.0))
f_cell(6, rw, f_fmt(v63), cTVal, f_heat(v63, 12.0))
if showPh
f_cell(7, rw, f_phName(p), f_phCol(p), bg)
brow = m + 2
f_cell(0, brow, "", cTKey, cTHdBg)
f_cellL(1, brow, f_short(bench) + " (abs)", cTKey, cTHdBg, bench)
f_cell(2, brow, "", cTKey, cTHdBg)
f_cell(3, brow, f_fmt(f_ret(b0, b1)), cTVal, cTHdBg)
f_cell(4, brow, f_fmt(f_ret(b0, b5)), cTVal, cTHdBg)
f_cell(5, brow, f_fmt(f_ret(b0, b21)), cTVal, cTHdBg)
f_cell(6, brow, f_fmt(f_ret(b0, b63)), cTVal, cTHdBg)
if showPh
f_cell(7, brow, "", cTKey, cTHdBg)
f_cellL(0, brow + 1, "Phase: " + str.tostring(midLb) + "- vs " + str.tostring(shLb) + "-session relative return" + (showStocks ? " · ◆ = sector listed below" : ""), cTKey, cTBg, "")
table.cell_set_text_size(tb, 0, brow + 1, size.tiny)
table.merge_cells(tb, 0, brow + 1, bCols - 1, brow + 1)
// ── 2. Stock list ──
if showStocks
C = array.from(c_0, c_1, c_2, c_3, c_4, c_5, c_6, c_7, c_8, c_9, c_10, c_11, c_12, c_13, c_14, c_15, c_16, c_17, c_18, c_19, c_20, c_21, c_22, c_23)
D1 = array.from(d_0, d_1, d_2, d_3, d_4, d_5, d_6, d_7, d_8, d_9, d_10, d_11, d_12, d_13, d_14, d_15, d_16, d_17, d_18, d_19, d_20, d_21, d_22, d_23)
W5 = array.from(w_0, w_1, w_2, w_3, w_4, w_5, w_6, w_7, w_8, w_9, w_10, w_11, w_12, w_13, w_14, w_15, w_16, w_17, w_18, w_19, w_20, w_21, w_22, w_23)
M1 = array.from(m_0, m_1, m_2, m_3, m_4, m_5, m_6, m_7, m_8, m_9, m_10, m_11, m_12, m_13, m_14, m_15, m_16, m_17, m_18, m_19, m_20, m_21, m_22, m_23)
MM = array.from(mm_0, mm_1, mm_2, mm_3, mm_4, mm_5, mm_6, mm_7, mm_8, mm_9, mm_10, mm_11, mm_12, mm_13, mm_14, mm_15, mm_16, mm_17, mm_18, mm_19, mm_20, mm_21, mm_22, mm_23)
SH = array.from(sh_0, sh_1, sh_2, sh_3, sh_4, sh_5, sh_6, sh_7, sh_8, sh_9, sh_10, sh_11, sh_12, sh_13, sh_14, sh_15, sh_16, sh_17, sh_18, sh_19, sh_20, sh_21, sh_22, sh_23)
EE = array.from(e_0, e_1, e_2, e_3, e_4, e_5, e_6, e_7, e_8, e_9, e_10, e_11, e_12, e_13, e_14, e_15, e_16, e_17, e_18, e_19, e_20, e_21, e_22, e_23)
useSec = basis == "Sector index"
idxSym = array.get(secSyms, selI)
iC = array.get(A0, selI)
bC = useSec ? iC : b0
bM = useSec ? array.get(SM, selI) : bm
bS = useSec ? array.get(SS, selI) : bs
baseNm = useSec ? f_short(idxSym) : f_short(bench)
secPh = array.get(secPhase, selI)
cnt = array.new<int>(4, 0)
nValid = 0
sIdx = array.new<int>()
sKey = array.new<float>()
sR1 = array.new<float>()
sR5 = array.new<float>()
sR21 = array.new<float>()
sMr = array.new<float>()
sSr = array.new<float>()
sPh = array.new<int>()
sEp = array.new<float>()
for k = 0 to math.max(nSt - 1, 0)
if k < nSt
c0 = array.get(C, k)
if not na(c0)
mRel = f_ret(c0, array.get(MM, k)) - f_ret(bC, bM)
sRel = f_ret(c0, array.get(SH, k)) - f_ret(bC, bS)
p = f_phase(mRel, sRel)
ev = array.get(EE, k)
epct = na(ev) or ev == 0 ? na : (c0 / ev - 1) * 100
nValid += 1
if p >= 0
array.set(cnt, p, array.get(cnt, p) + 1)
phOk = phShow == "All" or (phShow == "Leading + Improving" and (p == 0 or p == 3)) or (phShow == "Leading only" and p == 0) or (phShow == "Improving only" and p == 3)
emaOk = not emaOn or (not na(epct) and epct > 0)
if phOk and emaOk
v21 = f_ret(c0, array.get(M1, k))
key = sortBy == "Medium-term strength" ? nz(mRel, -1e9) : sortBy == "1M return" ? nz(v21, -1e9) : -f_phOrd(p) * 10000 + math.max(math.min(nz(sRel, -900), 900), -900)
array.push(sIdx, k)
array.push(sKey, key)
array.push(sR1, f_ret(c0, array.get(D1, k)))
array.push(sR5, f_ret(c0, array.get(W5, k)))
array.push(sR21, v21)
array.push(sMr, mRel)
array.push(sSr, sRel)
array.push(sPh, p)
array.push(sEp, epct)
shown = array.size(sIdx)
x = sCol0
y = sRow0
how = pickMode == "Chart symbol's sector" ? (autoHit ? " (from chart symbol)" : " (chart symbol not in lists)") : ""
f_cellL(x, y, "STOCKS · " + array.get(names, selI) + how + " · vs " + baseNm, cTHdTx, cTHdBg, "")
table.merge_cells(tb, x, y, x + 8, y)
f_cellL(x, y + 1, "Sector vs " + f_short(bench) + ": " + f_phName(secPh), f_phCol(secPh), cTHdBg, idxSym)
table.merge_cells(tb, x, y + 1, x + 3, y + 1)
f_cellL(x + 4, y + 1, "Showing: " + phShow + (emaOn ? " · above " + str.tostring(emaLen) + " EMA" : ""), cTKey, cTHdBg, "")
table.merge_cells(tb, x + 4, y + 1, x + 8, y + 1)
hdr2 = array.from("#", "Stock", "1D", "1W", "1M", "Rel " + str.tostring(midLb) + "d", "Rel " + str.tostring(shLb) + "d", "Phase", "vs " + str.tostring(emaLen) + "EMA")
for c = 0 to 8
f_cell(x + c, y + 2, array.get(hdr2, c), cTKey, cTHdBg)
rowN = y + 3
if nValid == 0
f_cellL(x, rowN, "No stock data. Check this sector's list in ⑤.", cTVal, cTBg, "")
table.merge_cells(tb, x, rowN, x + 8, rowN)
rowN += 1
else if shown == 0
f_cellL(x, rowN, "No stocks match the current filters.", cTVal, cTBg, "")
table.merge_cells(tb, x, rowN, x + 8, rowN)
rowN += 1
else
ord = array.sort_indices(sKey, order.descending)
for q = 0 to shown - 1
j = array.get(ord, q)
k = array.get(sIdx, j)
sym = array.get(syms, k)
isChart = syminfo.tickerid == sym or syminfo.ticker == f_short(sym)
bg = isChart ? cTChart : cTBg
tx = isChart ? cTChTx : cTVal
p = array.get(sPh, j)
ep = array.get(sEp, j)
v1 = array.get(sR1, j)
v5 = array.get(sR5, j)
v21 = array.get(sR21, j)
mr = array.get(sMr, j)
sr = array.get(sSr, j)
f_cell(x, rowN, str.tostring(q + 1), tx, bg)
f_cellL(x + 1, rowN, (isChart ? "► " : "") + f_short(sym), tx, bg, sym)
f_cell(x + 2, rowN, f_fmt(v1), cTVal, f_heat(v1, 2.0))
f_cell(x + 3, rowN, f_fmt(v5), cTVal, f_heat(v5, 4.0))
f_cell(x + 4, rowN, f_fmt(v21), cTVal, f_heat(v21, 8.0))
f_cell(x + 5, rowN, f_fmt(mr), cTVal, f_heat(mr, 15.0))
f_cell(x + 6, rowN, f_fmt(sr), cTVal, f_heat(sr, 6.0))
f_cell(x + 7, rowN, f_phName(p), f_phCol(p), bg)
f_cell(x + 8, rowN, na(ep) ? "—" : (ep > 0 ? "▲ " : "▼ ") + str.tostring(ep, "+0.0;-0.0") + "%", na(ep) ? cTKey : ep > 0 ? cUp : cDn, bg)
rowN += 1
missing = nSt - nValid
f_cellL(x, rowN, "Of " + str.tostring(nValid) + " stocks with data: Leading " + str.tostring(array.get(cnt, 0)) + " · Improving " + str.tostring(array.get(cnt, 3)) + " · Weakening " + str.tostring(array.get(cnt, 1)) + " · Lagging " + str.tostring(array.get(cnt, 2)) + (missing > 0 ? " · no data: " + str.tostring(missing) + " (check tickers in ⑤)" : ""), cTKey, cTHdBg, "")
table.merge_cells(tb, x, rowN, x + 8, rowN)
f_cellL(x, rowN + 1, "Rel = stock return minus " + baseNm + " return over " + str.tostring(midLb) + " / " + str.tostring(shLb) + " sessions · daily closes · educational tool, not a recommendation", cTKey, cTBg, "")
table.cell_set_text_size(tb, x, rowN + 1, size.tiny)
table.merge_cells(tb, x, rowN + 1, x + 8, rowN + 1)