OPEN-SOURCE SCRIPT
OLS Slope For Loop [MiesOnCharts]

Ordinary Least Squares Slope For Loop
An ensemble of least squares regression lines, one for every window length in a range. Each fits a straight line to recent price and votes on whether that line points up or down. The plotted score is the margin of the vote, running from -1 when every window is falling to +1 when every window is rising.
Built and tuned for the daily timeframe. The inputs adjust for others.
Why an ensemble
Any single regression length is an arbitrary pick, and the best one is only obvious afterwards. Short windows turn fast and catch noise. Long windows are steady and late. This runs the whole range at once and treats direction as a vote, so no single length decides the answer.
Why a slope instead of a crossing
Price above a moving average, or price above where it sat some bars ago, both compare one number against one other number. A single violent bar can push either across the line.
A regression slope is fitted to every bar in its window. One outlier is one point among many, so the fit barely moves. A wick, a gap or a liquidation candle does not get a vote of its own.
That resistance is the whole trade, and it cuts both ways. The reading holds steady when price is chopping inside a range, which is where crossing based tools whipsaw. It is also slower to commit during a clean fast run, where a simple moving average vote will usually be ahead of it. This is meant to sit beside those tools rather than replace them.
How it works
A loop steps from the min window to the max window. Each step fits a least squares line over that many bars and votes +1 if the line rises, -1 if it falls. The votes are averaged into the score.
Every member keeps two running totals that update once per bar, so its slope comes out at a fixed cost no matter how long its window is. Re-summing every window on every bar would not survive a long chart.
A second line shows the average move the fitted lines imply, as a percent of price. It is rescaled to share the vote's axis and clipped at the edges, so if it sits pinned at the top it has run off the scale rather than maxed out. The exact figure is in the data window as Consensus Slope %.
Signals
Score at or above the threshold turns the state to uptrend and everything green. Score at or below the mirror of it turns the state to downtrend and everything red. In between, the previous state holds.
Holding between the thresholds is deliberate. A brief loss of agreement does not end a regime, and the state only turns when the other side wins outright. Triangles mark each flip, and both flips have alerts.
Inputs
How to use it
Limits
Disclaimer
This indicator is provided for educational and informational purposes only and does not constitute financial, investment, or trading advice, nor a recommendation to buy or sell any asset.
An ensemble of least squares regression lines, one for every window length in a range. Each fits a straight line to recent price and votes on whether that line points up or down. The plotted score is the margin of the vote, running from -1 when every window is falling to +1 when every window is rising.
Built and tuned for the daily timeframe. The inputs adjust for others.
Why an ensemble
Any single regression length is an arbitrary pick, and the best one is only obvious afterwards. Short windows turn fast and catch noise. Long windows are steady and late. This runs the whole range at once and treats direction as a vote, so no single length decides the answer.
Why a slope instead of a crossing
Price above a moving average, or price above where it sat some bars ago, both compare one number against one other number. A single violent bar can push either across the line.
A regression slope is fitted to every bar in its window. One outlier is one point among many, so the fit barely moves. A wick, a gap or a liquidation candle does not get a vote of its own.
That resistance is the whole trade, and it cuts both ways. The reading holds steady when price is chopping inside a range, which is where crossing based tools whipsaw. It is also slower to commit during a clean fast run, where a simple moving average vote will usually be ahead of it. This is meant to sit beside those tools rather than replace them.
How it works
A loop steps from the min window to the max window. Each step fits a least squares line over that many bars and votes +1 if the line rises, -1 if it falls. The votes are averaged into the score.
Every member keeps two running totals that update once per bar, so its slope comes out at a fixed cost no matter how long its window is. Re-summing every window on every bar would not survive a long chart.
A second line shows the average move the fitted lines imply, as a percent of price. It is rescaled to share the vote's axis and clipped at the edges, so if it sits pinned at the top it has run off the scale rather than maxed out. The exact figure is in the data window as Consensus Slope %.
Signals
Score at or above the threshold turns the state to uptrend and everything green. Score at or below the mirror of it turns the state to downtrend and everything red. In between, the previous state holds.
Holding between the thresholds is deliberate. A brief loss of agreement does not end a regime, and the state only turns when the other side wins outright. Triangles mark each flip, and both flips have alerts.
Inputs
- Source: the price series the lines are fitted to.
- Min Window and Max Window: the shortest and longest lookbacks in the ensemble. A wider span mixes more timescales and gives a smoother, slower score.
- Window Step: the spacing between members. Smaller packs in more lines, finer but heavier. Larger uses fewer, coarser ones.
- Trend Threshold: how much agreement is needed before the state flips. Higher is more selective and later. Lower reacts sooner and flips more often.
- Show Consensus Slope and Fill To Zero: display toggles.
How to use it
- Read the colour as a regime filter. A score near an extreme means every horizon agrees. A score near the middle means they are split and directional trades have less behind them.
- Watch it beside a faster tool. When that one has flipped and this has not, the move so far rests on the newest bars rather than on the shape of the whole window.
- Check the slope as well as the score. A full vote on a shallow drift and a full vote on a steep run look identical in the score.
Limits
- It is reactive. It describes the trend price is already in and will lag turns. It does not forecast.
- Nearby window lengths give similar answers, so a narrow range delivers less of the averaging benefit than a wide one.
- The score moves in steps set by how many members there are. A small ensemble gives a coarse, stair stepped reading, and one member changing its mind moves the score a long way. Wider spans and smaller steps smooth this out.
- Runtime grows with the number of members.
- No setting is right everywhere. Test on your own market and timeframe.
Disclaimer
This indicator is provided for educational and informational purposes only and does not constitute financial, investment, or trading advice, nor a recommendation to buy or sell any asset.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.