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
polyline.new()
🔶 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.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Build your own edge with AI: luxalgo.com
All content provided by LuxAlgo is for informational & educational purposes only. Past performance does not guarantee future results.
All content provided by LuxAlgo is for informational & educational purposes only. Past performance does not guarantee future results.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Build your own edge with AI: luxalgo.com
All content provided by LuxAlgo is for informational & educational purposes only. Past performance does not guarantee future results.
All content provided by LuxAlgo is for informational & educational purposes only. Past performance does not guarantee future results.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.