Pressure Cloud_Merry

MerryAlgo · study · 141 行 · 点赞 16 · TradingView 原页

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

Pine Script

//@version=6
indicator("Merry | Pressure Cloud", "Merry PC", overlay = true, max_labels_count = 200)
 
// ══════════════════════════════════════════════════════════════════════════
//  Merry — Pressure Cloud (MPC)
//
//  Answers one question per bar: who is in control, and by how much?
//  Each bar's close is located inside its own range (0 = at the low,
//  1 = at the high), scaled by traded volume, z-score normalised, then
//  smoothed on two speeds. The slow pressure line bends a
//  pressure-weighted basis and breathes a layered cloud into the
//  direction of control; σ-rails track compression and flag squeeze /
//  release states. Flip and release events print as price-anchored
//  badges; the basis carries a live value tag.
//
//  Generic building blocks: close location value, z-score, EMA,
//  standard-deviation rails, percentile compression.
//  Original composition, presentation, and code. © Merry. Free to use.
// ══════════════════════════════════════════════════════════════════════════
 
// ── Inputs ────────────────────────────────────────────────────────────────
grpCore = "Core engine"
lenFast  = input.int(9,   "Fast pressure smoothing", minval = 1,  group = grpCore, tooltip = "EMA length for the fast pressure line. Lower reacts quicker and flips more often.")
lenSlow  = input.int(34,  "Slow pressure smoothing", minval = 2,  group = grpCore, tooltip = "EMA length for the slow pressure line that drives the cloud.")
lenZ     = input.int(100, "Normalisation window",    minval = 10, group = grpCore, tooltip = "Lookback for the z-score that puts pressure on a comparable scale across symbols.")
lenBasis = input.int(50,  "Weighted basis length",   minval = 2,  group = grpCore, tooltip = "Window of the pressure-weighted basis line.")
 
grpCloud = "Cloud and rails"
cloudScale = input.float(2.0, "Cloud reach (ATR ×)", minval = 0.25, step = 0.25, group = grpCloud, tooltip = "How far the cloud can extend from the basis, in ATR units at full pressure.")
railMult   = input.float(2.0, "Rail width (σ ×)",    minval = 0.25, step = 0.25, group = grpCloud, tooltip = "Standard-deviation multiplier for the outer rails.")
lenSq      = input.int(100,   "Squeeze window",      minval = 20, group = grpCloud, tooltip = "Lookback used to rank the current rail width against recent history.")
sqPct      = input.int(20,    "Squeeze percentile",  minval = 1, maxval = 99, group = grpCloud, tooltip = "Rail widths below this percentile count as squeezed.")
 
grpStyle = "Style"
bullCol   = input.color(#089981, "Bull",    group = grpStyle, inline = "c1")
bearCol   = input.color(#f23645, "Bear",    group = grpStyle, inline = "c1")
neutCol   = input.color(#787b86, "Neutral", group = grpStyle, inline = "c1")
maxOp     = input.float(85, "Max cloud opacity %", minval = 10, maxval = 95, group = grpStyle)
showCloud = input.bool(true, "Show pressure cloud", group = grpStyle)
showRails = input.bool(true, "Show σ-rails",        group = grpStyle)
showSig   = input.bool(true, "Show event badges",   group = grpStyle)
showSqBg  = input.bool(true, "Highlight squeeze zones", group = grpStyle)
showDash  = input.bool(true, "Show dashboard",      group = grpStyle)
 
// ── Pressure engine ───────────────────────────────────────────────────────
rng     = math.max(high - low, syminfo.mintick)
pos     = (close - low) / rng                    // 0 = close at the low, 1 = close at the high
vol     = nz(volume, 1.0)                        // symbols without volume degrade to pure close location
signedP = (pos - 0.5) * 2.0 * vol                // signed, volume-weighted control measure
 
meanP = ta.sma(signedP, lenZ)
sdP   = ta.stdev(signedP, lenZ)
zP    = sdP > 0 ? (signedP - meanP) / sdP : 0.0  // comparable scale on any symbol
 
pFast  = ta.ema(zP, lenFast)
pSlow  = ta.ema(zP, lenSlow)
 
// ── Pressure-weighted basis ───────────────────────────────────────────────
wgt   = 1.0 + math.abs(pSlow)
basis = math.sum(close * wgt, lenBasis) / math.sum(wgt, lenBasis)
 
basisCol = pSlow >= 0 ? color.from_gradient(pSlow, 0, 2, neutCol, bullCol) : color.from_gradient(pSlow, -2, 0, bearCol, neutCol)
 
// ── Cloud (two-stop depth) ────────────────────────────────────────────────
atr      = ta.atr(14)
cloudTop = basis + math.max(pSlow, 0.0) * atr * cloudScale
cloudBot = basis - math.max(-pSlow, 0.0) * atr * cloudScale
midTop   = (basis + cloudTop) / 2
midBot   = (basis + cloudBot) / 2
 
strength = math.min(math.abs(pSlow) / 2.0, 1.0)
cloudCol = color.new(pSlow >= 0 ? bullCol : bearCol, math.round(100 - strength * maxOp))
 
// ── Rails and squeeze state ───────────────────────────────────────────────
sd       = ta.stdev(close, lenBasis)
railUp   = basis + sd * railMult
railDn   = basis - sd * railMult
widthA   = (railUp - railDn) / math.max(atr, syminfo.mintick)
sqLevel  = ta.percentile_nearest_rank(widthA, lenSq, sqPct)
squeezed = widthA <= sqLevel
release  = squeezed[1] and not squeezed and math.abs(pFast) > 1.0
 
// ── Signals ───────────────────────────────────────────────────────────────
flipUp = ta.crossover(pFast, pSlow)
flipDn = ta.crossunder(pFast, pSlow)
 
// ── Rendering ─────────────────────────────────────────────────────────────
pTop  = plot(showCloud ? cloudTop : na, "Cloud top",    display = display.none, editable = false)
pMiT  = plot(showCloud ? midTop   : na, "Cloud mid top", display = display.none, editable = false)
pMiB  = plot(showCloud ? midBot   : na, "Cloud mid bot", display = display.none, editable = false)
pBot  = plot(showCloud ? cloudBot : na, "Cloud bottom", display = display.none, editable = false)
pBasI = plot(basis, "Basis fill anchor", display = display.none, editable = false)
fill(pTop, pMiT, color = color.new(cloudCol, 90), title = "Cloud halo")
fill(pMiT, pBasI, color = color.new(cloudCol, 68), title = "Cloud core upper")
fill(pBasI, pMiB, color = color.new(cloudCol, 68), title = "Cloud core lower")
fill(pMiB, pBot, color = color.new(cloudCol, 90), title = "Cloud halo lower")
 
plot(basis, "Basis glow", color = color.new(basisCol, 80), linewidth = 4)
plot(basis, "Basis halo", color = color.new(basisCol, 50), linewidth = 2)
plot(basis, "Pressure basis", color = basisCol, linewidth = 1)
 
plot(showRails ? railUp : na, "Upper σ-rail", color = color.new(neutCol, 55))
plot(showRails ? railDn : na, "Lower σ-rail", color = color.new(neutCol, 55))
 
// Price-anchored event badges (never pinned to pane edges)
plotshape(showSig and flipUp, "Pressure flip up",   shape.labelup,   location.belowbar, bullCol, text = "FLIP", textcolor = chart.bg_color, size = size.tiny)
plotshape(showSig and flipDn, "Pressure flip down", shape.labeldown, location.abovebar, bearCol, text = "FLIP", textcolor = chart.bg_color, size = size.tiny)
plotshape(showSig and release and pFast > 0, "Release up",   shape.labelup,   location.belowbar, bullCol, text = "REL", textcolor = chart.bg_color, size = size.tiny)
plotshape(showSig and release and pFast <= 0, "Release down", shape.labeldown, location.abovebar, bearCol, text = "REL", textcolor = chart.bg_color, size = size.tiny)
 
bgcolor(showSqBg and squeezed ? color.new(neutCol, 92) : na, title = "Squeeze zone")
 
// Right-edge live value tag
var label tag = na
if barstate.islast
    label.delete(tag)
    tagCol = pSlow >= 0 ? bullCol : bearCol
    tag := label.new(bar_index + 2, basis, "PC " + str.tostring(pSlow, "0.00"), style = label.style_label_left, color = color.new(tagCol, 20), textcolor = chart.bg_color, size = size.small)
 
// ── Dashboard v2 ──────────────────────────────────────────────────────────
regime    = pSlow > 0.35 ? 1 : pSlow < -0.35 ? -1 : 0
regimeTxt = regime == 1 ? "BULL PRESSURE" : regime == -1 ? "BEAR PRESSURE" : "NEUTRAL"
regimeCol = regime == 1 ? bullCol : regime == -1 ? bearCol : neutCol
stateTxt  = release ? "RELEASE" : squeezed ? "SQUEEZE" : "NORMAL"
stateCol  = release ? bullCol : squeezed ? neutCol : neutCol
 
var table dash = table.new(position.top_right, 2, 4, border_width = 0, frame_width = 1, frame_color = color.new(chart.fg_color, 75), bgcolor = color.new(chart.fg_color, 92))
if showDash and barstate.islast
    table.merge_cells(dash, 0, 0, 1, 0)
    table.cell(dash, 0, 0, "MERRY · PRESSURE CLOUD", text_color = chart.fg_color, text_size = size.tiny, bgcolor = color.new(chart.fg_color, 85))
    table.cell(dash, 0, 1, "Regime",   text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 1, regimeTxt,  text_color = chart.bg_color, bgcolor = color.new(regimeCol, 15), text_size = size.small)
    table.cell(dash, 0, 2, "Pressure", text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 2, str.tostring(pSlow, "0.00"), text_color = pSlow >= 0 ? bullCol : bearCol, text_size = size.small, text_halign = text.align_right)
    table.cell(dash, 0, 3, "Range",    text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 3, stateTxt,   text_color = chart.bg_color, bgcolor = color.new(stateCol, 15), text_size = size.small)
 
// ── Alerts ────────────────────────────────────────────────────────────────
alertcondition(flipUp,  "Merry PC — pressure flip up",   "Merry Pressure Cloud: fast pressure crossed above slow pressure.")
alertcondition(flipDn,  "Merry PC — pressure flip down", "Merry Pressure Cloud: fast pressure crossed below slow pressure.")
alertcondition(release, "Merry PC — squeeze release",    "Merry Pressure Cloud: squeeze released with strong directional pressure.")

← 返回列表