Indicator By Func
Basic indicator series has overloaded methods which accept not an instance of HistoryData, but delegate of the type Func<int, double>. The task of this delegate is to provide data at the specified offset.
For the example, let’s write an indicator which will use Medium, Typical and Weighted bar prices, and these prices we’ll calculate ourselves.
We will require the three delegates of the specified type:
private double getMedium(int index) { if (index >= CurrentData.Count) return 0; return (CurrentData.GetPrice(PriceType.High, index) + CurrentData.GetPrice(PriceType.Low, index)) / 2; } private double getTypical(int index) { if (index >= CurrentData.Count) return 0; return (CurrentData.GetPrice(PriceType.High, index) + CurrentData.GetPrice(PriceType.Low, index) + CurrentData.GetPrice(PriceType.Close, index)) / 3; } private double getWeighted(int index) { if (index >= CurrentData.Count) return 0; return (CurrentData.GetPrice(PriceType.High, index) + CurrentData.GetPrice(PriceType.Low, index) + 2* CurrentData.GetPrice(PriceType.Close, index)) / 4; }
These functions return values for Medium, Typical and Weighted prices respectively.
Then, we create indicators based on this data:
public override void Init() { indicatorMedium = Indicators.iMA(getMedium, Period, MAType); indicatorTypical = Indicators.iMA(getTypical, Period, MAType); indicatorWeighted = Indicators.iMA(getWeighted, Period, MAType); }
Now, we take data and display it on the chart. The adequacy of the obtained data can be assessed by calling the standard iMA with corresponding values of the PriceType parameter.