OPEN-SOURCE SCRIPT

Stochastic Triple Filter

6 928
Stochastic Triple Filter [ATP]

Overview

The Stochastic Triple Filter is an enhanced version of the classic Stochastic Oscillator designed to address its most fundamental weakness: the generation of excessive false signals during ranging markets and counter-trend conditions.

This script integrates three independently validated technical analysis components into a single unified system, where each filter serves a specific and complementary role in signal validation. The result is a significant reduction in low-quality crossover signals, keeping only those that occur within a confirmed trending environment with real directional momentum.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
THE PROBLEM WITH THE CLASSIC STOCHASTIC
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The classic Stochastic Oscillator generates crossover signals regardless of broader market context. A crossover in oversold territory looks identical whether the broader trend is strongly bullish, strongly bearish, or completely flat and ranging.

This context-blindness is the primary reason most Stochastic-based approaches underperform in live market conditions. The indicator fires during:
- Trending markets
- Ranging markets
- High-volatility periods
- Low-momentum consolidations

...treating all of them identically.

The Triple Filter addresses this by adding two additional layers of validation that specifically target the two most common failure modes:
- Trading against the dominant trend
- Trading during low-momentum, choppy market conditions

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
WHY THESE THREE COMPONENTS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The combination of the Stochastic Oscillator, the Gaussian Channel, and the Choppiness Index was chosen deliberately. Each component addresses a different dimension of market behavior that the others cannot measure on their own:
- Stochastic Oscillator — measures momentum and overbought/oversold conditions
- Gaussian Channel — measures trend direction with minimal lag
- Choppiness Index — measures whether the market is trending or ranging

Together, they form a three-dimensional filter that validates signals from three independent angles simultaneously: momentum, direction, and market structure.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
COMPONENT 1: STOCHASTIC OSCILLATOR — THE SIGNAL GENERATOR
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The Stochastic Oscillator, originally developed by George Lane in the 1950s, measures the position of the closing price relative to its high-low range over a defined lookback period.
- %K line — the raw stochastic value
- %D line — a smoothed version of %K, used as a signal line

Signal rules:
- Long signal — %K crosses above %D in the oversold zone (below threshold, default 20)
- Short signal — %K crosses below %D in the overbought zone (above threshold, default 80)

Default settings used in this script:
- %K Length: 21
- %K Smoothing: 3
- %D Smoothing: 5

These settings produce a smoother, less reactive version of the Stochastic compared to the classic defaults of 14, 1, and 3 — reducing the number of low-quality crossovers generated before any additional filtering is applied. All parameters are fully configurable.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
COMPONENT 2: GAUSSIAN CHANNEL — THE TREND FILTER
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The Gaussian Channel, originally developed and published on TradingView by © DonovanWall, applies a recursive Gaussian filter to price data to produce an extremely smooth trend estimate.

Unlike a simple moving average, the Gaussian filter uses a weighted multi-pole recursive calculation that minimizes lag while preserving directional accuracy.

The f_filt9x() recursive filter function and the f_pole() pole aggregation function used in this script are directly derived from DonovanWall's original published work. All mathematical credit for the Gaussian filter implementation belongs to DonovanWall.

How it works in this script:

Only the midline of the Gaussian Channel is used as a trend direction signal:
- Midline rising (current value > previous value) → trend is bullish → longs allowed
- Midline falling (current value < previous value) → trend is bearish → shorts allowed
- If the Stochastic fires a crossover but the Gaussian Channel disagrees → signal is blocked

Optional modes:
- Reduced Lag Mode — applies a lag correction to the source before filtering, making trend detection more reactive
- Fast Response Mode — blends the filtered output with the first-pole result to increase responsiveness at the cost of some smoothness

Both modes are optional and disabled by default.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
COMPONENT 3: CHOPPINESS INDEX — THE MOMENTUM FILTER
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The Choppiness Index, based on the concept originally introduced by E.W. Dreiss in 1993, measures the degree of market trendiness versus choppiness by comparing the sum of individual candle true ranges to the total true range over a lookback period.

The formula produces a normalized value:
- Lower values (~38.2) → strong directional trending behavior
- Higher values (~100) → choppy, non-directional price action

How it works in this script:

A user-configurable threshold (default 50) defines the boundary:
- Choppiness Index below threshold → market is trending → signals allowed
- Choppiness Index above threshold → market is choppy → all signals blocked, regardless of Stochastic crossover or Gaussian Channel direction

This prevents trading during low-momentum consolidation periods — historically the most damaging environment for crossover-based systems.

The Choppiness Index filter can be independently enabled or disabled by the user.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
HOW THE THREE COMPONENTS WORK TOGETHER
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Valid LONG signal requires all three:
- %K crosses above %D within the oversold zone
- Gaussian Channel midline is rising
- Choppiness Index is below the threshold

Valid SHORT signal requires all three:
- %K crosses below %D within the overbought zone
- Gaussian Channel midline is falling
- Choppiness Index is below the threshold

Crossovers that satisfy the Stochastic condition but fail one or both additional filters are displayed as small white cross markers on the panel — allowing traders to observe exactly which signals were blocked and why.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
VISUAL ELEMENTS AND PANEL LAYOUT
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Inside the indicator panel:
- %K line — changes color dynamically: green in oversold zone, red in overbought zone, grey in neutral zone. Color intensity is proportional to the distance from each threshold.
- %D line — follows the same color logic at reduced opacity.
- Overbought/Oversold lines — horizontal dashed lines at configurable thresholds.
- Midline — dotted line at level 50.
- Zone background — faint green when %K is oversold, faint red when overbought.
- Gaussian Channel dots — a row of colored circles below the Stochastic lines: green when rising, red when falling, grey when flat.
- Choppiness line — a horizontal line just below the GC dots: green when market is trending (below threshold), grey when choppy (above threshold).
- Signal triangles — green upward triangles for valid longs, red downward triangles for valid shorts.
- Filtered signal markers — small white crosses for signals blocked by the GC or Choppiness filters.
- Status table — bottom right corner, updated in real time, showing the current state of all three filters simultaneously.

On the price chart (optional):
- BUY/SELL labels — appear directly on the price chart at the moment of each valid signal using force_overlay.
- Bar color — candles colored according to Gaussian Channel direction and Choppiness state: bright green when trending up, bright red when trending down, grey when choppy.
- Background flash — optional faint background highlight on the price chart at signal bars.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
SETTINGS AND INPUTS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚙️ Stochastic:
- %K Length (default: 21)
- %K Smoothing (default: 3)
- %D Smoothing (default: 5)
- Overbought Level (default: 80)
- Oversold Level (default: 20)

📡 Gaussian Channel:
- Enable/Disable GC Filter
- Source (default: HLC3)
- Poles 1–9 (default: 4)
- Sampling Period (default: 144)
- Multiplier (default: 1.414)
- Reduced Lag Mode
- Fast Response Mode

📊 Choppiness Index:
- Enable/Disable Chop Filter
- Chop Length (default: 14)
- Chop Threshold (default: 50)

📍 Signals on Price Chart:
- Show BUY/SELL labels on chart
- Show Filter Status Table
- Color bars by signal state

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
CREDITS AND ATTRIBUTIONS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

- Gaussian Channel — Original concept and full implementation by © DonovanWall, published on TradingView as "Gaussian Channel [DW]". The f_filt9x() and f_pole() functions in this script are directly derived from DonovanWall's original work. All mathematical credit belongs to DonovanWall.
- Choppiness Index — Original concept by E.W. Dreiss (1993). Standard public domain implementation.
- Stochastic Oscillator — Original concept by George Lane (1950s). Standard public domain implementation.

The combination of these three components into a unified signal filtering system — including all visual design, panel layout, filter logic, position management, and status table — was developed independently by © AlgoTrade_Pro.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
DISCLAIMER
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This script is published for educational and informational purposes only. It is not financial advice and does not guarantee any specific trading results. Past performance in backtesting does not guarantee future results in live markets. Always conduct your own research and testing before making any trading decisions. Trading involves significant risk of loss.

Haftungsausschluss

Die Informationen und Veröffentlichungen sind nicht als Finanz-, Anlage-, Handels- oder andere Arten von Ratschlägen oder Empfehlungen gedacht, die von TradingView bereitgestellt oder gebilligt werden, und stellen diese nicht dar. Lesen Sie mehr in den Nutzungsbedingungen.