Home › Guides › MACD Indicator

WHY BOTS FAILFREENO ACCOUNTAUTOMATED TRADE BOTSMACD IndicatorThe arithmetic of how bots failEDUCATION ONLY | NO SIGNALSEXAMPLE NUMBERS ARE LABELLEDAUTOMATED TRADE BOTSautomatedtradebots.com
A free page on macd indicator. Education only, with example numbers labelled.
WHY BOTS FAIL

MACD Indicator

Short answer

MACD stands for moving average convergence divergence. The MACD line is a fast exponential moving average (EMA) minus a slow one, usually 12 and 26 bars.

MACD turns two moving averages into a line, a signal line and a histogram. The arithmetic is short, and so is the list of ways a bot misuses it.

The page explains the idea in plain words, works through the arithmetic with a table, and shows how it hits grid, martingale and DCA bots, then tells you what to check before you run one.

  • A worked example with real arithmetic
  • How it hits grid, martingale and DCA bots
  • How to check for it before you run a bot
  • No advice, no signals, no profit promises

Last reviewed 28 October 2026. Education only, not advice.

Free, no account Runs in your browser Worked numbers shown Not advice Examples labelled

Ready to go further

Next: size a trade before you test the rule

Whatever the entry, the loss you accept on a trade sets its size. The calculator turns a balance, a risk percent and a stop distance into a position size.

Reading time8 minutes
Worked exampleExact arithmetic
Tools linked1 on this page
Reviewed28 Oct 2026

What is included

  • What it is, in plain words
  • The formula or mechanism, with exact arithmetic
  • How it hits grid, martingale and DCA bots
  • A check you can run before you switch a bot on

Who it is for

  • People who run or plan to run a trading bot
  • Anyone reading a backtest or a bot sales page
  • Readers who want the numbers, not the pitch
  • Not advice, and not a signal service
MACD line and signal line on a made-up price path-10151525350Bar numberMade-up closes, EMA 12, 26 and 9. Calculated by us on 31 Oct 2026, exact to the rounding shown.
The picture for this page. Example numbers are made up unless the page says they were calculated.

Key facts

  • MACD stands for moving average convergence divergence. The MACD line is a fast exponential moving average (EMA) minus a slow one, usually 12 and 26 bars.
  • The signal line is a 9-bar EMA of the MACD line, and the histogram is the MACD line minus the signal line. It is zero where the two lines cross.
  • It is built only from past closes, so it always trails the price. In the made-up path below the price peaked at bar 29 and the cross down came 5 bars later (calculated by us on 31 Oct 2026, exact).
  • In the flat, choppy patch of the same path, the lines crossed 16 times in 18 bars with no real trend. A rule that trades every cross trades every one of them.
  • MACD describes what price did, not what it will do. A bot that uses it needs costs, testing and a size limit like any other rule.

What MACD is made of

MACD starts with the exponential moving average. An EMA is an average of recent closes that gives more weight to the newest bar. It reacts faster than a simple average of the same length. If you want the plain idea first, the EMA crossover guide covers one EMA against another.

MACD takes two EMAs of different lengths and subtracts them. When the fast EMA is above the slow one, the price has risen lately, and the MACD line is above zero. When it is below, the price has fallen lately and the line is below zero. The farther apart the two averages, the farther the line sits from zero.

  • The MACD line is EMA(12) minus EMA(26) of the close.
  • The signal line is EMA(9) of the MACD line. It is a smoothed copy, so it trails the MACD line.
  • The histogram is the MACD line minus the signal line. It is positive when the MACD line is above its signal and negative when below.

The numbers 12, 26 and 9 are a convention. Nothing in the maths makes them special, and many platforms let you change them.

The formula

An EMA with length N uses a smoothing weight, alpha, of 2 / (N + 1). Each new EMA is that weight times the new close, plus one minus the weight times the previous EMA.

alpha = 2 / (N + 1)
EMA(today) = alpha x close + (1 - alpha) x EMA(yesterday)
MACD line = EMA(12) - EMA(26)
Signal line = EMA(9) of the MACD line
Histogram = MACD line - Signal line

The weights are 0.1538 for 12 bars, 0.0741 for 26 bars and 0.2000 for the 9-bar signal. The first EMA value needs a starting point. This page starts each EMA at the first close, which is one common choice. Others start with a simple average of the first N closes, so early values differ between platforms and settle down after a few dozen bars.

A worked example

The picture uses 40 made-up closes. They swing between about 100 and 101.5 for the first 20 bars, then rise to 110.00 by bar 29, then fall. None of it is real data.

Take one step, at bar 30, where the close is 109.30. The EMA(12) on the bar before was 106.1045, so the new value is 0.1538 x 109.30 + 0.8462 x 106.1045 = 106.5961. The EMA(26) before was 103.9376, so the new value is 0.0741 x 109.30 + 0.9259 x 103.9376 = 104.3348.

The MACD line at bar 30 is 106.5961 - 104.3348 = 2.2614. The signal line before was 1.3669, so the new signal is 0.2000 x 2.2614 + 0.8000 x 1.3669 = 1.5458. The histogram is 2.2614 - 1.5458 = 0.7156. Still positive, so the MACD line is still above its signal, though the price has just started to fall.

Made-up example: where the price peaked and where the cross came110.00Peakbar 29105.90Cross downbar 34The cross came 5 bars after the peak, at a close 4.10 below it. Made-up closes, calculated by us on 31 Oct 2026, exact.
The cross down came after the price had already turned. The lines are built from past closes, so a turn shows up late.

Lag, shown with numbers

Because every part of MACD is an average of past closes, it cannot turn before the price does. The signal line is an average of an average, so it turns later still. The table shows the four bars around the first cross down after the peak. The MACD line falls through the signal line and the histogram changes sign.

BarCloseMACD lineSignal lineHistogram
32107.60+2.136+1.776+0.360
33106.80+1.962+1.813+0.149
34105.90+1.732+1.797-0.065
35105.00+1.459+1.729-0.270

The price peaked at 110.00 on bar 29. The cross down was confirmed at bar 34, 5 bars later, with the close at 105.90. A rule that waits for that cross has already given back 4.10 of the move. That is the cost of smoothing: fewer false turns than raw price, paid for with later entries and exits. A shorter setting lags less and gives more false turns, and a longer one does the reverse.

Lag also means the line is not a prediction. It says the fast average has been below the slow one and below its own average, which is a statement about the past.

How a bot rule on MACD is written

A simple MACD rule has three parts: an entry, an exit and a size. The usual textbook version looks like this.

Enter long when the MACD line crosses above the signal line
Exit when the MACD line crosses back below the signal line
Size each trade by a fixed risk rule, not by confidence

Variants add a filter. One requires the MACD line to be above zero for longs. Another waits for the histogram to grow for two bars. Another only takes crosses in the direction of a longer trend measure. Each filter is another setting. Each setting is another chance to fit the past, which is the trap described in overfitting. In code the rule is a comparison of two numbers on each closed bar, so it is easy to write and easy to run, which is part of why it is so common.

Where the rule fails

  • Chop. In a sideways market the two lines weave around each other. In the flat patch of the made-up path there were 16 crosses in 18 bars. Each one is an entry or an exit, and each pays the spread and possibly slippage. The price barely moved, so the costs are most of the result.
  • Late entries. By the time the lines cross up, part of the move is over. If the move was short, the exit cross arrives after the price has already turned back, which is the lag in the table above.
  • Crosses that reverse straight away. A cross is a moment, and the next bar can undo it. This is the same family of problem as a false breakout. Waiting for a bar close before acting helps with that, and costs another bar of lag.
  • One fixed exit. The rule above has no stop. A cross-down exit can be far from the entry in a fast fall. Pair any entry with a stop and a size set from the loss you accept, as in the position size calculator.
  • Tuned settings. Trying hundreds of fast, slow and signal lengths and keeping the best backtest is a search for noise. The winner is chosen because it fit the past.
  • Different values on different platforms. The EMA start point and rounding differ, so a bot ported from one platform to another may take different trades from the same prices.

The made-up path also had 8 up crosses. Do not read anything into that number. It depends on the path only, and a different made-up path would give a different count.

How to check a MACD rule

Follow the steps in how to backtest. Run the rule through a rising, a falling and a sideways stretch separately, because the sideways one is where it is most likely to bleed. Count the trades, then look at the average result per trade after costs, and the worst run of losses. Compare it against the same rule with the settings nudged a little, such as 10, 24, 8. If a small nudge flips the result, the rule is fitted to the past and not stable. Keep a stretch of data back, test once on it, then run the rule on a demo account before any real money.

Put the rule beside the other common indicator rules too. The RSI indicator measures the size of recent gains against losses, VWAP measures price against volume weighted average, and Bollinger Bands measure spread around an average. They all describe the past, and they all fail in some kind of market. For trades that act on a break of a level, see breakout trading.

What MACD does not tell you

It does not tell you where the price goes next, how big the next move is, or whether the cross is a good one. It has no idea about news, volume or costs. It can look clean in a chart of the past, because the eye picks out the crosses that came before large moves and skips the rest. This page is education about a calculation, not a signal and not advice.

FAQ

Frequently Asked Questions

What is the MACD indicator?

It is the difference between a fast and a slow exponential moving average of the close, usually 12 and 26 bars, drawn with a 9-bar average of itself called the signal line and a histogram of the gap.

How is MACD calculated?

Compute EMA(12) and EMA(26) of the close, subtract to get the MACD line, take a 9-bar EMA of that line for the signal, and subtract the signal from the MACD line for the histogram.

Why does MACD lag?

Every part of it is an average of past closes, and the signal line averages an average. A turn in price only shows once enough new bars have pulled the averages around.

What do the MACD settings 12, 26 and 9 mean?

They are the lengths in bars of the fast EMA, the slow EMA and the signal EMA. They are a convention, and nothing in the maths makes them best. Tuning them to fit the past is a way to overfit.

Can a bot trade on MACD crosses alone?

It can, but crosses are frequent in sideways markets and each one pays costs. Test a rule in rising, falling and flat stretches, with spread and slippage, before trusting it.

Do all platforms show the same MACD values?

Not exactly. The EMA starting value and rounding differ, so early values can differ and later ones converge.

Are the numbers on this page real?

Example numbers are made up to show how the maths works, and the page says so. A figure only counts as exact when the page says it was calculated on a stated date. Nothing you enter in a calculator leaves your browser.

Can I use this to decide whether to run a bot?

It can show whether a bot's numbers hold together and how much an account can lose. It cannot say what the market will do, and it is not advice. Use it to ask better questions, then read the terms of your own broker or exchange.

Is this financial advice?

No. It is general education about a calculation. The prices are made up and nothing here is a signal or a recommendation.

Know the Risk Before You Run It

Free calculators and guides, added every week. Leave your details and we will email you when new ones go up.

Trading carries a risk of loss