STRYK: EMA CLOUD

Es-Sniper · study · 433 行 · 点赞 24 · TradingView 原页

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

Pine Script

// =============================================================================
// STRYK: EMA CLOUD v1.4 (STRYK:EMC) — momentum cloud from STRYK: NOISE BANDS, ARP palettes, cloud signals + gates
// v1.0: fast / slow EMA cloud (22 / 55) whose colour brightens as the lines spread apart (gap in ATR) and turns
//       neutral when they tangle · the 14 ARP palettes (STRYK, 9 professional contrast sets with automatic light-chart
//       shades, 4 fun gradients) · CLOUD ▲ / ▼ significant flips (NB rule: cross, then the gap opens past N × ATR
//       within N bars and widens 2 bars in a row, both EMAs sloping the new way) · PULLBACK ▲ / ▼ (a dip into the
//       cloud in a strong cloud, then a close back through the fast EMA) · shared gates: volume, strong close,
//       higher-timeframe cloud, cooldown · 6 alert conditions + one dynamic alert. Non-repainting: signals on
//       confirmed candles; the HTF cloud is the previous COMPLETED higher-timeframe value ([1] + lookahead on).
// v1.0.1 (owner): MASTER SWITCH holds every on / off and the design — cloud, lines, labels, palette, light-chart
//       shades, line colour mode, both signal types, all three gates and the dynamic alert; the sections below hold
//       only their settings. A tooltip on every single input.
// v1.1 (GPT review applied, PATCH IDs): SG001 pullback setup expires after N bars · SG002 max pullback depth past the
//       fast EMA in ATR · SG003 max pullbacks per trend leg (count resets at each EMA cross) · SG004 optional same-candle
//       reclaim (off: the reclaim must come on a later candle) · SG005 explicit cancel (cloud turns / weakens, close
//       through the slow EMA, too deep, too old) · SG006 / VS001 / AL001 invalidation reference, setup age and depth in
//       label tooltips and the dynamic alert (flip: slow EMA at the signal · pullback: the setup's extreme) · GT001
//       efficiency-ratio gate · GT002 cloud-expansion gate · GT003 ATR over-extension gate · GT004 the HTF gate only
//       counts a timeframe above the chart's (otherwise ignored, with a warning tag) · GT005 Chicago session gate.
//       New gates are OFF by default — test each one alone first (GPT's advice). Slope gate moved to the master switch.
// v1.2 (GPT build brief): CP003 cross state (xBar / xDir / leg reset) updates on CONFIRMED candles only · SG007 an
//       opposite CLOUD flip or an opposite armed setup cancels a pullback · SG008 pullback depth measured past the
//       SELECTED touch line (consistent with 'dip must reach') · IN002 master switch 'Pullback lifecycle limits' (ON;
//       OFF = v1.0 behaviour: no age / depth / per-leg limits) · VS002 hover text and alert add the touch line, the
//       Chicago time and 'structural reference, not a guaranteed fill'. Trend leg = from one confirmed EMA cross to the
//       next; the per-leg count restarts there.
// v1.2.1 (GPT patch-response review): SG009 a trend leg now starts only when a cross ESTABLISHES (the NOISE BANDS
//       flip test — gap ≥ gapK × ATR within flWin, widening, slopes — run whether or not flips or gates are on), so
//       repeated crosses in chop can no longer reset the per-leg pullback count · SG010 pullback depth = the DEEPEST
//       reading during the setup (each candle: touch line − low, in that candle's ATR), so a fast EMA drifting toward an
//       old low cannot make the setup look shallower; the extreme and depth freeze at the signal.
// v1.3 (owner): VS003 label position (buys below / sells above · all above · all below) · VS004 gap between the candle
//       and the label in ATR · VS005 label sizes Micro → Large in points (Micro 6 · Tiny 8 · Small 10 · Normal 12 · Large 16).
// v1.4 (owner): 233 EMA CANDLE ENGINE — the only candle engine: candles in the palette's bullish colour while the close
//       is above the 233 EMA, bearish below (a close exactly on it keeps the last colour). The 233 EMA itself is never
//       drawn. Master switch: on / off + full candles (body, wick, border); length and source in section H.
// =============================================================================
//@version=6
indicator("STRYK: EMA CLOUD", shorttitle = "STRYK:EMC", overlay = true, max_labels_count = 500)
 
// ─────────────────────────────── A · Master switch (every on / off + the design) ───────────────────────────────
GA = "A · Master switch"
shCl   = input.bool(true, "Cloud", group = GA,
     tooltip = "Shows the coloured fill between the fast and slow EMA. Off hides only the fill — the signals and alerts keep working.")
shLn   = input.bool(true, "EMA lines", group = GA,
     tooltip = "Shows the fast and slow EMA lines. Width, transparency and style are in section D.")
shSg   = input.bool(true, "Signal labels", group = GA,
     tooltip = "Shows the CLOUD and PULLBACK labels on the chart. Off hides the labels only — the alerts still fire.")
pal    = input.string("Neon cyan / magenta", "Palette", options = ["STRYK green / red", "Pro green / red", "Okabe-Ito (colour-blind safe)", "Colour-blind blue / orange", "TradingView teal / red", "Neon cyan / magenta", "Lime / crimson", "Ocean aqua / coral", "Royal amber / violet", "High contrast yellow / blue", "Fun · Fire & ice (gradient)", "Fun · Synthwave (gradient)", "Fun · Aurora (gradient)", "Fun · Rainbow heat (gradient)"], group = GA,
     tooltip = "The ARP palettes, used by the cloud, the lines and the signals. STRYK: your own up / down / neutral colours (section C). PROFESSIONAL (9): contrast-tested sets for dark and light charts; Okabe-Ito, blue / orange, teal, aqua and yellow / blue are colour-blind safe. FUN (4): the cloud runs through a three-colour gradient as momentum builds. Default Neon cyan / magenta (the owner's ARP default).")
autoTh = input.bool(true, "Darker shades on a light chart", group = GA,
     tooltip = "ON (default): on a light chart background the professional palettes switch to their darker shades automatically so the cloud keeps its contrast. OFF: always the dark-chart shades.")
lnMode = input.string("Cloud colour", "Line colour", options = ["Cloud colour", "Custom"], group = GA,
     tooltip = "CLOUD COLOUR (default): the EMA lines take the cloud's live colour. CUSTOM: the fixed fast / slow colours in section D.")
flOn   = input.bool(true, "CLOUD flip signals", group = GA,
     tooltip = "Significant cloud flips — CLOUD ▲ / ▼. The rule and its settings are in section E.")
pbOn   = input.bool(true, "PULLBACK signals", group = GA,
     tooltip = "Trend pullbacks inside a strong cloud — PB ▲ / ▼. The rule and its settings are in section E.")
vgOn   = input.bool(false, "Volume gate", group = GA,
     tooltip = "Every signal needs above-average volume on its candle (settings in section F). Off by default.")
bdOn   = input.bool(false, "Strong-close gate", group = GA,
     tooltip = "Every signal candle must close near its extreme in the signal's direction (settings in section F). Off by default.")
htOn   = input.bool(false, "Higher-timeframe cloud gate", group = GA,
     tooltip = "Every signal needs the same cloud on a higher timeframe pointing its way (timeframe in section F). Off by default.")
flSlp  = input.bool(true, "Flip gate: both EMAs slope the new way", group = GA,
     tooltip = "CLOUD flips only: the fast AND the slow EMA must both be rising (▲) or falling (▼) on the signal candle, so the whole cloud is turning, not just the fast line. On by default (the NOISE BANDS rule).")
pbLife = input.bool(true, "Pullback lifecycle limits", group = GA,
     tooltip = "ON (default): a pullback setup expires after N candles, is cancelled when the dip goes too deep, and only N pullbacks count per trend leg (settings in section E). OFF: the v1.0 behaviour — a setup stays armed until it reclaims or the cloud breaks.")
pbSame = input.bool(false, "Pullback: allow same-candle reclaim", group = GA,
     tooltip = "OFF (default, GPT review SG004): the candle that dips into the cloud arms the setup and a LATER candle must close back through the fast EMA — a more selective entry. ON: one candle can both dip and reclaim (catches V-shaped dips, more signals).")
erOn   = input.bool(false, "Efficiency-ratio gate", group = GA,
     tooltip = "Every signal needs directional price action: net move ÷ total path over the last N bars at or above the minimum (section F). Filters back-and-forth chop. Off by default — test it on its own first.")
exOn   = input.bool(false, "Cloud-expansion gate", group = GA,
     tooltip = "Every signal needs the EMA gap wider in the signal's direction than N bars ago (section F) — no entries while the cloud is still contracting. Off by default — test it on its own first.")
extOn  = input.bool(false, "Over-extension gate", group = GA,
     tooltip = "Blocks a signal when the close is already more than N × ATR beyond the fast EMA (section F) — avoids late entries after the move has run. Off by default — test it on its own first.")
sesOn  = input.bool(false, "Session gate (Chicago time)", group = GA,
     tooltip = "Signals only inside the session window in section F (default the 08:30–15:00 CT cash session). Off by default — compare overnight vs cash-session signals before switching it on.")
ccOn   = input.bool(true, "Candle colours: 233 EMA engine", group = GA,
     tooltip = "Colours every candle by where it closes against the 233 EMA: the palette's BULLISH colour above it, BEARISH below. Only the bull and bear colours are used (no neutral). The 233 EMA line is not drawn. Length and source in section H. Off = the chart's own candle colours.")
ccFull = input.bool(true, "Colour full candles (body + wick + border)", group = GA,
     tooltip = "ON (default): the whole candle — body, wick and border — takes the engine colour (a coloured candle drawn exactly over the chart's own; uses 7 of the 64 plot slots). OFF: only the candle body is coloured.")
alOn   = input.bool(true, "Dynamic alert message", group = GA,
     tooltip = "ON: one alert() message per candle close with what fired (flip / pullback, side, price, cloud strength). Create the alert with 'Any alert() function call'. The six fixed alert conditions are always available.")
 
// ─────────────────────────────── B · Cloud ───────────────────────────────
GB = "B · Cloud"
src    = input.source(close, "Source", group = GB,
     tooltip = "Price the two EMAs are calculated from. Close (default) is what the NOISE BANDS cloud uses.")
lenF   = input.int(22, "Fast EMA", minval = 2, maxval = 400, group = GB, inline = "b1",
     tooltip = "Length of the fast EMA. 22 (default) is the NOISE BANDS momentum cloud. Keep it below the slow length.")
lenS   = input.int(55, "slow EMA", minval = 3, maxval = 500, group = GB, inline = "b1",
     tooltip = "Length of the slow EMA. 55 (default) is the NOISE BANDS momentum cloud — the trend gate in the 365-day NBS strategy test.")
atrLen = input.int(14, "ATR length", minval = 1, maxval = 200, group = GB,
     tooltip = "The gap between the EMAs is measured in ATR of this length, so cloud strength means the same on any market and timeframe. 14 (default) matches NOISE BANDS.")
fullK  = input.float(1.0, "Full strength at gap × ATR", minval = 0.1, maxval = 5.0, step = 0.05, group = GB, inline = "b2",
     tooltip = "The EMA gap (in ATR) at which the cloud reaches its brightest colour and 100 % strength. 1.0 (default, as in NOISE BANDS).")
flatK  = input.float(0.05, "flat below × ATR", minval = 0.0, maxval = 1.0, step = 0.01, group = GB, inline = "b2",
     tooltip = "While the gap is smaller than this, the EMAs are tangled: the cloud takes the palette's neutral colour and no pullback can arm. 0.05 ATR (default); 0 = never neutral.")
cfT    = input.int(65, "Cloud shade at full strength", minval = 0, maxval = 100, group = GB, inline = "b3",
     tooltip = "Transparency of the fill at full strength (0 solid, 100 invisible). 65 (default) reads clearly without hiding candles.")
cfFade = input.int(60, "weak-cloud fade %", minval = 0, maxval = 100, group = GB, inline = "b3",
     tooltip = "How much more see-through a weak cloud gets (0 = same shade at every strength, 100 = a barely-open cloud is almost invisible). 60 (default) is the NOISE BANDS look.")
 
// ─────────────────────────────── C · STRYK palette colours ───────────────────────────────
GC = "C · STRYK palette colours"
cUp    = input.color(#00e5a0, "Up", group = GC, inline = "c1",
     tooltip = "Cloud colour when the fast EMA is above the slow (STRYK palette only).")
cDn    = input.color(#ff3b6b, "Down", group = GC, inline = "c1",
     tooltip = "Cloud colour when the fast EMA is below the slow (STRYK palette only).")
cNeu   = input.color(#9aa4b2, "Neutral", group = GC, inline = "c1",
     tooltip = "Cloud colour while the EMAs are tangled (STRYK palette only).")
weakT  = input.int(55, "Weak-momentum transparency", minval = 0, maxval = 95, group = GC,
     tooltip = "Single-colour palettes run from this transparency of their colour (weak momentum) to the full colour (full strength). 55 (default) keeps a weak cloud visible but quiet. Fun palettes use their own gradient.")
 
// ─────────────────────────────── D · EMA lines ───────────────────────────────
GD = "D · EMA lines"
cLF    = input.color(#d1d4dc, "Custom fast colour", group = GD, inline = "d1",
     tooltip = "Fast-line colour when Line colour (master switch) is Custom.")
cLS    = input.color(#787b86, "slow colour", group = GD, inline = "d1",
     tooltip = "Slow-line colour when Line colour (master switch) is Custom.")
wF     = input.int(1, "Fast width", minval = 1, maxval = 6, group = GD, inline = "d2",
     tooltip = "Width of the fast EMA line.")
trF    = input.int(15, "transp.", minval = 0, maxval = 100, group = GD, inline = "d2",
     tooltip = "Transparency of the fast EMA line (0 solid).")
wS     = input.int(1, "Slow width", minval = 1, maxval = 6, group = GD, inline = "d3",
     tooltip = "Width of the slow EMA line.")
trS    = input.int(45, "transp.", minval = 0, maxval = 100, group = GD, inline = "d3",
     tooltip = "Transparency of the slow EMA line (0 solid). A lighter slow line keeps the fast one in front.")
lnSt   = input.string("Solid", "Line style", options = ["Solid", "Dashed", "Dotted"], group = GD,
     tooltip = "Style of both EMA lines.")
 
// ─────────────────────────────── E · Signals ───────────────────────────────
GE = "E · Signals"
gapK   = input.float(0.25, "Flip: gap ≥ × ATR", minval = 0.05, maxval = 3.0, step = 0.05, group = GE, inline = "e1",
     tooltip = "CLOUD FLIP (the NOISE BANDS rule): after the cross, the gap must open to at least this many ATR in the new direction. 0.25 (default, as in NOISE BANDS).")
flWin  = input.int(6, "within bars", minval = 1, maxval = 50, group = GE, inline = "e1",
     tooltip = "…within this many candles of the cross, while widening 2 candles in a row. A cross that tangles and crosses back never fires. 6 (default, as in NOISE BANDS). One signal per flip.")
pbTch  = input.string("Fast EMA", "Pullback: dip must reach", options = ["Fast EMA", "Cloud middle", "Slow EMA"], group = GE,
     tooltip = "TREND PULLBACK: in an up cloud, price dips to this line (the low touches it), then a candle CLOSES back above the fast EMA as an up candle → PULLBACK ▲ (mirror for ▼). A close through the slow EMA cancels the setup. Fast EMA (default) catches shallow dips in strong trends; Slow EMA waits for deeper ones.")
pbMinS = input.float(0.35, "Pullback: min cloud strength", minval = 0.0, maxval = 1.0, step = 0.05, group = GE,
     tooltip = "A pullback only counts while the cloud is at least this strong (0 = any open cloud, 1 = the gap is at full strength). 0.35 (default) keeps pullback signals out of a flat, tangled cloud, where EMA dips are just chop.")
pbAge  = input.int(8, "Pullback: setup expires after (bars)", minval = 0, maxval = 200, group = GE,
     tooltip = "A pullback setup that has not reclaimed the fast EMA within this many candles of its first touch is cancelled (0 = never expires). 8 (default, GPT's MES 5-minute starting point) = 40 minutes.")
pbDep  = input.float(1.25, "Pullback: max depth past the touch line × ATR", minval = 0.0, maxval = 10.0, step = 0.05, group = GE,
     tooltip = "The setup is cancelled if the dip reaches deeper than this many ATR past the touch line chosen in 'dip must reach' — a retest that deep has damaged the trend (0 = no limit). 1.25 ATR (default, GPT's starting point).")
pbLeg  = input.int(2, "Pullback: max per trend leg", minval = 0, maxval = 20, group = GE,
     tooltip = "At most this many pullback signals per trend leg. A new leg starts only when a cross ESTABLISHES (the gap opens past the flip threshold within the flip window), so crosses that tangle and cross back in chop do not reset the count (0 = unlimited). 2 (default): the first pullbacks of a trend are the cleanest, later ones come in an aging move. Set 1 for first-pullback-only.")
cool   = input.int(10, "Cooldown (bars) per side", minval = 0, maxval = 200, group = GE,
     tooltip = "Minimum candles between two signals on the same side (▲ or ▼), flips and pullbacks together. 10 (default) = 50 minutes on a 5-minute chart.")
 
// ─────────────────────────────── F · Gate settings ───────────────────────────────
GF = "F · Gate settings"
volK   = input.float(1.2, "Volume ≥ × average", minval = 0.5, maxval = 5.0, step = 0.1, group = GF, inline = "f1",
     tooltip = "Volume gate: the signal candle's volume must be at least this multiple of the average volume. 1.2 (default).")
volLen = input.int(20, "of bars", minval = 2, maxval = 200, group = GF, inline = "f1",
     tooltip = "Volume gate: the average is taken over this many previous COMPLETED candles (the signal candle is not in it). 20 (default).")
bdPct  = input.int(70, "Strong close in the outer %", minval = 50, maxval = 100, group = GF,
     tooltip = "Strong-close gate: the candle closes in the outer part of its range in the signal's direction (70 % = top 30 % for ▲, bottom 30 % for ▼). 70 (default).")
erLen  = input.int(20, "Efficiency ratio: bars", minval = 2, maxval = 300, group = GF, inline = "f2",
     tooltip = "Efficiency-ratio gate: look-back in candles. 20 (default, GPT's 5-minute starting point; about 100 on 1 m, 7 on 15 m).")
erMin  = input.float(0.25, "min ratio", minval = 0.0, maxval = 1.0, step = 0.05, group = GF, inline = "f2",
     tooltip = "Efficiency-ratio gate: minimum net move ÷ total path (0 = pure chop, 1 = a straight line). 0.25 (default, starting point).")
exLb   = input.int(3, "Expansion: gap wider than (bars ago)", minval = 1, maxval = 50, group = GF,
     tooltip = "Cloud-expansion gate: the EMA gap must be wider, in the signal's direction, than it was this many candles ago. 3 (default, starting point).")
extK   = input.float(1.5, "Over-extension: max close beyond fast EMA × ATR", minval = 0.1, maxval = 10.0, step = 0.1, group = GF,
     tooltip = "Over-extension gate: the signal close may be at most this many ATR beyond the fast EMA. 1.5 (default, starting point).")
sess   = input.session("0830-1500", "Session window (CT)", group = GF,
     tooltip = "Session gate: signals only between these Chicago times. 08:30–15:00 (default) is the US cash session.")
htTf   = input.timeframe("60", "Higher timeframe", group = GF,
     tooltip = "Higher-timeframe cloud gate: the same cloud (same lengths) on this timeframe, read from its previous COMPLETED candle so it never repaints. Choose a timeframe above the chart's. 60 minutes (default).")
 
// ─────────────────────────────── G · Signal style ───────────────────────────────
GG = "G · Signal style"
lbSt   = input.string("Label with text", "Look", options = ["Label with text", "Arrow only"], group = GG, inline = "g1",
     tooltip = "LABEL WITH TEXT: 'CLOUD ▲' / 'PB ▲' tags under / over the candle (hover for the full read). ARROW ONLY: just ▲ / ▼.")
lbSz   = input.string("Small", "size", options = ["Micro", "Tiny", "Small", "Normal", "Large"], group = GG, inline = "g1",
     tooltip = "Size of the signal labels: Micro (6 pt, smaller than TradingView's Tiny), Tiny (8), Small (10, default), Normal (12), Large (16).")
lbPos  = input.string("Buys below · sells above", "Label position", options = ["Buys below · sells above", "All above candles", "All below candles"], group = GG,
     tooltip = "Where the labels sit. BUYS BELOW · SELLS ABOVE (default): ▲ labels under the candle, ▼ labels over it. ALL ABOVE: every label over its candle. ALL BELOW: every label under its candle. The label's pointer always faces the candle.")
lbGap  = input.float(0.15, "Gap from the candle × ATR", minval = 0.0, maxval = 5.0, step = 0.05, group = GG,
     tooltip = "Space between the candle's wick and the label, in ATR so it looks the same on any market and timeframe. 0 = the label touches the wick; 0.15 (default) leaves a small gap; raise it to lift labels clear of busy candles.")
sgMode = input.string("Palette", "Signal colours", options = ["Palette", "Custom"], group = GG,
     tooltip = "PALETTE (default): signals use the palette's full up / down colours. CUSTOM: the two colours below.")
cSU    = input.color(#00e5a0, "Custom ▲", group = GG, inline = "g2",
     tooltip = "Buy-side label colour when Signal colours is Custom.")
cSD    = input.color(#ff3b6b, "Custom ▼", group = GG, inline = "g2",
     tooltip = "Sell-side label colour when Signal colours is Custom.")
pbDim  = input.int(35, "Pullback label transparency", minval = 0, maxval = 90, group = GG,
     tooltip = "Pullback labels are drawn lighter than flips so the bigger event stands out. 0 = same strength as flips.")
 
// ─────────────────────────────── H · 233 EMA candle engine ───────────────────────────────
GH = "H · 233 EMA candle engine"
ccLen  = input.int(233, "EMA length", minval = 2, maxval = 1000, group = GH,
     tooltip = "Length of the EMA the candles are coloured against. 233 (default, a Fibonacci length) is the slow trend line the owner trades around: above it = bullish colour, below = bearish. The line itself is never drawn.")
ccSrc  = input.source(close, "EMA source", group = GH,
     tooltip = "Price the 233 EMA is calculated from. Close (default). The candle is always judged by its CLOSE against the EMA.")
 
// ─────────────────────────────── palettes (from STRYK:ARP) ───────────────────────────────
bool LBG = autoTh and (color.r(chart.bg_color) * 0.299 + color.g(chart.bg_color) * 0.587 + color.b(chart.bg_color) * 0.114) > 140
bool FUN = str.startswith(pal, "Fun")
f_pal(string pn, bool lbg) =>
    var array<string> PALN = array.from("Pro green / red", "Okabe-Ito (colour-blind safe)", "Colour-blind blue / orange", "TradingView teal / red", "Neon cyan / magenta", "Lime / crimson", "Ocean aqua / coral", "Royal amber / violet", "High contrast yellow / blue")
    var array<string> FUNN = array.from("Fun · Fire & ice (gradient)", "Fun · Synthwave (gradient)", "Fun · Aurora (gradient)", "Fun · Rainbow heat (gradient)")
    var array<color> PDU = array.from(#22c55e, #56b4e9, #3d8bfd, #26a69a, #00e5ff, #76ff03, #1de9b6, #ffb300, #ffea00)
    var array<color> PDD = array.from(#f43f5e, #e69f00, #ff9f1c, #ef5350, #ff2bd6, #ff1744, #ff7043, #a77bff, #4f8cff)
    var array<color> PDM = array.from(#60a5fa, #bdbdbd, #b0bec5, #5b8def, #ffd600, #448aff, #9fa8da, #90a4ae, #cfd8dc)
    var array<color> PLU = array.from(#15803d, #0072b2, #1d4ed8, #00796b, #00838f, #2e7d32, #00796b, #b45309, #a16207)
    var array<color> PLD = array.from(#be123c, #d55e00, #c2410c, #c62828, #c2185b, #c62828, #d84315, #6d28d9, #1d4ed8)
    var array<color> PLM = array.from(#1d4ed8, #616161, #546e7a, #1e40af, #a16207, #1d4ed8, #3949ab, #546e7a, #37474f)
    var array<color> FU1 = array.from(#0091ff, #00f0ff, #00ff87, #00c853)
    var array<color> FU2 = array.from(#00e5ff, #7a5cff, #00e0ff, #ffd600)
    var array<color> FU3 = array.from(#e0fbff, #ff00e6, #8ffcff, #ff6d00)
    var array<color> FD1 = array.from(#ff8a00, #ffcc00, #ff00a0, #2962ff)
    var array<color> FD2 = array.from(#ff3d00, #ff6b00, #c400ff, #aa00ff)
    var array<color> FD3 = array.from(#ff0050, #ff2e63, #7a00ff, #ff1744)
    var array<color> FMD = array.from(#8a94a6, #9aa0c8, #a0a4ad, #b0bec5)
    int pIx = PALN.indexof(pn)
    int fIx = FUNN.indexof(pn)
    int pI  = math.max(pIx, 0)
    int fI  = math.max(fIx, 0)
    color u  = fIx >= 0 ? FU3.get(fI) : pIx < 0 ? cUp : lbg ? PLU.get(pI) : PDU.get(pI)
    color d  = fIx >= 0 ? FD3.get(fI) : pIx < 0 ? cDn : lbg ? PLD.get(pI) : PDD.get(pI)
    color md = fIx >= 0 ? FMD.get(fI) : pIx < 0 ? cNeu : lbg ? PLM.get(pI) : PDM.get(pI)
    [FU1.get(fI), FU2.get(fI), FU3.get(fI), FD1.get(fI), FD2.get(fI), FD3.get(fI), u, d, md]
[fu1, fu2, fu3, fd1, fd2, fd3, kUp, kDn, kMd] = f_pal(pal, LBG)
f_g3(float t, color a, color b, color c) =>
    t <= 0.5 ? color.from_gradient(t, 0, 0.5, a, b) : color.from_gradient(t, 0.5, 1, b, c)
// label sizes in points (v6 accepts an int): Micro is smaller than TradingView's Tiny
f_sz(string s) =>
    s == "Micro" ? 6 : s == "Tiny" ? 8 : s == "Normal" ? 12 : s == "Large" ? 16 : 10
 
// ─────────────────────────────── cloud ───────────────────────────────
float eF  = ta.ema(src, lenF)
float eS  = ta.ema(src, lenS)
float atr = ta.atr(atrLen)
float gap = atr > 0 ? (eF - eS) / atr : 0.0
int   cDir = gap > flatK ? 1 : gap < -flatK ? -1 : 0
float stg = math.max(0.0, math.min(1.0, math.abs(gap) / fullK))
color upC = FUN ? f_g3(stg, fu1, fu2, fu3) : color.from_gradient(stg, 0, 1, color.new(kUp, weakT), kUp)
color dnC = FUN ? f_g3(stg, fd1, fd2, fd3) : color.from_gradient(stg, 0, 1, color.new(kDn, weakT), kDn)
color clC = cDir == 1 ? upC : cDir == -1 ? dnC : kMd
int   clT = int(math.round(cfT + (100 - cfT) * (1 - stg) * cfFade / 100))
// line style is built at global scope from the input (plot linestyle takes an input-qualified value)
lsE = lnSt == "Dashed" ? plot.linestyle_dashed : lnSt == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid
bool  lnCl = lnMode == "Cloud colour"
pF = plot(shCl or shLn ? eF : na, "Fast EMA", shLn ? color.new(lnCl ? clC : cLF, trF) : na, wF, linestyle = lsE)
pS = plot(shCl or shLn ? eS : na, "Slow EMA", shLn ? color.new(lnCl ? clC : cLS, trS) : na, wS, linestyle = lsE)
fill(pF, pS, shCl ? color.new(clC, clT) : na, "EMA cloud")
 
// ─────────────────────────────── 233 EMA candle engine (no line drawn) ───────────────────────────────
float e233 = ta.ema(ccSrc, ccLen)
var int ccSide = 0
ccSide := close > e233 ? 1 : close < e233 ? -1 : ccSide
color ccCol = ccOn and ccSide != 0 ? (ccSide == 1 ? kUp : kDn) : na
barcolor(ccCol, title = "233 EMA candles")
plotcandle(ccOn and ccFull and not na(ccCol) ? open : na, high, low, close, "233 EMA full candles", color = ccCol, wickcolor = ccCol, bordercolor = ccCol, display = display.pane)
 
// ─────────────────────────────── gates ───────────────────────────────
bool  cfm  = barstate.isconfirmed
float g0   = eF - eS
float vAvg = ta.sma(volume, volLen)[1]
bool  vOk  = not vgOn or (not na(vAvg) and vAvg > 0 and volume >= volK * vAvg)
float rng  = high - low
bool  bU   = not bdOn or (rng > 0 and (close - low) / rng >= bdPct / 100.0)
bool  bD   = not bdOn or (rng > 0 and (high - close) / rng >= bdPct / 100.0)
// HTF cloud: previous COMPLETED higher-timeframe candle ([1] + lookahead on = the same on history and live)
[hF, hS] = request.security(syminfo.tickerid, htTf, [ta.ema(src, lenF)[1], ta.ema(src, lenS)[1]], lookahead = barmerge.lookahead_on)
// GT004: only a timeframe ABOVE the chart's counts; otherwise the gate is ignored (warning tag on the last candle)
bool  htOk = timeframe.in_seconds(htTf) > timeframe.in_seconds()
bool  htUse = htOn and htOk
bool  hU   = not htUse or (not na(hF) and hF > hS)
bool  hD   = not htUse or (not na(hF) and hF < hS)
// GT001: efficiency ratio = net move ÷ total path over erLen bars
float erNet = math.abs(src - src[erLen])
float erSum = math.sum(math.abs(src - src[1]), erLen)
float er    = erSum > 0 ? erNet / erSum : 0.0
bool  erOk  = not erOn or er >= erMin
// GT002: cloud expanding in the signal's direction · GT003: close not over-extended past the fast EMA · GT005: session
bool  xpU  = not exOn or g0 > nz(g0[exLb])
bool  xpD  = not exOn or g0 < nz(g0[exLb])
bool  oxU  = not extOn or atr <= 0 or close - eF <= extK * atr
bool  oxD  = not extOn or atr <= 0 or eF - close <= extK * atr
bool  sesOk = not sesOn or not na(time(timeframe.period, sess, "America/Chicago"))
var int lastU = na
var int lastD = na
bool  cU   = na(lastU) or bar_index - lastU >= cool
bool  cD   = na(lastD) or bar_index - lastD >= cool
bool  gU   = vOk and bU and hU and cU and erOk and xpU and oxU and sesOk
bool  gD   = vOk and bD and hD and cD and erOk and xpD and oxD and sesOk
 
// ─────────────────────────────── CLOUD flips (NOISE BANDS rule) ───────────────────────────────
var int  xBar = na
var int  xDir = 0
var bool fDone = true
bool xUp = ta.crossover(eF, eS)
bool xDn = ta.crossunder(eF, eS)
// CP003: cross state changes only on confirmed candles (a live cross that undoes itself never arms a flip)
var bool eDone = true
if cfm and (xUp or xDn)
    xBar := bar_index
    xDir := xUp ? 1 : -1
    fDone := false
    eDone := false
bool  widen = xDir * g0 > xDir * nz(g0[1]) and xDir * nz(g0[1]) > xDir * nz(g0[2])
bool  slOk  = not flSlp or (xDir == 1 ? eF > eF[1] and eS > eS[1] : eF < eF[1] and eS < eS[1])
// SG009: a cross ESTABLISHES a new trend leg when it passes the flip test (independent of the flip switch and gates)
bool estU = false
bool estD = false
if cfm and not eDone and not na(xBar) and atr > 0
    if bar_index - xBar > flWin
        eDone := true
    else if xDir * g0 >= gapK * atr and widen and slOk
        estU := xDir == 1
        estD := xDir == -1
        eDone := true
bool evFU = false
bool evFD = false
if flOn and cfm and not fDone and not na(xBar) and atr > 0
    if bar_index - xBar > flWin
        fDone := true
    else if xDir * g0 >= gapK * atr and widen and slOk and (xDir == 1 ? gU : gD)
        evFU := xDir == 1
        evFD := xDir == -1
        fDone := true
 
// ─────────────────────────────── PULLBACKS (SG001–SG005 lifecycle) ───────────────────────────────
float tch = pbTch == "Fast EMA" ? eF : pbTch == "Slow EMA" ? eS : (eF + eS) / 2
var bool  armU = false
var bool  armD = false
var int   pbBU = na
var int   pbBD = na
var float pbXU = na
var float pbXD = na
var float pbDU = na
var float pbDD = na
var int   legU = 0
var int   legD = 0
// SG003 / SG009: the per-leg count restarts only when a new leg in that direction establishes
if estU
    legU := 0
if estD
    legD := 0
int   pAge = pbLife ? pbAge : 0
float pDep = pbLife ? pbDep : 0.0
int   pLeg = pbLife ? pbLeg : 0
bool  evPU = false
bool  evPD = false
int   ageS = na
float depS = na
float invS = na
if cfm
    // the setup's extreme keeps updating while it is armed
    // SG010: the deepest reading so far, each candle against its own touch line and ATR
    if armU
        pbXU := math.min(pbXU, low)
        pbDU := atr > 0 ? math.max(pbDU, (tch - low) / atr) : pbDU
    if armD
        pbXD := math.max(pbXD, high)
        pbDD := atr > 0 ? math.max(pbDD, (high - tch) / atr) : pbDD
    // SG005: cancel — cloud turned or weakened, close through the slow EMA, too old (SG001), too deep (SG002)
    // SG007: an opposite CLOUD flip also cancels · SG008: depth is measured past the selected touch line
    if armU and (evFD or cDir != 1 or stg < pbMinS or close < eS or (pAge > 0 and bar_index - pbBU > pAge) or (pDep > 0 and nz(pbDU) > pDep))
        armU := false
    if armD and (evFU or cDir != -1 or stg < pbMinS or close > eS or (pAge > 0 and bar_index - pbBD > pAge) or (pDep > 0 and nz(pbDD) > pDep))
        armD := false
    // arm on the first touch (SG003: only while the leg still has pullbacks left)
    if pbOn and not armU and not evFD and (pLeg == 0 or legU < pLeg) and cDir == 1 and stg >= pbMinS and low <= tch and close >= eS and (pDep == 0 or atr <= 0 or (tch - low) / atr <= pDep)
        armU := true
        armD := false
        pbBU := bar_index
        pbXU := low
        pbDU := atr > 0 ? math.max(0.0, (tch - low) / atr) : 0.0
    if pbOn and not armD and not evFU and (pLeg == 0 or legD < pLeg) and cDir == -1 and stg >= pbMinS and high >= tch and close <= eS and (pDep == 0 or atr <= 0 or (high - tch) / atr <= pDep)
        armD := true
        armU := false
        pbBD := bar_index
        pbXD := high
        pbDD := atr > 0 ? math.max(0.0, (high - tch) / atr) : 0.0
    // reclaim (SG004: on a later candle unless same-candle reclaim is on)
    if armU and not evFU and (pbSame or bar_index > pbBU) and close > eF and close > open and gU
        evPU := true
        armU := false
        legU += 1
        ageS := bar_index - pbBU
        depS := pbDU
        invS := pbXU
    if armD and not evFD and (pbSame or bar_index > pbBD) and close < eF and close < open and gD
        evPD := true
        armD := false
        legD += 1
        ageS := bar_index - pbBD
        depS := pbDD
        invS := pbXD
if evFU or evFD
    invS := eS
if evFU or evPU
    lastU := bar_index
if evFD or evPD
    lastD := bar_index
 
// ─────────────────────────────── labels + alerts ───────────────────────────────
color sgU = sgMode == "Palette" ? kUp : cSU
color sgD = sgMode == "Palette" ? kDn : cSD
color txC = color.new(chart.bg_color, 0)
bool  arw = lbSt == "Arrow only"
bool  anyS = evFU or evFD or evPU or evPD
// SG006 / VS001: invalidation, setup age and depth go in the hover text and the alert (no chart clutter)
string stTx = not anyS ? "" : " · cloud strength " + str.tostring(math.round(stg * 100)) + " % · gap " + str.tostring(gap, "#.00") + " ATR · invalid " + ((evFU or evPU) ? "below " : "above ") + str.tostring(invS, format.mintick) + ((evFU or evFD) ? " (slow EMA at the signal)" : " (pullback extreme) · touch " + pbTch + " · setup " + str.tostring(ageS) + " bars · depth " + str.tostring(depS, "#.00") + " ATR · pullback " + str.tostring(evPU ? legU : legD) + " of this leg") + " — structural reference, not a guaranteed fill · " + str.format_time(time_close, "HH:mm", "America/Chicago") + " CT"
// VS003 / VS004: position and gap — a label below the candle points up at the low, above it points down at the high
bool  bBlw = lbPos != "All above candles"
bool  sBlw = lbPos == "All below candles"
float lGap = atr > 0 ? lbGap * atr : 0.0
if shSg and (evFU or evPU)
    string t = evFU ? (arw ? "▲" : "CLOUD ▲") : (arw ? "▲" : "PB ▲")
    label.new(bar_index, bBlw ? low - lGap : high + lGap, t, color = color.new(sgU, evFU ? 0 : pbDim), textcolor = txC, style = bBlw ? label.style_label_up : label.style_label_down, size = f_sz(lbSz), tooltip = (evFU ? "CLOUD FLIP ▲ — fast EMA crossed above the slow EMA and the gap is opening" : "PULLBACK ▲ — dip into the up cloud, closed back above the fast EMA") + " · close " + str.tostring(close, format.mintick) + stTx)
if shSg and (evFD or evPD)
    string t = evFD ? (arw ? "▼" : "CLOUD ▼") : (arw ? "▼" : "PB ▼")
    label.new(bar_index, sBlw ? low - lGap : high + lGap, t, color = color.new(sgD, evFD ? 0 : pbDim), textcolor = txC, style = sBlw ? label.style_label_up : label.style_label_down, size = f_sz(lbSz), tooltip = (evFD ? "CLOUD FLIP ▼ — fast EMA crossed below the slow EMA and the gap is opening" : "PULLBACK ▼ — rally into the down cloud, closed back below the fast EMA") + " · close " + str.tostring(close, format.mintick) + stTx)
if alOn and anyS
    string m = "STRYK EMC · " + syminfo.ticker + " · " + (evFU ? "CLOUD FLIP ▲" : evFD ? "CLOUD FLIP ▼" : evPU ? "PULLBACK ▲" : "PULLBACK ▼") + " at " + str.tostring(close, format.mintick) + stTx
    alert(m, alert.freq_once_per_bar_close)
// GT004 warning: an HTF gate set to the chart's timeframe or lower is ignored
var label htWarn = na
if barstate.islast
    label.delete(htWarn)
    htWarn := htOn and not htOk ? label.new(bar_index, high, "EMC: HTF gate ignored — pick a timeframe above the chart's", color = color.new(chart.fg_color, 85), textcolor = chart.fg_color, style = label.style_label_lower_left, size = size.small) : na
alertcondition(evFU, "EMC · Cloud flip ▲", "STRYK EMC: significant cloud flip UP")
alertcondition(evFD, "EMC · Cloud flip ▼", "STRYK EMC: significant cloud flip DOWN")
alertcondition(evPU, "EMC · Pullback ▲", "STRYK EMC: pullback buy in the up cloud")
alertcondition(evPD, "EMC · Pullback ▼", "STRYK EMC: pullback sell in the down cloud")
alertcondition(cfm and xUp, "EMC · Cloud turned up", "STRYK EMC: fast EMA crossed above the slow EMA")
alertcondition(cfm and xDn, "EMC · Cloud turned down", "STRYK EMC: fast EMA crossed below the slow EMA")

← 返回列表