Iteratively Reweighted Least Squares (IRLS) [Jamallo]Author's Note: This script is published as a unique mathematical contribution to TradingView's open-source public library. It is intended to introduce a novel application of a robust statistical method for the community and developers to study, adapt, and build upon, rather than to serve as a standalone, out-of-the-box trading strategy.
Introduction
Almost all moving averages and smoothing filters in technical analysis treat historical price data equally or apply a fixed mathematical decay (like an EMA). The problem? A massive, anomalous wick or a sudden volatility spike will inevitably drag the average away from the true underlying market consensus.
Enter Iteratively Reweighted Least Squares (IRLS) .
IRLS is a robust statistical method that calculates a "consensus" price by actively identifying and down-weighting outliers. Instead of letting a large wick distort the line, the algorithm assigns less weight to prices that deviate furthest from the current estimate. The result is a filter that cuts through noise, ignores price-distant spikes, and naturally locks onto the dominant, high-density price levels.
How It Works
The indicator uses the Hardy weight function to determine how heavily each historical candle influences the current estimate. On every bar, the algorithm checks the distance of each sample from the current consensus and iteratively refines the line until it converges on a robust mean. Epsilon — the outlier rejection scale — is derived dynamically from the average High–Low range, keeping the filter dimensionless and consistent across all instruments and timeframes.
Parameters
Window Size (N) : The rolling lookback window of historical samples the kernel considers. Larger values produce a smoother, slower-responding line.
Sparsity (s/N) : The core behavioral control. Dictates the fraction of the window allowed to "vote" on the estimate.
Low Sparsity (e.g., 0.1) : Only the 10% of samples closest to the current estimate participate. Produces a snappy, selective line that locks tightly onto the most dominant price cluster.
High Sparsity (e.g., 1.0) : All samples participate, resulting in a smoother, more conventional robust mean.
Gamma (ε scale) : Controls the strength of outlier rejection. Lower values enforce harsh, median-like rejection. Higher values soften the rejection toward a standard weighted mean.
Iterations : The number of reweighting convergence passes per bar. 2–3 is sufficient for practical convergence.
Potential Applications
The Hardy IRLS filter provides a unique lens into market structure by shifting the focus from simple time-averaged prices to spatial price consensus. Because it rejects price-distant wicks and noise spikes by design, it can serve as a foundation for:
Custom trailing stops
Dynamic support and resistance trackers
Baseline trend or regime filters
Feel free to inspect the open-source code, experiment with extreme sparsity and gamma settings, and integrate the IRLS core into your own quantitative projects.
References
Li Shuang, "Sparse Representation of Hardy Function by Iteratively Reweighted Least Squares," 2020 International Symposium on Computer Engineering and Intelligent Communications (ISCEIC), IEEE, 2020. DOI: 10.1109/ISCEIC51027.2020.00020
אינדיקטור Pine Script®






















