Momentum indicator
MACD Calculator
Compute the MACD line and histogram for one point from your two moving averages and the signal line. The concept behind it, the relationship between two EMAs, matters more than any single reading.
What the MACD is
MACD, short for Moving Average Convergence Divergence, was created by Gerald Appel in the late 1970s. It turns the relationship between two exponential moving averages into a single momentum measure. The MACD line is simply the fast EMA minus the slow EMA, by default the 12-period EMA minus the 26-period EMA.
When the fast average pulls away above the slow one, the MACD line rises; when they converge, it falls toward zero; when the fast average drops below the slow one, MACD turns negative. To smooth the line and highlight turns, a signal line is calculated as the EMA of the MACD line, usually over 9 periods. The histogram is the difference between the MACD line and the signal line: it grows as the two separate and shrinks toward zero as they come together, giving a quick visual read on whether momentum is expanding or fading.
Why the MACD is used
MACD is used to study momentum and the interaction between short- and long-term trends in one view. Analysts commonly watch for the MACD line crossing its signal line, the line crossing the zero level, and divergences where price and MACD disagree. The histogram is often read as an early hint that a crossover may be approaching.
The 12/26/9 defaults are conventional, not required, and many people tune them to the market and timeframe they follow. How you interpret any of these signals is entirely up to you.
What are TradingView and Pine Script?
TradingView is one of the most widely used charting and market-analysis platforms, where traders and analysts study price movement across stocks, crypto, forex, and futures on interactive charts. Pine Script is TradingView's own lightweight programming language, created so anyone can build custom tools that run directly on those charts.
People use Pine Script to build four main kinds of tools. Indicators calculate and plot values on the chart, exactly like the calculation above, but recomputed automatically on every bar. Strategies add explicit entry and exit rules and can be backtested against historical data in TradingView's Strategy Tester to see how they would have behaved. Screeners scan many symbols at once for conditions you define. Alerts notify you the moment a condition you specified occurs, so you do not have to watch the screen.
The value is precision and automation. Instead of eyeballing a chart, you describe exactly what you want measured, visualized, or notified about, and TradingView runs it consistently across any market and timeframe. That is why traders, analysts, and developers write Pine Script: it turns a manual charting idea into a repeatable tool. These tools are for tracking, visualizing, and testing market ideas; they do not tell you what to trade, and that decision always remains yours.
Writing that code by hand means learning Pine Script's syntax, its type system, and the exact names of hundreds of built-in functions. It is a real programming language, and small mistakes stop a script from compiling in the Pine Editor.
Turn this into Pine Script
This tool returns MACD and the histogram at a single point. On a chart, both update every bar along with the signal line. Pine Script hands you the entire thing through ta.macd(close, 12, 26, 9), which returns all three series at once, but building the crossover logic, colored histogram, and alerts around it is the real work.
//@version=6
indicator("MACD", overlay=false)
[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)
plot(macdLine, "MACD", color=color.blue)
plot(signalLine, "Signal", color=color.orange)
plot(histLine, "Histogram", color=color.gray, style=plot.style_histogram)PineScripter is an AI built specifically for Pine Script. You describe what you want in plain English and it writes TradingView-ready v5 or v6 code. Because it is specialized on the Pine Script language and its exact function signatures, it tends to produce code that compiles far more reliably than general-purpose models like ChatGPT, which often invent functions that do not exist in Pine Script.
PineScripter is an AI developer tool that helps you write Pine Script code. It is not a financial advisor and will never offer financial, investment, or trading advice. Everything on this page, including the calculator and the explanations, is provided purely for educational and informational purposes. Any decision about how to interpret an indicator or trade a market is entirely your own. See our full disclaimer for more.