OPEN-SOURCE SCRIPT

Lagging Session Regression Channel

Updated
Hello Traders !

Note :
This is my very first published script on trading view & from brainstorming an idea to developing to the finched product it was imperative to me for the indiactor and every one of its features to be of some meaningfull use. If you like the idea of statsitics being able to predict future prices in the market then this indicator may be usefull in your trading arsenal.

Introduction :
Lagging Session Regression Channel (LSRC) is a statistical trend analysis indicator that "laggs" the market by the user defined session, by defualt a day, by doing so the indicator leverges the ability of simple linear regression to predict future asset price.(This can be used on any asset in any market in any time frame)

Options & inputs :

- Bar regression lookback :
The value of bars back from the lats session change, if the seesion time is equivelnt to the the chart timefrmae then the regression line will not lag price, i.e it will act as a stantdard lineer regression channel chnaging on evrey last confimred bar.

- Standard Deviation lookback :
The value of bars from the last session change to cacluate the unbiased standard deviation, The lookback can be set to > or < the regression lookback to cauture > or < less asset volatility. (note this is the same as the residual standard deviation)

- Predicted price at nth bar :
if you whant to know the predicted close price value at any given point in the regression and to the RHS of the regression.

- Regression Line colors group :
Changes the colors of each plotted line.
- OLS Line color : is only changeable when trend color is set to false / unticked.

- Visable deviations group :
Plots the lines that you want on chart, e.g if "Show DEV1" and "Sow DEV SUB1" are the only inputs ticked then they will be the only lines ploted along with the simple linear regression line.

- Regression Line Dynamics group :
All inputs in this group change the regressions calculations given the bar lookback is constant / the same.
- Trend color : if set too true, when the close of the proceding real time bar is greater than the simple linear regression line from the last confimred session the line will be colored green, if otherwise the close is below the simple linear regression line the line will be colored red.

- Extend regression line :
snapshot

This is the same chart image as seen on the publication chart image but with Extend regression line set to true, this allows the trader to test the valdity of the regression and how well it predicts future price, as seen on the M15 chart of BTCUSD above the indicator was pritty good at doing this.

- Standard deviation channel source :
Source for standard deviation to be calculated on. note if this is set to a varible other than the close then this will no longer be the resdiaul standard deviation, as of now "LSRC 1.0" the regression uses only the close for y / predicted values.

- Time elasped unitl next regression calculation :
The session time until the next LSRC will be calculated and plotted

Label LSRC stats :
snapshot

- STAN DEV : the standard deviation used to cacluateed the deviation channels
- MIN : The lowest price across the regression
- MAX : The highest price across the regression
- n bars above dev 1 : The number of bars that closed above the first standard deviation channel across the entire regression calculation
- n bars below sub dev1 : The number of bars that closed below the first standard deviation channel.
- Regression Price : The output of "Predicted price at nth bar" input.



Hope you find this usefull !
I will continue too try improve this script and update it accordingly.
Release Notes
LSRC V2

Hello Traders !

snapshot

I have updated the existing LSRC, updates include :
- Trend Trailing Regression Lines from priviois sessions, I took inpiration from @LoneomeTheBlue Linear Regression Channel
- R^2 feature, this is a bounded statistic ranging from 0 - 1, This statistic measures the accuracy of the regression, more formaly.
"The coefficient of determination, or R^2 is a measure that provides information about the goodness of fit of a model. In the context of regression it is a statistical measure of how well the regression line approximates the actual data. It is therefore important when a statistical model is used either to predict future outcomes"
- Max bars back, from 500 - 1000
- general bug fixes

In conclusion, I belive this is now one they best linear regression lines in trading view, i will continue to update the script and am open to any ideas, Enjoy !!!
Release Notes
LSRC V3 updates :

Line backround fills
- You can now change the colors of the line fills between each regression line

T test for testing significance of a slope coeffiecnt estimate
- To test if there is a statistically significant relationship between our xj and y variable or if xj is a valid and useful linear predictor of y , in this case ia time (e.g. 1 day) a useful linear predictor of close price, A T test on the regression slope coefficient estimate of xj may be carried out.

The regression T test, tests if the slope coefficient of xj equals 0, if so no relationship exists between x and y, in this case this would mean time (over the given lookback) is not a useful linear predictor of close price, note however a trend between time and close price may just exist just not in the linear case, an exponential or logarithmic regression may fit the data and describe the relationship between time and close price very well i.e. validating a market trend over a certain lookback

The T test measures how many estimated standard deviations the slope coefficient is away from zero and given the degrees of freedom (n-k-1 = n-2 in the SLR case) and significance level a critical value may be found to test the test statistical against and decide if the effect of xj on y is statistically different from 0. In the two tail case (The case this model takes into consideration) were we test to see if the slope coefficient estimate is not > 0 or < 0 but not equal to 0 in the alternative case, the rejection rule were we reject Ho (slope coefficient equals 0) in favor of H1 (slope coefficient is different from or not equal to 0) would be given by the identity for rejection which is, if the absolute value of the t-test is greater than the critical value then reject H0 in favor of H1 at the alpha significance level. visually this would mean the t value lies within the rejection region were its value is greater / less than the critical values.

If the number of degrees of freedom is large, the t-distribution can be approximated by the standard normal distribution, in practice when the number of observations (number of j's such that their are xj observations) is > 122 the critical values for the t-test for 10% alpha is 1.64, for the 5% alpha is 1.96 and for the 1% alpha is 2.58, this means that when the t-test stat is > than these critical values it at a higher standard deviation away from 0 and it falls in all the rejection regions, if all their is a statistically significant relationship the table will state "β1 ≠ 0, reject H0 at the alpha significance level, β1 is statistically significant / different from 0.".

note this feature is best to apply on the 1D timeframe, as the chart time frame decreases the t-test value decreases and cannot be displayed.

General code clean up

Linear RegressionstatisticsTrend Analysis

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?

Disclaimer