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
// MACD 12/26/9 : long tant que la ligne MACD est au-dessus de sa signal, plat sinon.
// Le croisement haussier ouvre, le croisement baissier ferme : ces etats en sont la lecture.
strategy("MACD Momentum", overlay=true, pyramiding=0, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1, process_orders_on_close=true, margin_long=0)
[macdLine, signalLine, hist] = ta.macd(close, 12, 26, 9)
longSignal = macdLine > signalLine
exitSignal = macdLine < signalLine
if longSignal
    strategy.entry("Long", strategy.long)
if exitSignal
    strategy.close("Long")

Judge this code on BTCUSDT 1D →Free, in your browser, on the same candles as the table below.

Online judge verdict on BTCUSDT (measured 29 November 2026)

TimeframeSealed periodTradesReturnBuy & holdVerdict
1d2024-12-17 → 2026-10-1629-16.10 %-23.25 %No evidence of an edge
4h2026-03-12 → 2026-10-17504.59 %15.83 %Inconclusive
1h2026-08-05 → 2026-10-17656.57 %27.14 %No evidence of an edge

Past data, 10 bps per fill, last 20% of the bars sealed. Windows: full history on 1d, last 3 years on 4h, last year on 1h. Research, not advice: no result is promised.

Compared with a real TradingView run on 31 October 2026 (BINANCE:BTCUSDT, this code): identical trades 118/120 on 1D, 231/232 on 4h, same prices on all matching trades.

Measured on 29 November 2026 with the online judge’s Pine v5 engine (sa-pine-vm 0.2.1), on Binance BTCUSDT candles up to 16/10/2026 on 1D and up to 17/10/2026 on 4h and 1h. Differences with TradingView’s Strategy Tester remain possible. Binance data, CC BY-NC-SA 4.0.

Open this code in the free judge (1 click) →

Replay each verdict yourself, in your browser: 1d · 4h · 1h. The script is filled in; nothing runs until you click “Judge”.

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: survives, no evidence of an edge 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 ComboFaber 10-Month TimingGolden Cross SMA 50/200Bollinger Squeeze BreakoutKeltner Channel Breakout 20/1.5Donchian Turtle 20/10ADX 25 & DMI 14 TrendParabolic SAR 0.02/0.2Hull Moving Average 55ALMA 20 Gaussian TrendCCI 20 (+100) BreakoutMoney Flow Index MFI 14 (20/80)ATR 14 Chandelier StopIchimoku Tenkan 9 / Kijun 26Linear Regression 20ROC 14 + EMA 200 FilterWilliams %R 14 PullbackNo-Leak Weekly EMA FilterPivot High/Low BreakoutAI-style ATR Breakout + EMA 200
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.