Back to Guides
Guide

Linear Regression Slope Indicator: Formula and Units

The linear regression slope indicator formula, what its units mean, why the raw number cannot be compared between charts, and how to make it comparable.

The linear regression slope fits a least squares straight line through the last n closes and plots that line's slope, one value per bar. The formula is the sum of (bar position minus its mean) times (price minus its mean), divided by the sum of (bar position minus its mean) squared, and the answer comes out in price units per bar.

Those units decide what the number is good for, and most pages skip them. A slope of 5 on gold and a slope of 0.002 on EURUSD can be the same trend, so the raw reading cannot be compared between two charts or two timeframes until it is normalised.

What is the linear regression slope indicator?

A trend rate indicator. It answers how fast the best fit line through the last n bars is rising or falling, in price per bar. Each bar gets its own fit over its own window, and the plot is the series of those slopes.

Tulip Indicators puts it this way: "The Linear Regression Slope is a momentum indicator. It fits a line over a moving window using simple linear regression, and returns the slope of that line". Trading Technologies describes the fit itself, a trendline that "uses the least squares method to plot a straight line through prices so as to minimize the distances between the prices and the resulting trendline".

A positive value means the fitted line over that window rises, and zero means it is flat. There is no neutral band and no upper limit, because the output is a rate rather than a level.

What is the linear regression slope formula?

x_mean = the mean of the bar positions in the window, 1 to n
y_mean = the mean of the closes in the window

slope = sum over i of (x_i - x_mean) x (y_i - y_mean)
        -------------------------------------------
        sum over i of (x_i - x_mean) squared

That is Tulip's published form. Three notations are in circulation and they are the same formula: this one in deviations from the means, the version written with sums of products (n times the sum of xy, minus the sum of x times the sum of y, over n times the sum of x squared minus the sum of x, squared), and blueberrymarkets' "Slope = Covariance(x, y) / Variance(x)". Count brackets before you trust any copy and paste: wallstreetmojo.com has a stray closing parenthesis, and Trading Technologies' formulas arrive with the LaTeX unrendered.

Where the slope is in ChartLabs

ChartLabs has it built in, under the name Linear Regression Slope. Open the chart's Indicators menu, go to the Standard list, and look under the Trend heading. It draws in its own pane below the price, with a zero line, which is the only natural level this output has.

Ours follows TA-Lib, so Length is 14 and Source is the close by default, and the plot is the raw slope in price units per bar. A threshold written in percent per bar therefore has to be turned back into price for each symbol you test.

Two of its neighbours are menu entries in their own right. Linear Regression Curve, under Moving Averages, plots the fitted value at every bar, which is the LSMA, and its legend says so. Linear Regression Channel, under Trend, draws the fit and its outer lines on the price pane. R squared has no entry of its own.

A worked example with round numbers

An example, not a recommendation. Five closes, period 5, bars numbered 1 to 5.

BarCloseBar minus meanClose minus meanProduct
1100-2-24
2102-100
31010-10
4104122
5103212

The closes average 102 and the bar numbers average 3. The products add up to 8. The squared bar deviations add up to 4 + 1 + 0 + 1 + 4, which is 10. So the slope is 8 divided by 10, which is 0.8 price units per bar.

To check your own implementation against a published one, Tulip Indicators prints a 15 row worked table at period 5 with 11 slope values in it. All 11 reproduce to two decimals, rounding half up. One trap in Python: the built in round rounds halves to even, which turns 0.575 into 0.57, while the published table rounds half up.

What are the units, and why can't you compare two charts?

The slope is in price units per bar, so its size depends on what the instrument costs and on how long one bar lasts. That makes the raw number useless as a strength score across instruments.

We took one price path in percentage terms and priced it twice, once as EURUSD starting at 1.0850 and once as XAUUSD starting at 2650.00, identical percentage moves bar for bar. The slope over the last 20 bars came out at -0.00230362 on the EURUSD version and -5.626364 on the gold version, a ratio of 2442.40, which is exactly the ratio of the two prices: the same trend reported as two numbers 2,442 times apart.

Timeframe does the same thing. The same total move resampled into four times as many bars gave -0.21269029 over 20 coarse bars and -0.05034868 over the matching 80 fine bars, a ratio of 4.2243.

LuxAlgo states the problem outright: "Raw slope is in price units per bar, so it is not comparable across symbols or timeframes; many tools normalize it by ATR, by percentage, or against its own history before applying thresholds". Other pages tell the reader to do the opposite. blueberrymarkets.com says "The steeper the slope, the stronger the trend", and wallstreetmojo.com that "This slope's absolute value indicates the trend's relative strength. While higher values typically indicate stronger trends...". On a more expensive instrument, or a shorter bar, a steeper slope can be the same trend or a weaker one.

How do you make the slope comparable?

Divide the slope by the latest close and multiply by 100. That gives percent per bar, the same number on any instrument. The two versions above, normalised that way, both read -0.173732% per bar: 2,442 times apart as raw numbers, one reading once normalised. LuxAlgo names a second option, dividing by ATR, which gives the slope relative to the instrument's own recent range rather than to its price.

In the worked example above, a slope of 0.8 against a latest close of 103 is 0.7767% per bar.

What period should you use for the linear regression slope?

Nothing published names a default. Trading Technologies lists Period as a configuration option, described only as "Number of bars to use in the calculations", and Tulip gives none, using 5 in its worked table purely as an example. So choose it from what the period does.

At period 2 the indicator is exactly the one bar price change, and on 400 bars the two agree on every bar. Above that, the window weights are fixed and antisymmetric: the newest and oldest bars carry equal and opposite weight, and the denominator grows as n(n squared minus 1) divided by 12, so 10.0 at period 5, 227.5 at period 14 and 665.0 at period 20. The newest bar's weight falls from 0.200000 to 0.028571 to 0.014286 across those three, and the weights always sum to zero. A longer period is therefore a slower, smoother reading of the same thing, with each individual bar mattering less. Tulip states the trade off: "Larger values for n will have a greater smoothing effect on the input data but will also create more lag."

Because the weights sum to zero, the output has no level, only a rate, so it cannot tell you where price is, only how it is changing.

Does it matter whether my platform numbers the bars from 0 or from 1?

No. Shifting the x axis cannot change the slope. We checked it both ways on the same 20 bars: numbering them 0 to 19 gave -0.21269029, and numbering them 1 to 20 gave the same -0.21269029.

Stretching the axis is a different matter. Numbering those same bars 0, 4, 8 and so on gave -0.05317257, exactly a quarter of the original, because the run between two bars is now four times longer. So if two platforms disagree on a slope value, the cause is the period, the price input or the bar spacing, never where the numbering starts.

What is the difference between the slope, the regression line and R squared?

Three readings come out of one fit. In LuxAlgo's words, the slope is "the fitted change per bar", the fitted value at the newest bar traces the LSMA, the least squares moving average, and R squared is "the squared correlation between price and time over the window", which "grades the fit from 0 to 1". A fourth thing, the linear regression channel, is published both ways: as a drawing tool you drag over a window you choose, and as an indicator refitted on every bar, which is the form ChartLabs ships.

Is the linear regression slope built into MT4, MT5 or TradingView?

TradingView's own built-in indicators page lists "Linear Regression" and carries no entry named Linear Regression Slope. Trading Technologies documents a Linear Regression Slope (LRS) and, separately, a Linear Regression Slope R2. Tulip Indicators ships the function as linregslope. For MT4 and MT5 the question stays open: there is no MetaQuotes documentation under this indicator name to cite, and the two platforms ship different built in sets, so check the indicator list in your own terminal.

ToolWhat its own page publishesDefault periodPrice input
Tulip Indicators (linregslope)Full formula, a 15 row worked table at period 5, references to Kaufman 2013 and Chande 1994None publishedNot configurable on the page
Trading Technologies (LRS)Least squares described in words, slope and intercept formulas, a separate R2 indicatorNone publishedNine choices, from Open to OHLC/4
TradingView built-in listLists "Linear Regression" by nameNot publishedNot published
LuxAlgo library pageSlope, LSMA and R squared separated, and the units warningNot publishedNot published

Tulip adds a warning about its own documentation: "This documentation is still a work in progress. It has omissions, and it probably has errors too."

How do you write a slope rule you can test?

Write down four things before the first bar: the price input, the period, the normalisation, and the threshold in percent per bar rather than in price. Record the period, the price input and the normalisation against every trade you log too, because the same threshold on the same symbol with a different price input is a different rule.

The price input matters because Trading Technologies' Field option offers nine of them, Open, High, Low, Close, Adjusted Close, HL/2, HLC/3, HLCC/4 and OHLC/4, and publishes no default, so "the slope of the chart" is not one number until you have picked one. A threshold like "slope above 0.5" means something different on every symbol, while "slope above 0.1% per bar" means the same thing everywhere.

trendspider.com and wallstreetmojo.com both build rules on a crossing of "the mean value", which neither page defines, and an undefined term cannot be tested.

Where does the linear regression slope fall apart?

The output is unbounded and has no natural neutral level except zero. There is no overbought reading, by construction, so any threshold is yours to justify.

The window is a straight line by assumption, so a curved move, an acceleration or a V shaped reversal inside it is reported as the average rate over that window and nothing else.

And a steep slope is a different question from a well fitted one, though on our own series the two moved together. Taking the 20 steepest 20 bar slopes out of 381 windows, R squared ran from 0.921 to 0.991 with a mean of 0.968, and none of the 20 sat under 0.50. They are still two separate measurements, and the fit has to be read off R squared.

Next step

A slope rule is easy to write down and easy to fool yourself about, which makes it worth a hand test on data you trust. On ChartLabs you replay cleaned data from Dukascopy bar by bar and place the trades yourself, with per-symbol commission and slippage feeding every trade's P&L, and tags to split the analytics by which variant of the rule you were testing. The line itself is the menu entry described above, at Length 14 on the close. One plan, $20 a month or $200 a year, with a 7-day free trial and no credit card required: start the free trial.

Related reading: backtesting a strategy for beginners, forex backtesting, and manual against automated backtesting.

Related Topics

linear-regression-slopeindicator-formulatrend-indicatorsindicator-guidebacktesting