本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// ─────────────────────────────────────────────────────────────────────────────
// INDEX BULL STACK SCANNER (TradingView FREE plan — uses 1 indicator slot)
// Bull stack = Close > EMA20 > EMA40 AND Close > SMA50 AND Close > SMA200
//
// Pick a Universe + Batch in Settings. Pine Script allows 40 symbols per script,
// so the bigger indexes are split into batches of 40:
// S&P 500 (503 stocks) → batches 1–13
// Nasdaq-100 (103 stocks) → batches 1–3
// Dow Jones Industrial Average (30) → batch 1
// Straits Times Index, Singapore (30) → batch 1
// Tip: add the indicator to the chart twice (free plan allows 2) and set
// different batches to see 80 symbols at a time.
//
// Built-in lists: S&P 500 as of Nov 2026; Nasdaq-100 per Nasdaq's 3 Aug 2026
// file; Dow 30 after the 29 Jun 2026 change (GOOGL in, VZ out); STI 30
// unchanged through the Sep 2026 review. Edit the lists below when members change.
// Tip for STI: load an SGX chart (e.g. SGX:ES3) so the last bar lines up with
// Singapore trading days.
// ─────────────────────────────────────────────────────────────────────────────
indicator("Index Bull Stack Scanner (Price > EMA20 > EMA40, > SMA50, > SMA200)", shorttitle = "Index Bull Stack", overlay = true, calc_bars_count = 1500)
// ───── Built-in universes (edit freely when index members change) ─────
string SPX_LIST = "A,AAPL,ABBV,ABNB,ABT,ACGL,ACN,ADBE,ADI,ADM,ADP,ADSK,AEE,AEP,AES,AFL,AIG,AIZ,AJG,AKAM,ALB,ALGN,ALL,ALLE,AMAT,AMCR,AMD,AME,AMGN,AMP,AMT,AMZN,ANET,AON,AOS,APA,APD,APH,APO,APP,APTV,ARE,ARES,ATO,AVGO,AVY,AWK,AXON,AXP,AZO," +
"BA,BAC,BALL,BAX,BBY,BDX,BE,BEN,BF.B,BG,BIIB,BKNG,BKR,BLK,BMY,BNY,BR,BRK.B,BRO,BSX,BX,BXP,C,CAH,CARR,CASY,CAT,CB,CB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,META,MNST,MPWR,MRVL,MSFT,MSTR,MU,NBIS,NFLX,NVDA,NXPI,ODFL,ORLY,PANW,PAYX,PCAR,PDD,PEP,PLTR,PYPL,QCOM,REGN,RKLB,ROP,ROST,SBUX,SHOP,SNDK,SNPS,SPCX,STX,TER,TMUS,TRI,TSLA,TTWO,TXN,VRTX,WBD,WDAY," +
"WDC,WMT,XEL"
string DJI_LIST = "AAPL,AMGN,AMZN,AXP,BA,CAT,CRM,CSCO,CVX,DIS,GOOGL,GS,HD,HON,IBM,JNJ,JPM,KO,MCD,MMM,MRK,MSFT,NKE,NVDA,PG,SHW,TRV,UNH,V,WMT"
string STI_LIST = "SGX:A17U,SGX:C38U,SGX:9CI,SGX:C09,SGX:D05,SGX:D01,SGX:J69U,SGX:BUOU,SGX:G13,SGX:H78,SGX:J36,SGX:BN4,SGX:AJBU,SGX:ME8U,SGX:M44U,SGX:N2IU,SGX:O39,SGX:S58,SGX:5E2,SGX:U96,SGX:C6L,SGX:S68,SGX:Z74,SGX:S63,SGX:Y92,SGX:U11,SGX:U14,SGX:V03,SGX:F34,SGX:BS6"
// ───── Inputs ─────
string universeInput = input.string("S&P 500", "Universe", options = ["S&P 500", "Nasdaq-100", "Dow Jones 30", "Straits Times Index (STI)"], group = "Universe")
int batchInput = input.int(1, "Batch (40 symbols each)", minval = 1, maxval = 13, group = "Universe")
string scanTf = input.timeframe("D", "Scan timeframe", group = "Settings")
int fastLen = input.int(20, "Fast EMA", minval = 1, group = "Settings")
int slowLen = input.int(40, "Slow EMA", minval = 1, group = "Settings")
int midLen = input.int(50, "SMA 1", minval = 1, group = "Settings")
int longLen = input.int(200, "SMA 2", minval = 1, group = "Settings")
bool strict = input.bool(false, "Strict stack: also require EMA40 > SMA50 > SMA200", group = "Settings")
bool showAll = input.bool(true, "Also list symbols that do NOT qualify", group = "Display")
bool plotMAs = input.bool(true, "Plot the 4 MAs on the current chart", group = "Display")
string posInput = input.string("Top Right", "Table position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = "Display")
string sizeInput = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = "Display")
// ───── Condition flags, evaluated inside each requested symbol ─────
// 1 = Close > EMA fast | 2 = EMA fast > EMA slow | 4 = Close > SMA 1 | 8 = Close > SMA 2 | 16 = EMA slow > SMA 1 > SMA 2
stackFlags() =>
float e1 = ta.ema(close, fastLen)
float e2 = ta.ema(close, slowLen)
float s1 = ta.sma(close, midLen)
float s2 = ta.sma(close, longLen)
int f = 0
f += close > e1 ? 1 : 0
f += e1 > e2 ? 2 : 0
f += close > s1 ? 4 : 0
f += close > s2 ? 8 : 0
f += e2 > s1 and s1 > s2 ? 16 : 0
na(s2) ? -1 : f
hasBit(int f, int b) =>
f >= 0 and math.floor(f / b) % 2 == 1
isBull(int f) =>
hasBit(f, 1) and hasBit(f, 2) and hasBit(f, 4) and hasBit(f, 8) and (not strict or hasBit(f, 16))
// ───── Build the universe and pick this batch (runs once) ─────
var array<string> symbols = array.new<string>()
var int totalCount = 0
var int batchCount = 0
var int startIdx = 0
if barstate.isfirst
string raw = switch universeInput
"S&P 500" => SPX_LIST
"Nasdaq-100" => NDX_LIST
"Dow Jones 30" => DJI_LIST
=> STI_LIST
string cleaned = str.upper(str.replace_all(str.replace_all(raw, "\n", ","), " ", ""))
array<string> universe = array.new<string>()
for s in str.split(cleaned, ",")
if str.length(s) > 0
universe.push(s)
totalCount := universe.size()
batchCount := math.ceil(totalCount / 40.0)
startIdx := (batchInput - 1) * 40
if startIdx < totalCount
int endIdx = math.min(startIdx + 40, totalCount)
for i = startIdx to endIdx - 1
symbols.push(universe.get(i))
// ───── Request each symbol in the batch (one value per symbol) ─────
array<int> results = array.new<int>()
for sym in symbols
int r = request.security(sym, scanTf, stackFlags(), ignore_invalid_symbol = true)
results.push(na(r) ? -2 : r)
// ───── Results table ─────
tablePos = switch posInput
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
=> position.top_right
txtSize = switch sizeInput
"Tiny" => size.tiny
"Normal" => size.normal
=> size.small
color HDR_BG = color.rgb(30, 34, 45)
color ROW_BG = color.rgb(19, 23, 34)
color BULL_BG = color.rgb(8, 153, 129)
color NO_BG = color.rgb(120, 40, 50)
color OK_FG = color.rgb(38, 166, 154)
color BAD_FG = color.rgb(239, 83, 80)
color TXT = color.white
var table tbl = table.new(tablePos, 6, 46, frame_color = color.gray, frame_width = 1, border_color = color.new(color.gray, 60), border_width = 1)
putCell(int c, int r, string t, color bg, color fg) =>
tbl.cell(c, r, t, text_color = fg, bgcolor = bg, text_size = txtSize)
mark(int f, int b) =>
hasBit(f, b) ? "✓" : "✗"
markColor(int f, int b) =>
hasBit(f, b) ? OK_FG : BAD_FG
if barstate.islast
tbl.clear(0, 0, 5, 45)
string title = universeInput + " · Batch " + str.tostring(batchInput) + " of " + str.tostring(batchCount)
if startIdx < totalCount
title += " (#" + str.tostring(startIdx + 1) + "–" + str.tostring(startIdx + symbols.size()) + " of " + str.tostring(totalCount) + ")"
putCell(0, 0, title, HDR_BG, TXT)
tbl.merge_cells(0, 0, 5, 0)
if startIdx >= totalCount
putCell(0, 1, "This universe only has " + str.tostring(batchCount) + " batches. Lower the Batch number in Settings.", NO_BG, TXT)
tbl.merge_cells(0, 1, 5, 1)
else
putCell(0, 1, "Symbol (" + scanTf + ")", HDR_BG, TXT)
putCell(1, 1, "> EMA" + str.tostring(fastLen), HDR_BG, TXT)
putCell(2, 1, "EMA" + str.tostring(fastLen) + " > EMA" + str.tostring(slowLen), HDR_BG, TXT)
putCell(3, 1, "> SMA" + str.tostring(midLen), HDR_BG, TXT)
putCell(4, 1, "> SMA" + str.tostring(longLen), HDR_BG, TXT)
putCell(5, 1, "BULL STACK", HDR_BG, TXT)
int row = 2
int matches = 0
// Pass 0 lists the matches first, pass 1 lists the rest
for pass = 0 to 1
for [i, sym] in symbols
int f = results.get(i)
bool bull = isBull(f)
if (pass == 0 and bull) or (pass == 1 and not bull and showAll)
putCell(0, row, sym, ROW_BG, TXT)
if f >= 0
putCell(1, row, mark(f, 1), ROW_BG, markColor(f, 1))
putCell(2, row, mark(f, 2), ROW_BG, markColor(f, 2))
putCell(3, row, mark(f, 4), ROW_BG, markColor(f, 4))
putCell(4, row, mark(f, 8), ROW_BG, markColor(f, 8))
putCell(5, row, bull ? "YES" : "no", bull ? BULL_BG : NO_BG, TXT)
else
string why = f == -1 ? "not enough history" : "invalid symbol"
putCell(1, row, "-", ROW_BG, TXT)
putCell(2, row, "-", ROW_BG, TXT)
putCell(3, row, "-", ROW_BG, TXT)
putCell(4, row, "-", ROW_BG, TXT)
putCell(5, row, why, NO_BG, TXT)
row += 1
if bull
matches += 1
putCell(0, row, "Matches: " + str.tostring(matches) + " / " + str.tostring(symbols.size()), HDR_BG, TXT)
tbl.merge_cells(0, row, 5, row)
// ───── Same check on the symbol currently on the chart (for visual verification) ─────
float cE1 = ta.ema(close, fastLen)
float cE2 = ta.ema(close, slowLen)
float cS1 = ta.sma(close, midLen)
float cS2 = ta.sma(close, longLen)
plot(plotMAs ? cE1 : na, "Fast EMA", color = color.rgb(41, 98, 255), linewidth = 1)
plot(plotMAs ? cE2 : na, "Slow EMA", color = color.rgb(255, 152, 0), linewidth = 1)
plot(plotMAs ? cS1 : na, "SMA 1", color = color.rgb(156, 39, 176), linewidth = 2)
plot(plotMAs ? cS2 : na, "SMA 2", color = color.rgb(239, 83, 80), linewidth = 2)
bool chartBull = close > cE1 and cE1 > cE2 and close > cS1 and close > cS2 and (not strict or (cE2 > cS1 and cS1 > cS2))
bgcolor(plotMAs and chartBull ? color.new(color.green, 92) : na, title = "Chart symbol in bull stack")
plot(chartBull ? 1 : 0, "Bull stack (1 = yes)", display = display.data_window)
alertcondition(chartBull and not chartBull[1], "Enters bull stack", "{{ticker}} entered the bull stack")