API Support Forum
OEC API > API Support > Recalculate Method of CustomIndicator class
Author Topic: Recalculate Method of CustomIndicator class
(2 messages, Page 1 of 1)
Moderators: VPfau
JGronemus87
Posts: 14
Joined: Nov 28, 2007


Posted: Mar 23, 2010 @ 10:07 AM             Msg. 1 of 2
Hello,

I'm trying to write a custom indicator and I'm having some difficulty understanding the parameters that are passed into the method in RecalculateArg. Here is a specific example...

Say I have a data series of 20 bars (0 - 19 with 19 being the earliest in time).

When is Recalculate called?

What would the value of arg.End be?

What would the value of arg.Start be?

What is the value of arg.Stock? Is arg.Stock all the bars in the series all the time? Or is it just the bars that need to be recalculated?

The indicator that I'm trying to write needs to analyze all the bars in the series for each bar. So if bar 0 needs to be recalculated, I need a way to analyze bars 0 - 18. Is there a way to do this?

Thanks,
John

John
VictorV
Posts: 746
Joined: May 08, 2007


Posted: Mar 23, 2010 @ 10:32 AM             Msg. 2 of 2
Hello,

Recalculate method is calling on updaing of input series (the last bar updated, a new bar arrived, all data of series have been replaced)

arg.End will be the index of the last bar + 1
arg.Start will be the index of the first invalidated bar
So, you can use the next loop:
for (int i = arg.Start; i < arg.End; ++i)
{
// your calculations
}

arg.Stock contains the input bars (OHLCV). To access to them, you can use the next code inside the loop from above:

double high = arg.Stock.High[i, SeriesOrder.Direct];
double low = arg.Stock.Low[i, SeriesOrder.Direct];
double high_1 = arg.Stock.High[Math.Max(0, i - 1), SeriesOrder.Direct];

SeriesOrder.Direct is a sign that the index is increasing from the past to now
SeriesOrder.Back is a sign that 0 index is the latest bar.

arg.Stock is a reference to all available bars.

Recalculate(..) method is intended for optimized calculations, when large update can be recalculated faster than one calculation per each new bar. So, probably, it will be better for you to use the method Calculate() of OEC.Chart.Custom.CustomIndicator that will be called for each bar incrementally. In this case your code will be looked like this one:

protected override void Calculate()
{
Result.Value = (Close-Open[18])/Close*100;
}

Chart Package contains several samples of C# indicators. You can find out them in My Documents\My Custom Indicators folder.