OPEN-SOURCE SCRIPT

Quadratic & Linear Time Series Regression [SS]

Hey everyone,

Releasing the Quadratic/Linear Time Series regression indicator.

About the indicator:
Most of you will be familiar with the conventional linear regression trend boxes (see below):

snapshot

This is an awesome feature in Tradingview and there are quite a few indicators that follow this same principle.
However, because of the exponential and cyclical nature of stocks, linear regression tends to not be the best fit for stock time series data. From my experience, stocks tend to fit better with quadratic (or curvlinear) regression, which there really isn't a lot of resources for.

To put it into perspective, let's take SPX on the 1 month timeframe and plot a linear regression trend from 1930 till now:

snapshot

You can see that its not really a great fit because of the exponential growth that SPX has endured since the 1930s. However, if we take a quadratic approach to the time series data, this is what we get:

snapshot

This is a quadratic time series version, extended by up to 3 standard deviations. You can see that it is a bit more fitting.

Quadratic regression can also be helpful for looking at cycle patterns. For example, if we wanted to plot out how the S&P has performed from its COVID crash till now, this is how it would look using a linear regression approach:

snapshot

But this is how it would look using the quadratic approach:

snapshot

So which is better?

Both linear regression and quadratic regression are pivotal and important tools for traders. Sometimes, linear regression is more appropriate and others quadratic regression is more appropriate.

In general, if you are long dating your analysis and you want to see the trajectory of a ticker further back (over the course of say, 10 or 15 years), quadratic regression is likely going to be better for most stocks.

If you are looking for short term trades and short term trend assessments, linear regression is going to be the most appropriate.

The indicator will do both and it will fit the linear regression model to the data, which is different from other linreg indicators. Most will only find the start of the strongest trend and draw from there, this will fit the model to whatever period of time you wish, it just may not be that significant.

But, to keep it easy, the indicator will actually tell you which model will work better for the data you are selecting. You can see it in the example in the main chart, and here:

snapshot

Here we see that the indicator indicates a better fit on the quadratic model.

And SPY during its recent uptrend:

snapshot

For that, let's take a look at the Quadratic Vs the Linear, to see how they compare:

Quadratic:
snapshot

Linear:
snapshot

Functions:
You will see that you have 2 optional tables. The statistics table which shows you:

  • The R Squared to assess for Variance.
  • The Correlation to assess for the strength of the trend.
  • The Confidence interval which is set at a default of 1.96 but can be toggled to adjust for the confidence reading in the settings menu. (The confidence interval gives us a range of values that is likely to contain the true value of the coefficient with a certain level of confidence).
  • The strongest relationship (quadratic or linear).


Then there is the range table, which shows you the anticipated price ranges based on the distance in standard deviations from the mean.

snapshot

The range table will also display to you how often a ticker has spent in each corresponding range, whether that be within the anticipated range, within 1 SD, 2 SD or 3 SD.

You can select up to 3 additional standard deviations to plot on the chart and you can manually select the 3 standard deviations you want to plot. Whether that be 1, 2, 3, or 1.5, 2.5 or 3.5, or any combination, you just enter the standard deviations in the settings menu and the indicator will adjust the price targets and plotted bands according to your preferences. It will also count the amount of time the ticker spent in that range based on your own selected standard deviation inputs.

Tips on Use:

  • This works best on the larger timeframes (1 hour and up), with RTH enabled.
  • The max lookback is 5,000 candles.
  • If you want to ascertain a longer term trend (over years to months), its best to adjust your chart timeframe to the weekly and/or monthly perspective.



And that's the indicator! Hopefully you all find it helpful.
Let me know your questions and suggestions below!

Safe trades to all!
forecastingregressionsstatistics

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?


For real-time updates and premium indicators, consider joining my group at: patreon.com/steversteves
Also on:

Disclaimer