Trend Fabric_Merry

MerryAlgo · study · 148 行 · 点赞 11 · TradingView 原页

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

Pine Script

//@version=6
indicator("Merry | Trend Fabric", "Merry TFB", overlay = true, max_labels_count = 200)
 
// ══════════════════════════════════════════════════════════════════════════
//  Merry — Trend Fabric (TFB)
//
//  Eight moving-average threads woven into one fabric. Every thread is
//  tinted by its own normalized slope, and the weave between adjacent
//  threads is filled so the fabric visibly bends with the trend. The
//  outer edges glow; the inner weave breathes. The spread between the
//  fastest and slowest thread — ranked against its own history —
//  reports three weave states: TIGHT (coiled), NORMAL, STRETCHED
//  (mature/exhausted). Events print as price-anchored badges.
//
//  Generic building blocks: EMA family, slope normalization by ATR,
//  percentile ranking. Original composition and presentation.
//  © Merry. Free to use and modify.
// ══════════════════════════════════════════════════════════════════════════
 
// ── Inputs ────────────────────────────────────────────────────────────────
grpCore = "Core engine"
baseLen  = input.int(8, "First thread length", minval = 2, group = grpCore, tooltip = "Length of the fastest thread. Slower threads are spaced from this one.")
spacing  = input.int(6, "Thread spacing",      minval = 1, group = grpCore, tooltip = "Length step between adjacent threads.")
atrLen   = input.int(14, "Slope normalizer (ATR)", minval = 1, group = grpCore)
 
grpWeave = "Weave states"
lenWv   = input.int(150, "Weave ranking window", minval = 30, group = grpWeave)
tightP  = input.int(20,  "Tight percentile",     minval = 1, maxval = 49, group = grpWeave)
frayP   = input.int(85,  "Stretched percentile", minval = 51, maxval = 99, group = grpWeave)
 
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")
fillOp   = input.float(90, "Weave fill opacity %", minval = 40, maxval = 98, group = grpStyle, tooltip = "Transparency of the fills between adjacent threads. Lower = denser fabric.")
showSig  = input.bool(true, "Show event badges", group = grpStyle)
showDash = input.bool(true, "Show dashboard", group = grpStyle)
 
// ── Threads ───────────────────────────────────────────────────────────────
atrV = ta.atr(atrLen)
l1 = baseLen
l2 = baseLen + spacing
l3 = baseLen + spacing * 2
l4 = baseLen + spacing * 3
l5 = baseLen + spacing * 4
l6 = baseLen + spacing * 5
l7 = baseLen + spacing * 6
l8 = baseLen + spacing * 7
 
e1 = ta.ema(close, l1)
e2 = ta.ema(close, l2)
e3 = ta.ema(close, l3)
e4 = ta.ema(close, l4)
e5 = ta.ema(close, l5)
e6 = ta.ema(close, l6)
e7 = ta.ema(close, l7)
e8 = ta.ema(close, l8)
 
// Normalized slope of a thread, in ATR units per bar
f_sl(float s) =>
    (s - s[3]) / math.max(atrV, syminfo.mintick)
 
s1 = f_sl(e1)
s2 = f_sl(e2)
s3 = f_sl(e3)
s4 = f_sl(e4)
s5 = f_sl(e5)
s6 = f_sl(e6)
s7 = f_sl(e7)
s8 = f_sl(e8)
 
c1 = color.from_gradient(s1, -1.0, 1.0, bearCol, bullCol)
c2 = color.from_gradient(s2, -1.0, 1.0, bearCol, bullCol)
c3 = color.from_gradient(s3, -1.0, 1.0, bearCol, bullCol)
c4 = color.from_gradient(s4, -1.0, 1.0, bearCol, bullCol)
c5 = color.from_gradient(s5, -1.0, 1.0, bearCol, bullCol)
c6 = color.from_gradient(s6, -1.0, 1.0, bearCol, bullCol)
c7 = color.from_gradient(s7, -1.0, 1.0, bearCol, bullCol)
c8 = color.from_gradient(s8, -1.0, 1.0, bearCol, bullCol)
 
// ── Weave fills ───────────────────────────────────────────────────────────
f_pair(float sa, float sb) => color.from_gradient((sa + sb) / 2, -1.0, 1.0, bearCol, bullCol)
 
// Glow on the outer edges, crisp core threads inside
p1g = plot(e1, "Thread 1 glow", color = color.new(c1, 82), linewidth = 4)
p1  = plot(e1, "Thread 1", color = c1, linewidth = 1)
p2  = plot(e2, "Thread 2", color = c2, linewidth = 1)
p3  = plot(e3, "Thread 3", color = c3, linewidth = 1)
p4  = plot(e4, "Thread 4", color = c4, linewidth = 1)
p5  = plot(e5, "Thread 5", color = c5, linewidth = 1)
p6  = plot(e6, "Thread 6", color = c6, linewidth = 1)
p7  = plot(e7, "Thread 7", color = c7, linewidth = 1)
p8g = plot(e8, "Thread 8 glow", color = color.new(c8, 82), linewidth = 4)
p8  = plot(e8, "Thread 8", color = c8, linewidth = 1)
 
fill(p1, p2, color = color.new(f_pair(s1, s2), fillOp))
fill(p2, p3, color = color.new(f_pair(s2, s3), fillOp))
fill(p3, p4, color = color.new(f_pair(s3, s4), fillOp))
fill(p4, p5, color = color.new(f_pair(s4, s5), fillOp))
fill(p5, p6, color = color.new(f_pair(s5, s6), fillOp))
fill(p6, p7, color = color.new(f_pair(s6, s7), fillOp))
fill(p7, p8, color = color.new(f_pair(s7, s8), fillOp))
 
// ── Weave state machine ───────────────────────────────────────────────────
width  = math.abs(e1 - e8) / math.max(atrV, syminfo.mintick)
wTight = ta.percentile_nearest_rank(width, lenWv, tightP)
wFray  = ta.percentile_nearest_rank(width, lenWv, frayP)
weave  = width <= wTight ? 1 : width >= wFray ? 3 : 2
 
flipUp = ta.crossover(e1, e8)
flipDn = ta.crossunder(e1, e8)
netUp  = e1 > e8
 
// ── Rendering: price-anchored badges ──────────────────────────────────────
plotshape(showSig and flipUp,  "Fabric turns up",   shape.labelup,   location.belowbar, bullCol, text = "TURN", textcolor = chart.bg_color, size = size.tiny)
plotshape(showSig and flipDn,  "Fabric turns down", shape.labeldown, location.abovebar, bearCol, text = "TURN", textcolor = chart.bg_color, size = size.tiny)
plotshape(showSig and weave == 1 and weave[1] != 1, "Weave tight", shape.diamond, location.belowbar, neutCol, text = "TIGHT", textcolor = chart.fg_color, size = size.tiny)
plotshape(showSig and weave == 3 and weave[1] != 3, "Weave stretched", shape.diamond, location.abovebar, color.new(bearCol, 20), text = "STRETCH", textcolor = chart.bg_color, size = size.tiny)
 
// Right-edge live value tag on the fast thread
var label tag = na
if barstate.islast
    label.delete(tag)
    tagCol = netUp ? bullCol : bearCol
    tag := label.new(bar_index + 2, e1, "TFB " + (netUp ? "▲ " : "▼ ") + str.tostring(math.min(math.abs(s8) / 1.5, 1.0) * 100, "0") + "%", style = label.style_label_left, color = color.new(tagCol, 20), textcolor = chart.bg_color, size = size.small)
 
// ── Dashboard v2 ──────────────────────────────────────────────────────────
strTxt  = netUp ? "UP" : "DOWN"
strCol  = netUp ? bullCol : bearCol
weaveTxt = weave == 1 ? "TIGHT" : weave == 3 ? "STRETCHED" : "NORMAL"
weaveCol = weave == 1 ? neutCol : weave == 3 ? bearCol : bullCol
 
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 FABRIC", text_color = chart.fg_color, text_size = size.tiny, bgcolor = color.new(chart.fg_color, 85))
    table.cell(dash, 0, 1, "Weave",   text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 1, weaveTxt,  text_color = chart.bg_color, bgcolor = color.new(weaveCol, 15), text_size = size.small)
    table.cell(dash, 0, 2, "Drift",   text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 2, strTxt,    text_color = chart.bg_color, bgcolor = color.new(strCol, 15), text_size = size.small)
    table.cell(dash, 0, 3, "Strength", text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 3, str.tostring(math.min(math.abs(s8) / 1.5, 1.0) * 100, "0") + "%", text_color = strCol, text_size = size.small, text_halign = text.align_right)
 
// ── Alerts ────────────────────────────────────────────────────────────────
alertcondition(flipUp,  "Merry TFB — fabric flips up",   "Merry Trend Fabric: fast thread crossed above slow thread.")
alertcondition(flipDn,  "Merry TFB — fabric flips down", "Merry Trend Fabric: fast thread crossed below slow thread.")
alertcondition(weave == 1 and weave[1] != 1, "Merry TFB — weave tight",  "Merry Trend Fabric: threads coiled into a tight weave.")
alertcondition(weave == 3 and weave[1] != 3, "Merry TFB — weave stretched", "Merry Trend Fabric: weave stretched — trend mature or exhausting.")

← 返回列表