Below is a detailed description of the "Cycle Finder with Polynomial Regression" script:
---
**Overview**
This TradingView indicator is designed to reveal the cyclical behavior of a stock’s price performance over the course of a year. It aggregates historical weekly data—specifically, the percentage change from the start-of-year price—and then applies a quadratic (degree‑2) polynomial regression to smooth out the cycle, especially addressing the abrupt drop at the start of each new year.
---
**Key Components**
1. **Data Aggregation Across Years**
- **Year Initialization:** At the start of each year, the indicator records the opening price as a baseline (i.e., the first trading day’s price for that year).
- **Weekly Calculation:** For each completed week, it calculates the percentage change from the recorded year-open to the close of the last bar in that week. This calculation is performed for every year, and the results are stored in persistent arrays that hold cumulative sums and counts for each week (from week 1 to week 53).
2. **Raw Weekly Average Move**
- For any given week (based on the current week number), the script computes the average percentage move. This "raw" average is obtained by dividing the cumulative sum of weekly moves by the number of years that have data for that week. - This raw average represents the unadjusted, historical average performance of the stock for that particular week relative to the start-of-year price.
3. **Polynomial Regression for Smoothing**
- **Purpose:** Because the raw cycle data resets at the start of each new year (often resulting in an abrupt drop to zero), the script employs polynomial regression to create a smooth, continuous cycle.
- **Method:** It fits a quadratic polynomial (i.e., \( y = a_0 + a_1x + a_2x^2 \)) to the set of average weekly moves across weeks 1 to 53. - It accumulates the necessary sums (e.g., sum of week indices, squares, cubes, etc.) for all weeks with available data. - These sums are used to solve the normal equations for quadratic regression, yielding coefficients that define the best-fit curve.
- **Evaluation:** The polynomial is then evaluated at the current week number to provide a smoothed cycle value that transitions seamlessly across the year boundary.
4. **Plotting the Indicator**
- **Smoothed Cycle Curve (Blue):** The indicator plots the output of the polynomial regression, which represents the smoothed cyclical pattern of the stock’s price move relative to the start of the year.
- **Raw Data Points (Red, Optional):** For reference, the script can also plot the raw weekly average moves as red markers. This allows you to compare the underlying historical data with the smoothed regression curve.
---
**Usage and Benefits**
- **Visualizing Cycles:** By plotting a smooth, continuous curve that represents average weekly performance over many years, traders can better visualize seasonal or cyclical patterns in the stock’s price behavior.
- **Smoothing Transitions:** The polynomial regression mitigates the sharp drop at the beginning of a new year, offering a more realistic view of how the cycle evolves continuously throughout the year.
- **Adaptable to Any Ticker:** The indicator automatically uses the primary ticker symbol on the chart, making it versatile and applicable across different stocks without further configuration.
---
**Conclusion**
This script is a powerful tool for analyzing cyclical trends in stock performance. It takes a straightforward approach—calculating weekly percentage moves from the year's start, aggregating historical data, and then applying a quadratic regression to smooth the results—making it easier to identify recurring patterns that may inform trading decisions.
---
Feel free to adjust the polynomial degree or refine the aggregation method based on your specific analysis needs.
Script de código aberto
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.
For quick access on a chart, add this script to your favorites — learn more here.
As informações e publicações não devem ser e não constituem conselhos ou recomendações financeiras, de investimento, de negociação ou de qualquer outro tipo, fornecidas ou endossadas pela TradingView. Leia mais em Termos de uso.
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.
For quick access on a chart, add this script to your favorites — learn more here.
As informações e publicações não devem ser e não constituem conselhos ou recomendações financeiras, de investimento, de negociação ou de qualquer outro tipo, fornecidas ou endossadas pela TradingView. Leia mais em Termos de uso.