OPEN-SOURCE SCRIPT
Isotonic Regression [LuxAlgo]

The Isotonic Regression indicator provides a monotonic fit to price data, ensuring the resulting line is either non-decreasing or non-increasing over a specified lookback period. This tool is particularly useful for identifying underlying trends and significant price plateaus without the lagging or "overshooting" common in standard moving averages or linear regressions.
🔶 USAGE
Isotonic regression is used to find the best-fitting line to a set of data points under the constraint that the line must move in a specific direction (always up or always down). This creates a "staircase" effect where the model alternates between trending segments and flat plateaus.
🔹 Direction Modes
The indicator offers three ways to determine the fit direction:
🔹 Flat Period Extensions
One of the most powerful features of isotonic regression is the identification of "blocks" or price levels where the trend pauses. When the duration of such a plateau exceeds the Flat Period Threshold, the indicator extends a dashed horizontal line to the current bar, highlighting potential support or resistance levels derived from the regression model.
🔶 DETAILS
The script implements the Pool Adjacent Violators Algorithm (PAVA), which is the standard method for computing isotonic regression.
The algorithm works by partitioning the data into "blocks." If a subsequent data point violates the monotonicity constraint (e.g., price drops in a "non-decreasing" fit), the algorithm pools the current block with the previous one and calculates a weighted average. This process repeats until the entire sequence is monotonic.
For performance efficiency, the indicator utilizes thePine Script® function to render the regression line as a single continuous object rather than hundreds of individual line segments.
🔶 SETTINGS
🔹 Style
Note: This indicator calculates its values based on a historical lookback window, which means the regression line will repaint as new bars are added and the window shifts.
🔶 USAGE
Isotonic regression is used to find the best-fitting line to a set of data points under the constraint that the line must move in a specific direction (always up or always down). This creates a "staircase" effect where the model alternates between trending segments and flat plateaus.
🔹 Direction Modes
The indicator offers three ways to determine the fit direction:
- Auto: Automatically detects the trend by comparing the start and end prices of the lookback period. If the end price is higher, it fits a non-decreasing line; otherwise, it fits a non-increasing line.
- Non-Decreasing: Forces the fit to only move upwards or stay flat, ideal for analyzing bullish structures.
- Non-Increasing: Forces the fit to only move downwards or stay flat, ideal for analyzing bearish structures.
🔹 Flat Period Extensions
One of the most powerful features of isotonic regression is the identification of "blocks" or price levels where the trend pauses. When the duration of such a plateau exceeds the Flat Period Threshold, the indicator extends a dashed horizontal line to the current bar, highlighting potential support or resistance levels derived from the regression model.
🔶 DETAILS
The script implements the Pool Adjacent Violators Algorithm (PAVA), which is the standard method for computing isotonic regression.
The algorithm works by partitioning the data into "blocks." If a subsequent data point violates the monotonicity constraint (e.g., price drops in a "non-decreasing" fit), the algorithm pools the current block with the previous one and calculates a weighted average. This process repeats until the entire sequence is monotonic.
For performance efficiency, the indicator utilizes the
🔶 SETTINGS
- Lookback Length: The number of bars used to calculate the isotonic fit.
- Source: The price data used for the calculation (default is Close).
- Direction: Sets the monotonicity constraint (Auto, Non-Decreasing, or Non-Increasing).
- Flat Period Threshold: The minimum number of bars a price plateau must last to be highlighted with an extension line.
🔹 Style
- Fit Bullish/Bearish: Colors for the regression line based on the detected trend.
- Fit Style/Width: Controls the visual representation (Solid, Dashed, Dotted) and thickness of the main regression line.
- Ext Bullish/Bearish: Colors for the flat period extension levels.
- Ext Style/Width: Controls the visual representation and thickness of the plateau extensions.
開源腳本
秉持TradingView一貫精神,這個腳本的創作者將其設為開源,以便交易者檢視並驗證其功能。向作者致敬!您可以免費使用此腳本,但請注意,重新發佈代碼需遵守我們的社群規範。
All content & scripts provided by LuxAlgo are for educational purposes only and are not financial advice.
免責聲明
這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。
開源腳本
秉持TradingView一貫精神,這個腳本的創作者將其設為開源,以便交易者檢視並驗證其功能。向作者致敬!您可以免費使用此腳本,但請注意,重新發佈代碼需遵守我們的社群規範。
All content & scripts provided by LuxAlgo are for educational purposes only and are not financial advice.
免責聲明
這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。