本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//
// NYSE ADD+
// ─────────────────────────────────────────────────────────────────────────────
// CREDIT
// Based on "ADD for SPX intraday (NYSE Adv-Decl)" by Tom1trader (published
// May 2020, https://www.tradingview.com/v/gjR35HwQ/), also under MPL 2.0.
// The core idea is his: plot NYSE advancers minus decliners in its own pane,
// coloured by direction, with a 10-bar average, as an intraday guide for SPX
// and correlated instruments (SPY, ES, 0DTE options). Thank you, Tom1trader.
//
// WHAT'S NEW IN THIS VERSION
// - Pine v6. Symbol, timeframe and value (Close or HLC3) are settings, so no
// code edits are needed.
// - Direction colour is worked out on the ADD timeframe itself. In the original,
// a 1-min chart showing 5-min ADD turned red for 4 of every 5 bars because the
// value hadn't changed.
// - Hidden outside 9:30–16:00 ET, so each day is its own segment rather than a
// flat line across the overnight session.
// - The average can restart each day, so yesterday's close doesn't skew today's
// first hour. Default colour is orange (black disappears on dark themes).
// - ±1000 / ±1500 level lines (adjustable), optional shading beyond Level 2.
// - Prior day's closing ADD as a reference line.
// - Table: current ADD, day high, day low, prior close.
// - Optional price-vs-ADD divergence markers, plus alerts.
//
// HOW TO USE
// 1. Add it to an ES, SPY or SPX intraday chart (1–15 min works best). It isn't
// meant for daily or higher timeframes.
// 2. Settings > Data: keep USI:ADD for NYSE breadth, or switch to USI:ADDQ for
// Nasdaq (useful alongside NQ, or when mega-cap tech is driving ES).
// 3. Reading it:
// - Direction matters more than level. ES tends to track whether ADD is
// rising (green) or falling (red).
// - ADD trending steadily beyond ±1000 and making new day highs/lows suggests
// a trend day: don't fade it. ADD flipping around zero suggests chop.
// - Day high / day low in the table: the broadest buying and selling seen so
// far. If ES retests its high while ADD is well below its day high, the
// rally is narrowing.
// 4. Divergence markers:
// - Triangle up: chart makes a new session low while ADD stays at least
// "Tolerance" above its own session low (selling is less broad).
// - Triangle down: the mirror at session highs.
// The first 15 minutes after the open are skipped by default (adjustable),
// because ADD swings hard while stocks open.
// They can fire several bars in a row on strong trend days. Treat them as a
// heads-up and wait for price to confirm (failed new low, level reclaimed).
// 5. Alerts (Create alert > condition "ADD+"): ADD crossing zero, ADD moving
// beyond ±Level 1, bullish or bearish divergence.
//
// HISTORY
// The indicator shows ADD for every bar loaded on your chart, so more history
// on the chart means more history here. A 5-min chart holds about 5× as many
// days as a 1-min chart; scroll left to load more. To see how much ADD history
// TradingView has, open USI:ADD on its own chart.
// ─────────────────────────────────────────────────────────────────────────────
//@version=6
indicator("NYSE ADD+", shorttitle = "ADD+", overlay = false)
// ───────── Inputs ─────────
grpD = "Data"
sym = input.symbol("USI:ADD", "Breadth symbol", group = grpD, tooltip = "USI:ADD = NYSE advancers − decliners. USI:ADDQ = Nasdaq.")
followTF = input.bool(false, "Follow chart timeframe", group = grpD)
tfIn = input.timeframe("5", "Timeframe (when not following chart)", group = grpD)
srcMode = input.string("Close", "Value", options = ["Close", "HLC3"], group = grpD, tooltip = "Original script used HLC3. Close is the usual ADD reading.")
rthOnly = input.bool(true, "Hide outside 9:30–16:00 ET", group = grpD)
grpS = "Display"
styleMode = input.string("Line", "Style", options = ["Line", "Columns"], group = grpS)
lineWidth = input.int(3, "Line width", minval = 1, maxval = 6, group = grpS)
maLen = input.int(10, "Average length (ADD bars)", minval = 1, group = grpS)
maReset = input.bool(true, "Restart average each day", group = grpS)
maCol = input.color(color.orange, "Average colour", group = grpS)
showPrev = input.bool(true, "Show prior day's closing ADD", group = grpS)
showTable = input.bool(true, "Show value table", group = grpS)
grpL = "Levels"
lvl1 = input.int(1000, "Level 1 (±)", minval = 0, group = grpL)
lvl2 = input.int(1500, "Level 2 (±)", minval = 0, group = grpL)
shadeTrend = input.bool(false, "Shade background beyond Level 2", group = grpL)
grpX = "Divergence"
showDiv = input.bool(true, "Mark price / ADD divergences", group = grpX, tooltip = "Triangle up: chart makes a new session low but ADD is still above its own session low by the tolerance. Triangle down: the mirror at highs.")
divTol = input.int(200, "Tolerance (ADD points)", minval = 0, group = grpX)
divWarm = input.int(15, "Skip first N minutes after the open", minval = 0, group = grpX, tooltip = "ADD swings hard in the first minutes as stocks open, which fires false divergences. Day high/low still track from the first bar.")
// ───────── Data (computed on the ADD timeframe) ─────────
tf = followTF ? timeframe.period : tfIn
addCalc() =>
v = srcMode == "HLC3" ? hlc3 : close
newDay = timeframe.isintraday and timeframe.change("D")
var int cnt = 0
var float daySum = 0.0
cnt := newDay ? 1 : cnt + 1
daySum := newDay ? v : daySum + v
sma = ta.sma(v, maLen)
ma = maReset and timeframe.isintraday and cnt < maLen ? daySum / cnt : sma
[v, ma, v - v[1]]
[addRaw, maRaw, chgRaw] = request.security(sym, tf, addCalc())
prevClose = request.security(sym, "D", close[1], lookahead = barmerge.lookahead_on)
inRTH = not na(time(timeframe.period, "0930-1600:23456", "America/New_York"))
show = not timeframe.isintraday or not rthOnly or inRTH
add = show ? addRaw : na
ma = show ? maRaw : na
// ───────── Colours & plots ─────────
dirCol = na(chgRaw) ? color.gray : chgRaw >= 0 ? color.green : color.red
colCol = add >= 0 ? (chgRaw >= 0 ? color.green : color.new(color.green, 55)) : (chgRaw <= 0 ? color.red : color.new(color.red, 55))
plot(styleMode == "Line" ? add : na, "ADD", dirCol, lineWidth, plot.style_linebr)
plot(styleMode == "Columns" ? add : na, "ADD columns", colCol, style = plot.style_columns)
plot(ma, "Average", maCol, 2, plot.style_linebr)
plot(showPrev and show ? prevClose : na, "Prior day close", color.new(color.blue, 30), 1, plot.style_linebr)
hline(0, "Zero", color.gray, hline.style_solid)
hline(lvl1, "+Level 1", color.new(color.green, 50), hline.style_dashed)
hline(-lvl1, "−Level 1", color.new(color.red, 50), hline.style_dashed)
hline(lvl2, "+Level 2", color.new(color.green, 20), hline.style_dotted)
hline(-lvl2, "−Level 2", color.new(color.red, 20), hline.style_dotted)
bgcolor(shadeTrend and add >= lvl2 ? color.new(color.green, 90) : shadeTrend and add <= -lvl2 ? color.new(color.red, 90) : na)
// ───────── Session high/low and divergence (chart context) ─────────
rthStart = inRTH and not inRTH[1]
var float dHi = na
var float dLo = na
var float pxHi = na
var float pxLo = na
var int openTime = na
bullDiv = false
bearDiv = false
if timeframe.isintraday and inRTH and not na(addRaw)
if rthStart
dHi := addRaw
dLo := addRaw
pxHi := high
pxLo := low
openTime := time
else
warmedUp = not na(openTime) and (time - openTime) >= divWarm * 60000
newPxLo = not na(pxLo) and low < pxLo
newPxHi = not na(pxHi) and high > pxHi
bullDiv := showDiv and warmedUp and newPxLo and not na(dLo) and addRaw > dLo + divTol
bearDiv := showDiv and warmedUp and newPxHi and not na(dHi) and addRaw < dHi - divTol
pxLo := na(pxLo) ? low : math.min(pxLo, low)
pxHi := na(pxHi) ? high : math.max(pxHi, high)
dLo := na(dLo) ? addRaw : math.min(dLo, addRaw)
dHi := na(dHi) ? addRaw : math.max(dHi, addRaw)
plotshape(bullDiv, "Bullish divergence", shape.triangleup, location.bottom, color.green, size = size.tiny)
plotshape(bearDiv, "Bearish divergence", shape.triangledown, location.top, color.red, size = size.tiny)
// ───────── Table ─────────
var table tb = table.new(position.top_right, 2, 4, bgcolor = color.new(color.gray, 85))
f_num(x) => na(x) ? "—" : str.tostring(x, "#")
if showTable and barstate.islast
table.cell(tb, 0, 0, "ADD", text_color = chart.fg_color, text_size = size.small)
table.cell(tb, 1, 0, f_num(addRaw), text_color = dirCol, text_size = size.small)
table.cell(tb, 0, 1, "Day high", text_color = chart.fg_color, text_size = size.small)
table.cell(tb, 1, 1, f_num(dHi), text_color = chart.fg_color, text_size = size.small)
table.cell(tb, 0, 2, "Day low", text_color = chart.fg_color, text_size = size.small)
table.cell(tb, 1, 2, f_num(dLo), text_color = chart.fg_color, text_size = size.small)
table.cell(tb, 0, 3, "Prior close", text_color = chart.fg_color, text_size = size.small)
table.cell(tb, 1, 3, f_num(prevClose), text_color = chart.fg_color, text_size = size.small)
// ───────── Alerts ─────────
alertcondition(ta.cross(add, 0), "ADD crossed zero", "NYSE ADD crossed zero")
alertcondition(ta.crossover(add, lvl1) or ta.crossunder(add, -lvl1), "ADD beyond Level 1", "NYSE ADD moved beyond ±Level 1")
alertcondition(bullDiv, "Bullish ADD divergence", "New price low, ADD holding above its session low")
alertcondition(bearDiv, "Bearish ADD divergence", "New price high, ADD holding below its session high")