SimpleMovingAverage


Summary

The simple moving average is an average of price within n previous periods.

Remarks

The simple moving average is the unweighted mean of the previous n price data, where n is the period used for the calculation and price data the price data source, e.g. The closing price.

Syntax

public interface SimpleMovingAverage : MovingAverage, IIndicator

Members

NameTypeSummary

Example 1

[Indicator]
public class SimpleMovingAverageExample : Indicator
{
    [Parameter]
    public DataSeries Source { get; set; }
    [Parameter(DefaultValue = 14, MinValue = 2)]
    public int Periods { get; set; }
    [Output("Result", Color = Colors.Orange)]
    public IndicatorDataSeries Result { get; set; }
    private SimpleMovingAverage _simpleMovingAverage;
    protected override void Initialize()
    {
        _simpleMovingAverage = Indicators.SimpleMovingAverage(Source, Periods);
    }
    public override void Calculate(int index)
    {
        var average = _simpleMovingAverage.Result[index];
        double sum = 0;
        for (var period = 0; period < Periods; period++)
        {
            sum += Math.Pow(Source[index - period] - average, 2.0);
        }
        Result[index] = Math.Sqrt(sum / Periods);
    }
}