Tenacity Ribbon_Merry

MerryAlgo · study · 88 行 · 点赞 146 · TradingView 原页

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

Pine Script

//@version=6
indicator("Merry | Tenacity Ribbon", "Merry TNR", overlay = true, max_labels_count = 200)
 
// ══════════════════════════════════════════════════════════════════════════
//  Merry — Tenacity Ribbon (TNR)
//
//  A ribbon around a base EMA whose thickness reflects how entrenched
//  the trend is: every consecutive bar on the same side of the base
//  thickens the ribbon, up to a cap. Thickness reads as conviction —
//  FRESH, BUILDING, ENTRENCHED. When a long-held side flips, a SNAP
//  badge marks the break and the ribbon collapses.
//
//  Generic building blocks: EMA baseline, consecutive-bar counting,
//  ATR scaling. Original composition/presentation.
//  © Merry. Free to use and modify.
// ══════════════════════════════════════════════════════════════════════════
 
// ── Inputs ────────────────────────────────────────────────────────────────
grpCore = "Core engine"
lenB     = input.int(50, "Base EMA", minval = 2, group = grpCore)
normLen  = input.int(40, "Entrenchment norm (bars)", minval = 5, group = grpCore, tooltip = "Persistence count that maps to fully entrenched.")
baseMult = input.float(1.0, "Ribbon base width (ATR ×)", minval = 0.1, step = 0.1, group = grpCore)
strongN  = input.int(15, "Snap threshold (bars)", minval = 3, group = grpCore, tooltip = "Minimum prior persistence for a flip to count as a snap.")
 
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")
showSig  = input.bool(true, "Show snap badges", group = grpStyle)
showDash = input.bool(true, "Show dashboard", group = grpStyle)
 
// ── Tenacity engine ───────────────────────────────────────────────────────
atrV = ta.atr(14)
base = ta.ema(close, lenB)
sig  = close > base ? 1 : close < base ? -1 : 0
 
var int pers = 1
pers := sig == nz(sig[1], sig) ? pers + 1 : 1
 
normP = math.min(pers / float(normLen), 1.0)              // 0..1 conviction
hw    = atrV * baseMult * (0.4 + normP * 1.6)             // half-width grows with tenacity
ribU  = base + hw
ribD  = base - hw
 
snapUp = sig == 1  and nz(sig[1], sig) != 1 and pers[1] >= strongN
snapDn = sig == -1 and nz(sig[1], sig) != -1 and pers[1] >= strongN
 
// ── Rendering ─────────────────────────────────────────────────────────────
dirCol  = sig >= 0 ? bullCol : bearCol
ribT    = math.round(92 - normP * 45)                      // entrenched = solid
pU = plot(ribU, "Ribbon upper", display = display.none, editable = false)
pD = plot(ribD, "Ribbon lower", display = display.none, editable = false)
fill(pU, pD, color = color.new(dirCol, ribT), title = "Tenacity ribbon")
 
plot(base, "Base glow", color = color.new(dirCol, 80), linewidth = 4)
plot(base, "Base halo", color = color.new(dirCol, 50), linewidth = 2)
plot(base, "Base", color = color.from_gradient(normP, 0.0, 1.0, neutCol, dirCol), linewidth = 1)
 
// Price-anchored snap badges
plotshape(showSig and snapUp, "Snap up",   shape.labelup,   location.belowbar, bullCol, text = "SNAP", textcolor = chart.bg_color, size = size.tiny)
plotshape(showSig and snapDn, "Snap down", shape.labeldown, location.abovebar, bearCol, text = "SNAP", textcolor = chart.bg_color, size = size.tiny)
 
// Right-edge live conviction tag
var label tag = na
if barstate.islast
    label.delete(tag)
    tagTxt = "TNR " + str.tostring(pers) + " · " + (normP >= 0.75 ? "ENTRENCHED" : normP >= 0.35 ? "BUILDING" : "FRESH")
    tag := label.new(bar_index + 2, base, tagTxt, style = label.style_label_left, color = color.new(dirCol, 20), textcolor = chart.bg_color, size = size.small)
 
// ── Dashboard v2 ──────────────────────────────────────────────────────────
sideTxt = sig > 0 ? "ABOVE BASE" : sig < 0 ? "BELOW BASE" : "AT BASE"
sideCol = sig > 0 ? bullCol : sig < 0 ? bearCol : neutCol
stateTxt = normP >= 0.75 ? "ENTRENCHED" : normP >= 0.35 ? "BUILDING" : "FRESH"
 
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 · TENACITY RIBBON", text_color = chart.fg_color, text_size = size.tiny, bgcolor = color.new(chart.fg_color, 85))
    table.cell(dash, 0, 1, "Side",   text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 1, sideTxt,  text_color = chart.bg_color, bgcolor = color.new(sideCol, 15), text_size = size.small)
    table.cell(dash, 0, 2, "Run",    text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 2, str.tostring(pers) + " bars", text_color = sideCol, text_size = size.small, text_halign = text.align_right)
    table.cell(dash, 0, 3, "State",  text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 3, stateTxt, text_color = chart.bg_color, bgcolor = color.new(sideCol, 15), text_size = size.small)
 
// ── Alerts ────────────────────────────────────────────────────────────────
alertcondition(snapUp or snapDn, "Merry TNR — snap", "Merry Tenacity Ribbon: long-held side flipped — snap event.")
alertcondition(normP >= 0.75 and normP[1] < 0.75, "Merry TNR — entrenched", "Merry Tenacity Ribbon: trend reached entrenched conviction.")

← 返回列表