Cập nhật
[blackcat] L2 Ehlers Two Pole Butterworth Filter

Level: 2
Background
John F. Ehlers introuced Two Pole Butterworth Filter in his "Cybernetic Analysis for Stocks and Futures" chapter 13 on 2004.
Function
Dr. Ehlers translated analog Butterworth filters to their digital approximations. The transfer response is characterized by a single variable—the cutoff frequency. The cutoff frequency is that frequency where the input is attenuated by 3 dB. Below the cutoff frequency, the input frequency components are passed to the output; above the cutoff frequency, the input frequency components are rejected to the extent possible by the filter characteristics. Since traders are more comfortable with period, which is the reciprocal of frequency, the equations for the Butterworth digital filters are characterized in terms of the cutoff period.
As opposed to the Regularized filter, the order of Butterworth filters can be increased indefinitely to increase the sharpness of the filter rejection. For traders, this quickly reaches the point of diminishing returns because increasing the number of poles in the filter means the lag of the filter is also increased. A three-pole filter gives just about the limit of tolerable lag for a selected cutoff period.
Key Signal
Butter ---> Two Pole Butterworth Filter fast line
Trigger ---> Two Pole Butterworth Filter slow line
Pros and Cons
100% John F. Ehlers definition translation of original work, even variable names are the same. This help readers who would like to use pine to read his book. If you had read his works, then you will be quite familiar with my code style.
Remarks
The 29th script for Blackcat1402 John F. Ehlers Week publication.
Readme
In real life, I am a prolific inventor. I have successfully applied for more than 60 international and regional patents in the past 12 years. But in the past two years or so, I have tried to transfer my creativity to the development of trading strategies. Tradingview is the ideal platform for me. I am selecting and contributing some of the hundreds of scripts to publish in Tradingview community. Welcome everyone to interact with me to discuss these interesting pine scripts.
The scripts posted are categorized into 5 levels according to my efforts or manhours put into these works.
Level 1 : interesting script snippets or distinctive improvement from classic indicators or strategy. Level 1 scripts can usually appear in more complex indicators as a function module or element.
Level 2 : composite indicator/strategy. By selecting or combining several independent or dependent functions or sub indicators in proper way, the composite script exhibits a resonance phenomenon which can filter out noise or fake trading signal to enhance trading confidence level.
Level 3 : comprehensive indicator/strategy. They are simple trading systems based on my strategies. They are commonly containing several or all of entry signal, close signal, stop loss, take profit, re-entry, risk management, and position sizing techniques. Even some interesting fundamental and mass psychological aspects are incorporated.
Level 4 : script snippets or functions that do not disclose source code. Interesting element that can reveal market laws and work as raw material for indicators and strategies. If you find Level 1~2 scripts are helpful, Level 4 is a private version that took me far more efforts to develop.
Level 5 : indicator/strategy that do not disclose source code. private version of Level 3 script with my accumulated script processing skills or a large number of custom functions. I had a private function library built in past two years. Level 5 scripts use many of them to achieve private trading strategy.
Background
John F. Ehlers introuced Two Pole Butterworth Filter in his "Cybernetic Analysis for Stocks and Futures" chapter 13 on 2004.
Function
Dr. Ehlers translated analog Butterworth filters to their digital approximations. The transfer response is characterized by a single variable—the cutoff frequency. The cutoff frequency is that frequency where the input is attenuated by 3 dB. Below the cutoff frequency, the input frequency components are passed to the output; above the cutoff frequency, the input frequency components are rejected to the extent possible by the filter characteristics. Since traders are more comfortable with period, which is the reciprocal of frequency, the equations for the Butterworth digital filters are characterized in terms of the cutoff period.
As opposed to the Regularized filter, the order of Butterworth filters can be increased indefinitely to increase the sharpness of the filter rejection. For traders, this quickly reaches the point of diminishing returns because increasing the number of poles in the filter means the lag of the filter is also increased. A three-pole filter gives just about the limit of tolerable lag for a selected cutoff period.
Key Signal
Butter ---> Two Pole Butterworth Filter fast line
Trigger ---> Two Pole Butterworth Filter slow line
Pros and Cons
100% John F. Ehlers definition translation of original work, even variable names are the same. This help readers who would like to use pine to read his book. If you had read his works, then you will be quite familiar with my code style.
Remarks
The 29th script for Blackcat1402 John F. Ehlers Week publication.
Readme
In real life, I am a prolific inventor. I have successfully applied for more than 60 international and regional patents in the past 12 years. But in the past two years or so, I have tried to transfer my creativity to the development of trading strategies. Tradingview is the ideal platform for me. I am selecting and contributing some of the hundreds of scripts to publish in Tradingview community. Welcome everyone to interact with me to discuss these interesting pine scripts.
The scripts posted are categorized into 5 levels according to my efforts or manhours put into these works.
Level 1 : interesting script snippets or distinctive improvement from classic indicators or strategy. Level 1 scripts can usually appear in more complex indicators as a function module or element.
Level 2 : composite indicator/strategy. By selecting or combining several independent or dependent functions or sub indicators in proper way, the composite script exhibits a resonance phenomenon which can filter out noise or fake trading signal to enhance trading confidence level.
Level 3 : comprehensive indicator/strategy. They are simple trading systems based on my strategies. They are commonly containing several or all of entry signal, close signal, stop loss, take profit, re-entry, risk management, and position sizing techniques. Even some interesting fundamental and mass psychological aspects are incorporated.
Level 4 : script snippets or functions that do not disclose source code. Interesting element that can reveal market laws and work as raw material for indicators and strategies. If you find Level 1~2 scripts are helpful, Level 4 is a private version that took me far more efforts to develop.
Level 5 : indicator/strategy that do not disclose source code. private version of Level 3 script with my accumulated script processing skills or a large number of custom functions. I had a private function library built in past two years. Level 5 scripts use many of them to achieve private trading strategy.
Phát hành các Ghi chú
thanks to contribution of sal157011, i corrected the wrong parameter in script, i will reference rocket book 2001:rocket book page 155:
a = ExpValue(-1.414*3.14159/P);
b = 2*a*Cos (1.414*180/P);
y=b*y(1)-a*a*y(2)+((1-b+a*a)/4)*(x+2*x(1)+x(2));
Phát hành các Ghi chú
OVERVIEW This script implements the Ehlers Two Pole Butterworth Filter with enhanced divergence detection capabilities. It overlays filtered price data and identifies various types of divergences to assist traders in making informed decisions.FEATURES
• Two Pole Butterworth Filter calculation
• Real-time BUY/SELL signals with visual labels
• Divergence detection including:
Regular Bullish Divergence
Hidden Bullish Divergence
Regular Bearish Divergence
Hidden Bearish Divergence • Customizable alert conditions for each divergence type • Adjustable lookback periods and range parameters
HOW TO USE
Add the indicator to your chart
Configure the following inputs:
• Price: Select the price source (default: HL2)
• Period: Set the filter period (default: 15)
• Pivot Lookback Right/Left: Adjust divergence detection range
• Max/Min Lookback Range: Define the acceptable divergence timeframe
• Plot settings for bullish/bearish signals
Monitor the Butterworth and Trigger lines for trend direction
Watch for BUY/SELL labels at crossover points
Use divergence detection to confirm potential trend reversals
LIMITATIONS
• Requires sufficient historical data for accurate calculations
• Divergence detection may generate false signals during volatile markets
• Best used in conjunction with other technical indicators
NOTES
• The Butterworth Filter helps smooth price data while minimizing phase lag
• Divergence signals are more reliable when confirmed by multiple timeframes
• Adjust lookback periods based on your trading timeframe and market conditions
Phát hành các Ghi chú
█ OVERVIEWThis indicator smooths price with John Ehlers' Two Pole Butterworth Filter and reads momentum from the cross between the filter and its one-bar lag line. It packs line-cross signals, four divergence detectors, optional ATR-scaled take-profit and stop-loss reference levels, and full alert coverage into one overlay.
█ CONCEPTS
• Butterworth Filter: a low-pass smoothing filter that removes high-frequency noise while keeping turning points sharp and lag moderate.
• Lag Line Cross: the yellow filter line is compared with its fuchsia one-bar-lagged copy; crossing up reads as building upward momentum, crossing down as fading momentum.
• Signal Governance: an optional state machine suppresses repeated same-direction signals and enforces a cooldown between signals.
• Divergences: regular and hidden, bullish and bearish divergences between price and the filtered line flag momentum exhaustion at pivots.
• Risk Reference: stop-loss and take-profit guide lines are sized in ATR multiples and re-anchor on every new signal, adapting to each symbol's volatility.
█ HOW TO USE
1 — Add the script to your chart; the filter, lag line and colored fill appear over price.
2 — Read BUY / SELL labels where the filter crosses its lag line; the background tint shows the current bias state.
3 — Tune the Divergence group (pivot lookbacks, hidden variants) if you want more or fewer reversal warnings.
4 — Create alerts from any of the eight conditions: BUY, SELL, combined cross, and the four divergence types.
5 — Use the red / teal guide lines as optional stop-loss / take-profit references sized in ATR multiples.
Input Parameters
• Price Source: source price fed to the filter (default hl2).
• Period: smoothing period, min 2 (default 15); larger = smoother with more lag.
• Enable Governance: ON = state machine plus cooldown; OFF = raw crossover behavior.
• Cooldown Bars: minimum bars between governed signals (default 3; 0 = off).
• Show TP/SL Reference Levels, ATR Length, SL xATR, TP xATR: risk reference controls (defaults 14 / 2.0 / 3.0).
• Pivot Lookback Left / Right, Min / Max of Lookback Range: divergence pivot detection window.
• Plot Bullish / Bearish / Hidden variants: toggle each divergence family.
• Visuals: cross labels, ATR-scaled label offset, position background, fill transparency, dashboard.
█ FEATURES
• Eight alertcondition hooks covering cross signals and all four divergence types, plus a bar-close dynamic alert message with price.
• Status dashboard: position bias, trend direction, ATR, current SL/TP references, cooldown remaining.
• Volatility-adaptive signal labels and transparent state background for fast visual reading.
█ SIGNALS
• BUY label (green, below the line): filter crossed above its lag line.
• SELL label (red, above the line): filter crossed below its lag line.
• Bull / Bear pivot labels: regular divergence between price and the filtered line.
• H Bull / H Bear labels: hidden divergences (continuation context), off by default.
• Yellow vs fuchsia fill: direction of the filter relative to its lag line.
█ LIMITATIONS
• Like all smoothing filters it lags price; very small Period values track noise instead of trend.
• Line-cross systems whipsaw in flat, ranging markets; combine with divergence or higher-timeframe context.
• TP/SL lines are informational references for manual trading, not exchange orders.
• Divergence signals confirm only after the pivot lookback-right bars have closed.
█ NOTES
• Filter concept from John F. Ehlers' work on Butterworth low-pass filters; this version adds signal governance, risk references, a dashboard and full alerting.
• Educational tool, not financial advice; always test on your symbols and timeframes.
════════════════════════════════════════════════════════════════════════
For questions or feedback, please use the comments section below.
Feel free to use and modify — attribution appreciated.
Don't miss out! The first and most important thing to do is to join my Discord chat now! Click here to start your adventure: discord.com/invite/ZTGpQJq My new github for more pine development soon: github.com/wxt-coder
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Don't miss out! The first and most important thing to do is to join my Discord chat now! Click here to start your adventure: discord.com/invite/ZTGpQJq My new github for more pine development soon: github.com/wxt-coder
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.