OPEN-SOURCE SCRIPT

Variable Sine Wave Fit [LuxAlgo]

2 544
The Variable Sine Wave Fit indicator uses Ordinary Least Squares (OLS) to fit a dynamic, damped, or expanding sine wave with an underlying linear trend to recent price action. This tool aims to identify cyclical patterns and project their potential continuation into the future, providing a mathematical framework for understanding market regimes and turning points.
This indicator is subject to repainting and is displayed retrospectively.


🔶 USAGE

The indicator fits a complex trigonometric model to the price data within a user-defined window. The resulting fit is displayed as a solid line over historical bars and transitions into a dashed extrapolation for the forecasted period.

To use the indicator effectively, traders should observe the relationship between the price and the RMSE bands. If the price remains within these bands, the current cyclical model is considered to be tracking the price action effectively. If the price breaks significantly outside, the cycle may be shifting or breaking down.

🔹 Extrema Markers

Small dot markers are placed at the local maxima and minima of the dashed forecast line. These serve as visual guides for the timing of potential future turning points based on the current mathematical fit.

🔹 Market Regime Dashboard

The dashboard provides a real-time summary of the fitted model's characteristics:
  • State: Classified based on the amplitude behavior (Damped, Expanding, or Constant) and the linear component (Trending or Ranging).
  • Best Period: The cycle length (in bars) that currently provides the best fit to the data.
  • RMSE: The Root Mean Square Error, representing the average deviation of price from the fit.


🔶 DETAILS

The script solves for the best parameters of the following equation:
y = e^(λ * t) * (a * sin(ω * t) + b * cos(ω * t)) + m * t + c

Where:
  • e^(λ * t): The damping/expansion factor. If λ > 0, the cycle is expanding; if λ < 0, it is damping.
  • a, b: Coefficients determining the phase and initial amplitude of the sine wave.
  • m * t + c: A linear regression component that accounts for the underlying price trend.


The "Best Period" is determined through a grid search that minimizes the Sum of Squared Errors (SSE), ensuring the frequency (ω) matches the most dominant local cycle within the search range.

🔶 SETTINGS

🔹 Settings

  • Window Size (N): The number of historical bars used to calculate the fit.
  • Auto Period: When enabled, the script searches for the best period within the specified min/max range.
  • Fixed Period (P): The period used if Auto Period is disabled.
  • Min/Max Search Period: Defines the boundaries for the automatic cycle search.
  • Forecast Length: The number of bars to project the fit into the future.
  • RMSE Band Multiplier: Determines the width of the bands surrounding the fit based on the fit error.


🔹 Visuals

  • Bullish/Bearish Color: Colors used for the fit line and extrema markers based on the final slope.
  • Band Color: The color of the RMSE-based envelope.


🔹 Dashboard

  • Dashboard: Toggles the visibility of the data table.
  • Position: Moves the dashboard to different corners of the chart.
  • Size: Adjusts the text and table scale.

إخلاء المسؤولية

لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.