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.
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
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.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
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.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.