Rolling Window Linear Regression

This linear regression indicator automatically selects data from a specified window of time and is only rolled forward once a new trading session starts. This way the linear regression that's charted does not include any data from the current trading session, it is truly forecasted, and it won't repaint after each candle like most linear regression indicators.

Trading linear regressions is always based on the assumption that the forecasted trend will continue. It's also based on this assumption that I choose not to automate trading the rolling window regressions. Because if you don't assume that over the forecasted month, week, or day that the trend will continue; then you can anticipate a break of the 3rd deviation and do a "counter trend" trade.

When the mean is orange, it identifies the range of candles that's being sampled to create the linear regression calculation.

The sample sizes are 3 months for the daily timeframe, 3 weeks for the 4H, and 3 days, for anything under 4H.

The reasoning for 3 of each is because 2 happens to be too little data and 4, 5, 6 reduces the number of signals you get between the second and third deviations for a mean reversion trade. However, you can change the lookback from 1 to infinity.

When the mean is red, new data isn't taken into account. Red signifies out of sample price action with a forecasted linear regression , and that is what's actually tradable.

Just as a rule, no trades past the third deviation unless you're anticipating a counter trend; and in most cases when you anticipate it, you're likely wrong.

I have made a lot of modifications over the last year to alexgrover's TF Segmented Linear Regression code to create this indicator. The math relating to the linear regression and standard deviations were created by him in his indicator.

The Creative Commons Attribution-ShareAlike 4.0 International License attached to it requires that I link to the license.

Joe Baus
Protected script
This script is published closed-source and you may use it freely. You can favorite it to use it on a chart. You cannot view or modify its source code.

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?