Linear Regression: A Line Refitted Daily
Linear regression fits the straight line that minimizes the squared distance to every close in a chosen window, and its slope states the trend as a number. The line is refitted on every new bar, so its historical portion moves, and the window length changes the answer entirely.
Covered on this page: TradingView.
How it works
Linear regression draws the straight line that best fits a window of prices. Not a line through two points you picked by eye, as with trend lines, but one computed from every close in the window.
The fit minimizes the sum of the squared vertical distances between the line and each closing price. It is ordinary least squares applied to a price series.
The slope is the output that matters. It states direction and steepness as one figure, which makes trend analysis comparable across instruments trading at very different prices. The line’s end point is the fitted value for the current bar.
The line that will not stay still
And the whole line moves when a new bar arrives. It is refitted from scratch on every bar, so the line you are looking at today is not the line that was there yesterday, and the history it appears to describe was never on the chart at the time.
This is where backtests go wrong quietly. Any test referencing the line has to recompute it bar by bar; using the final fit across the whole history is look-ahead bias.
Change the lookback and you change the answer. A short fit can report a rising slope across the same stretch where a long fit reports a falling one. Both are correct; they answer different questions.
The channel version adds bands at a multiple of the standard deviation of the residuals. The construction resembles Bollinger Bands, except the center is a sloping fit rather than a moving average. Keltner channels and donchian channels use range and extremes instead.
It assumes price is approximately linear in time, and price is not. A stretch that swings hard both ways and ends near where it began fits a straight line badly, and a great many stretches look like that.
In practice
It fits price only and ignores participation. A stretch that trends on thin volume and one that trends on heavy volume produce the same slope.
A longer fit is steadier and slower to turn. One bar matters less, so the slope stops flickering — and the line keeps pointing up after the move that produced it has ended.
And one outlier drags the whole line with it. A single bar several times wider than its neighbors pulls the fit toward itself, because squaring the distances gives it disproportionate weight. An opening gap does the same.
The lower channel is the usual invalidation. It gives a stop loss a level that moves with the fit rather than a round number, which is the honest argument for the channel.
Costs do not care which window you chose. A trader who re-enters every time the refitted slope flips pays the full round trip on each flip — at a hypothetical 0.1% of the position, a dozen flips cost 1.2% of it.
It describes the window and forecasts nothing. Extending the line forward is an assumption you are adding, not an output of the calculation.
Choosing a window honestly
Pick the window from the horizon you actually trade. If your positions last days, fit days; if they last months, fit months. The length is a statement about which trend you care about, which is a decision about you rather than about the market.
The dishonest method is to try several lengths and keep the one that looks best. That is overfitting done by eye, and the line it produces describes your preferences at least as well as it describes the price.
The useful test is whether the conclusion survives at neighboring lengths. Shorten the window a little and lengthen it a little. If the slope keeps its sign both times the trend is in the data; if it flips, you have found a property of the window.
So write the length down before you look. A parameter chosen in advance can be wrong. One chosen afterwards cannot even be evaluated.
What linear regression is not
It is not a forecast. The line stops where the data stops.
It is not support or resistance. Nobody else is trading a fitted line they cannot see.
It is not fixed. It is recomputed on every bar, including its own past.
And it is not a measure of relationship. That is correlation.
How this connects
A straight line fitted through price by least squares - which makes it a trend analysis tool with an explicit definition rather than a drawn one.
That is its genuine advantage over a trend line. Two people fitting a regression to the same bars get the same line; two people drawing trend lines do not.
Standard deviation supplies the channel. Bands at one or two deviations either side turn it into an envelope, in the same family as Bollinger bands.
A moving average answers a similar question differently - the average of the window rather than the best-fit slope through it.
And it repaints as the window moves. The fitted line changes when new bars arrive, so the historical line you see was never the line at the time.
When it fails
In a trading range the slope falls near zero and says so. The tool is working; the failure is reading a flat line as a coming breakout rather than as an absence of trend.
The second failure is the refit itself. A channel that held every bar for weeks can be redrawn by one new bar and no longer hold them.
A third is a short window on a market that turns constantly. When direction changes every few bars, a short fit spends most of its time measuring noise.
A fourth is trading the bands as mean reversion inside a strong trend. Price rides the upper band while the slope keeps rising, and every fade is taken against the fit.
A fifth is the outlier. One unusual bar moves the line, the bands and the invalidation level together.
And a sixth is comparing raw slopes across instruments. The same figure means different things at different price levels.
The original data
A scan of the 24,971 videos in the site’s search study found two with “linear regression” in the title: median 12,034 views, two channels, a maximum of 18,855. “Bollinger” found 268 videos from 204 channels at a median of 4,842. More than a hundred to one, for two tools that both draw a channel around a central line. The difference is the name: one sounds like statistics and the other sounds like trading. “Vwap” found 323 videos at 6,568 and “overfitting” two at 299.
The fit’s own residuals decide whether the line means anything. A straight line is a poor description of any stretch that wanders more than it travels, and the tool’s own goodness-of-fit is the honest way to see that. Check the slope at two or three window lengths before believing any of them; if they disagree, the trend belongs to the window, not the market.
Related
Trend analysis is the wider question the slope is trying to answer. Standard deviation sets the width of the channel and brings its own assumptions. And a moving average is the other way to draw a center line, lagging rather than sloping, and never refitted behind you.
I keep in mind that a regression channel is recomputed as new bars arrive, so the bands are not fixed levels other people can see. A fit that contained everything can stop containing it without anything about the past changing. Treat the channel as a description of the sample, not as support and resistance.
— Michael Whitman
This page is educational, not financial advice. Test every idea on your own charts before risking money. Some links on this page earn a commission if you buy through them. It costs you nothing and it does not decide what appears here or in what order — how these pages are made is set out in our methodology.