WhitmanTrading

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

A candlestick chart of the site's shared price history. The headline on the chart reads: The straight line that best fits the prices.
The straight line that best fits the prices. Illustrative chart - not real market data.

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.

A gently rising stretch of the long price series. The headline on the chart reads: It minimises the squared distance to every close.
It minimises the squared distance to every close. Illustrative chart - not real market data.

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.

A calmly advancing stretch of the long price series. The headline on the chart reads: The slope is the trend, stated as a number.
The slope is the trend, stated as a number. Illustrative chart - not real market data.

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

A choppy, directionless stretch of the long price series. The headline on the chart reads: And the whole line moves when a new bar arrives.
And the whole line moves when a new bar arrives. Illustrative chart - not real market data.

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.

A flat, quiet stretch of the long price series. The headline on the chart reads: Change the lookback and you change the answer.
Change the lookback and you change the answer. Illustrative chart - not real market data.

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.

A strongly rising stretch of the long price series. The headline on the chart reads: The channel version adds bands at a fixed deviation.
The channel version adds bands at a fixed deviation. Illustrative chart - not real market data.

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.

A declining stretch of the long price series. The headline on the chart reads: It assumes a straight line, and price is not one.
It assumes a straight line, and price is not one. Illustrative chart - not real market data.

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

A candlestick chart with a volume histogram beneath it, with the volume histogram emphasised. The headline on the chart reads: It fits price only and ignores participation.
It fits price only and ignores participation. Illustrative chart - not real market data.

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 long-horizon candlestick view of the same price series. The headline on the chart reads: A longer fit is steadier and slower to turn.
A longer fit is steadier and slower to turn. Illustrative chart - not real market data.

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.

A candlestick series containing several opening gaps, with the largest opening gap marked. The headline on the chart reads: And one outlier drags the whole line with it.
And one outlier drags the whole line with it. Illustrative chart - not real market data.

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.

A declining stretch of the long price series, with the entry price and the level at which a stop would trigger drawn as horizontal lines. The headline on the chart reads: The lower channel is the usual invalidation.
The lower channel is the usual invalidation. Illustrative chart - not real market data.

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.

A candlestick chart of the site's shared price history, annotated with the round-trip cost. The headline on the chart reads: Every round trip costs 2% of a bar.
Every round trip costs 2% of a bar. Illustrative chart - not real market data.

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.

A 72-bar candlestick section of the shared price history. The headline on the chart reads: It describes the window and forecasts nothing.
It describes the window and forecasts nothing. Illustrative chart - not real market data.

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

A sideways, range-bound candlestick series. The headline on the chart reads: In a range the slope is near zero and says so.
In a range the slope is near zero and says so. Illustrative chart - not real market data.

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.

A 72-bar window of the shared price history, cut short at the decision bar. The headline on the chart reads: The slope is up and price is at the top band. Buy?
The slope is up and price is at the top band. Buy? Illustrative chart - not real market data.

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.

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.

What I actually do

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.