Skip to content
Open RaamTrade

LinearRegressionSlope — Linear Regression Slope

The least-squares slope of price against bar index over the window, in price units per bar. Positive means rising, negative falling; the magnitude is the trend’s speed.

Signature

LinearRegressionSlope(period, on=<source>)
Argument Type Required Notes
period int Param or literal Yes Regression window. Default 14, bounds 2–400.
on= symbol, OHLCV field, or indicator No Source. Defaults to the trade symbol’s close.

Formula

$$ \text{slope} = \frac{n\sum iC_i - \sum i \sum C_i}{n\sum i^2 - (\sum i)^2} $$

with $i = 0 \ldots n-1$, oldest to newest. The denominator depends only on n and is never zero for n ≥ 2.

Warmup

period bars; 0 until then.

Reading the value

ls.value
ls.value_at(1)
ls > 0
ls.crossed_above(0)

Example

class SlopeFilter(Strategy):
"""Only take breakouts while the 50-bar slope is up."""
period = Param(50, min=2, max=400)
channel = Param(20, min=1, max=400)
trade = Symbol()
sizing = FixedCapital()
def execute(self):
ls = LinearRegressionSlope(self.period, on=self.trade)
dh = DonchianHighTrue(self.channel, on=self.trade)
if ls.value > 0 and self.trade.close >= dh.value and self.trade.is_flat:
self.buy(self.trade, reason=f"Breakout, slope {ls.value:.3f}")
if ls.value < 0 and self.trade.is_long:
self.sell(self.trade, self.trade.position, reason="Slope down")

Notes

  • Ordinary least squares, identical math to LinearRegression.slope — this single-value form exists so the slope can be smoothed (SMA(5, on=ls)) and crossed, which multi-value outputs cannot.
  • Proven by an assert-via-trade run on SPY daily (matched a strategy-side OLS reference to 1e-4 on every bar; zero alarm trades).
  • In price units per bar — divide by price (or use a z-scored source) to compare across instruments.