Click or drag to resize

Step4. Update Method, Value Properties

Override Update Method. Create value properties

The main step is overriding Update method, method in which you will update indicators values when new data will be added. Here is a blank Update methods for different indicator types:

  1. Simple Single Value Indicator

    C#
    1protected override bool Update(double value, DateTime time, out double result)
    2{
    3    result = double.NaN;
    4    return false;
    5}
  2. Bar Indicator

    C#
    1protected override bool Update(double open, double high, double low, double close, double volume, DateTime time, out double result)
    2{
    3    result = double.NaN;
    4    return false;
    5}
  3. Two Series Indicator

    C#
    1protected override Boolean Update(double value1, double value2, DateTime time, out double result)
    2{
    3    result = double.NaN;
    4    return false;
    5}
  4. Tick Indicator

    C#
    1protected override bool Update(double trade, double volume, DateTime time, out double result)
    2{
    3    result = double.NaN;
    4    return false;
    5}

As you can see all Update parameters except the last one are parameters which are passed to Add method. All you need is to calculate new indicator value and store value to result parameter. If the calculation succeeded it returns true, else false.

C#
 1public class TrueRange : BaseBarIndicator<double>
 2{
 3    protected override bool Update(double open, double high, double low, double close, double volume, DateTime time, out double result)
 4    {
 5        bool ret = false;
 6        result = DefaultValue;
 7
 8        if (!double.IsNaN(closePrev))
 9        {
10            result = Math.Max(high, closePrev) - Math.Min(low, closePrev);
11            ret = true;
12        }
13        closePrev = close;
14        return ret;
15    }
16}

After that, you can create properties for indicator, like this:

C#
1public Double TR {get {return Last;} }

Note 1: You do not need to validate input parameters; base classes do it for you.

Note 2: Type of result parameter is a type which you specify for generic base class, so it must be Slice for multiple values indicators.

Note 3: Our indicators library also supports Ready flag, to indicate whether indicator is ready to return some value. It will be set, after you specify true.

Note 4: Our library also provides a lot of useful classes to simplify work with sliding windows (especially time based), and make this work invariant of window type. Here is a list of those classes (see indicator source code for usage example):

Class

Description

TimeSeriesDataQueueT

This queue stores custom data on point or time based sliding window (automatically remove outdated data). Or you can use it on manual mode as Dequeue data structure. Also you can apply listener on it and get OnPush/OnPop notification.

Indicator history is stored in this queue, so if you specify period for storing indicator values, you can apply listener for indicators too.

SimpleDataQueue

This queue is a simple version of the previous one. It can operate only with double values. But unlike TimeSeriesDataQueueT it can optionally calculate simple statistics like first two moments, sum, means, variance, standard deviation etc. You can apply listener to it too.

MinMaxQueue

This queue allows to determine higher or lower value on point or time based sliding window, and when this value occurs (Time or number of points ago).

For information on listeners see the topic Listeners.