MACD with Right Shift Offset

salva1111 · study · 44 行 · 点赞 6 · TradingView 原页

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

Pine Script

//@version=6
indicator("MACD with Right Shift Offset", shorttitle="MACD Shift", overlay=false)
 
// ── Inputs ──
fastLength = input.int(12, "Fast Length", minval=1)
slowLength = input.int(26, "Slow Length", minval=1)
signalLength = input.int(9, "Signal Smoothing", minval=1)
 
shiftBars = input.int(0, "Right Shift Offset (bars)", minval=0, maxval=500)
 
// ── MACD Calculation ──
fastMA = ta.ema(close, fastLength)
slowMA = ta.ema(close, slowLength)
 
macdLine = fastMA - slowMA
signalLine = ta.ema(macdLine, signalLength)
histogram = macdLine - signalLine
 
// ── Histogram Colors ──
histColor = histogram >= 0 ?
     (histogram > histogram[1] ? color.lime : color.green) :
     (histogram < histogram[1] ? color.red : color.maroon)
 
// ── Plots with Right Shift ──
hline(0, "Zero Line", color=color.gray)
 
plot(histogram, title="Histogram", style=plot.style_columns,
     color=histColor, offset=shiftBars)
 
plot(macdLine, title="MACD Line", color=color.blue,
     linewidth=2, offset=shiftBars)
 
plot(signalLine, title="Signal Line", color=color.orange,
     linewidth=2, offset=shiftBars)
 
// ── Alerts: Based on Actual, Unshifted Values ──
bullCross = ta.crossover(macdLine, signalLine)
bearCross = ta.crossunder(macdLine, signalLine)
 
alertcondition(bullCross, title="Bullish MACD Cross",
     message="MACD crossed above Signal Line")
 
alertcondition(bearCross, title="Bearish MACD Cross",
     message="MACD crossed below Signal Line")

← 返回列表