本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Ultimate MACD Momentum Panel v2 — Separated Arrows", shorttitle="Ultimate MACD v2", overlay=false, precision=5)
// MACD calculations (always use the active chart timeframe).
groupMACD = "MACD Settings"
fastLen = input.int(12, "Fast EMA Length", minval=1, group=groupMACD)
slowLen = input.int(26, "Slow EMA Length", minval=2, group=groupMACD)
signalLen = input.int(9, "Signal Smoothing", minval=1, group=groupMACD)
// Visual styling.
groupStyle = "Colors & Lines"
macdColor = input.color(color.lime, "MACD Line Color", group=groupStyle)
signalColor = input.color(color.red, "Signal Line Color", group=groupStyle)
positiveColor = input.color(color.lime, "Positive Histogram Color", group=groupStyle)
negativeColor = input.color(color.red, "Negative Histogram Color", group=groupStyle)
zeroColor = input.color(color.white, "Zero Line Color", group=groupStyle)
macdWidth = input.int(2, "MACD Line Width", minval=1, maxval=5, group=groupStyle)
signalWidth = input.int(2, "Signal Line Width", minval=1, maxval=5, group=groupStyle)
showHistogram = input.bool(true, "Show Histogram", group=groupStyle)
showMacd = input.bool(true, "Show MACD Line", group=groupStyle)
showSignal = input.bool(true, "Show Signal Line", group=groupStyle)
// Markers sit outside a rolling envelope around MACD, signal, and histogram.
groupArrows = "Crossover Arrows"
showArrows = input.bool(true, "Show Crossover Arrows", group=groupArrows)
arrowColorUp = input.color(color.lime, "Bullish Arrow Color", group=groupArrows)
arrowColorDown = input.color(color.red, "Bearish Arrow Color", group=groupArrows)
arrowSize = input.string("Small", "Arrow Size", options=["Tiny", "Small", "Normal"], group=groupArrows)
envelopeLookback = input.int(100, "Arrow Placement Lookback (Bars)", minval=10, maxval=500, group=groupArrows)
arrowGapPct = input.float(18.0, "Arrow Distance (% of MACD Range)", minval=2.0, maxval=100.0, step=2.0, group=groupArrows)
[macdLine, signalLine, histogram] = ta.macd(close, fastLen, slowLen, signalLen)
hline(0, "Zero Line", color=zeroColor, linestyle=hline.style_dashed, linewidth=1)
plot(showHistogram ? histogram : na, title="MACD Histogram", style=plot.style_columns, color=histogram >= 0 ? positiveColor : negativeColor)
plot(showMacd ? macdLine : na, title="MACD Line", color=macdColor, linewidth=macdWidth)
plot(showSignal ? signalLine : na, title="Signal Line", color=signalColor, linewidth=signalWidth)
// Include both histogram and oscillator lines when measuring visual bounds.
upperValue = math.max(math.max(macdLine, signalLine), math.max(histogram, 0.0))
lowerValue = math.min(math.min(macdLine, signalLine), math.min(histogram, 0.0))
upperBand = ta.highest(upperValue, envelopeLookback)
lowerBand = ta.lowest(lowerValue, envelopeLookback)
bandRange = math.max(upperBand - lowerBand, 0.000001)
padding = bandRange * arrowGapPct / 100.0
upArrowY = lowerBand - padding
downArrowY = upperBand + padding
bullCross = ta.crossover(macdLine, signalLine) and barstate.isconfirmed
bearCross = ta.crossunder(macdLine, signalLine) and barstate.isconfirmed
// Plotshape sizes must be compile-time constants, so use one plot per size.
plotshape(showArrows and arrowSize == "Tiny" and bullCross ? upArrowY : na, title="Bullish Arrow Tiny", style=shape.arrowup, location=location.absolute, color=arrowColorUp, size=size.tiny)
plotshape(showArrows and arrowSize == "Small" and bullCross ? upArrowY : na, title="Bullish Arrow Small", style=shape.arrowup, location=location.absolute, color=arrowColorUp, size=size.small)
plotshape(showArrows and arrowSize == "Normal" and bullCross ? upArrowY : na, title="Bullish Arrow Normal", style=shape.arrowup, location=location.absolute, color=arrowColorUp, size=size.normal)
plotshape(showArrows and arrowSize == "Tiny" and bearCross ? downArrowY : na, title="Bearish Arrow Tiny", style=shape.arrowdown, location=location.absolute, color=arrowColorDown, size=size.tiny)
plotshape(showArrows and arrowSize == "Small" and bearCross ? downArrowY : na, title="Bearish Arrow Small", style=shape.arrowdown, location=location.absolute, color=arrowColorDown, size=size.small)
plotshape(showArrows and arrowSize == "Normal" and bearCross ? downArrowY : na, title="Bearish Arrow Normal", style=shape.arrowdown, location=location.absolute, color=arrowColorDown, size=size.normal)
alertcondition(bullCross, title="MACD Bullish Crossover", message="MACD crossed ABOVE signal on {{ticker}} ({{interval}}).")
alertcondition(bearCross, title="MACD Bearish Crossover", message="MACD crossed BELOW signal on {{ticker}} ({{interval}}).")