OPEN-SOURCE SCRIPT
Phase Regression Oscillator [LuxAlgo]

The Phase Regression Oscillator indicator is a cycle analysis tool that uses an analytical linear least squares fit to extract the instantaneous phase of a specified frequency relative to price action. By solving a system of normal equations on every bar, the script determines the "best fit" sine wave for a rolling window, providing a high-resolution view of the market's cyclical state.
đź”¶ USAGE
The indicator is designed to identify the current stage of a market cycle by fitting a theoretical sine wave to the most recent price data. Unlike traditional oscillators that rely on simple averages or price extremes, this tool uses regression to find the mathematical phase of the trend.
🔹 Interpreting the Components
đź”¶ DETAILS
The script employs a Linear Least Squares approach to model price as:
y = a·sin(ωx) + b·cos(ωx) + c
Instead of using a computationally expensive "grid search" to guess the phase, the indicator uses a 3x3 matrix solver (Cramer's Rule) to analytically find the coefficients a, b, and c that minimize the squared error. The phase (φ) is then extracted using the arctangent of the coefficients.
By defining the period as a percentage of the window size, the tool ensures that the regression always looks for a cycle length that is proportional to the observed lookback, making it adaptable across different timeframes and asset classes.
đź”¶ SETTINGS
🔹 Main Settings
🔹 Levels
🔹 Style
đź”¶ USAGE
The indicator is designed to identify the current stage of a market cycle by fitting a theoretical sine wave to the most recent price data. Unlike traditional oscillators that rely on simple averages or price extremes, this tool uses regression to find the mathematical phase of the trend.
🔹 Interpreting the Components
- Sine Oscillator (Solid Line): This is the primary signal. When the line is positive and green, the cycle is in its ascending or peaking phase. When negative and red, the cycle is in its descending or troughing phase.
- Cosine Component (Dotted Line): This represents the "quadrature" or in-phase component. In cycle analysis, the lead/lag relationship between the sine and cosine lines can help traders identify when a cycle is losing momentum or reaching a turning point.
- Overbought/Oversold Levels: Dashed horizontal lines indicate levels where the cycle fit has reached extreme mathematical synchronization, often preceding a phase shift or reversal.
đź”¶ DETAILS
The script employs a Linear Least Squares approach to model price as:
y = a·sin(ωx) + b·cos(ωx) + c
Instead of using a computationally expensive "grid search" to guess the phase, the indicator uses a 3x3 matrix solver (Cramer's Rule) to analytically find the coefficients a, b, and c that minimize the squared error. The phase (φ) is then extracted using the arctangent of the coefficients.
By defining the period as a percentage of the window size, the tool ensures that the regression always looks for a cycle length that is proportional to the observed lookback, making it adaptable across different timeframes and asset classes.
đź”¶ SETTINGS
🔹 Main Settings
- Window Size (N): The rolling lookback period (in bars) used to calculate the regression fit.
- Period % of Window: Defines the fixed period (P) of the sine wave as a percentage of the Window Size. A value of 100% means the regression looks for a cycle equal to the window length.
🔹 Levels
- Overbought Level: The upper threshold used to identify cycle peaks.
- Oversold Level: The lower threshold used to identify cycle troughs.
🔹 Style
- Bullish Color: Color for the oscillator when positive, the oversold level, and the upward gradient fill.
- Bearish Color: Color for the oscillator when negative, the overbought level, and the downward gradient fill.
- Cosine Color: The color of the dotted quadrature reference line.
- Zero Line Color: The color of the center baseline.
Script open-source
Nello spirito di TradingView, l'autore di questo script lo ha reso open source, in modo che i trader possano esaminarne e verificarne la funzionalità . Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricordiamo che la ripubblicazione del codice è soggetta al nostro Regolamento.
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.
Declinazione di responsabilitĂ
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.
Script open-source
Nello spirito di TradingView, l'autore di questo script lo ha reso open source, in modo che i trader possano esaminarne e verificarne la funzionalità . Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricordiamo che la ripubblicazione del codice è soggetta al nostro Regolamento.
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.
Declinazione di responsabilitĂ
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.