Example: Simple Moving Average
In this example we will develop a simple moving average indicator. Let's, create an indicator template project and start.

# Input parameters

First of all it is important to decide what parameters will our indicator have. We are building a simple moving average, so it will have only two parameters: Period and Price type, that will be used for calculations
1
#region Parameters
2
// First input parameter
3
[InputParameter("Period of Simple Moving Average", 0, 1, 999, 1, 1)]
4
public int Period = 10;
5
6
// Second input parameter
7
[InputParameter("Sources prices for MA", 1, variants: new object[]{
8
"Close", PriceType.Close,
9
"Open", PriceType.Open,
10
"High", PriceType.High,
11
"Low", PriceType.Low,
12
"Typical", PriceType.Typical,
13
"Median", PriceType.Median,
14
"Weighted", PriceType.Weighted
15
})]
16
public PriceType SourcePrice = PriceType.Close;
17
#endregion Parameters
Copied!

# Indicator's general info

Our next step is to set a general indicators info, all this information you will see in Indicator's lookup, when you decide to select it. Also, here we need to define how many data series will our indicator have and should it be drawn in separate window or directly on a chart.
All these tasks we can solve in constructor function, ok great, let's do it
1
public SMA()
2
: base()
3
{
4
// Here we set an indicator's name and description
5
Name = "Simple Moving Average Example";
6
Description = "Average price for the last N periods";
7
ShortName = "SMA (" + Period + ":" + SourcePrice.ToString() + ")";
8
9
// Our indicator has only one line
10
11
12
//Indicator will be drawn directly on chart
13
SeparateWindow = false;
14
}
Copied!

# Core logic

We will save OnInit function empty, because our indicator does not require any one-time logic that should be executed when we add indicator on a chart
1
protected override void OnInit()
2
{
3
4
}
Copied!
All calculations will occur when we receive a new quote, to process it we need to override OnUpdate function
1
protected override void OnUpdate(UpdateArgs args)
2
{
3
// Checking, if current amount of bars
4
// more, than period of moving average. If it is
5
// then the calculation is possible
6
if (Count <= Period)
7
return;
8
9
double sum = 0.0; // Sum of prices
10
for (int i = 0; i < Period; i++)
11
// Adding bar's price to the sum
12
sum += GetPrice(SourcePrice, i);
13
14
// Set value to the "SMA" line buffer.
15
SetValue(sum / Period);
16
}
Copied!
1
if (Count <= Period)
2
return;
Copied!
Here we check is it enough historical bars to calculate one indicator's point, if yes - we continue calculation otherwise skip it.
To calculate average price we need to request price data, we can do it by calling GetPrice function
1
sum += GetPrice(SourcePrice, i);
Copied!
Once, all calculations are done we set the result value to indicators data serie
1
// Set value to the "SMA" line buffer.
2
SetValue(sum / Period);
Copied!

# All source code

That is all, that was easy. As a conclusion take a look at all source code
1
2
using System;
3
using System.Drawing;
4
5
6
namespace MovingAverages
7
{
8
public class SMA : Indicator
9
{
10
#region Parameters
11
// First input parameter
12
[InputParameter("Period of Simple Moving Average", 0, 1, 999, 1, 1)]
13
public int Period = 10;
14
15
// Second input parameter
16
[InputParameter("Sources prices for MA", 1, variants: new object[]{
17
"Close", PriceType.Close,
18
"Open", PriceType.Open,
19
"High", PriceType.High,
20
"Low", PriceType.Low,
21
"Typical", PriceType.Typical,
22
"Median", PriceType.Median,
23
"Weighted", PriceType.Weighted
24
})]
25
public PriceType SourcePrice = PriceType.Close;
26
#endregion Parameters
27
28
/// <summary>
29
/// Indicator's constructor. Contains general information: name, description, LineSeries etc.
30
/// </summary>
31
public SMA()
32
: base()
33
{
34
// Here we set an indicator's name and description
35
Name = "Simple Moving Average Example";
36
Description = "Average price for the last N periods";
37
ShortName = "SMA (" + Period + ":" + SourcePrice.ToString() + ")";
38
39
// Our indicator has only one line
40
41
42
//Indicator will be drawn directly on chart
43
SeparateWindow = false;
44
}
45
46
/// <summary>
47
/// This function will be called after creating an indicator as well as after its input params reset or chart (symbol or timeframe) updates.
48
/// </summary>
49
protected override void OnInit()
50
{
51
52
}
53
54
/// <summary>
55
/// Calculation entry point. This function is called when a price data updates.
56
57
/// Under NewTick during realtime.
58
/// Under NewBar if start of the new bar is required.
59
/// </summary>
60
/// <param name="args">Provides data of updating reason and incoming price.</param>
61
protected override void OnUpdate(UpdateArgs args)
62
{
63
// Checking, if current amount of bars
64
// more, than period of moving average. If it is
65
// then the calculation is possible
66
if (Count <= Period)
67
return;
68
69
double sum = 0.0; // Sum of prices
70
for (int i = 0; i < Period; i++)
71
// Adding bar's price to the sum
72
sum += GetPrice(SourcePrice, i);
73
74
// Set value to the "SMA" line buffer.
75
SetValue(sum / Period);
76
}
77
}
78
}
Copied!