Skip to content

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.

Signature

1
public abstract interface SimpleMovingAverage

Namespace

cAlgo.API.Indicators

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
        [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);
            }
        }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 using cAlgo.API;
 using cAlgo.API.Indicators;
 namespace cAlgo.Robots
 {
     // This sample cBot shows how to use the Simple Moving Average indicator
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class SimpleMovingAverageSample : Robot
     {
         private double _volumeInUnits;
         private SimpleMovingAverage _fastSimpleMovingAverage;
         private SimpleMovingAverage _slowSimpleMovingAverage;
         [Parameter("Source", Group = "Fast MA")]
         public DataSeries FastMaSource { get; set; }
         [Parameter("Period", DefaultValue = 9, Group = "Fast MA")]
         public int FastMaPeriod { get; set; }
         [Parameter("Source", Group = "Slow MA")]
         public DataSeries SlowMaSource { get; set; }
         [Parameter("Period", DefaultValue = 20, Group = "Slow MA")]
         public int SlowMaPeriod { get; set; }
         [Parameter("Volume (Lots)", DefaultValue = 0.01, Group = "Trade")]
         public double VolumeInLots { get; set; }
         [Parameter("Stop Loss (Pips)", DefaultValue = 10, Group = "Trade")]
         public double StopLossInPips { get; set; }
         [Parameter("Take Profit (Pips)", DefaultValue = 10, Group = "Trade")]
         public double TakeProfitInPips { get; set; }
         [Parameter("Label", DefaultValue = "Sample", Group = "Trade")]
         public string Label { get; set; }
         public Position[] BotPositions
         {
             get
             {
                 return Positions.FindAll(Label);
             }
         }
         protected override void OnStart()
         {
             _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
             _fastSimpleMovingAverage = Indicators.SimpleMovingAverage(FastMaSource, FastMaPeriod);
             _slowSimpleMovingAverage = Indicators.SimpleMovingAverage(SlowMaSource, SlowMaPeriod);
         }
         protected override void OnBar()
         {
             if (_fastSimpleMovingAverage.Result.HasCrossedAbove(_slowSimpleMovingAverage.Result, 0))
             {
                 ClosePositions(TradeType.Sell);
                 ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
             }
             else if (_fastSimpleMovingAverage.Result.HasCrossedBelow(_slowSimpleMovingAverage.Result, 0))
             {
                 ClosePositions(TradeType.Buy);
                 ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
             }
         }
         private void ClosePositions(TradeType tradeType)
         {
             foreach (var position in BotPositions)
             {
                 if (position.TradeType != tradeType) continue;
                 ClosePosition(position);
             }
         }
     }
 }