Moving Average Convergence Divergence MACD
The fast EMA minus the slow EMA, a signal average of that difference, and a histogram of the gap between the two.
It measures the distance between two horizons of the same average, which is a way of measuring how fast the trend is separating from itself. Readings: the line against zero as the trend divide, line against signal as a momentum shift, and histogram flips. The conventions genuinely differ: MT5 smooths the signal with a SIMPLE moving average — its own input is named Signal SMA period — while TA-Lib uses an EMA. The EMA seeding difference applies on top.
01The study, live
Computed by Bentrix’s own code over the same synthetic bars on every page — an illustration of how the study behaves, not a market.
What this figure shows
The figure is computed over 300 sample bars. MACD has no value on the first 33 — the bars it needs before it can answer — and its first value stands on bar 34. Its line runs between -0.0023 and 0.0024 on these bars, and the last value is 0.0007. Counting the conditions the study declares on these bars: crosses above 0 twice; crosses below 0 twice; crosses above its signal line 9 times; crosses below its signal line 9 times; the histogram turns up 33 times; the histogram turns down 34 times. Press a condition under the figure to see each place marked. As states rather than moments: is above 0 on 130 bars; is below 0 on 137 bars; is above its signal line on 99 bars; is below its signal line on 168 bars.
02Its parts, named
03What you see
In its own pane: the MACD line, its signal line, and a histogram of the difference between them, around a zero line.
04Where it comes from
Gerald Appel developed the MACD in the late 1970s; the histogram was added by Thomas Aspray in the mid-1980s. Appel describes its use in Technical Analysis: Power Tools for Active Investors (2005). The 12 / 26 / 9 defaults date from those early years.
05The formula
MACD = EMA(P, 12) − EMA(P, 26)
Signal = EMA(MACD, 9), Histogram = MACD − Signal
The MACD line is the gap between a fast and a slow average, in price units. Positive means the fast average is above the slow one.
06A worked example
Take the closes 20, 21, 23, 22, 24, 25, 24, 26 with a fast EMA of 2, a slow EMA of 4 and a signal of 2. Both averages start on the fourth bar, each at the plain average of its own window ending there — 22.50 for the fast one (the closes 23 and 22) and 21.50 for the slow one — which is how TA-Lib, the default calculation, starts them.
On the last bar the fast EMA reads 25.39 and the slow one 24.62.
MACD = 25.39 − 24.62 = 0.77
The MACD line from the fourth bar on is 1.00, 1.00, 1.00, 0.47, 0.77. The signal is an EMA of that line, started at the average of its first two values; on the last bar it reads 0.73. The histogram is the difference of the two: 0.77 − 0.73 = 0.04.
The line, the signal and the histogram are all in price units. A MACD of 0.50 on an instrument priced at 25 and one of 0.50 on an instrument priced at 2500 are not the same thing; the percentage form (PPO) exists for that comparison.
07Built in Assay
The same study written in Assay, Bentrix’s script language, and drawn by Bentrix itself as the script’s graph: each block is one step of the formula. On the ladder the steps stand in the script’s order, one a row; on the board each wire carries a value from the block that gives it to the block that reads it, left to right. The script computes exactly what the study computes — Bentrix checks the two against each other, candle by candle.
//@version=6 indicator("MACD") fastLength = input.int(12, "Fast") slowLength = input.int(26, "Slow") signalLength = input.int(9, "Signal") // TA-Lib's MACD: the fast and the slow average start on the same bar, and the three lines appear once the // signal has its first value. [macd, signal, histogram] = talib.MACD(close, fastLength, slowLength, signalLength) plot(macd, "MACD") plot(signal, "Signal") plot(histogram, "Histogram", style = plot.style_histogram)
Paste it into the Assay IDE to put it on a chart, or to change it. Looking at the graph is free in every package; editing it as blocks is Plus.
08Readings — what traders look for
The zero line. MACD crossing above zero is read as the 12-bar EMA moving above the 26-bar EMA — the same event as those two averages crossing.
MACD crossing its signal is read as the gap between the averages starting to widen or narrow.
The histogram's height is read as how fast that gap is changing; a shrinking histogram as the gap's change slowing.
Divergence between MACD's peaks and price's peaks is one of the most common readings of all.
How the signal crossing fails. In a market without direction the two averages stay close, the MACD line hovers near zero and it crosses its signal again and again. Gerald Appel's own writing reads crossings far from zero differently from crossings near it.
How the zero line fails. A crossing of zero is the two EMAs crossing each other — an event that arrives well after the turn in price, by the lag of the slower average.
How divergence fails. A divergence can form two, three or four times in a row while price keeps going; it describes a slowing pace, and a move can keep slowing for a long while.
09In different markets and timeframes
MACD is a trend-following momentum study: at its best where trends last long enough for two averages to separate, and at its worst in ranges, where the line hugs zero and crosses its signal continually. Its values are in price units, so they grow with the instrument's price and cannot be compared across markets. Appel designed it on daily and weekly bars; on intraday charts the default periods cover minutes.
10With other studies
Read beside the PPO when instruments or decades are compared; beside a longer-timeframe MACD for the larger direction (Elder's triple-screen idea); and with its own histogram, which shows the line pulling away from or closing on its signal.
11What it does not tell you
MACD is built from two moving averages and inherits their lag: every crossing comes after the price move that produced it. Its values are in price units, so they cannot be compared between markets or across long periods with very different prices — PPO fixes that.
12Inputs and defaults
- 1Fast period — 12The shorter EMA. It follows price closely; raising it brings the two averages together and shrinks the whole line.
- 2Signal period — 9The EMA of the MACD line. Longer: fewer crossings, each later, and a larger histogram.
- 3Slow period — 26The longer EMA: the background the fast one is measured against. Raising it makes the line larger and its zero crossings later.
- 4Applied price — CloseAny of the seven prices, and several at once, each its own line with its own look.
- 5Timeframe — ChartThe chart's own by default; any higher one, read on the last closed candle. A finer one is never offered.
- 6Calculation — TA-LibOr MT5, which computes it as MetaTrader 5 does — see the calculation convention below.
- 7Wait for the candle to close — OnThe value of a higher timeframe is taken only from a candle that has finished.
The settings in use
- In use12, 26, 9 is Appel's. 5, 35, 5 is a quicker, wider variant; 19, 39, 9 was his setting for the other side of the market. The signal's period controls how early its crossings come.
13Calculation convention
This is one of the studies whose result differs between the two conventions Bentrix offers. The difference is in how the first values are seeded and how the series is smoothed, so the two agree after the warm-up and differ at its start. Choose it on the settings card's Calculation row.
defaultTA-Lib
The convention of the TA-Lib library, which most charting software follows.
MT5
Computed as MetaTrader 5 computes it, so a value matches the terminal's own.
14Limits and common misreadings
TA-Lib and MT5 differ. MetaTrader 5 draws its main line as a histogram and its signal as an SMA; TA-Lib uses an EMA signal. Bentrix offers both; see Calculation convention.
15Conditions an alert or a strategy can watch
an event happens on one bar a state holds on every bar while it lasts
These are the conditions the study itself declares — the same rules the alert editor and the strategy engine read, and the ones the figure's chips show.
16Practise it in the laboratory
Toggle the signal-line crossings on the figure above, then on a replay count how many of them came after more than half of the price move had already happened.
Open the laboratory18On pages of their own
19In Bentrix
- From the menu: a chart's Indicators button › Oscillators › MACD, or type MACD in its search.
- Its settings: the settings card opens when it is added; its legend's name opens it again.
20From an agent or a program
Two tools reach it. study_values computes it over a workbench’s candles up to the clock — no chart needed — and study_attach puts it on a chart, where it is drawn and saved with the chart ({"chart": "<chart uri>", "kind": "macd"}).
The study, as a chart files it
This is a new MACD’s entry — its kind, its calculation, the price it reads and its settings. Change a setting here to read it with other inputs.
{
"applied": "close",
"convention": "talib",
"kind": "macd",
"settings": {
"fast": 12,
"shape": "fast_slow_signal",
"signal": 9,
"slow": 26
}
}An agent
An agent calls the MCP tool with it:
{
"name": "study_values",
"arguments": {
"bench": "bentrix://lab/\u2026/bench/\u2026",
"symbol": "EURUSD",
"timeframe": "H1",
"limit": 5,
"study": {
"applied": "close",
"convention": "talib",
"kind": "macd",
"settings": {
"fast": 12,
"shape": "fast_slow_signal",
"signal": 9,
"slow": 26
}
}
}
}A program
A program does the same in its language — bench and market as in the built-in indicators guide:
Python
values = client.study.values(bench, market, "H1", {"applied": "close", "convention": "talib", "kind": "macd", "settings": {"fast": 12, "shape": "fast_slow_signal", "signal": 9, "slow": 26}}, limit=5)
print("times:", values["times"])
for line in values["lines"]:
print("line:", line["line"])
print(" newest values:", line["values"])JavaScript
const values = await client.study.values({ bench, symbol: market, timeframe: "H1", study: { "applied": "close", "convention": "talib", "kind": "macd", "settings": { "fast": 12, "shape": "fast_slow_signal", "signal": 9, "slow": 26 } }, limit: 5 });
console.log("times:", values.times);
for (const line of values.lines) {
console.log("line:", line.line);
console.log(" newest values:", line.values);
}C#
var values = await client.Study.Values(bench, market, "H1", new Dictionary<string, object?> { ["applied"] = "close", ["convention"] = "talib", ["kind"] = "macd", ["settings"] = new Dictionary<string, object?> { ["fast"] = 12, ["shape"] = "fast_slow_signal", ["signal"] = 9, ["slow"] = 26 } }, limit: 5);
Console.WriteLine($"times: {values!["times"]!}");
foreach (var line in values!["lines"]!.AsArray())
{
Console.WriteLine($"line: {line!["line"]!}");
Console.WriteLine($" newest values: {line!["values"]!}");
}Go
values := must(client.Study.Values(ctx, mcp.StudyValuesArgs{Bench: bench, Symbol: market, Timeframe: "H1", Study: map[string]any{"applied": "close", "convention": "talib", "kind": "macd", "settings": map[string]any{"fast": 12, "shape": "fast_slow_signal", "signal": 9, "slow": 26}}, Limit: mcp.Ptr[int64](5)}))
fmt.Println("times:", at(values, "times"))
for _, line := range at(values, "lines").([]any) {
fmt.Println("line:", at(line, "line"))
fmt.Println(" newest values:", at(line, "values"))
}Java
Object values = client.study.values(new Bentrix.StudyApi.ValuesArgs(bench, market, "H1", obj("applied", "close", "convention", "talib", "kind", "macd", "settings", obj("fast", 12, "shape", "fast_slow_signal", "signal", 9, "slow", 26))).limit(5L));
System.out.println("times:" + " " + at(values, "times"));
for (Object line : (List<Object>) at(values, "lines")) {
System.out.println("line:" + " " + at(line, "line"));
System.out.println(" newest values:" + " " + at(line, "values"));
}C++
auto values = client.study.values_(StudyApi::ValuesArgs(bench, market, "H1", Json::Object{{"applied", "close"}, {"convention", "talib"}, {"kind", "macd"}, {"settings", Json::Object{{"fast", 12}, {"shape", "fast_slow_signal"}, {"signal", 9}, {"slow", 26}}}}).limit(5));
std::cout << "times:" << " " << values["times"].dump() << std::endl;
for (const Json &line : values["lines"].arr()) {
std::cout << "line:" << " " << line["line"].dump() << std::endl;
std::cout << " newest values:" << " " << line["values"].dump() << std::endl;
}Julia
values = client.study.values(bench, market, "H1", Dict{String,Any}("applied" => "close", "convention" => "talib", "kind" => "macd", "settings" => Dict{String,Any}("fast" => 12, "shape" => "fast_slow_signal", "signal" => 9, "slow" => 26)); limit = 5)
println("times:", " ", values["times"])
for line in values["lines"]
println("line:", " ", line["line"])
println(" newest values:", " ", line["values"])
endR
values <- client$study$values(bench, market, "H1", list(applied = "close", convention = "talib", kind = "macd", settings = list(fast = 12, shape = "fast_slow_signal", signal = 9, slow = 26)), limit = 5)
cat("times:", format(values$times), "\n")
for (line in values$lines) {
cat("line:", format(line$line), "\n")
cat(" newest values:", format(line$values), "\n")
}Rust
let values = client.study().values(mcp::StudyValuesArgs::new(bench.clone(), market.clone(), "H1", serde_json::json!({"applied": "close", "convention": "talib", "kind": "macd", "settings": {"fast": 12, "shape": "fast_slow_signal", "signal": 9, "slow": 26}})).limit(5))?;
println!("times: {}", values["times"]);
for line in values["lines"].as_array().into_iter().flatten() {
println!("line: {}", line["line"]);
println!(" newest values: {}", line["values"]);
}Reading the answer
times— the candles’ open times, in milliseconds since 1970 (UTC), oldest first.lines— one entry per line, in this order, each with itslinename and itsvalues;values[i]belongs to the candle attimes[i].- The last value is the candle at the clock’s, which may still be forming: it changes until that candle closes, as on the chart. Nothing after the clock is ever computed.
nullis a candle with no value yet — the warm-up, about 1 × the period at the start of the history.- Its fixed levels, drawn on the chart:
[0.0].
line | What it is |
|---|---|
Main | the indicator's own value |
Signal | a smoothing of the main line, drawn beside it |
Histogram | the gap between the main line and the signal, drawn as bars |
In a strategy
Filed under a key of your choice in a strategy document’s studies — here macd: {"macd": {"applied": "close", "convention": "talib", "kind": "macd", "settings": {"fast": 12, "shape": "fast_slow_signal", "signal": 9, "slow": 26}}} — its rules can name:
| Write | What it is |
|---|---|
macd | its main line, as a value in a compare |
macd.signal | this line, as a value in a compare |
macd.histogram | this line, as a value in a compare |
{"cond": "macd:level.cross_up:0"} | a condition it declares itself |
{"cond": "macd:level.cross_down:0"} | a condition it declares itself |
{"cond": "macd:level.above:0"} | a condition it declares itself |
{"cond": "macd:level.below:0"} | a condition it declares itself |
{"cond": "macd:signal.cross_up"} | a condition it declares itself |
{"cond": "macd:signal.cross_down"} | a condition it declares itself |
{"cond": "macd:signal.above"} | a condition it declares itself |
{"cond": "macd:signal.below"} | a condition it declares itself |
{"cond": "macd:histogram.turns_up"} | a condition it declares itself |
{"cond": "macd:histogram.turns_down"} | a condition it declares itself |
21Sources
- Appel, Gerald. Technical Analysis: Power Tools for Active Investors. FT Prentice Hall, 2005.
- Murphy, John J. Technical Analysis of the Financial Markets. New York Institute of Finance, 1999, ch. 10.
Bentrix 0.1.382Every number, name and default on these pages is read from the application itself.What is comingWhat changed