OPEN-SOURCE SCRIPT
Updated Segment Regression

As an example of the descriptive power of Pine Script, this very short example traces a 'segment regression', a result not entirely obvious with so few lines of code, repositioning them when the previous inference moves away from the graph beyond the pre-set limit.
A trick used is to restart the new inference segment
- from the maximum reached in the previous trend, when positive (slope>0)
- from the minimum reached in the previous trend, when negative (slope<0)
The result can in my opinion be easily used to build strategies.
A trick used is to restart the new inference segment
- from the maximum reached in the previous trend, when positive (slope>0)
- from the minimum reached in the previous trend, when negative (slope<0)
The result can in my opinion be easily used to build strategies.
Release Notes
Version 2: added a line to determine a channel, not considering a fixed number of bars to avoid the obvious impact of the last lines before the breakout
Release Notes
This is an update that also includes more detail on the logic underlying the indicator.The study presented here extends the classic 'linear regression' to that of 'segmental linear regression'.
The linreg() tool is therefore assumed to be known, which is initially applied without any variation, with the starting point equal to the first bar of the graph and with a minimum number of bars equal to those expected.
At each subsequent frame, then:
1) the percentage distance between the real value and the one inferred with the current regression segment is evaluated
2) if this calculated percentage distance is lower than the desired one or if it has the same sign as the slope of the current regression segment, nothing is done
3) if this percentage distance is higher than the desired one in the opposite direction to the slope of the regression segment, the starting point of the new regression segment is updated with
3.1) the point where the previous graph touched the minimum, if the slope of the previous inference segment was negative
3.2) the point where the previous graph touched the maximum, if the slope of the previous inference segment was positive
Please note that in the moments in which the graph is lateral, the slope of the regression segment can change sign even without activating point 3) above.
Ultimately, you get an indicator that is able to follow the graph in a much more adequate way than an ordinary linear regression.
It is my opinion that with this indicator as a starting point, good strategies can be implemented.
Open-source script
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.
Disclaimer
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.
Open-source script
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.
Disclaimer
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.