Step4. Update Method, 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:
Simple Indicator
1protected override bool Update(double value[], DateTime time, double[] result) 2{ 3 result = new double[value.Length]; 4 return false; 5}
Bar Indicator
1protected override bool Update(double[] open, double[] high, double[] low, double[] close, double[] volume, DateTime time, double[] result) 2{ 3 result = new double[value.Length]; 4 return false; 5}
Two Series Indicator
1protected override Boolean Update(double[] value1, double[] value2, DateTime time, double[] result) 2{ 3 result = new double[value.Length]; 4 return false; 5}
Tick Indicator
1protected override bool Update(double[] trade, double[] volume, DateTime time, double[] result) 2{ 3 result = new double[value.Length]; 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.
1public class TrueRange : BaseBarPortfolioIndicator<double> 2{ 3 protected override bool Update(double[] open, double[] high, double[] low, double[] close, double[] volume, DateTime time, double[] result) 4 { 5 bool ret = false; 6 result = new double[value.Length]; 7 8 for (int i = 0; i < value.Length; i++) 9 { 10 if (!double.IsNaN(closePrev[i])) 11 { 12 result[i] = Math.Max(high[i], closePrev[i]) - Math.Min(low[i], closePrev[i]); 13 ret = true; 14 } 15 closePrev[i] = close[i]; 16 } 17 return ret; 18 } 19}
After that, you can create properties for indicator, like this:
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 |
---|---|
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. | |
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. | |
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.