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
// 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)
| Timeframe | Sealed period | Trades | Return | Buy & hold | Verdict |
|---|---|---|---|---|---|
| 1d | 2024-12-17 → 2026-10-16 | 29 | -16.10 % | -23.25 % | No evidence of an edge |
| 4h | 2026-03-12 → 2026-10-17 | 50 | 4.59 % | 15.83 % | Inconclusive |
| 1h | 2026-08-05 → 2026-10-17 | 65 | 6.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
| 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: survives, no evidence of an edge 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.