Trend Velocity_Merry

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

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

Pine Script

//@version=6
indicator("Merry | Trend Velocity", "Merry TVC", overlay = false, max_labels_count = 200)
 
// ══════════════════════════════════════════════════════════════════════════
//  Merry — Trend Velocity (TVC)
//
//  Measures how fast the trend moves, not where it points: the drift of
//  a smoothed price line, expressed in ATR units per bar, rendered as a
//  gradient column stream with a glowing velocity core. The pane itself
//  tints with velocity. Acceleration rides as a neutral line, and a
//  speed limit — the 90th percentile of recent |velocity| — flags
//  overheat when velocity is extreme while acceleration rolls over.
//
//  Generic building blocks: EMA drift, ATR normalization, percentile
//  ranking. Original composition/presentation.
//  © Merry. Free to use and modify.
// ══════════════════════════════════════════════════════════════════════════
 
// ── Inputs ────────────────────────────────────────────────────────────────
grpCore = "Core engine"
lenV  = input.int(20, "Velocity source EMA", minval = 2, group = grpCore)
lagN  = input.int(3,  "Drift lag (bars)",    minval = 1, group = grpCore, tooltip = "Bars used to measure the EMA's drift. Higher smooths the velocity reading.")
atrLn = input.int(14, "Velocity normalizer (ATR)", minval = 1, group = grpCore)
lenSp = input.int(100,"Speed-limit window",  minval = 30, group = grpCore)
spPct = input.int(90, "Speed-limit percentile", minval = 50, maxval = 99, 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")
hotCol   = input.color(#f7b32b, "Overheat", group = grpStyle)
showSig  = input.bool(true, "Show overheat badges", group = grpStyle)
showDash = input.bool(true, "Show dashboard", group = grpStyle)
 
// ── Velocity engine ───────────────────────────────────────────────────────
e   = ta.ema(close, lenV)
a   = ta.atr(atrLn)
vel = (e - e[lagN]) / (math.max(a, syminfo.mintick) * lagN)   // ATR units per bar
acc = ta.ema(vel - vel[1], 3)                                  // smoothed acceleration
 
limitV = ta.percentile_nearest_rank(math.abs(vel), lenSp, spPct)
overheat = math.abs(vel) > limitV and (vel > 0 ? acc < 0 : acc > 0)
 
velCol = color.from_gradient(vel, -limitV, limitV, bearCol, bullCol)
 
// ── Rendering ─────────────────────────────────────────────────────────────
bgcolor(color.new(velCol, 94), title = "Velocity tint")
plot(vel, "Velocity", style = plot.style_columns, color = color.new(velCol, 55))
plot(vel, "Velocity glow", color = color.new(velCol, 80), linewidth = 4)
plot(vel, "Velocity core", color = velCol, linewidth = 1)
plot(acc, "Acceleration", color = color.new(neutCol, 30))
plot(limitV,  "Speed limit",  color = color.new(hotCol, 50))
plot(-limitV, "Speed limit −", color = color.new(hotCol, 50))
hline(0, "Zero", color = color.new(neutCol, 60), linestyle = hline.style_dotted)
 
// Price-anchored overheat badges
plotshape(showSig and overheat ? vel : na, "Overheat", shape.labeldown, location.absolute, hotCol, text = "HOT", textcolor = chart.bg_color, size = size.tiny)
 
// ── Dashboard v2 ──────────────────────────────────────────────────────────
stateTxt = overheat ? "OVERHEAT" : acc > 0.01 ? (vel > 0 ? "ACCELERATING UP" : "FALLING FASTER") : acc < -0.01 ? (vel > 0 ? "LOSING STEAM" : "DECELERATING DOWN") : vel > 0 ? "CRUISING UP" : "CRUISING DOWN"
stateCol = overheat ? hotCol : vel > 0 ? bullCol : bearCol
 
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 VELOCITY", text_color = chart.fg_color, text_size = size.tiny, bgcolor = color.new(chart.fg_color, 85))
    table.cell(dash, 0, 1, "Velocity", text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 1, str.tostring(vel, "0.00") + " ATR/bar", text_color = chart.bg_color, bgcolor = color.new(vel > 0 ? bullCol : bearCol, 15), text_size = size.small, text_halign = text.align_right)
    table.cell(dash, 0, 2, "State",   text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 2, stateTxt,  text_color = chart.bg_color, bgcolor = color.new(stateCol, 15), text_size = size.small)
    table.cell(dash, 0, 3, "Limit",   text_color = chart.fg_color, text_size = size.small)
    table.cell(dash, 1, 3, "±" + str.tostring(limitV, "0.00"), text_color = hotCol, text_size = size.small, text_halign = text.align_right)
 
// ── Alerts ────────────────────────────────────────────────────────────────
crossUp = ta.crossover(vel, 0)
crossDn = ta.crossunder(vel, 0)
alertcondition(crossUp, "Merry TVC — velocity turns positive", "Merry Trend Velocity: drift crossed above zero.")
alertcondition(crossDn, "Merry TVC — velocity turns negative", "Merry Trend Velocity: drift crossed below zero.")
alertcondition(overheat and not overheat[1], "Merry TVC — overheat", "Merry Trend Velocity: extreme velocity with acceleration rolling over.")

← 返回列表