OPEN-SOURCE SCRIPT

OLS Slope For Loop [MiesOnCharts]

304
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

  • 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.

Penafian

Maklumat dan penerbitan adalah tidak bertujuan, dan tidak membentuk, nasihat atau cadangan kewangan, pelaburan, dagangan atau jenis lain yang diberikan atau disahkan oleh TradingView. Baca lebih dalam Terma Penggunaan.