本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// ─────────────────────────────────────────────────────────────────────────────
// BTM - MACD V+ v2 – restyled with dashboard
//
// Combines the improvements from the research:
// 1. MACD-V scaling (Alex Spiroglou, 2022): (EMA fast − EMA slow) / ATR × 100,
// so the ±50 / ±150 zones mean the same thing on any market or timeframe.
// 2. Neutral-zone mute: signal crosses between −50 and +50 are ignored
// (the high-whipsaw zone), in the spirit of LazyBear's Impulse MACD.
// 3. Elder Impulse permission filter: no longs on red Elder bars,
// no shorts on green Elder bars.
// 4. 200 EMA regime filter, plus "pullback" buys in a bull regime when
// MACD-V turns up from −50 or lower.
// 5. Optional de-lagged fast EMA only (the slow EMA keeps its lag on purpose).
// 6. Shorts off by default: published backtests found MACD short signals weak.
//
// Signals only print on closed bars, so they don't repaint.
// For research and education only, not financial advice.
// ─────────────────────────────────────────────────────────────────────────────
indicator("BTM - MACD V+", shorttitle = "BTM - MACD V+", overlay = false, precision = 1)
// ── Inputs ───────────────────────────────────────────────────────────────────
grpCore = "Core (MACD-V)"
src = input.source(close, "Source", group = grpCore)
fastLen = input.int(12, "Fast length", minval = 1, group = grpCore)
slowLen = input.int(26, "Slow length", minval = 2, group = grpCore)
sigLen = input.int(9, "Signal length", minval = 1, group = grpCore)
atrLen = input.int(26, "ATR length", minval = 1, group = grpCore, tooltip = "Spiroglou uses the same length as the slow EMA (26).")
zeroLagFast = input.bool(false, "De-lag the fast EMA only", group = grpCore, tooltip = "Uses a zero-lag EMA for the fast line so turns show up a bar or two earlier. The slow line keeps its lag, which is what makes the crossover meaningful. Expect a few more false signals in chop.")
grpZones = "Zones"
neutral = input.float(50, "Neutral zone (±)", minval = 0, group = grpZones, tooltip = "Crosses inside ±this level are ignored.")
extreme = input.float(150, "Extreme zone (±)", minval = 0, group = grpZones, tooltip = "Beyond ±this level momentum is overstretched.")
histExt = input.float(40, "Histogram full-colour level (±)", minval = 1, group = grpZones, tooltip = "Histogram bars fade in as they approach this size (Spiroglou's MACD-VH extreme is ±40).")
grpFilt = "Filters"
useElder = input.bool(true, "Elder Impulse permission filter", group = grpFilt, tooltip = "Blocks long signals on red Elder bars and short signals on green Elder bars.")
elderLen = input.int(13, "Elder EMA length", minval = 1, group = grpFilt)
useRegime = input.bool(true, "Regime filter (price vs long EMA)", group = grpFilt, tooltip = "Momentum longs only above the regime EMA, momentum shorts only below it.")
regimeLen = input.int(200, "Regime EMA length", minval = 1, group = grpFilt)
grpSig = "Signals"
showMom = input.bool(true, "Momentum crosses (outside neutral zone)", group = grpSig)
showPull = input.bool(true, "Pullback crosses (with the regime)", group = grpSig, tooltip = "Long: bull regime and MACD-V crosses up through its signal at −50 or lower. Short is the mirror.")
showExh = input.bool(true, "Exhaustion marks (leaving ±extreme)", group = grpSig)
allowShorts = input.bool(false, "Show short signals", group = grpSig, tooltip = "Off by default because backtests found MACD short signals weak.")
grpVis = "Style"
theme = input.string("Neon", "Theme", options = ["Neon", "Ocean", "Sunset", "Classic"], group = grpVis)
histStyle = input.string("Faded columns", "Histogram", options = ["Faded columns", "Area", "Hidden"], group = grpVis)
showCloud = input.bool(true, "Momentum cloud (MACD-V vs signal)", group = grpVis)
showGlow = input.bool(true, "Line glow", group = grpVis)
showZones = input.bool(true, "Shaded zones", group = grpVis)
showExtBg = input.bool(true, "Highlight overextended momentum", group = grpVis)
colorBars = input.bool(true, "Colour price bars (Elder Impulse)", group = grpVis)
grpDash = "Dashboard"
showTable = input.bool(true, "Show dashboard", group = grpDash)
tablePos = input.string("Top right", "Position", options = ["Top right", "Bottom right", "Top left", "Bottom left", "Middle right"], group = grpDash)
tableSize = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal", "Large"], group = grpDash)
showHTF = input.bool(true, "Show higher-timeframe MACD-V", group = grpDash)
htfTf = input.timeframe("D", "Higher timeframe", group = grpDash, tooltip = "Uses the last closed higher-timeframe bar, so it does not repaint.")
// ── Theme palette ────────────────────────────────────────────────────────────
[cBull, cBear, cExt, cNeu, cSig] = switch theme
"Ocean" => [#00b8d4, #ff4081, #ffd740, #607d8b, chart.fg_color]
"Sunset" => [#ffab40, #7c4dff, #ff5252, #8d6e63, chart.fg_color]
"Classic" => [#26a69a, #ef5350, #ff9800, #9e9e9e, #2962ff]
=> [#00e676, #ff1744, #ffea00, #78909c, #d500f9]
// ── Helpers ──────────────────────────────────────────────────────────────────
zlema(series float s, simple int len) =>
lag = math.max(1, math.floor((len - 1) / 2.0))
ta.ema(s + (s - s[lag]), len)
macdvCalc() =>
f = ta.ema(src, fastLen)
s = ta.ema(src, slowLen)
a = ta.atr(atrLen)
a > 0 ? (f - s) / a * 100 : na
zoneName(float v) =>
na(v) ? "—" : v > extreme ? "Overextended ▲" : v > neutral ? "Strong bull" : v >= -neutral ? "Neutral / range" : v >= -extreme ? "Strong bear" : "Overextended ▼"
zoneColor(float v) =>
na(v) ? cNeu : math.abs(v) > extreme ? cExt : v > neutral ? cBull : v < -neutral ? cBear : cNeu
// Text gauge from −200 to +200, centred on zero
gauge(float v) =>
n = na(v) ? 0 : math.round(math.max(-200, math.min(200, v)) / 20)
left = n < 0 ? str.repeat("░", 10 + n) + str.repeat("█", -n) : str.repeat("░", 10)
right = n > 0 ? str.repeat("█", n) + str.repeat("░", 10 - n) : str.repeat("░", 10)
left + "┃" + right
// ── Calculation ──────────────────────────────────────────────────────────────
emaFast = ta.ema(src, fastLen)
zlFast = zlema(src, fastLen)
fastMA = zeroLagFast ? zlFast : emaFast
slowMA = ta.ema(src, slowLen)
atrVal = ta.atr(atrLen)
macdv = atrVal > 0 ? (fastMA - slowMA) / atrVal * 100 : na
signal = ta.ema(macdv, sigLen)
hist = macdv - signal
// Higher timeframe MACD-V (last closed bar, non-repainting)
htfMacdv = request.security(syminfo.tickerid, htfTf, macdvCalc()[1], lookahead = barmerge.lookahead_on)
// Elder Impulse states
elderEma = ta.ema(close, elderLen)
elderGreen = elderEma > elderEma[1] and hist > hist[1]
elderRed = elderEma < elderEma[1] and hist < hist[1]
// Regime
regimeEma = ta.ema(close, regimeLen)
bullRegime = close > regimeEma
bearRegime = close < regimeEma
regimeDist = (close - regimeEma) / regimeEma * 100
// Permissions
elderLongOK = not useElder or not elderRed
elderShortOK = not useElder or not elderGreen
longOK = elderLongOK and (not useRegime or bullRegime)
shortOK = elderShortOK and (not useRegime or bearRegime)
// Crosses (computed every bar)
crossUp = ta.crossover(macdv, signal)
crossDn = ta.crossunder(macdv, signal)
leaveTop = ta.crossunder(macdv, extreme)
leaveBottom = ta.crossover(macdv, -extreme)
confirmed = barstate.isconfirmed
// Momentum: cross in the strong band, not yet overextended
momLong = showMom and crossUp and macdv > neutral and macdv < extreme and longOK and confirmed
momShort = allowShorts and showMom and crossDn and macdv < -neutral and macdv > -extreme and shortOK and confirmed
// Pullback: turn back with the regime from the opposite strong/extreme band
pullLong = showPull and crossUp and macdv <= -neutral and bullRegime and elderLongOK and confirmed
pullShort = allowShorts and showPull and crossDn and macdv >= neutral and bearRegime and elderShortOK and confirmed
// Exhaustion: momentum rolling out of the extreme zone
exhTop = showExh and leaveTop and confirmed
exhBottom = showExh and leaveBottom and confirmed
// Remember the most recent signal for the dashboard
var string lastSig = "None yet"
var int lastSigBar = na
var color lastSigCol = cNeu
if momLong
lastSig := "MOM long ▲"
lastSigBar := bar_index
lastSigCol := cBull
else if pullLong
lastSig := "Pullback long ▲"
lastSigBar := bar_index
lastSigCol := cBull
else if momShort
lastSig := "MOM short ▼"
lastSigBar := bar_index
lastSigCol := cBear
else if pullShort
lastSig := "Pullback short ▼"
lastSigBar := bar_index
lastSigCol := cBear
else if exhTop
lastSig := "Exhaustion top ◆"
lastSigBar := bar_index
lastSigCol := cExt
else if exhBottom
lastSig := "Exhaustion bottom ◆"
lastSigBar := bar_index
lastSigCol := cExt
// ── Colours ──────────────────────────────────────────────────────────────────
// MACD-V line fades from neutral grey into bull/bear colour as momentum builds,
// and switches to the "extreme" colour beyond ±extreme.
lineCol = math.abs(macdv) > extreme ? cExt : macdv >= 0 ? color.from_gradient(macdv, 0, extreme, cNeu, cBull) : color.from_gradient(macdv, -extreme, 0, cBear, cNeu)
// Histogram fades in with size
histMag = math.min(math.abs(hist) / histExt, 1.0)
histCol = color.new(hist >= 0 ? cBull : cBear, 85 - histMag * 55)
// ── Zones ────────────────────────────────────────────────────────────────────
pUpExt = plot(extreme, "+Extreme level", color.new(cExt, 65), 1)
pUpNeu = plot(neutral, "+Neutral level", color.new(cNeu, 75), 1)
plot(0, "Zero line", color.new(cNeu, 55), 1)
pDnNeu = plot(-neutral, "−Neutral level", color.new(cNeu, 75), 1)
pDnExt = plot(-extreme, "−Extreme level", color.new(cExt, 65), 1)
fill(pUpNeu, pUpExt, showZones ? color.new(cBull, 94) : na, "Bull zone")
fill(pDnNeu, pDnExt, showZones ? color.new(cBear, 94) : na, "Bear zone")
fill(pUpNeu, pDnNeu, showZones ? color.new(cNeu, 95) : na, "Neutral zone")
bgcolor(showExtBg and math.abs(macdv) > extreme ? color.new(cExt, 92) : na, title = "Overextended highlight")
// ── Histogram ────────────────────────────────────────────────────────────────
plot(histStyle == "Faded columns" ? hist : na, "Histogram (columns)", histCol, style = plot.style_columns)
plot(histStyle == "Area" ? hist : na, "Histogram (area)", histCol, style = plot.style_area)
// ── Lines, glow and cloud ────────────────────────────────────────────────────
plot(showGlow ? macdv : na, "MACD-V glow", color.new(lineCol, 82), 7)
pMain = plot(macdv, "MACD-V", lineCol, 2)
pSig = plot(signal, "Signal", color.new(cSig, 25), 1)
fill(pMain, pSig, showCloud ? (macdv >= signal ? color.new(cBull, 78) : color.new(cBear, 78)) : na, "Momentum cloud")
// ── Signals (drawn on the MACD-V line) ───────────────────────────────────────
plotshape(momLong ? macdv : na, "Momentum long", shape.labelup, location.absolute, cBull, text = "MOM", textcolor = color.black, size = size.tiny)
plotshape(pullLong ? macdv : na, "Pullback long", shape.labelup, location.absolute, color.new(cBull, 35), text = "PB", textcolor = color.black, size = size.tiny)
plotshape(momShort ? macdv : na, "Momentum short", shape.labeldown, location.absolute, cBear, text = "MOM", textcolor = color.white, size = size.tiny)
plotshape(pullShort ? macdv : na, "Pullback short", shape.labeldown, location.absolute, color.new(cBear, 35), text = "PB", textcolor = color.white, size = size.tiny)
plotshape(exhTop ? macdv : na, "Exhaustion (top)", shape.diamond, location.absolute, cExt, size = size.tiny)
plotshape(exhBottom ? macdv : na, "Exhaustion (bottom)", shape.diamond, location.absolute, cExt, size = size.tiny)
// ── Price bar colours ────────────────────────────────────────────────────────
barcolor(colorBars ? (elderGreen ? cBull : elderRed ? cBear : cNeu) : na, title = "Elder bar colour")
// ── Dashboard ────────────────────────────────────────────────────────────────
pos = switch tablePos
"Bottom right" => position.bottom_right
"Top left" => position.top_left
"Bottom left" => position.bottom_left
"Middle right" => position.middle_right
=> position.top_right
sz = switch tableSize
"Tiny" => size.tiny
"Normal" => size.normal
"Large" => size.large
=> size.small
var table dash = table.new(pos, 2, 10, frame_color = color.new(cNeu, 60), frame_width = 1, border_color = color.new(cNeu, 85), border_width = 1)
row(int r, string label, string value, color valueCol) =>
bg = r % 2 == 0 ? color.new(cNeu, 88) : color.new(chart.bg_color, 0)
table.cell(dash, 0, r, label, text_color = color.new(chart.fg_color, 40), text_size = sz, bgcolor = bg, text_halign = text.align_left)
table.cell(dash, 1, r, value, text_color = valueCol, text_size = sz, bgcolor = bg, text_halign = text.align_right)
if showTable and barstate.islast
zCol = zoneColor(macdv)
// Header, coloured by the current momentum state
table.cell(dash, 0, 0, "BTM - MACD V+ " + zoneName(macdv), text_color = chart.bg_color, text_size = sz, bgcolor = zCol, text_halign = text.align_center)
table.merge_cells(dash, 0, 0, 1, 0)
// Gauge
table.cell(dash, 0, 1, gauge(macdv), text_color = zCol, text_size = sz, bgcolor = color.new(chart.bg_color, 0), text_halign = text.align_center, text_font_family = font.family_monospace)
table.merge_cells(dash, 0, 1, 1, 1)
momTxt = hist > hist[1] ? "Accelerating ↑" : "Fading ↓"
momCol = hist > hist[1] ? cBull : cBear
impulseTxt = elderGreen ? "Green · longs only" : elderRed ? "Red · shorts only" : "Neutral · either"
impulseCol = elderGreen ? cBull : elderRed ? cBear : cNeu
regimeTxt = (bullRegime ? "Bull " : "Bear ") + (regimeDist >= 0 ? "+" : "") + str.tostring(regimeDist, "#.0") + "% vs EMA " + str.tostring(regimeLen)
barsAgo = na(lastSigBar) ? "" : " · " + str.tostring(bar_index - lastSigBar) + " bars ago"
row(2, "MACD-V", str.tostring(macdv, "#.0") + " (signal " + str.tostring(signal, "#.0") + ")", zCol)
row(3, "Histogram", (hist >= 0 ? "+" : "") + str.tostring(hist, "#.0"), hist >= 0 ? cBull : cBear)
row(4, "Momentum", momTxt, momCol)
row(5, "Regime", regimeTxt, bullRegime ? cBull : cBear)
row(6, "Impulse", impulseTxt, impulseCol)
if showHTF
row(7, "MACD-V " + htfTf, str.tostring(htfMacdv, "#.0") + " · " + zoneName(htfMacdv), zoneColor(htfMacdv))
row(8, "Last signal", lastSig + barsAgo, lastSigCol)
row(9, "Shorts", allowShorts ? "On" : "Off", color.new(chart.fg_color, 40))
// ── Alerts (set to "Once per bar close") ─────────────────────────────────────
alertcondition(momLong, "BTM - MACD V+ momentum long", "BTM - MACD V+ momentum long on {{ticker}} ({{interval}})")
alertcondition(pullLong, "BTM - MACD V+ pullback long", "BTM - MACD V+ pullback long on {{ticker}} ({{interval}})")
alertcondition(momShort, "BTM - MACD V+ momentum short", "BTM - MACD V+ momentum short on {{ticker}} ({{interval}})")
alertcondition(pullShort, "BTM - MACD V+ pullback short", "BTM - MACD V+ pullback short on {{ticker}} ({{interval}})")
alertcondition(exhTop, "BTM - MACD V+ exhaustion top", "BTM - MACD V+ leaving overbought on {{ticker}} ({{interval}})")
alertcondition(exhBottom, "BTM - MACD V+ exhaustion bottom", "BTM - MACD V+ leaving oversold on {{ticker}} ({{interval}})")