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.

Depending on inherited class, you should specify a number of parameters to implement the constructor from base class:

Indicator type

Constructor

simple

method#ctor(Int32, PortfolioAbsenceStrategy, Int32, Int32, SimpleAggregationType, InputValidationStrategy)

bar

method#ctor(Int32, PortfolioAbsenceStrategy, Int32, Int32, InputValidationStrategy)

tick

method

#ctor(Int32, PortfolioAbsenceStrategy, Int32, Int32, TickAggregationType, InputValidationStrategy)

two sets

method

#ctor(Int32, PortfolioAbsenceStrategy, Int32, Int32, Int32, SimpleAggregationType, InputValidationStrategy)

C#
1updatePointPeriodicity
or
C#
1updateTimePeriodicity
sets length for points/time updating period.

C#
1absenceStrategy
parameter indicates what to do if series hasn't values on some update period. It must be value from enum PortfolioAbsenceStrategy and it offers options to skip updating, approximate current value using previous one, or just pass double.NaN.

C#
1seriesCount
and
C#
1valuesPerSerie
indicates series number and amount of values per series.

C#
1aggregationMethod
parameter defines how values are aggregated before passing into the indicator. In simple and two sets cases it must be value from enum SimpleAggregationType and it offers options to store last, sum, average, minimum, maximum, or do not aggregate just call update as soon as values come. In case of tick indicator it must be value from SimpleAggregationType enum, which contains the same set except sum option.

C#
1validationStrategy
parameter defines what we must do with invalid input values. It must be value from enum InputValidationStrategy and it offers options to validate all, validate all except volume, or do not validate.

Lets consider an example of simple indicator:

C#
 1public class MyMovingAverage : BaseSimplePortfolioIndicator<double>
 2{
 3    private int period;
 4
 5    public MyMovingAverage(TimeSpan updateTimePeriodicity, int seriesCount, int valuesPerSerie, int period)
 6        : base(updateTimePeriodicity, PortfolioAbsenceStrategy.AbsenceSkip, seriesCount, valuesPerSerie,
 7            SimpleAggregationType.AggregateAverage, InputValidationStrategy.ValidateAll)
 8    {
 9        this.period = period;
10    }
11}

Note 1: If you set validationStrategy parameter to ValidateAll value, 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 set for skipping volume value validation. The rule is very simple: if you use volume value in calculations set this flag, else do not.

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 class MyMovingAverage : BaseSimplePortfolioIndicator<double>
 2{
 3    private int period;
 4
 5    public MyMovingAverage(TimeSpan updateTimePeriodicity, int seriesCount, int valuesPerSerie, TimeSpan period)
 6        : base(updateTimePeriodicity, PortfolioAbsenceStrategy.AbsenceSkip, seriesCount, valuesPerSerie,
 7            SimpleAggregationType.AggregateAverage, InputValidationStrategy.ValidateAll)
 8    {
 9        EnsureDateTime();
10        this.period = period;
11    }
12}

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 class MyMovingAverage : BaseSimplePortfolioIndicator<double>
     2{
     3    private int period;
     4
     5    public MyMovingAverage(TimeSpan updateTimePeriodicity, int seriesCount, int valuesPerSerie, int period)
     6        : base(updateTimePeriodicity, PortfolioAbsenceStrategy.AbsenceSkip, seriesCount, valuesPerSerie,
     7            SimpleAggregationType.AggregateAverage, InputValidationStrategy.ValidateAll)
     8    {
     9        this.period = period;
    10        unstableValuesCount = period;
    11    }
    12}
  2. Specify  unstablePeriod. Stable flag will be set after unstablePeriod delay from first calculated value.

    C#
     1public class MyMovingAverage : BaseSimplePortfolioIndicator<double>
     2{
     3    private int period;
     4
     5    public MyMovingAverage(TimeSpan updateTimePeriodicity, int seriesCount, int valuesPerSerie, TimeSpan period)
     6        : base(updateTimePeriodicity, PortfolioAbsenceStrategy.AbsenceSkip, seriesCount, valuesPerSerie,
     7            SimpleAggregationType.AggregateAverage, InputValidationStrategy.ValidateAll)
     8    {
     9        this.period = period;
    10        unstableValuesCount = period;
    11    }
    12}
  3. Use customStabilityFlag. Just unset it in constructor and set when you think that value is stable.

    C#
     1public class MyMovingAverage : BaseSimplePortfolioIndicator<double>
     2{
     3    private int period;
     4
     5    public MyMovingAverage(TimeSpan updateTimePeriodicity, int seriesCount, int valuesPerSerie, int period)
     6        : base(updateTimePeriodicity, PortfolioAbsenceStrategy.AbsenceSkip, seriesCount, valuesPerSerie,
     7            SimpleAggregationType.AggregateAverage, InputValidationStrategy.ValidateAll)
     8    {
     9        this.period = period;
    10        customStabilityFlag = false;
    11    }
    12}