Code Library / MACD
Pine Script MACD Crossover
MACD crossover strategy with zero-line filter.
Language: Pine Script · Category: MACD · Tags: MACD, crossover, Pine Script, strategy, zero line
Source code
//@version=5
strategy("MACD Cross",overlay=false)
[m,s,h]=ta.macd(close,12,26,9)
if ta.crossover(m,s) and m<0
strategy.entry("Long",strategy.long)
if ta.crossunder(m,s) and m>0
strategy.entry("Short",strategy.short)
plot(m,"MACD",color.blue,2); plot(s,"Signal",color.orange)
plot(h,"Hist",h>=0?color.green:color.red,1,plot.style_histogram)How to use this snippet
This is a building block, not a finished trading system. Paste it into your own Pine Script project, compile it, and test it on a demo account with your broker's real spreads before relying on it. You can convert it to MQL4 or MQL5 with the Pine Script converter.