Butterworth Filter Butterworth
John Ehlers' two-pole Butterworth low-pass filter of the price from his Swiss Army Knife set: a smoothing that removes the cycles shorter than the period with less lag than a moving average of the same length.
It measures the smoothed price 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, and Butterworth has a value from the first. Of the 300 bars that have a value, the close is above the line on 149 and below it on 151. Counting the conditions the study declares on these bars: price crosses above the line 24 times; price crosses below the line 24 times. Press a condition under the figure to see each place marked. As states rather than moments: price is above the line on 148 bars; price is below the line on 151 bars.
02Its parts, named
03What you see
A filter borrowed from electrical engineering: a smoother — an average with sharper control over what it lets through.
The Butterworth filter is a single smooth line on the price pane. It removes short swings more completely than a moving average of comparable lag does.
04Where it comes from
The Butterworth filter is a classic of electrical engineering: Stephen Butterworth described it in 1930 as the filter with the flattest possible response in the band it passes. John Ehlers brought two- and three-pole versions to trading; the one here is the two-pole filter of his Swiss Army Knife set (2006), a family of filters that share one recursion and differ only in their constants.
05The formula
The Butterworth filter is a short recursive filter on price: its output is a weighted sum of the last three prices and its own last two outputs, with one constant computed from the chosen period :
s = sin π/N, b = 2.415 · 2s², a = −b + √(b² + 2b)
yₜ = a²/4 (Pₜ + 2P_(t−1) + P_(t−2)) + 2(1−a) y_(t−1) − (1−a)² y_(t−2)
This is the two-pole filter of Ehlers' Swiss Army Knife set as the TA-Lib library computes it, which is what Bentrix draws.
06A worked example
The filter is a recursion: each value is made from the last two values of the filter and the last three closes. With a period of 10 the constants are
s = sin(π / 10) = 0.309, b = 2.415 × 2s² = 0.461, a = −b + √(b² + 2b) = 0.604
which give the three weights a² / 4 = 0.091, 2(1 − a) = 0.792 and −(1 − a)² = -0.157.
Suppose the last two values of the filter were 100.00 and 100.20, and the last three closes 100.20, 100.60 and 101.00 (the newest). The closes enter weighted 1, 2, 1: 100.20 + 2 × 100.60 + 101.00 = 402.40.
0.792 × 100.20 − 0.157 × 100.00 + 0.091 × 402.40 = 100.38
The newest close alone has a weight of only 0.091; most of the value is a continuation of where the line was already heading. That momentum in the line is what makes it smooth.
On real closes — 20, 21, 23, 22, 24, 25, 24, 26 — a Butterworth filter of period 4 reads 24.62 on the last bar, where the close is 26 and the simple average of the last four closes 24.75.
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("Butterworth Filter", overlay = true) length = input.int(20, "Period") // The two-pole filter's coefficients, from Ehlers' Swiss Army Knife. s = math.sin(math.pi / length) b = 2.415 * (2.0 * s * s) a = -b + math.sqrt(b * b + 2.0 * b) keep = 1.0 - a c0 = a * a / 4.0 a1 = 2.0 * keep a2 = -(keep * keep) // The filter starts as if every bar before the first had closed at the first close. var float x1 = na var float x2 = na var float y1 = na var float y2 = na if bar_index == 0 x1 := close x2 := close y1 := close y2 := close // Each value: two parts of the last two values, plus the last three closes weighted 1, 2, 1. y = a1 * y1 + a2 * y2 + c0 * (close + 2.0 * x1 + x2) x2 := x1 x1 := close y2 := y1 y1 := y plot(y, "Butterworth")
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
A smooth trend line. Its direction is read as the direction of the swings longer than the cutoff. It fails at sharp corners, where a recursive filter overshoots and then rings — one or two small waves in the line that are not in the price.
09In different markets and timeframes
A Butterworth filter removes swings shorter than its period more sharply than an average does; it suits a reader who wants the trend with the short cycle gone. At a sharp reversal it overshoots slightly and rings.
10With other studies
Read beside its own high-pass counterpart — together the two split price into trend and cycle — and beside a Gaussian filter of the same period for comparison.
11What it does not tell you
A filter separates the past series into slow and fast parts; it cannot know whether the cycle it passes goes on.
12Inputs and defaults
- 1Time Period — 20Cutoff period
- 2Applied price — CloseAny of the seven prices, and several at once, each its own line with its own look.
- 3Timeframe — ChartThe chart's own by default; any higher one, read on the last closed candle. A finer one is never offered.
- 4Wait for the candle to close — OnThe value of a higher timeframe is taken only from a candle that has finished.
What each input changes
- Time PeriodThe cutoff period: swings shorter than this many bars are strongly reduced, longer ones pass. A longer cutoff gives a smoother, later line.
- In useA period of 10 to 40 bars: the length of the shortest swing to be kept.
13Limits and common misreadings
The period is a cutoff, not a window. No bar ever leaves the calculation; old prices fade.
Start-up. The first values depend on how the recursion was started and settle after a few periods.
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
Put the Butterworth 20 and an EMA 20 on one chart and replay a market with small fast swings inside a trend; compare how much of the swing each line lets through.
Open the laboratory17On pages of their own
18In Bentrix
- From the menu: a chart's Indicators button › Moving averages › Butterworth, or type Butterworth in its search.
- Its settings: the settings card opens when it is added; its legend's name opens it again.
19From 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": "swak_butter"}).
The study, as a chart files it
This is a new Butterworth’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": "swak_butter",
"settings": {
"shape": "ta",
"values": {
"optInTimePeriod": 20.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": "swak_butter",
"settings": {
"shape": "ta",
"values": {
"optInTimePeriod": 20.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": "swak_butter", "settings": {"shape": "ta", "values": {"optInTimePeriod": 20.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": "swak_butter", "settings": { "shape": "ta", "values": { "optInTimePeriod": 20.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"] = "swak_butter", ["settings"] = new Dictionary<string, object?> { ["shape"] = "ta", ["values"] = new Dictionary<string, object?> { ["optInTimePeriod"] = 20.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": "swak_butter", "settings": map[string]any{"shape": "ta", "values": map[string]any{"optInTimePeriod": 20.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", "swak_butter", "settings", obj("shape", "ta", "values", obj("optInTimePeriod", 20.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", "swak_butter"}, {"settings", Json::Object{{"shape", "ta"}, {"values", Json::Object{{"optInTimePeriod", 20.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" => "swak_butter", "settings" => Dict{String,Any}("shape" => "ta", "values" => Dict{String,Any}("optInTimePeriod" => 20.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 = "swak_butter", settings = list(shape = "ta", values = list(optInTimePeriod = 20.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": "swak_butter", "settings": {"shape": "ta", "values": {"optInTimePeriod": 20.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.
line | What it is |
|---|---|
Main | line |
In a strategy
Filed under a key of your choice in a strategy document’s studies — here swak_butter: {"swak_butter": {"applied": "close", "convention": "talib", "kind": "swak_butter", "settings": {"shape": "ta", "values": {"optInTimePeriod": 20.0}}}} — its rules can name:
| Write | What it is |
|---|---|
swak_butter | its main line, as a value in a compare |
{"cond": "swak_butter:price.cross_up"} | a condition it declares itself |
{"cond": "swak_butter:price.cross_down"} | a condition it declares itself |
{"cond": "swak_butter:price.above"} | a condition it declares itself |
{"cond": "swak_butter:price.below"} | a condition it declares itself |
20Sources
- Ehlers, John F. Cybernetic Analysis for Stocks and Futures. Wiley, 2004.
- Ehlers, John F. Cycle Analytics for Traders. Wiley, 2013.
- Butterworth, Stephen. "On the Theory of Filter Amplifiers." Experimental Wireless and the Wireless Engineer, vol. 7, 1930.
- Ehlers, John F. "Swiss Army Knife Indicator." Technical Analysis of Stocks & Commodities, January 2006.
Bentrix 0.1.382Every number, name and default on these pages is read from the application itself.What is comingWhat changed