The ta.macd function in Pine Script v5 calculates the entire MACD in one call — MACD line, signal line and histogram — without you having to build three moving averages by hand. The problem is that most tutorials show only half of it: they plot the line and forget to explain what each of the three returned values means, and that is exactly where wrong signals are born. Here you get the correct syntax, what each value returns and a ready-to-paste indicator for TradingView, with crossovers and a colored histogram. Trading is a high-risk activity; use this as a study tool, not as a promise of profit.
An indicator is only half of the journey — executing on emotion blows up the account. See how to turn MACD signals into automated execution, and always test on a demo account.
See webhook automation →The function syntax
In Pine Script v5, this is the signature:
It takes four arguments and returns three values at once (a tuple):
source — the price series used in the calculation (almost always close).
fastLen — the period of the fast average (classic default: 12).
slowLen — the period of the slow average (default: 26).
signalLen — the period of the signal average applied to the MACD (default: 9).
macdLine — the difference between the fast and the slow EMA.
signalLine — the EMA of the MACD itself (the crossover trigger).
histLine — the histogram: macdLine - signalLine.
ta.macd returns the three values in a fixed order. If you swap signalLine and histLine when assigning the tuple, the crossover comes out reversed and you trade backwards without noticing. Respect the order: MACD, signal, histogram.
Detecting the crossovers (signals)
The classic MACD signal is not the value itself, but the crossover between the MACD line and the signal line. Use ta.crossover and ta.crossunder:
Many traders add a filter: they only take the buy when the crossover happens below the zero line (more room for the trend to run) and the sell when it happens above it. This reduces signals in a sideways market.
Complete indicator (ready to paste)
Paste this code into TradingView’s Pine Editor (v5). It plots the two lines and the colored histogram, and marks the crossovers:
Common mistakes
Swapping the tuple order: the sequence is MACD, signal, histogram. Reversing it breaks the signals silently.
Trading every crossover: in a sideways market the MACD crosses dozens of times. Combine it with a trend filter (e.g. a 200-period moving average).
Confusing the histogram with the signal: the histogram measures the distance between the lines; the classic trigger is the crossover, not the histogram peak.
Expecting prediction: the MACD is a lagging indicator, derived from moving averages. It describes what has already happened; it does not predict tops and bottoms.
FAQ
Does ta.macd exist in Pine v4?
The built-in function that returns the three series belongs to v5. In v4 it was common to calculate the MACD by hand with ta.ema. Migrate to v5.
Can I use a source other than close?
Yes. You can pass hlc3, open or any series as the source, but the established standard is close.
Can the signals be automated?
Yes: alertcondition creates alerts that can fire webhooks. From there, an external Python script can execute the order — always tested on a demo account first.
Does the MACD work for binary options?
It is only a momentum indicator. On very short expiries, noise dominates and the signals lose reliability. Treat it as a context filter, never as a standalone trigger.
Disclaimer: binary options and trading in general are very high-risk activities and most retail traders lose money. This content is educational and technical; it does not constitute an investment recommendation, an offer or financial advice. Indicators do not predict the future and past results do not guarantee future results. Always test on a demo account before risking real capital, and never invest more than you can afford to lose.
