Click or drag to resize

Least Squares Moving Average

The Least Squares Moving Average (Lsma) first calculates a least squares regression line over the preceding time periods, and then projects it forward to the current period.  In essence, it calculates what the value would be if the regression line continued.

Market Signals

Lsma gives trading signals like any moving average – if Lsma line crosses price from above it is a buy signal and vice versa.


Least Squares Moving Average 001

Least Squares Moving Average 002

where x is the price, t is the time of this price, n is the period

Least Squares Moving Average 003

Chart Example

Least Squares Moving Average 004

Implementation and Usage

To initialize Lsma indicator, use one of the following constructors:

Lsma – sets default values: period = 14, useTime = false.

Lsma(Int32, Boolean) – sets period and useTime flag.  If this flag is true, to calculate the indicator values, time (in 100 nanoseconds) will be used and you need to use method Add() with time.  If this flag is false, a number of data points will be used for calculation.  In this case you can use method Add() without time.

Lsma(TimeSpan, Boolean) – sets time period and useTime flag.


LSMA - property to get current value.

 1// Create new instance
 2Lsma lsma = new Lsma(20, false);
 4// Number of stored values
 5lsma.HistoryCapacity = 2;
 7// Add new data point
10// Get indicator value
11double IndicatorValue = lsma.LSMA;
12// Get previous value
13if (lsma.HistoryCount == 2)
15    double IndicatorPrevValue = lsma[1];