ZH EP Filter 001

htflagz · study · 293 行 · 点赞 6 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
 
indicator(title="ZH EP Filter", shorttitle="ZH EP Filter", overlay=true, max_labels_count=500)
 
// =============================================================
// ZH EP FILTER
//   1. Background   - weekly MACD (6, 20, 9): green when the MACD line is above
//                     its signal; teal "early" when the daily 10 SMA is above the
//                     20 SMA and the weekly MACD is rising but not yet confirmed
//   2. SMAs         - 10 / 20 / 50 / 100 / 200, each with colour and show/hide
//   3. Trend Stopper - ⚠️ on rare high-volume liquidation candles
//   4. QQQ / BRD    - QQQ daily % change and breadth, top right
//   5. EP dots      - every EP in the ZH backlog; hover a dot for the ticker
// Built for SPY or QQQ on the daily chart.
// =============================================================
 
fastLen   = input.int(6,  "MACD Fast", minval=1, group="MACD")
slowLen   = input.int(20, "MACD Slow", minval=1, group="MACD")
signalLen = input.int(9,  "MACD Signal", minval=1, group="MACD")
macdTF    = input.timeframe("W", "MACD timeframe", group="MACD")
 
greenCol = input.color(color.new(color.rgb(0, 150, 0), 50), "Above (green)", group="Colors")
 
// Standard MACD computed on the weekly timeframe
f_macd = request.security(syminfo.tickerid, macdTF, ta.ema(close, fastLen) - ta.ema(close, slowLen), lookahead=barmerge.lookahead_off)
f_sig  = request.security(syminfo.tickerid, macdTF, ta.ema(ta.ema(close, fastLen) - ta.ema(close, slowLen), signalLen), lookahead=barmerge.lookahead_off)
// Prior WEEK's MACD line value (so "rising" is measured week-over-week, not
// per daily bar — the weekly line is flat within a week otherwise).
f_macd_prev = request.security(syminfo.tickerid, macdTF, (ta.ema(close, fastLen) - ta.ema(close, slowLen))[1], lookahead=barmerge.lookahead_off)
 
bullish = f_macd > f_sig
 
crossUp   = bullish and not bullish[1]
crossDown = not bullish and bullish[1]
 
// Secondary "early" background: 10 SMA above 20 SMA (daily) BEFORE the weekly
// MACD confirms. Once MACD turns bullish, MACD green takes over and this drops.
showEarly = input.bool(true, "Shade when 10>20 (pre-MACD)", group="Colors")
earlyCol  = input.color(color.new(color.teal, 75), "Early (10>20, MACD not yet bullish)", group="Colors")
 
earlyBull = ta.sma(close, 10) > ta.sma(close, 20) and f_macd > f_macd_prev
 
bgColor = bullish                     ? greenCol :
          (showEarly and earlyBull)   ? earlyCol :
          na
bgcolor(bgColor, title="Regime")
 
alertcondition(crossUp,   title="Weekly MACD 6/20/9 cross up",   message="Weekly MACD line crossed above signal")
alertcondition(crossDown, title="Weekly MACD 6/20/9 cross down", message="Weekly MACD line crossed below signal")
 
// ── Simple Moving Averages ───────────────────────────────────────────────────
show10  = input.bool(true, "Show SMA 10",  group="Moving Averages")
show20  = input.bool(true, "Show SMA 20",  group="Moving Averages")
show50  = input.bool(true, "Show SMA 50",  group="Moving Averages")
show100 = input.bool(true, "Show SMA 100", group="Moving Averages")
show200 = input.bool(true, "Show SMA 200", group="Moving Averages")
 
col10  = input.color(color.red,    "SMA 10 Color",  group="Moving Averages")
col20  = input.color(color.black,  "SMA 20 Color",  group="Moving Averages")
col50  = input.color(color.blue,   "SMA 50 Color",  group="Moving Averages")
col100 = input.color(color.orange, "SMA 100 Color", group="Moving Averages")
col200 = input.color(color.purple, "SMA 200 Color", group="Moving Averages")
 
sma10  = ta.sma(close, 10)
sma20  = ta.sma(close, 20)
sma50  = ta.sma(close, 50)
sma100 = ta.sma(close, 100)
sma200 = ta.sma(close, 200)
 
plot(show10  ? sma10  : na, color=col10,  linewidth=1, title="SMA 10")
plot(show20  ? sma20  : na, color=col20,  linewidth=1, title="SMA 20")
plot(show50  ? sma50  : na, color=col50,  linewidth=1, title="SMA 50")
plot(show100 ? sma100 : na, color=col100, linewidth=1, title="SMA 100")
plot(show200 ? sma200 : na, color=col200, linewidth=2, title="SMA 200")
 
// ── Kill Bar / Trend Stopper ─────────────────────────────────────────────────
grp_kill    = "Trend Stopper"
useStopper  = input.bool(true, "Detect trend-stopping candles", group=grp_kill)
requireDrop = input.bool(false, "Require the % threshold below", group=grp_kill)
dropPct     = input.float(-2.5, "Candle % change (<=)", maxval=0.0, step=0.1, group=grp_kill)
droughtRef  = input.float(-3.0, "Drought reference: decline size to count", maxval=0.0, step=0.1, group=grp_kill)
droughtMin  = input.int(40, "Min sessions since that decline", minval=0, group=grp_kill)
avgLen      = input.int(40, "Baseline length for range/volume", minval=5, group=grp_kill)
rngMult     = input.float(2.2, "Range vs baseline (>= x)", minval=1.0, step=0.1, group=grp_kill)
volMult     = input.float(1.2, "Volume vs baseline (>= x)", minval=1.0, step=0.1, group=grp_kill)
bodyMin     = input.float(80, "Body as % of range (>=)", minval=0, maxval=100, group=grp_kill)
closeLocMax = input.float(15, "Close within bottom % of range (<=)", minval=0, maxval=100, group=grp_kill)
trendMA     = input.int(50, "Prior-uptrend MA (close above)", minval=5, group=grp_kill)
useMaFilter = input.bool(true, "Also require fast MA > slow MA", group=grp_kill)
showStats   = input.bool(false, "Label with measurements (tuning aid)", group=grp_kill)
diagMode    = input.bool(false, "Diagnostic: show why a candle failed", group=grp_kill)
diagMinDrop = input.float(-1.5, "Diagnose down days below (%)", maxval=0.0, step=0.1, group=grp_kill)
stopColor   = input.color(#F23645, "Marker colour", group=grp_kill)
 
pctChg   = (close - close[1]) / close[1] * 100.0
rng      = high - low
bodyPct  = rng > 0 ? math.abs(close - open) / rng * 100.0 : 0.0
closeLoc = rng > 0 ? (close - low) / rng * 100.0 : 50.0
avgRng   = ta.sma(high - low, avgLen)[1]
avgVol   = ta.sma(volume,     avgLen)[1]
rngX     = nz(avgRng) > 0 ? rng / avgRng : 0.0
volX     = nz(avgVol) > 0 ? volume / avgVol : 0.0
 
maBull       = sma10 > sma20
isRefDown    = pctChg <= droughtRef
sessSince    = nz(ta.barssince(isRefDown[1]), bar_index)
priorUptrend = close[1] > ta.sma(close, trendMA)[1]
 
c1 = requireDrop ? pctChg <= dropPct : pctChg < 0
c2 = sessSince >= droughtMin
c3 = rngX >= rngMult
c4 = volX >= volMult
c5 = bodyPct >= bodyMin
c6 = closeLoc <= closeLocMax
c7 = priorUptrend
c8 = not useMaFilter or maBull[1]
 
allMet  = c1 and c2 and c3 and c4 and c5 and c6 and c7 and c8
stopper = useStopper and allMet and not na(avgRng) and not na(avgVol)
 
markSize = input.string("Small", "Marker size", options=["Tiny", "Small", "Normal", "Large", "Huge"], group=grp_kill)
markSz = switch markSize
    "Tiny"   => size.tiny
    "Small"  => size.small
    "Normal" => size.normal
    "Large"  => size.large
    "Huge"   => size.huge
    => size.small
 
// Drawn as a label (no box, no arrow) so the emoji's size follows the setting.
if stopper
    label.new(bar_index, high, "⚠️", yloc=yloc.price, style=label.style_label_down,
         color=color.new(stopColor, 100), textcolor=stopColor, size=markSz)
 
if stopper and showStats
    txt = "TREND STOPPER" +
          "\nchange   " + str.tostring(pctChg, "#.##") + "%" +
          "\ndrought  " + str.tostring(sessSince) + " sessions" +
          "\nrange    " + str.tostring(rngX, "#.##") + "x" +
          "\nvolume   " + str.tostring(volX, "#.##") + "x" +
          "\nbody     " + str.tostring(bodyPct, "#") + "% of range" +
          "\nclose    bottom " + str.tostring(closeLoc, "#") + "%"
    label.new(bar_index, high, txt, yloc=yloc.price, style=label.style_label_down,
         color=color.new(color.red, 15), textcolor=color.white,
         size=size.small, textalign=text.align_left)
 
f_mark(bool ok) => ok ? "PASS  " : "FAIL  "
 
if diagMode and pctChg <= diagMinDrop and not na(avgRng) and not na(avgVol)
    nFail = (c1 ? 0:1) + (c2 ? 0:1) + (c3 ? 0:1) + (c4 ? 0:1) + (c5 ? 0:1) + (c6 ? 0:1) + (c7 ? 0:1) + (c8 ? 0:1)
    dtxt = str.tostring(pctChg, "#.##") + "%   " + str.tostring(nFail) + " of 8 failed" +
           "\n" + f_mark(c1) + "down day" +
           "\n" + f_mark(c2) + "drought  " + str.tostring(sessSince) + " / " + str.tostring(droughtMin) +
           "\n" + f_mark(c3) + "range    " + str.tostring(rngX, "#.##") + "x / " + str.tostring(rngMult, "#.#") + "x" +
           "\n" + f_mark(c4) + "volume   " + str.tostring(volX, "#.##") + "x / " + str.tostring(volMult, "#.#") + "x" +
           "\n" + f_mark(c5) + "body     " + str.tostring(bodyPct, "#") + "% / " + str.tostring(bodyMin, "#") + "%" +
           "\n" + f_mark(c6) + "close    bot " + str.tostring(closeLoc, "#") + "% / " + str.tostring(closeLocMax, "#") + "%" +
           "\n" + f_mark(c7) + "above " + str.tostring(trendMA) + " MA" +
           "\n" + f_mark(c8) + "fast MA > slow MA"
    label.new(bar_index, low, dtxt, yloc=yloc.price, style=label.style_label_up,
         color=color.new(nFail == 0 ? color.green : color.gray, 20),
         textcolor=color.white, size=size.tiny, textalign=text.align_left)
 
alertcondition(stopper, "Trend Stopper", "Trend-stopping candle on {{ticker}} — {{close}}")
 
// ── Metrics: QQQ daily % and Breadth (top-right) ─────────────────────────────
grp_metrics = "Metrics"
show_metrics = input.bool(true, title="Show QQQ / Breadth", group=grp_metrics)
met_size     = input.string("Small", title="Text Size", options=["Tiny", "Small", "Normal", "Large"], group=grp_metrics)
met_bg       = input.color(color.new(color.black, 100), title="Background Color", group=grp_metrics)
met_lbl      = input.color(color.white, title="Label Color", group=grp_metrics)
met_txt      = input.color(color.orange, title="Value Fallback Color", group=grp_metrics)
 
qqqClose = request.security("QQQ", "D", close)
qqqPrev  = request.security("QQQ", "D", close[1])
qqqPct   = not na(qqqClose) and not na(qqqPrev) and qqqPrev != 0 ? (qqqClose - qqqPrev) / qqqPrev * 100 : na
 
athiClose = request.security("ATHI.US", "1", close)
atloClose = request.security("ATLO.US", "1", close)
breadth   = not na(athiClose) and not na(atloClose) and atloClose != 0 ? (athiClose - atloClose) / atloClose * 100 : na
 
met_sz = switch met_size
    "Tiny"   => size.tiny
    "Small"  => size.small
    "Normal" => size.normal
    "Large"  => size.large
    => size.small
 
col_qqq = not na(qqqPct)  ? (qqqPct  > 0 ? color.green : color.red) : met_txt
col_brd = not na(breadth) ? (breadth > 0 ? color.green : color.red) : met_txt
 
var table t_top = table.new(position.top_right, 2, 2, bgcolor=met_bg, border_width=0, frame_width=0)
 
if show_metrics and barstate.islast
    qqq_str = not na(qqqPct)  ? (qqqPct  > 0 ? "+" : "") + str.tostring(qqqPct,  "0.00") + "%" : "N/A"
    brd_str = not na(breadth) ? (breadth > 0 ? "+" : "") + str.tostring(breadth, "0.00") + "%" : "N/A"
 
    table.cell(t_top, 0, 0, "QQQ",   text_color=met_lbl, bgcolor=met_bg, text_size=met_sz, text_halign=text.align_center)
    table.cell(t_top, 1, 0, "BRD",   text_color=met_lbl, bgcolor=met_bg, text_size=met_sz, text_halign=text.align_center)
    table.cell(t_top, 0, 1, qqq_str, text_color=col_qqq, bgcolor=met_bg, text_size=met_sz, text_halign=text.align_center)
    table.cell(t_top, 1, 1, brd_str, text_color=col_brd, bgcolor=met_bg, text_size=met_sz, text_halign=text.align_center)
 
// ── EP Calendar Dots ─────────────────────────────────────────────────────────
// One dot above the candle for every EP in the ZH backlog.
// Several EPs on the same date stack vertically, lowest = first alphabetically.
// HOVER a dot to read the ticker (Pine has no click event).
// 280 EPs across 222 distinct dates, 2023-01-06 to 2026-10-30.
 
grpS     = "EP Dots"
showDots = input.bool(true, "Show EP dots", group = grpS)
dotCol   = input.color(color.white, "Dot / ticker colour", group = grpS)
dotGlyph = input.string("·  smallest", "Dot style", options = ["·  smallest","•  small","▪  square","●  filled circle (largest)"], group = grpS)
dotSize  = input.string("tiny", "Dot size", options = ["tiny","small","normal"], group = grpS)
gapATR   = input.float(0.30, "Gap between stacked dots (ATR)", minval = 0.05, step = 0.05, group = grpS)
baseATR  = input.float(0.40, "Height of first dot above the bar (ATR)", minval = 0.0, step = 0.05, group = grpS)
showTxt  = input.bool(true, "Show ticker names instead of dots", group = grpS)
fromY    = input.int(2023, "From year", minval = 2023, maxval = 2026, group = grpS)
toY      = input.int(2026, "To year",   minval = 2023, maxval = 2026, group = grpS)
 
// ---- data: parallel arrays, sorted by date ----
var int[]    epDate = array.from(
  20230106, 20230111, 20230113, 20230328, 20230410, 20230411, 20230413, 20230419, 20230503, 20230504, 20230505, 20230508,
   20230509, 20230510, 20230510, 20230515, 20230515, 20230516, 20230518, 20230524, 20230526, 20230607, 20230804, 20230804,
   20230831, 20231027, 20231102, 20231102, 20231102, 20231107, 20231108, 20231110, 20231117, 20231129, 20231212, 20231221,
   20240108, 20240108, 20240116, 20240119, 20240123, 20240125, 20240131, 20240207, 20240208, 20240212, 20240215, 20240215,
   20240220, 20240222, 20240226, 20240227, 20240301, 20240304, 20240305, 20240308, 20240312, 20240327, 20240404, 20240408,
   20240501, 20240502, 20240502, 20240508, 20240513, 20240516, 20240516, 20240529, 20240604, 20240719, 20240807, 20240821,
   20240905, 20240909, 20240909, 20240910, 20240911, 20240920, 20240923, 20240924, 20240926, 20241003, 20241008, 20241015,
   20241016, 20241024, 20241028, 20241028, 20241030, 20241105, 20241106, 20241107, 20241111, 20241113, 20241121, 20241122,
   20241125, 20241125, 20241213, 20241223, 20250103, 20250121, 20250121, 20250203, 20250205, 20250206, 20250211, 20250303,
   20250312, 20250317, 20250403, 20250403, 20250403, 20250403, 20250407, 20250423, 20250423, 20250430, 20250501, 20250505,
   20250508, 20250508, 20250508, 20250508, 20250509, 20250513, 20250513, 20250514, 20250516, 20250523, 20250602, 20250624,
   20250630, 20250701, 20250708, 20250708, 20250708, 20250711, 20250716, 20250724, 20250728, 20250801, 20250801, 20250804,
   20250805, 20250806, 20250806, 20250807, 20250807, 20250807, 20250811, 20250812, 20250814, 20250814, 20250814, 20250815,
   20250820, 20250821, 20250825, 20250828, 20250828, 20250829, 20250829, 20250902, 20250904, 20250908, 20250909, 20250911,
   20250916, 20250922, 20250923, 20250924, 20250924, 20251001, 20251006, 20251006, 20251010, 20251013, 20251017, 20251020,
   20251020, 20251029, 20251030, 20251112, 20251125, 20251125, 20251208, 20251208, 20251209, 20251210, 20251210, 20251212,
   20251219, 20251219, 20251222, 20251229, 20251231, 20260108, 20260112, 20260115, 20260120, 20260129, 20260209, 20260212,
   20260218, 20260224, 20260225, 20260227, 20260227, 20260303, 20260303, 20260305, 20260312, 20260320, 20260406, 20260408,
   20260408, 20260414, 20260415, 20260429, 20260430, 20260430, 20260430, 20260505, 20260506, 20260508, 20260511, 20260512,
   20260512, 20260514, 20260515, 20260520, 20260521, 20260521, 20260521, 20260521, 20260521, 20260527, 20260527, 20260602,
   20260608, 20260615, 20260623, 20260709, 20260714, 20260727, 20260730, 20260804, 20260806, 20260807, 20260807, 20260810,
   20260811, 20260811, 20260813, 20260814, 20260818, 20260819, 20260819, 20260819, 20260827, 20260827, 20260903, 20260909,
   20260924, 20260924, 20260928, 20260930, 20261001, 20261013, 20261020, 20261020, 20261023, 20261026, 20261027, 20261028,
   20261029, 20261029, 20261030, 20261030 )
 
var string[] epTkr = array.from(
  "AEHR", "BWEN", "GNS", "VKTX", "TGTX", "WW", "CXAI", "YARW", "SMCI", "LMND", "OPEN", "CVNA",
   "PLTR", "CELH", "LI", "AI", "UPST", "APLD", "ENVB", "ANF", "COHR", "AUR", "AAOI", "TDS",
   "CGC", "DECK", "AFRM", "LMND", "ROKU", "DDOG", "PRCH", "DRCT", "GAP", "VVOS", "CCCC", "CYTK",
   "ARDX", "NVDA", "DJT", "SMCI", "RVSN", "CRBP", "POWL", "HOLO", "ARM", "BMR", "APP", "JMIA",
   "IOVA", "ROOT", "KRYS", "CEG", "CRDF", "SG", "AISP", "APPS", "SIGA", "OUST", "CADL", "WAVE",
   "TMDX", "ASPN", "EBS", "INOD", "NVAX", "ASTS", "IREN", "BNAI", "CORZ", "SERV", "UPST", "GDC",
   "NIO", "SMMT", "TIL", "VRDN", "PLCE", "OKLO", "UXIN", "WVE", "TIGR", "EVGO", "WW", "DRUG",
   "SMR", "CLS", "GLUE", "PRCH", "RDDT", "PLTR", "COIN", "QBTS", "BITX", "QUBT", "CRNC", "QMCO",
   "KULR", "ZENA", "WKEY", "RUM", "CRNC", "TEM", "VATE", "IVVD", "ACB", "APPS", "AIFF", "TGTX",
   "GRPN", "NIU", "SOXS", "SQQQ", "UVIX", "UVXY", "DFDV", "PONY", "XXI", "ODD", "KIDZ", "GPUS",
   "DAVE", "LEU", "QBTS", "SEZL", "OUST", "MVST", "SMR", "OKLO", "QUBT", "UROY", "APLD", "NKTR",
   "BMNR", "ATAI", "BTCS", "PROK", "ZEPP", "INKT", "RGTI", "BE", "VWAV", "ATEC", "RDDT", "VSAT",
   "XMTR", "RIGL", "SSRM", "APP", "CDE", "MIRM", "TLS", "FRMM", "EQX", "GAU", "WULF", "PGEN",
   "CNTN", "NIO", "RR", "P", "SOGP", "BABA", "IPDN", "IONS", "SNDK", "PL", "FCEL", "MAZE",
   "PLUG", "BKKT", "FLNA", "LAC", "QURE", "ASTS", "ONMD", "SPRB", "GWH", "STI", "LBRT", "BYND",
   "GSIT", "CMBMF", "INTS", "CYPH", "ANF", "KSS", "GPCR", "WVE", "DBI", "BEAT", "VELO", "CGC",
   "LUNR", "SIDU", "HYMC", "BNAI", "INBS", "ANPA", "BDSX", "SPHL", "CRVS", "LUV", "VAL", "FSLY",
   "RXT", "IOVA", "CRCL", "AAOI", "DELL", "MOBX", "TURB", "SOC", "LWLG", "ANNA", "AIXI", "AEHR",
   "BBGI", "IONQ", "BIRD", "SIMO", "BAND", "LLY", "MRAM", "DGXX", "OSS", "RKLB", "LQDA", "AMBQ",
   "TDIC", "SIDU", "PIII", "GH", "GH", "IBM", "QBTS", "QUBT", "RGTI", "ASTC", "RKTO", "LASE",
   "SUNE", "GPUS", "BLZE", "FBRX", "LEDS", "DFNS", "CYCU", "PLTR", "CAI", "BTG", "HALO", "ABCL",
   "NIQ", "QMCO", "AVAH", "HTFL", "AMLX", "ARCT", "MRNA", "TEM", "CELU", "OKTA", "CHPT", "SIG",
   "GLND", "P", "KOD", "TGE", "AISP", "TCRT", "HAS", "SAIQ", "AIOK", "RRC", "KNSA", "BOOM",
   "GKOS", "PRCH", "GDYN", "REGN" )
 
dotATR = ta.atr(14)
 
// bar date as yyyymmdd
barDate = year * 10000 + month * 100 + dayofmonth
 
if showDots and year >= fromY and year <= toY
    stack = 0
    for i = 0 to array.size(epDate) - 1
        if array.get(epDate, i) == barDate
            tkr   = array.get(epTkr, i)
            glyph = str.substring(dotGlyph, 0, 1)
            y     = high + dotATR * (baseATR + gapATR * stack)
            label.new(bar_index, y,
              text      = showTxt ? tkr : glyph,
              tooltip   = tkr,
              style     = label.style_none,
              color     = color.new(color.black, 100),
              textcolor = dotCol,
              size      = dotSize == "tiny" ? size.tiny : dotSize == "small" ? size.small : size.normal,
              yloc      = yloc.price)
            stack += 1

← 返回列表