PineScript16 MACD Zero Line Crossover

voonology · study · 207 行 · 点赞 11 · TradingView 原页

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

Pine Script

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Voonology
// PineScript16 — Developed by Voonology
// Version: 1.0.0
 //@version=6
indicator("MACD Zero Line Crossover", shorttitle="MACD 0 Crossover", overlay=false, format=format.price, precision=2)
 
// ─────────────────────────────────────────────────────
// 01. INPUTS
// ─────────────────────────────────────────────────────
 
// MACD Settings
groupMACD = "01. MACD Settings"
 
fastLen   = input.int(12, "Fast Length", minval=1, group=groupMACD)
slowLen   = input.int(26, "Slow Length", minval=1, group=groupMACD)
signalLen = input.int(9, "Signal Length", minval=1, group=groupMACD)
 
// EMA Settings
groupEMA = "02. EMA Filter"
 
emaLen  = input.int(20, "EMA Length", minval=1, group=groupEMA)
showEMA = input.bool(true, "Show EMA", group=groupEMA)
 
// Signal Settings
groupSignal = "03. Signal Settings"
 
confirmClose = input.bool(true, "Confirm On Bar Close", group=groupSignal)
useEMAFilter = input.bool(true, "Use EMA Filter", group=groupSignal)
showSignals = input.bool(true, "Show BUY/SELL On Chart", group=groupSignal)
showPanelSignals = input.bool(false, "Show BUY/SELL In MACD Panel", group=groupSignal)
 
// Visual Settings
groupVisual = "04. MACD Visuals"
 
showMACD = input.bool(true, "Show MACD Histogram", group=groupVisual)
showSignal = input.bool(true, "Show Signal Line", group=groupVisual)
showHistogram = input.bool(false, "Show Traditional Histogram", group=groupVisual)
 
// ─────────────────────────────────────────────────────
// 02. CALCULATIONS
// ─────────────────────────────────────────────────────
 
[macdLine, signalLine, histLine] = ta.macd(
     close,
     fastLen,
     slowLen,
     signalLen
     )
 
emaFilter = ta.ema(close, emaLen)
 
// ─────────────────────────────────────────────────────
// 03. SIGNAL CONDITIONS
// ─────────────────────────────────────────────────────
 
// MACD Zero Cross
macdCrossUp = ta.crossover(macdLine, 0)
macdCrossDown = ta.crossunder(macdLine, 0)
 
// EMA Direction Filter
longFilter = not useEMAFilter or close > emaFilter
shortFilter = not useEMAFilter or close < emaFilter
 
// Bar Close Confirmation
barConfirmed = not confirmClose or barstate.isconfirmed
 
// Final Signals
longSignal = macdCrossUp and longFilter and barConfirmed
shortSignal = macdCrossDown and shortFilter and barConfirmed
 
// Raw MACD Signals
rawLongSignal = macdCrossUp and barConfirmed
rawShortSignal = macdCrossDown and barConfirmed
 
// ─────────────────────────────────────────────────────
// 04. MAIN PRICE CHART
// ─────────────────────────────────────────────────────
 
// EMA 20
plot(
     showEMA ? emaFilter : na,
     title="EMA Filter",
     color=color.rgb(255, 152, 0),
     linewidth=1,
     force_overlay=true
     )
 
// BUY Signal
plotshape(
     showSignals and longSignal,
     title="BUY Signal",
     style=shape.triangleup,
     location=location.belowbar,
     color=color.green,
     size=size.tiny,
     force_overlay=true
     )
 
// SELL Signal
plotshape(
     showSignals and shortSignal,
     title="SELL Signal",
     style=shape.triangledown,
     location=location.abovebar,
     color=color.red,
     size=size.tiny,
     force_overlay=true
     )
 
// ─────────────────────────────────────────────────────
// 05. MACD LOWER PANEL
// ─────────────────────────────────────────────────────
 
// MACD Line displayed as Blue Histogram
plot(
     showMACD ? macdLine : na,
     title="MACD Line",
     color=color.rgb(57, 117, 255),
     style=plot.style_histogram,
     linewidth=1
     )
 
// Orange Signal Line
plot(
     showSignal ? signalLine : na,
     title="Signal Line",
     color=color.rgb(255, 152, 0),
     linewidth=1
     )
 
// Optional Traditional MACD Histogram
histColor = histLine >= 0 ? color.green : color.red
 
plot(
     showHistogram ? histLine : na,
     title="Traditional Histogram",
     color=histColor,
     style=plot.style_histogram,
     linewidth=1
     )
 
// MACD Zero Line
hline(
     0,
     title="Zero Line",
     color=color.gray,
     linestyle=hline.style_dashed
     )
 
// ─────────────────────────────────────────────────────
// 06. OPTIONAL PANEL SIGNALS
// ─────────────────────────────────────────────────────
 
// Raw MACD BUY
plotshape(
     showPanelSignals and rawLongSignal,
     title="MACD Zero BUY",
     style=shape.triangleup,
     location=location.bottom,
     color=color.green,
     size=size.tiny,
     text="Buy"
     )
 
// Raw MACD SELL
plotshape(
     showPanelSignals and rawShortSignal,
     title="MACD Zero SELL",
     style=shape.triangledown,
     location=location.top,
     color=color.red,
     size=size.tiny,
     text="Sell"
     )
 
// ─────────────────────────────────────────────────────
// 07. ALERT CONDITIONS
// ─────────────────────────────────────────────────────
 
// Filtered BUY
alertcondition(
     longSignal,
     title="MACD EMA BUY",
     message="MACD Zero BUY | {{ticker}} | TF {{interval}} | Close {{close}}"
     )
 
// Filtered SELL
alertcondition(
     shortSignal,
     title="MACD EMA SELL",
     message="MACD Zero SELL | {{ticker}} | TF {{interval}} | Close {{close}}"
     )
 
// Raw MACD BUY
alertcondition(
     rawLongSignal,
     title="MACD Zero BUY (Raw)",
     message="MACD crossed ABOVE zero | {{ticker}} | TF {{interval}} | Close {{close}}"
     )
 
// Raw MACD SELL
alertcondition(
     rawShortSignal,
     title="MACD Zero SELL (Raw)",
     message="MACD crossed BELOW zero | {{ticker}} | TF {{interval}} | Close {{close}}"
     )

← 返回列表