OPEN-SOURCE SCRIPT
已更新 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.
版本注释
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
版本注释
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.
免责声明
这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。