Linear Regression: A Line Refitted Daily
Linear regression fits the straight line that minimises 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.
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 minimises 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 centre 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. The ten-bar efficiency ratio on this site’s shared 576-bar history has a median of 0.34, so most stretches wander more than they travel.
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. The largest bar range here is 2.338 against a smallest of 0.022, and 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 round trip here is 0.0098 price units, 2% of a median bar’s range and 45% of the smallest bar.
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 neighbouring 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.
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 series that turns constantly. Direction runs here average 2.01 bars across 286 runs, with a longest of 11, so 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 31,760 videos in research/search-study-corpus.jsonl found three with “linear regression”
in the title: median 5,214 views, two channels, a maximum of 18,855. “Bollinger bands” found 311 videos
from 173 channels at a median of 3,816. 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 478 videos at 4,113 and “overfitting” two at 299, in research/broker-coverage.json.
The second figure decides whether the line means anything. That ratio has a tenth percentile of 0.08
and a ninetieth of 0.71, with 30% of bars above 0.5, measured by site/measure_series.py into
research/series-measurements.json. A straight line is a poor description of this series most of the time,
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 centre line, lagging rather than sloping, and never refitted behind you.
I had a regression channel on a chart for most of a week and it looked immaculate. Every low sat on the lower band, every high on the upper, and I had started treating the bands as levels other people could see. Then a fresh run of bars arrived, the fit was recomputed, and the channel that had contained everything no longer did. Nothing about the past had changed, only the line drawn through it.
— Michael Whitman
This page is educational, not financial advice. Test every idea on your own charts before risking money.