All indicators

BB Machine Learning - Pattern Recognition

Feb 22, 2026

Static chart image
SignalsMachine LearningCandlestickDashboardLiquidityMoving AveragesPatternsVolatility

The BB Machine Learning - Pattern Recognition indicator analyzes historical price behavior at Bollinger Band extremes using K-Nearest Neighbors (KNN) and candlestick pattern recognition to generate high-probability signals. It adapts to current market conditions by learning which patterns led to profitable outcomes on specific instruments and timeframes.

Usage

The Usage section describes how the script can be used to identify mean reversion or breakout opportunities. The tool functions by detecting price proximity to Bollinger Band boundaries and qualifying those touches through a machine learning confidence score.

Mean Reversion Trading

When the market is range-bound, users can look for signals at the band extremes. A long signal occurs at the lower band when the KNN model finds historically successful bounce patterns in similar volatility environments. Users typically set the exit target at the middle band (20-period SMA).

Squeeze Breakouts

The indicator tracks "Squeeze" states where volatility is compressed. Long-duration squeezes often precede explosive moves. The machine learning engine calculates a "Squeeze Outlook," providing a historical ratio of bullish versus bearish breakouts to help traders anticipate the eventual direction.

Pattern Validation

Traders can use the dashboard to verify the statistical reliability of specific candlestick patterns (e.g., Pin Bars, Engulfing) on the active chart. If a Bullish Engulfing pattern at the lower band has a 70% historical win rate while a Pin Bar has only 40%, the ML engine will automatically weight signals from the Engulfing pattern more heavily.

Details

The script's architecture is divided into three primary components:

  • Volatility Engine: Based on standard Bollinger Bands (20 SMA, 2 STDEV), it calculates derived metrics like %B (price position) and BB Width (volatility level) to categorize the market into Squeeze, Expanding, Contracting, or Normal states.
  • KNN Algorithm: The tool describes each price bar using a 10-point normalized feature vector including RSI, volume, body ratio, and %B. When price hits a band extreme, the KNN algorithm searches the last 300+ bars for similar feature vectors. It then measures the historical outcome of those neighbors (up to 10 bars into the future) to determine a confidence score.
  • Pattern Recognition: It identifies 8 specific candlestick formations at the bands (Pin Bar, Engulfing, Doji, Rejection, Multi-touch, Band Walk, Breakout Candle, and Strong Close). Each pattern is tracked individually for its historical success rate on the current chart.

Settings

Bollinger Bands

  • BB Length: The period for the middle SMA (default 20).
  • BB Multiplier: The standard deviation for the upper/lower bands.
  • Source: The price data used for calculation (default: close).

Machine Learning

  • ML Lookback Bars: The historical depth the KNN model scans for matching patterns.
  • Min Confidence %: The minimum threshold required for a signal to trigger (default 60%).
  • Min Historical Samples: The minimum number of similar historical instances needed to validate a signal.
  • Success % Threshold: The percentage of price movement required in historical data to classify a "neighbor" as a success.

BB State Detection

  • Squeeze Threshold %: The width percentage below which the bands are considered to be in a squeeze.
  • Expansion Rate: The rate of width increase required to classify the state as "Expanding."

Risk Management

  • Stop Loss %: The percentage-based stop level from the entry price.
  • Take Profit %: The percentage-based target level from the entry price.

FAQ

How do I interpret the ML Confidence score? The score represents the weighted percentage of similar historical setups that reached the "Success % Threshold" within the defined future candle lookback. Higher percentages indicate stronger historical precedent.

Why are there no signals appearing on my chart? Signals only trigger when price is at a Bollinger Band extreme, the KNN model finds enough historical matches, and the final confidence score exceeds your "Min Confidence %" setting. Try lowering the confidence threshold or increasing the ML lookback.

How can I access the BB Machine Learning - Pattern Recognition indicator? You can get access on the LuxAlgo Library for charting platforms like TradingView, MetaTrader (MT4/MT5), and NinjaTrader for free.

Free indicator

Get free access to this indicator on the platforms below.

TradingView
NinjaTrader
MetaTrader 4/5

Unlock the entire LuxAlgo Library

Every indicator, every strategy, full charts, and complete access to Quant — our AI agent.