Regime Prism_Merry

MerryAlgo · study · 83 行 · 点赞 5 · TradingView 原页

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

Pine Script

//@version=6
indicator("Merry | Regime Prism", "Merry RGP", overlay = true, max_labels_count = 200)
 
// ══════════════════════════════════════════════════════════════════════════
//  Merry — Regime Prism (RGP)
//
//  Three independent votes on market direction — local EMA structure,
//  Donchian channel position, and a higher-timeframe bias — refract
//  into one regime: BULL, BEAR, or RANGE. The chart background is
//  tinted by the regime, and the tint's intensity follows the vote
//  margin, so a 3-0 sweep paints far stronger than a 2-1 split.
//  Regime changes print as labeled badges on the chart; a live score
//  tag rides the right edge.
//
//  Generic building blocks: EMA cross, Donchian midpoint, HTF EMA.
//  Original composition/presentation. © Merry. Free to use and modify.
// ══════════════════════════════════════════════════════════════════════════
 
// ── Inputs ────────────────────────────────────────────────────────────────
grpCore = "Core engine"
lenF  = input.int(20, "Fast EMA",            minval = 2, group = grpCore)
lenS  = input.int(50, "Slow EMA",            minval = 3, group = grpCore)
lenD  = input.int(20, "Channel lookback",    minval = 2, group = grpCore)
htf   = input.timeframe("D", "Bias timeframe", group = grpCore, tooltip = "Higher timeframe for the third vote. Its EMA is fetched without lookahead.")
lenH  = input.int(20, "Bias EMA length",     minval = 2, group = grpCore)
 
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")
bgMax    = input.float(25, "Max background opacity %", minval = 5, maxval = 60, group = grpStyle)
showSig  = input.bool(true, "Show regime badges", group = grpStyle)
showDash = input.bool(true, "Show dashboard", group = grpStyle)
 
// ── Three votes ───────────────────────────────────────────────────────────
eF = ta.ema(close, lenF)
eS = ta.ema(close, lenS)
dcMid = (ta.highest(high, lenD)[1] + ta.lowest(low, lenD)[1]) / 2
htfE  = request.security(syminfo.tickerid, htf, ta.ema(close, lenH), lookahead = barmerge.lookahead_off)
 
sA = eF > eS ? 1 : -1
sB = close > dcMid ? 1 : -1
sC = close > htfE ? 1 : -1
score = sA + sB + sC                                    // −3 … +3
 
regime = score >= 2 ? 1 : score <= -2 ? -1 : 0
regimeTxt = regime == 1 ? "BULL" : regime == -1 ? "BEAR" : "RANGE"
regimeCol = regime == 1 ? bullCol : regime == -1 ? bearCol : neutCol
 
// ── Prism background (the hero) ───────────────────────────────────────────
intensity = regime == 0 ? 0.2 : (math.abs(score) - 1) / 2.0   // 0.5 … 1.0 in a decided regime
bgCol = regime == 0 ? color.new(neutCol, 96) : color.new(regimeCol, math.round(100 - intensity * bgMax))
bgcolor(bgCol, title = "Regime tint")
 
// ── Rendering: dynamic regime badges at the bar ───────────────────────────
if showSig and regime != regime[1]
    label.new(bar_index, high, regimeTxt, style = label.style_label_down, color = color.new(regimeCol, 10), textcolor = chart.bg_color, size = size.small)
 
// Right-edge live score tag
var label tag = na
if barstate.islast
    label.delete(tag)
    tag := label.new(bar_index + 2, close, "RGP " + (score >= 0 ? "+" : "") + str.tostring(score, "0") + "/3", style = label.style_label_left, color = color.new(regimeCol, 20), textcolor = chart.bg_color, size = size.small)
 
// ── Dashboard v2 ──────────────────────────────────────────────────────────
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 · REGIME PRISM", text_color = chart.fg_color, text_size = size.tiny, bgcolor = color.new(chart.fg_color, 85))
    table.cell(dash, 0, 1, "EMAs",    text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 1, sA > 0 ? "BULL" : "BEAR", text_color = chart.bg_color, bgcolor = color.new(sA > 0 ? bullCol : bearCol, 15), text_size = size.small)
    table.cell(dash, 0, 2, "Channel", text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 2, sB > 0 ? "BULL" : "BEAR", text_color = chart.bg_color, bgcolor = color.new(sB > 0 ? bullCol : bearCol, 15), text_size = size.small)
    table.cell(dash, 0, 3, "Regime",  text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 3, regimeTxt + " · " + (score >= 0 ? "+" : "") + str.tostring(score, "0") + "/3", text_color = chart.bg_color, bgcolor = color.new(regimeCol, 15), text_size = size.small)
 
// ── Alerts ────────────────────────────────────────────────────────────────
regBull = regime == 1  and regime[1] != 1
regBear = regime == -1 and regime[1] != -1
regNeut = regime == 0  and regime[1] != 0
alertcondition(regBull, "Merry RGP — regime turns bull",   "Merry Regime Prism: at least two of three votes flipped bullish.")
alertcondition(regBear, "Merry RGP — regime turns bear",   "Merry Regime Prism: at least two of three votes flipped bearish.")
alertcondition(regNeut, "Merry RGP — regime turns range",  "Merry Regime Prism: votes split — range regime.")

← 返回列表