Trend Spine_Merry

MerryAlgo · study · 103 行 · 点赞 8 · TradingView 原页

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

Pine Script

//@version=6
indicator("Merry | Trend Spine", "Merry TSP", overlay = true, max_labels_count = 200)
 
// ══════════════════════════════════════════════════════════════════════════
//  Merry — Trend Spine (TSP)
//
//  A single adaptive "spine" that shortens its memory when the market
//  moves efficiently and lengthens it when price chops. Around the spine
//  sits a conviction plate: a channel whose opacity follows the market's
//  efficiency ratio, so strong trends cast a solid plate and choppy
//  phases leave only a ghost. Vertebra dots stamp the spine while slope
//  conviction holds; turns print as price-anchored badges and the spine
//  carries a live conviction tag.
//
//  Generic building blocks: efficiency ratio (Kaufman), recursive
//  adaptive smoothing, ATR channel. Original composition/presentation.
//  © Merry. Free to use and modify.
// ══════════════════════════════════════════════════════════════════════════
 
// ── Inputs ────────────────────────────────────────────────────────────────
grpCore = "Core engine"
lenEff  = input.int(21, "Efficiency window", minval = 2, group = grpCore, tooltip = "Lookback for the efficiency ratio that adapts the spine.")
minLen  = input.int(4,  "Fastest spine length", minval = 2, group = grpCore)
maxLen  = input.int(64, "Slowest spine length", minval = 4, group = grpCore, tooltip = "Spine length when efficiency is zero (pure chop).")
 
grpPlate = "Conviction plate"
plateMult = input.float(1.6, "Plate width (ATR ×)", minval = 0.2, step = 0.1, group = grpPlate)
convFloor = input.float(0.15, "Slope conviction floor (ATR/bar)", minval = 0.0, step = 0.05, group = grpPlate)
stepN     = input.int(6, "Vertebra spacing (bars)", minval = 1, group = grpPlate)
 
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")
showVert = input.bool(true, "Show vertebrae", group = grpStyle)
showSig  = input.bool(true, "Show event badges", group = grpStyle)
showDash = input.bool(true, "Show dashboard", group = grpStyle)
 
// ── Adaptive spine ────────────────────────────────────────────────────────
atrV = ta.atr(14)
chg  = math.abs(ta.change(close))
sumC = math.sum(chg, lenEff)
er   = sumC > 0 ? math.abs(close - close[lenEff]) / sumC : 0.0   // efficiency 0..1
 
adapt = math.min(math.max(lenEff * (1.0 - er) * 3.0, minLen), maxLen)
alpha = 2.0 / (adapt + 1.0)
 
var float spine = na
spine := na(spine[1]) ? close : spine[1] + alpha * (close - spine[1])
 
slopeS = spine - spine[2]
slopeSign = slopeS > atrV * convFloor ? 1 : slopeS < -atrV * convFloor ? -1 : 0
 
// ── Conviction plate ──────────────────────────────────────────────────────
plateTop = spine + atrV * plateMult
plateBot = spine - atrV * plateMult
plateT   = math.round(97 - math.min(er * 1.6, 1.0) * 42)      // solid when efficient, ghost when choppy
plateCol = color.new(slopeSign >= 0 ? bullCol : bearCol, plateT)
 
spineCol = slopeSign >= 0 ? color.from_gradient(slopeS / (atrV * 2), 0, 1, neutCol, bullCol) : color.from_gradient(slopeS / (atrV * 2), -1, 0, bearCol, neutCol)
 
pTop = plot(plateTop, "Plate top", display = display.none, editable = false)
pBot = plot(plateBot, "Plate bottom", display = display.none, editable = false)
fill(pTop, pBot, color = plateCol, title = "Conviction plate")
 
plot(spine, "Spine glow", color = color.new(spineCol, 80), linewidth = 4)
plot(spine, "Spine halo", color = color.new(spineCol, 50), linewidth = 2)
plot(spine, "Spine", color = spineCol, linewidth = 1)
 
plotshape(showVert and bar_index % stepN == 0 and slopeSign != 0 ? spine : na, "Vertebra", shape.circle, location.absolute, slopeSign > 0 ? bullCol : bearCol, size = size.tiny)
 
// ── Rendering: price-anchored badges ──────────────────────────────────────
plotshape(showSig and slopeSign == 1  and slopeSign[1] != 1, "Spine turns up",   shape.labelup,   location.belowbar, bullCol, text = "TURN", textcolor = chart.bg_color, size = size.tiny)
plotshape(showSig and slopeSign == -1 and slopeSign[1] != -1, "Spine turns down", shape.labeldown, location.abovebar, bearCol, text = "TURN", textcolor = chart.bg_color, size = size.tiny)
 
// Right-edge live conviction tag
var label tag = na
if barstate.islast
    label.delete(tag)
    tagCol = slopeSign >= 0 ? bullCol : bearCol
    tag := label.new(bar_index + 2, spine, "TSP " + str.tostring(er * 100, "0") + "%", style = label.style_label_left, color = color.new(tagCol, 20), textcolor = chart.bg_color, size = size.small)
 
// ── Dashboard v2 ──────────────────────────────────────────────────────────
dirTxt = slopeSign > 0 ? "UP" : slopeSign < 0 ? "DOWN" : "FLAT"
dirCol = slopeSign > 0 ? bullCol : slopeSign < 0 ? bearCol : 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 · TREND SPINE", text_color = chart.fg_color, text_size = size.tiny, bgcolor = color.new(chart.fg_color, 85))
    table.cell(dash, 0, 1, "Trend",      text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 1, dirTxt,       text_color = chart.bg_color, bgcolor = color.new(dirCol, 15), text_size = size.small)
    table.cell(dash, 0, 2, "Conviction", text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 2, str.tostring(er * 100, "0") + "%", text_color = er > 0.35 ? dirCol : neutCol, text_size = size.small, text_halign = text.align_right)
    table.cell(dash, 0, 3, "Spine length", text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 3, str.tostring(adapt, "0"), text_color = neutCol, text_size = size.small, text_halign = text.align_right)
 
// ── Alerts ────────────────────────────────────────────────────────────────
flipUp = slopeSign == 1 and slopeSign[1] != 1
flipDn = slopeSign == -1 and slopeSign[1] != -1
alertcondition(flipUp, "Merry TSP — spine turns up",   "Merry Trend Spine: spine slope turned up through the conviction floor.")
alertcondition(flipDn, "Merry TSP — spine turns down", "Merry Trend Spine: spine slope turned down through the conviction floor.")
alertcondition(er > 0.4 and er[1] <= 0.4, "Merry TSP — high conviction", "Merry Trend Spine: efficiency ratio above 40% — directional phase.")

← 返回列表