Skip to main content

MACD Momentum — Pine Script v5 strategy

MOMENTUMMACDPine Script v5

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

ParameterValueRole
Fast length12Second argument of ta.macd, the fast EMA length.
Slow length26Third argument of ta.macd, the slow EMA length.
Signal length9Fourth argument of ta.macd, the length used for the signal line.
SourceclosePrice series passed to ta.macd.
OverlaytrueSet in the strategy declaration, so plots go on the price chart.
Strategy nameMACD MomentumFirst argument of the strategy declaration.
Entry identifierLongLabel given to the position opened by strategy.entry.
Entry directionstrategy.longDirection argument of strategy.entry.
Exit identifierLongPosition closed by strategy.close.
Histogram serieshistThird return of ta.macd, assigned but never used.

Known limits of this rule

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.

Get the free Lab →

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

RSI Mean ReversionEMA Crossover 9/21EMA Ribbon 8/21/55Bollinger Band BounceSuperTrendStochastic RSIATR BreakoutVWAP BounceRSI + EMA Combo
TradingView Starter
Choose the account path. Prove the strategy separately.
Your first chart and price alert do not require the Lab. Strategy Arena proof remains required before trusting or exporting a strategy.
Partner attribution is for first-time users and web purchases only. Strategy Arena may earn a commission on eligible purchases; starting with a free account needs no payment.
I have never had an account · Start free I already have an account Check a strategy
More TradingView paths
Evidence-gated Pine review

Educational code on past data. No investment advice; no result is promised.