Click or drag to resize

Simple Moving Average

Simple Moving Averages (Sma) are used to smooth the data in an array to help eliminate noise and identify trends. The Sma is literally the simplest form of a moving average. Each output value is the average of the previous n values. In a Sma, each value in the time period carries equal weight, and values outside of the time period are not included in the average. This makes it less responsive to recent changes in the data, which can be useful for filtering out those changes.

Market Signals

Typically used with a simple trading rule – buy if price crosses Sma from below, sell if price crosses Sma from above.

Calculation

Simple Moving Average 001

Chart Example

Simple Moving Average 002

Implementation and Usage

To initialize Sma indicator use one of the following constructors:

Sma – sets default values: period = 14

Sma(Int32) – sets values for period

Sma(TimeSpan) – sets time period

Use

SMA - property to get current value

Example
C#
 1// Create new instance
 2Sma sma = new Sma(28);
 3
 4// Number of stored values
 5sma.HistoryCapacity = 2;
 6
 7// Add new data point
 8sma.Add(CurrentPrice);
 9
10// Get indicator value
11double IndicatorValue = sma.SMA;
12// Get previous value
13if (sma.HistoryCount == 2)
14{
15    double IndicatorPrevValue = sma[1];
16}