MACD, its averages chosen (MACD (MA))
The difference of a fast and a slow average of the price, with a signal line and the histogram between them, as in MACD, but with each of the three averages chosen rather than exponential.
It measures momentum in price units.
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 (MA) 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.0026 and 0.0026 on these bars, and the last value is 0.00082. Counting the conditions the study declares on these bars: crosses above 0 twice; crosses below 0 twice; crosses above its signal line 14 times; crosses below its signal line 13 times; the histogram turns up 23 times; the histogram turns down 22 times. Press a condition under the figure to see each place marked. As states rather than moments: is above 0 on 136 bars; is below 0 on 131 bars; is above its signal line on 131 bars; is below its signal line on 136 bars.
02Its parts, named
03What you see
MACD (MA) is two lines and a histogram around zero in a pane of its own: the difference of a fast and a slow average of price, a signal line that is an average of that difference, and the histogram of the gap between the two. It differs from the MACD in one thing: the kind of each of the three averages is an input.
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
Line = MAₐ(P, f) − MA_b(P, s)
Signal = MA_c(Line, g), Histogram = Line − Signal
, and are the fast, slow and signal periods (12, 26, 9), and , , the kind of each average, chosen separately — simple by default, or exponential, weighted, double or triple exponential, triangular, Kaufman's, MESA's or T3.
The line is the gap between the fast and the 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. Each average starts on the fourth bar, 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.
On the last bar the fast EMA reads 25.39 and the slow one 24.62.
Line = 25.39 − 24.62 = 0.77
The 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.
MACD (MA) is this same construction with the kind of each average open to choice. With simple averages in place of the EMAs, the fast line on the last bar is (24 + 26) / 2 = 25.00 and the slow one (24 + 25 + 24 + 26) / 4 = 24.75, so Line = 0.25 — a different number from the same closes and the same periods.
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, its averages chosen") fastPeriod = input.int(12, "Fast Period") fastMa = input.float(0.0, "Fast MA") slowPeriod = input.int(26, "Slow Period") slowMa = input.float(0.0, "Slow MA") signalPeriod = input.int(9, "Signal Period") signalMa = input.float(0.0, "Signal MA") [macd, signal, histogram] = talib.MACDEXT(close, fastPeriod, fastMa, slowPeriod, slowMa, signalPeriod, signalMa) // A MACD whose three averages are each of a chosen kind. plot(macd, "MACD") plot(signal, "Signal") plot(histogram, "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. The line crossing above zero is read as the fast average moving above the slow average — the same event as those two averages crossing.
The line 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 the line'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 averages crossing each other — an event that arrives well after the turn in price, by the lag of the slower average.
09In different markets and timeframes
With the kind of each average open to choice, the character follows the choice: simple averages give a stepped line with drop-off effects; double and triple exponential ones a quicker, livelier line.
10With other studies
Read beside the ordinary MACD of the same periods to see what the change of average does.
11What it does not tell you
MACD (MA) is built from two moving averages and inherits their lag, whichever kind of average is chosen: 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 — 12Period of the fast MA
- 2Fast MA — 0Type of Moving Average for fast MA
- 3Slow Period — 26Period of the slow MA
- 4Slow MA — 0Type of Moving Average for slow MA
- 5Signal Period — 9Smoothing for the signal line (period length)
- 6Signal MA — 0Type of Moving Average for signal line
- 7Applied price — CloseAny of the seven prices, and several at once, each its own line with its own look.
- 8Timeframe — ChartThe chart's own by default; any higher one, read on the last closed candle. A finer one is never offered.
- 9Wait for the candle to close — OnThe value of a higher timeframe is taken only from a candle that has finished.
What each input changes
- Fast PeriodThe window of the fast average.
- Fast MAThe kind of the fast average (simple, exponential, weighted and the others TA-Lib offers).
- Slow PeriodThe window of the slow average.
- Slow MAThe kind of the slow average.
- Signal PeriodThe window of the signal line.
- Signal MAThe kind of the signal's average.
- In use12, 26, 9, each average simple by default; any of TA-Lib's nine kinds for each of the three.
13Limits and common misreadings
It is TA-Lib's form. MACD (MA) is computed as TA-Lib's MACDEXT function computes it: a line, a signal and a histogram of the gap between them. MetaTrader 5's own MACD draws its main line as a histogram and its signal as an SMA; that form is a setting of the MACD study, on its own page.
14Conditions 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.
15Practise it in the laboratory
Set all three averages to simple and compare the line with the ordinary MACD of the same periods; note the steps that appear as bars leave the simple windows.
Open the laboratory17In Bentrix
- From the menu: a chart's Indicators button › Oscillators › MACD (MA), or type MACD (MA) in its search.
- Its settings: the settings card opens when it is added; its legend's name opens it again.
18From 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_ext"}).
The study, as a chart files it
This is a new MACD (MA)’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_ext",
"settings": {
"shape": "ta",
"values": {
"optInFastMAType": 0.0,
"optInFastPeriod": 12.0,
"optInSignalMAType": 0.0,
"optInSignalPeriod": 9.0,
"optInSlowMAType": 0.0,
"optInSlowPeriod": 26.0
}
}
}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_ext",
"settings": {
"shape": "ta",
"values": {
"optInFastMAType": 0.0,
"optInFastPeriod": 12.0,
"optInSignalMAType": 0.0,
"optInSignalPeriod": 9.0,
"optInSlowMAType": 0.0,
"optInSlowPeriod": 26.0
}
}
}
}
}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_ext", "settings": {"shape": "ta", "values": {"optInFastMAType": 0.0, "optInFastPeriod": 12.0, "optInSignalMAType": 0.0, "optInSignalPeriod": 9.0, "optInSlowMAType": 0.0, "optInSlowPeriod": 26.0}}}, 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_ext", "settings": { "shape": "ta", "values": { "optInFastMAType": 0.0, "optInFastPeriod": 12.0, "optInSignalMAType": 0.0, "optInSignalPeriod": 9.0, "optInSlowMAType": 0.0, "optInSlowPeriod": 26.0 } } }, 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_ext", ["settings"] = new Dictionary<string, object?> { ["shape"] = "ta", ["values"] = new Dictionary<string, object?> { ["optInFastMAType"] = 0.0, ["optInFastPeriod"] = 12.0, ["optInSignalMAType"] = 0.0, ["optInSignalPeriod"] = 9.0, ["optInSlowMAType"] = 0.0, ["optInSlowPeriod"] = 26.0 } } }, 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_ext", "settings": map[string]any{"shape": "ta", "values": map[string]any{"optInFastMAType": 0.0, "optInFastPeriod": 12.0, "optInSignalMAType": 0.0, "optInSignalPeriod": 9.0, "optInSlowMAType": 0.0, "optInSlowPeriod": 26.0}}}, 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_ext", "settings", obj("shape", "ta", "values", obj("optInFastMAType", 0.0, "optInFastPeriod", 12.0, "optInSignalMAType", 0.0, "optInSignalPeriod", 9.0, "optInSlowMAType", 0.0, "optInSlowPeriod", 26.0)))).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_ext"}, {"settings", Json::Object{{"shape", "ta"}, {"values", Json::Object{{"optInFastMAType", 0.0}, {"optInFastPeriod", 12.0}, {"optInSignalMAType", 0.0}, {"optInSignalPeriod", 9.0}, {"optInSlowMAType", 0.0}, {"optInSlowPeriod", 26.0}}}}}}).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_ext", "settings" => Dict{String,Any}("shape" => "ta", "values" => Dict{String,Any}("optInFastMAType" => 0.0, "optInFastPeriod" => 12.0, "optInSignalMAType" => 0.0, "optInSignalPeriod" => 9.0, "optInSlowMAType" => 0.0, "optInSlowPeriod" => 26.0))); 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_ext", settings = list(shape = "ta", values = list(optInFastMAType = 0.0, optInFastPeriod = 12.0, optInSignalMAType = 0.0, optInSignalPeriod = 9.0, optInSlowMAType = 0.0, optInSlowPeriod = 26.0))), 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_ext", "settings": {"shape": "ta", "values": {"optInFastMAType": 0.0, "optInFastPeriod": 12.0, "optInSignalMAType": 0.0, "optInSignalPeriod": 9.0, "optInSlowMAType": 0.0, "optInSlowPeriod": 26.0}}})).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_ext: {"macd_ext": {"applied": "close", "convention": "talib", "kind": "macd_ext", "settings": {"shape": "ta", "values": {"optInFastMAType": 0.0, "optInFastPeriod": 12.0, "optInSignalMAType": 0.0, "optInSignalPeriod": 9.0, "optInSlowMAType": 0.0, "optInSlowPeriod": 26.0}}}} — its rules can name:
| Write | What it is |
|---|---|
macd_ext | its main line, as a value in a compare |
macd_ext.signal | this line, as a value in a compare |
macd_ext.histogram | this line, as a value in a compare |
{"cond": "macd_ext:level.cross_up:0"} | a condition it declares itself |
{"cond": "macd_ext:level.cross_down:0"} | a condition it declares itself |
{"cond": "macd_ext:level.above:0"} | a condition it declares itself |
{"cond": "macd_ext:level.below:0"} | a condition it declares itself |
{"cond": "macd_ext:signal.cross_up"} | a condition it declares itself |
{"cond": "macd_ext:signal.cross_down"} | a condition it declares itself |
{"cond": "macd_ext:signal.above"} | a condition it declares itself |
{"cond": "macd_ext:signal.below"} | a condition it declares itself |
{"cond": "macd_ext:histogram.turns_up"} | a condition it declares itself |
{"cond": "macd_ext:histogram.turns_down"} | a condition it declares itself |
19Sources
- 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