OPEN-SOURCE SCRIPT

Isotonic Regression Oscillator [LuxAlgo]

2 932
The Isotonic Regression Oscillator indicator aims to quantify the degree of trendiness and structural complexity in price movement by comparing non-decreasing and non-increasing fits. It uses the Pool Adjacent Violators Algorithm (PAVA) to determine the best-fitting monotonic sequence for a given period, providing a normalized oscillator that highlights the strength and direction of the underlying trend.

Note: The isotonic regression fit displayed on the price chart is subject to repainting and is displayed retrospectively to illustrate the most recent calculation window.


🔶 USAGE

The indicator consists of an oscillator oscillating between -100 and 100, and a visual fit line displayed on the price chart.

🔹 Interpretation
  • Positive Values: Indicate that a non-decreasing (bullish) fit has a lower Mean Squared Error (MSE) than a non-increasing fit. Higher values suggest a more complex, multi-step bullish structure.
  • Negative Values: Indicate that a non-increasing (bearish) fit has a lower MSE. Lower values suggest a more complex bearish structure.
  • Zero Crosses: A crossing of the zero line indicates a shift in the "best fit" direction, signaling a potential change in the dominant trend bias.


🔹 Fit Complexity
The magnitude of the oscillator is determined by the number of "pools" or steps in the regression fit. A value near 100 or -100 suggests a highly granular fit that closely follows the price movement, while values near 0 suggest a very simple, flat, or linear-like monotonic structure.

🔶 DETAILS

🔹 The PAVA Algorithm
Isotonic regression involves finding a series of non-decreasing (or non-increasing) values that are as close as possible to the original data points. The script implements the Pool Adjacent Violators Algorithm (PAVA). This algorithm works by iteratively averaging adjacent values that violate the monotonic constraint (e.g., in a non-decreasing fit, if a previous value is greater than the current value, they are pooled together and averaged).

🔹 MSE-Based Selection
For every bar, the indicator calculates two regressions: one forced to be non-decreasing and one forced to be non-increasing. It calculates the Mean Squared Error (MSE) for both. The fit with the lower MSE is selected as the representative model for the current price action.

🔹 Normalization
The oscillator value is normalized based on the number of unique "pools" (constant segments) found by the PAVA. The formula used is:
((Number of Pools - 1) / (Period - 1)) * 100
This scales the complexity of the trend into a readable range of 0 to 100 (or -100 for bearish fits).

🔶 SETTINGS

  • Period: The lookback window used to calculate the isotonic regression fits.
  • Source: The price data used for the calculations (defaults to Close).


🔹 Style
  • Bullish Color: The color used for the oscillator and fit line when the bullish fit is dominant.
  • Bearish Color: The color used for the oscillator and fit line when the bearish fit is dominant.
  • Fit Line Width: Controls the thickness of the polyline fit displayed on the chart.
  • Fit Line Style: Sets the visual style (Solid, Dashed, or Dotted) of the regression fit line.

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.