Click or drag to resize

Step3. Implement constructor

Implement constructor

The next step is implementation of constructor. At the beginning you implement your own constructor for indicator with all necessary parameters.

C#
1public class MyMovingAverage : BaseSimpleIndicator<double>
2{
3    private int period;
4
5    public MyMovingAverage(int period)
6    {
7        this.period = period;
8    }
9}

Note 1: If your indicator is bar indicator you should call base class constructor and specify validateVolume flag. Data supplier wrappers in base classes validate incoming data and skip calls with double.NaN or double.Infinity values. But for many markets you cannot get valid volume values, so this flag is necessary to skip volume value validation.  The rule is very simple: if you use volume value in calculations set this flag, else do not.

C#
1public class MyBandIndicator : BaseBarIndicator<MyBandIndicator.Slice>
2{
3    private int period;
4
5    public MyBandIndicator(int period) : base(true)
6    {
7        this.period = period;
8    }
9}

Note 2: As you can see our technical indicators library supports time based indicators, with TimeSpan used as period. If you want to create time based indicator please call EnsureDateTime() function in constructor of time period use case (it forces indicator user to use Add method with DateTime).

C#
1public MyMovingAverage(TimeSpan period)
2{
3    EnsureDateTime();
4    this.timePeriod = period;
5}

Note 3: As you can see our technical indicators library provides Stable flag for all indicators, which indicates whether indicator values are already stable or more data is needed. If you need this flag for your indicator, you can use one of the following options:

  1. Specify  unstableValuesCount. Stable flag will be set after unstableValuesCount values will be calculated by indicator.

    C#
    1public MyMovingAverage(int period)
    2{
    3    this.period = period;
    4    unstableValuesCount = period;
    5}
  2. Specify  unstablePeriod. Stable flag will be set after unstablePeriod delay from first calculated value.

    C#
    1public MyMovingAverage(TimeSpan period)
    2{
    3    EnsureDateTime();
    4    this.timePeriod = period;
    5    unstablePeriod = period;
    6}
  3. Use customStabilityFlag. Just unset it in constructor and set when you think that value is stable.

    C#
    1public MyMovingAverage()
    2{
    3    customStabilityFlag = false;
    4}