Skip to main content

MACD Indicator Explained: Every Line in ThinkOrSwim (Free Code)

thinkScript tutorialFreeIntermediate10 min video16 lines of codeNov 20, 2022Updated Oct 21, 2026

The MACD indicator explained line by line: how ThinkOrSwim calculates the Value line, the Avg line and the histogram, and which settings change them.

MACD Indicator, a complete breakdown on a SPY chart in ThinkOrSwim
On this page

Overview

In this video I explain the MACD indicator in ThinkOrSwim by opening its code and breaking down each of its 3 plots: the cyan Value line, the yellow Avg line and the histogram. I show how each one is calculated, then prove the Value line by rebuilding it from two exponential moving averages and comparing the numbers candle by candle.

It's for traders who use the MACD for divergences or crossovers and want to know what the lines measure, and for anyone who wants to backtest the MACD and needs to know which settings they can change. It's Part 1 of our MACD series.

Key Takeaways

  • The MACD Value line (the cyan line in ThinkOrSwim) is the 12-period EMA of the close minus the 26-period EMA. Other platforms call it the MACD line.
  • The Avg line (yellow) is a 9-period exponential moving average of the Value line. Other platforms call it the signal line.
  • The histogram, the Diff plot, is the Value line minus the Avg line. It crosses zero on the same bar the two lines cross, which is where the built-in up and down arrows print.
  • In the video, a chart bubble showing EMA 12 minus EMA 26 read -0.094 on one candle, where the Value line read -0.09395, and 0.1058 on another, where it read 0.10584.
  • The settings you can change are the fast length (12), slow length (26), MACD length (9) and average type (exponential). Any change to them moves the histogram and the signals.

What Is the MACD Indicator?

The MACD (Moving Average Convergence Divergence) is a momentum indicator built from two moving averages of price. Gerald Appel developed it in the late 1970s. When the two averages pull apart, the MACD moves away from zero, and when they come back together, it moves toward zero.

The MACD is a lagging indicator. Moving averages trail price action, so whatever you see on your price chart is usually a few candles ahead of what the MACD is calculating. Keep that delay in mind when you read any of its lines.

ThinkOrSwim's built-in MACD study uses a 12-period fast average and a 26-period slow average by default, both exponential. Most of what follows comes from reading its code.

The 3 MACD Plots in ThinkOrSwim

The ThinkOrSwim MACD draws 3 plots in a lower panel: a cyan line, a yellow line and a histogram. Each one is a plot variable in the code, and the table maps the colors to the names in the code and the names other platforms use.

On the chartPlot in the codeOther common nameCalculation (defaults)
Cyan lineValueMACD line12 EMA minus 26 EMA of the close
Yellow lineAvgSignal line9-period EMA of the Value line
HistogramDiffMACD histogramValue minus Avg
ThinkOrSwim MACD (12, 26, 9, EXPONENTIAL) lower study on an intraday chart: cyan Value line, pale yellow Avg line, green and red histogram bars, and green up arrows and red down arrows along the zero line
The built-in MACD at 12, 26, 9 and exponential, with breakout signals on. The cyan line is Value, the yellow line is Avg, and the bars are the Diff histogram.

The study also has a zero line and two signal plots, which I cover below. Everything on the panel comes from one input, the close, run through moving averages.

The Value Line: 12 EMA Minus 26 EMA

The MACD Value line is the difference between two moving averages of the close. These are the lines that calculate it in ThinkOrSwim's code:

This step’s code is free with an account. Create one or sign in.

Read it as the fast exponential average (12) minus the slow exponential average (26). Because fastLength, slowLength and averageType are inputs, you can change all three from the settings window without touching the code.

The sign of the Value line tells you which average is on top. Above zero, the 12 EMA is above the 26 EMA. Below zero, it's under it. The line crosses zero on the bar where the two averages cross.

MACD lower panel with an arrow labeled Value Line pointing at the cyan line
The Value line is the cyan line. It measures the distance between the 12 EMA and the 26 EMA.

Proving the Value Line With Two EMAs

The quickest way to prove what the Value line is: rebuild it yourself. In the video I wrote a few lines of thinkScript with a 12-period and a 26-period exponential average, and a chart bubble at the close that prints the first minus the second:

This step’s code is free with an account. Create one or sign in.

The video doesn't read out the bubble's first argument, the condition for drawing it. The code above uses yes, which puts a bubble on every candle. Swap in your own condition if you only want bubbles on some bars.

If the Value line is the 12 EMA minus the 26 EMA, every yellow bubble should match the cyan line on the same candle. I picked two candles at random and compared them.

Candle in the videoChart bubble (EMA 12 minus EMA 26)MACD Value line
First candle-0.094-0.09395, which rounds to -0.094
Second candle, a positive one0.10580.10584

Both match, so the Value line is the difference between two moving averages and nothing more. The full check is on the Code tab. If you'd rather print a value in the corner of the chart than in a bubble on each candle, our AddLabel tutorial shows how.

The Avg Line: The MACD Signal Line

The MACD Avg line is a moving average of the Value line. Instead of the close, it takes the differences between the 12 and 26 EMAs as its data and averages the last 9 of them:

This step’s code is free with an account. Create one or sign in.

The Avg line is calculated with the same average type as the Value line, so at the defaults it's a 9-period EMA. It shows the overall direction of the Value line, with a lag: are the differences between the EMAs getting more positive or more negative?

Together the two lines give you a jagged curve, Value, and a smoothed one, Avg. The Avg line is what other platforms call the signal line.

MACD lower panel with an arrow labeled MACD Avg Line (aka MACD Signal Line) pointing at the yellow line
The Avg line, also known as the signal line, is a 9-period moving average of the Value line.

The Diff Histogram

The MACD histogram measures the gap between the Value line and the Avg line. In the code it's the Diff plot, set to the histogram painting strategy:

This step’s code is free with an account. Create one or sign in.

When the cyan line is above the yellow line, the bars are above zero, and when it's below, they're under zero. The bars get taller as the lines spread apart and shorter as they come together. In the screenshots, the bars are bright green while they grow above zero and turn darker green as they shrink, with the same two shades of red below zero.

The histogram is what many traders mean when they say "the MACD", but it's one of 3 parts. A cross between the two lines is when we tend to think the direction is changing, and the histogram shows that cross as a move through zero.

MACD lower panel with an arrow labeled MACD Histogram pointing at the green bars above the zero line
The Diff histogram plots Value minus Avg as bars above and below zero.

The Up and Down Signals

ThinkOrSwim's MACD has two built-in signals, UpSignal and DownSignal, which show as arrows on the zero line when you set show breakout signals to Yes. An up arrow prints when the histogram crosses above zero, the bar where the Value line crosses above the Avg line. A down arrow prints on the cross below. Each arrow is a crossover of the Value and Avg lines.

The way ThinkOrSwim uses this: when the Value line crosses the Avg line to the downside, it expects momentum to pick up to the downside, and the histogram drops below zero. If you also find a divergence with price at that point, it may be a sign that momentum is starting to shift.

MACD Customizing window in ThinkOrSwim: fast length 12 and slow length 26 highlighted, macd length 9, average type EXPONENTIAL, show breakout signals Yes, and plot tabs Value, Avg, Diff, ZeroLine, UpSignal and DownSignal
The MACD settings window. The plot tabs list all 6 plots, including the zero line and the 2 signals.

Part 2 of the series, our MACD signals backtester, loads ThinkOrSwim's built-in strategy that trades these arrows and tests them on different markets, time frames and settings.

How Traders Use the MACD: Divergences and Crossovers

MACD divergence is the traditional way to trade the indicator. A bearish divergence is when price makes a higher high and the MACD makes a lower high. A bullish divergence is when price makes a lower low and the MACD makes a higher low.

In the video I pulled up a 1-minute SPY chart from a Friday, and there were examples right away. Price made a higher high while the MACD didn't. A more obvious one came toward the end of the day: price made a new high, the MACD didn't, and the divergence ended with a sharp drop into the close.

SPY daily chart (5 years, 1-day bars, dated 11/18/22) with the MACD below: a blue line slopes down across the price lows from May to June 2022 while a blue line on the MACD slopes up
A divergence on the SPY daily chart: lower lows in price from May to June 2022, higher lows on the MACD.

The two screenshots below zoom in on each kind on the SPY daily chart.

Close-up of SPY daily price making a lower low from early May to mid-June 2022 while the MACD line makes a higher low
Bullish divergence: price makes a lower low, the MACD line makes a higher low.
SPY daily price making a higher high into the 479.98 high in early January 2022 while the MACD line makes a lower high
Bearish divergence: price makes a higher high into the 479.98 high, the MACD line makes a lower high.

The other common use is the crossover, as a momentum signal. That one leans on the histogram and the two lines together. If you want to scan for crossovers, start with our MACD crossover scans.

The MACD is half of a divergence. The other half is price, and the MACD lags it. Treat a divergence or a cross as a read on momentum, decide where your stop goes before you act on it, and size the trade so a stop-out costs a small, fixed share of your account.

The MACD Settings You Can Change

The ThinkOrSwim MACD has 4 settings that change its math. Knowing what each one feeds tells you what you can test.

InputDefaultWhat it changes
fast length12The fast average in the Value line, so the Avg line, the histogram and the signals change too
slow length26The slow average in the Value line, with the same knock-on effects
macd length9How many bars the Avg line averages: its sensitivity
average typeEXPONENTIALThe type of moving average used for both lines, for example simple, which reacts more slowly

Change the fast or slow length and the Value line changes. The Avg line is calculated from the Value line, so it changes with it, and so does the histogram, which means the breakout signals move as well. The MACD length adjusts the Avg line alone. That gives you two sensitivity levers plus the average type.

That's where a backtester comes in: test the variations instead of guessing. For more tests like that, see our ThinkOrSwim backtesters.

Where the MACD Series Goes Next

The MACD series builds on this breakdown in two more tutorials. Part 2 is the MACD signals backtester, which tests the up and down arrows. The Smarter MACD condenses the lines into one green or red line calculated on a higher time frame, with fast, medium and slow settings. For more lessons on reading and writing code like this, browse our thinkScript tutorials.

MACD on a real chart

Drawn from real daily bars with the code on this page, through Oct 20, 2026. The MACD here is ThinkOrSwim's default: 12 and 26-period exponential averages, a 9-period average of their difference. The code on this page draws a bubble with EMA 12 minus EMA 26 on every candle; that number is the Value line.

SPY: the three parts

SPY daily chart in ThinkOrSwim with the MACD below: the cyan Value line, the yellow Avg line and the histogram, plus the EMA 12 minus EMA 26 bubble on Oct 20, 2026
SPY, the last 120 sessions. The bubble on the last candle prints the same number as the Value line on that day.

On Oct 20, 2026, SPY's 12-day EMA was 770.44 and its 26-day EMA 769.48, so the Value line read 0.97. The Avg line was 1.98, and the histogram, Value minus Avg, was -1.01: the Value line sat below its average.

A bearish divergence: Jun 11, 2025 to Jul 31, 2025

SPY daily chart with a bearish MACD divergence between Jun 11, 2025 and Jul 31, 2025: price makes a higher high while the MACD Value line makes a lower high
SPY made a higher high on Jul 31, 2025 while the Value line made a lower high.

The most recent one in SPY's last two years, using swing points with 5 bars on each side. Price went from 605.06 to 639.85; the Value line went from 9.04 to 7.32. Twenty trading days after the second swing, SPY closed at $648.92, +2.7% from that bar. A divergence says momentum is fading; it does not say when, or whether, price turns.

Full thinkScript code

MACD Indicator, a complete breakdown.tsthinkScript · 16 lines
The full code is free with an account

Create a free account or sign in to copy all 16 lines and download the file.

Download

MACD Indicator, a complete breakdown

Download MACD Indicator, a complete breakdown for ThinkOrSwim.

Download free

Video: MACD Indicator, a complete breakdown, built line by line

10 minutes on YouTube.

Watch on YouTube

Questions

What do the lines on the MACD mean in ThinkOrSwim?

The cyan line is the Value line, the 12-period EMA minus the 26-period EMA. The yellow line is the Avg line, a 9-period EMA of the Value line. The bars are the Diff histogram, the Value line minus the Avg line. Other platforms call the first two the MACD line and the signal line.

How is the MACD calculated?

Take the 12-period exponential moving average of the close and subtract the 26-period one: that is the MACD line. Average that line over 9 periods to get the signal line. Subtract the signal line from the MACD line to get the histogram. In the video, a chart bubble with EMA 12 minus EMA 26 matched the Value line on both candles I checked.

What are the default MACD settings in ThinkOrSwim?

The defaults are a fast length of 12, a slow length of 26, a MACD length of 9 and an exponential average type. There is also a show breakout signals input that turns the up and down arrows on or off. All of them are inputs, so you can change them in the study settings without editing the code.

What is the difference between the MACD line and the signal line?

The MACD line, called Value in ThinkOrSwim, is the distance between the 12 EMA and the 26 EMA. The signal line, called Avg, is a 9-period moving average of the MACD line. The MACD line is the jagged curve, the signal line the smoother, lagging one, and the histogram measures the gap between them.

What does the MACD histogram show?

It shows the gap between the Value line and the Avg line. Bars are above zero when the Value line is above the Avg line and below zero when it is under. They grow as the lines spread apart and shrink as they come together. The histogram crosses zero on the bar where the two lines cross.

What are the up and down arrows on the ThinkOrSwim MACD?

They are the UpSignal and DownSignal plots, which show when show breakout signals is set to Yes. An up arrow prints when the histogram crosses above zero, where the Value line crosses above the Avg line. A down arrow prints on the cross below. ThinkOrSwim has a built-in strategy that trades them, MACDStrat.

What is a MACD divergence?

A divergence is when price and the MACD disagree. A bearish divergence is a higher high in price with a lower high on the MACD. A bullish divergence is a lower low in price with a higher low on the MACD. It is the traditional way to use the indicator. Treat it as a read on momentum, since the MACD lags price.

Is the MACD a leading or a lagging indicator?

The MACD is a lagging indicator. It is built from moving averages, which trail price, so what you see on the price chart is usually a few candles ahead of what the MACD is calculating. Shorter lengths make it react faster, and longer lengths or a simple average make it slower.

Get each new indicator by email

One email on Sundays with the week's new tool and its code. A free account also unlocks every .txt download.

No credit card. Unsubscribe anytime.