All indicators

Support & Resistance AI (K means/median)

Aug 21, 2023

Static chart image
Support and ResistanceMachine Learning

The Support & Resistance AI (K means/median) indicator utilizes unsupervised machine learning clustering algorithms to identify historically significant price zones where reversals or continuations are likely to occur.

Usage

The Usage section describes how the script can be used to identify key market levels through data clustering. Traders can use this tool to determine precise entries, exits, and risk management zones based on statistical density rather than subjective trendlines.

  • Directional Trading: When the price is within a cluster zone and the trader expects a continuation, the cluster center serves as a reference point. A stop-loss can be placed at the 1 standard deviation (SD) band surrounding the cluster to define the risk-to-reward ratio.
  • Reversal Trading: Cluster centers act as high-probability support and resistance levels. Traders can look for price pivots at these levels to initiate counter-trend positions.
  • Cluster Density Analysis: A table in the top-right corner displays the density of each cluster. Higher density percentages indicate that price has historically spent more time at that level, suggesting a "gravitational" pull or stronger significance for future reactions.

Details

The script applies the K-means (or K-median) algorithm to historical price data. The process involves initializing cluster centers, assigning historical bars to the nearest cluster based on Euclidean distance, and iteratively updating the center until the points stabilize.

Unlike traditional support and resistance identification—which relies on manual trendlines, psychological round numbers, or moving averages—the K-clustering approach groups similar price behavior patterns mathematically. By calculating the standard deviation within each cluster, the tool provides a measure of volatility:

  • Low Standard Deviation: Indicates price stability within the cluster, allowing for tighter stop-losses.
  • High Standard Deviation: Indicates higher volatility, suggesting the need for wider stops to avoid premature exits.

The inclusion of the K-median method addresses a common limitation of standard K-means: sensitivity to outliers. While K-means uses averages, K-median is more robust against extreme price spikes caused by news events or market anomalies.

Settings

  • Number of clusters: Selects between 3 to 5 clusters. Fewer clusters (3) are recommended for intraday trading, while more clusters (4-5) are better suited for daily timeframes.
  • Cluster Method: Allows switching between "K means" (for data without major outliers) and "K median" (more robust for volatile data).
  • Bars back to train on: Determines the historical lookback period used for the clustering calculation.
  • Show SD Bands: Toggles the visibility of the 1 standard deviation bands around the cluster centers.

FAQ

How do I interpret the Cluster Density table? The table shows the percentage of data points assigned to each cluster. Higher percentages indicate stronger historical significance, meaning the level has acted as a frequent area of interest for market participants.

What is the benefit of using K-median over K-means? K-median is less sensitive to price outliers. If the historical data contains extreme spikes or flash crashes, K-median will provide a more accurate representation of the "true" support/resistance levels by focusing on the median rather than the average.

How do I access the Support & Resistance AI (K means/median) 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.