MACD Momentum — Pine Script v5 strategy
The script computes a MACD on the closing price and uses its two lines as a crossover trigger. It opens one long position on a bullish crossover and closes that position on the next bearish crossover.
Full code (free to copy)
//@version=5
strategy("MACD Momentum", overlay=true)
[macdLine, signalLine, hist] = ta.macd(close, 12, 26, 9)
if (ta.crossover(macdLine, signalLine))
strategy.entry("Long", strategy.long)
if (ta.crossunder(macdLine, signalLine))
strategy.close("Long")How the script works
The first line declares a strategy named MACD Momentum with overlay set to true, so the script is drawn on the price chart instead of a separate pane. The next line calls ta.macd with the close price and the lengths 12, 26 and 9, which returns three series: the MACD line, the signal line and the histogram.
Only the two lines are used afterwards; the histogram value is assigned but never read. The entry rule tests ta.crossover of the MACD line over the signal line. When that condition is true on a bar, strategy.entry is called with the identifier Long and the direction strategy.long, which sends a market long entry order.
The exit rule is the mirror condition: ta.crossunder checks whether the MACD line crosses below the signal line. If it does, strategy.close is called with the same identifier, Long, so the open position is closed. There is no stop loss, take profit or short side in the code.
Parameters in the code
| Parameter | Value | Role |
|---|---|---|
Fast length | 12 | Second argument of ta.macd, the fast EMA length. |
Slow length | 26 | Third argument of ta.macd, the slow EMA length. |
Signal length | 9 | Fourth argument of ta.macd, the length used for the signal line. |
Source | close | Price series passed to ta.macd. |
Overlay | true | Set in the strategy declaration, so plots go on the price chart. |
Strategy name | MACD Momentum | First argument of the strategy declaration. |
Entry identifier | Long | Label given to the position opened by strategy.entry. |
Entry direction | strategy.long | Direction argument of strategy.entry. |
Exit identifier | Long | Position closed by strategy.close. |
Histogram series | hist | Third return of ta.macd, assigned but never used. |
Known limits of this rule
- MACD is built from moving averages, so its crossovers arrive after the move they describe and the entry can come late in a swing.
- In a ranging market the two lines cross repeatedly and the rule can open and close positions on noise rather than on a trend.
- The script sets no commission, slippage or order size, so the strategy tester applies its defaults and does not model real trading costs.
Before you trust it: test it on data it never saw
A backtest in TradingView's Strategy Tester shows one past path with the settings you picked. Strategy Arena Lab (free, Windows and Linux) replays this Pine on your PC, compares its trades one by one with TradingView's for supported Pine, counts the variants you try, then judges the rule on a period kept sealed during the search: holds, mirage or inconclusive.
FAQ
Does it work on crypto?
The code only reads the closing price and calls ta.macd, so it can run on any symbol offered by the platform, crypto pairs included. You can replay it on a period you have never seen in Strategy Arena Lab, which is free.
How do I add a stop loss?
Add a strategy.exit call with a stop price and the same Long identifier, or pass a stop argument to strategy.entry. The current code contains neither.
Other free Pine Script v5 strategies
More TradingView paths
Educational code on past data. No investment advice; no result is promised.