LSMA — Least Squares Moving Average
תוכן זה אינו זמין עדיין בשפה שלך.
The endpoint of the rolling least-squares fit: where the regression line says price “should” be right now. Less lag than an SMA of the same length because a fitted line extrapolates the trend rather than averaging it away.
Signature
LSMA(period, on=<source>)| Argument | Type | Required | Notes |
|---|---|---|---|
period |
int Param or literal | Yes | Regression window. Default 25, bounds 2–400. |
on= |
symbol, OHLCV field, or indicator | No | Source. Defaults to the trade symbol’s close. |
Formula
$$ \text{LSMA} = \text{intercept} + \text{slope},(n-1) $$
with slope and intercept from the ordinary least-squares fit of price on bar index.
Warmup
period bars; the raw close until the window fills.
Reading the value
lsma.valuelsma.crossed_above(sma)Example
class LsmaCross(Strategy): """LSMA leads a same-length SMA — trade the spread between them.""" period = Param(25, min=2, max=400) trade = Symbol() sizing = FixedCapital()
def execute(self): lsma = LSMA(self.period, on=self.trade) sma = SMA(self.period, on=self.trade) if lsma.crossed_above(sma) and self.trade.is_flat: self.buy(self.trade, reason="LSMA over SMA") if lsma.crossed_below(sma) and self.trade.is_long: self.sell(self.trade, self.trade.position, reason="LSMA under SMA")Notes
- Ordinary least squares; identical math to
LinearRegression.fittedandLinearRegressionCurve— three names because members search for all three. - Proven by an assert-via-trade run on SPY daily (matched a strategy-side OLS reference to 1e-3 on every bar; zero alarm trades).
TSFis this line extended one bar further.