Slope Rails_Merry

MerryAlgo · study · 104 行 · 点赞 17 · TradingView 原页

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

Pine Script

//@version=6
indicator("Merry | Slope Rails", "Merry SLR", overlay = true, max_lines_count = 50, max_labels_count = 200)
 
// ══════════════════════════════════════════════════════════════════════════
//  Merry — Slope Rails (SLR)
//
//  A live linear-regression channel — two "rails" around a fitted spine —
//  whose fill intensity tracks the fit quality (correlation). A dotted
//  projection cone extends the fit to the right, widening by residual
//  dispersion. Stretch events (price at the top decile of historical
//  deviation from the fit) print as price-anchored σ badges; the spine
//  glows and carries a live fit-quality tag.
//
//  Generic building blocks: linear regression, correlation, residual
//  standard deviation. Original composition/presentation.
//  © Merry. Free to use and modify.
// ══════════════════════════════════════════════════════════════════════════
 
// ── Inputs ────────────────────────────────────────────────────────────────
grpCore = "Core engine"
fitLen = input.int(100, "Fit window",        minval = 20, group = grpCore)
multR  = input.float(2.0, "Rail width (σ ×)", minval = 0.25, step = 0.25, group = grpCore)
projN  = input.int(15,  "Projection bars",    minval = 1,  group = grpCore, tooltip = "How far the dotted projection cone extends to the right of the last bar.")
lenDev = input.int(150, "Deviation ranking window", minval = 30, 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")
maxOp    = input.float(75, "Max channel opacity %", minval = 20, maxval = 95, group = grpStyle)
showProj = input.bool(true, "Show projection cone", group = grpStyle)
showSig  = input.bool(true, "Show stretch badges", group = grpStyle)
showDash = input.bool(true, "Show dashboard", group = grpStyle)
 
// ── Regression engine ─────────────────────────────────────────────────────
fit   = ta.linreg(close, fitLen, 0)
slope = fit - ta.linreg(close, fitLen, 1)               // per-bar fitted drift
resid = close - fit
sdR   = ta.stdev(resid, fitLen)
corr  = ta.correlation(bar_index, close, fitLen)
fitQ  = math.abs(corr)                                  // fit quality 0..1
 
railU = fit + sdR * multR
railD = fit - sdR * multR
railCol = slope >= 0 ? bullCol : bearCol
 
devNow = math.abs(close - fit) / math.max(sdR, syminfo.mintick)
devHi  = ta.percentile_nearest_rank(devNow, lenDev, 90)
stretched = devNow >= devHi
 
// ── Rendering ─────────────────────────────────────────────────────────────
pU = plot(railU, "Upper rail", color = color.new(neutCol, 60))
pD = plot(railD, "Lower rail", color = color.new(neutCol, 60))
fill(pU, pD, color = color.new(railCol, math.round(100 - fitQ * maxOp)), title = "Fit channel")
 
plot(fit, "Spine glow", color = color.new(railCol, 82), linewidth = 4)
plot(fit, "Spine halo", color = color.new(railCol, 55), linewidth = 2)
plot(fit, "Fitted spine", color = railCol, linewidth = 1)
 
// Price-anchored stretch badges
plotshape(showSig and stretched and close > fit, "Stretched high", shape.labeldown, location.abovebar, bullCol, text = "σ+", textcolor = chart.bg_color, size = size.tiny)
plotshape(showSig and stretched and close < fit, "Stretched low",  shape.labelup,   location.belowbar, bearCol, text = "σ−", textcolor = chart.bg_color, size = size.tiny)
 
// Projection cone (last bar only)
var line lnM = na
var line lnU = na
var line lnD = na
if showProj and barstate.islast
    line.delete(lnM)
    line.delete(lnU)
    line.delete(lnD)
    x2 = bar_index + projN
    lnM := line.new(bar_index, fit, x2, fit + slope * projN, color = color.new(railCol, 20), style = line.style_dotted, width = 1)
    lnU := line.new(bar_index, railU, x2, fit + slope * projN + sdR * multR, color = color.new(neutCol, 55), style = line.style_dotted)
    lnD := line.new(bar_index, railD, x2, fit + slope * projN - sdR * multR, color = color.new(neutCol, 55), style = line.style_dotted)
 
// Right-edge live fit-quality tag
var label tag = na
if barstate.islast
    label.delete(tag)
    tag := label.new(bar_index + projN + 2, fit + slope * projN, "SLR " + str.tostring(fitQ * 100, "0") + "%", style = label.style_label_left, color = color.new(railCol, 20), textcolor = chart.bg_color, size = size.small)
 
// ── Dashboard v2 ──────────────────────────────────────────────────────────
driftN = slope / math.max(ta.atr(14), syminfo.mintick)
driftTxt = driftN > 0.05 ? "UP" : driftN < -0.05 ? "DOWN" : "FLAT"
driftCol = driftN > 0.05 ? bullCol : driftN < -0.05 ? 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 · SLOPE RAILS", text_color = chart.fg_color, text_size = size.tiny, bgcolor = color.new(chart.fg_color, 85))
    table.cell(dash, 0, 1, "Drift",  text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 1, driftTxt, text_color = chart.bg_color, bgcolor = color.new(driftCol, 15), text_size = size.small)
    table.cell(dash, 0, 2, "Rate",   text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 2, str.tostring(driftN, "0.00") + " ATR/bar", text_color = driftCol, text_size = size.small, text_halign = text.align_right)
    table.cell(dash, 0, 3, "Fit quality", text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 3, str.tostring(fitQ * 100, "0") + "%", text_color = fitQ > 0.6 ? driftCol : neutCol, text_size = size.small, text_halign = text.align_right)
 
// ── Alerts ────────────────────────────────────────────────────────────────
slopeFlipUp = driftN > 0.05 and driftN[1] <= 0.05
slopeFlipDn = driftN < -0.05 and driftN[1] >= -0.05
alertcondition(slopeFlipUp, "Merry SLR — drift turns up",   "Merry Slope Rails: fitted drift turned up beyond 0.05 ATR/bar.")
alertcondition(slopeFlipDn, "Merry SLR — drift turns down", "Merry Slope Rails: fitted drift turned down beyond −0.05 ATR/bar.")
alertcondition(stretched and not stretched[1], "Merry SLR — stretched", "Merry Slope Rails: price stretched beyond the 90th deviation percentile of its fit.")

← 返回列表