I ran into a problem while using Sum.Series to build my own indicator. The example code is as follows:
CODE:
Please log in to see this code.
When I sum the values like this...
QUOTE:
dsValue1 = Sum.Series(dsValue1, period);
dsValue2 = Sum.Series(dsValue2, period);
DataSeries dsBadSeries = dsValue1 / dsValue2;
All dsBadSeries values are calculated as 1.0 since the numerator (dsValue1) and denominator (dsValue2) appear to have the same value.
When I sum the values like this...
QUOTE:
DataSeries dsGoodSeries = new DataSeries(Bars, "");
for(int bar = period; bar < Bars.Count; bar++)
{
dsGoodSeries[bar] = Sum.Value(bar, dsValue1, period) / Sum.Value(bar, dsValue2, period);
}
The series dsGoodSeries is what I would expect.
I've noticed in the Community.Indicators library that the Series method typically checks the bars.Cache using the description of the indicator to see if it was already calculated. The DataSeries is pulled from the cache instead of calculating a new one if the description matches. I assume that is what happened in this case. As an example, this is what is done for MACDEx in the Community.Indicator library...
CODE:
Please log in to see this code.
If you refer to my example code at the top, dsValue1 and dsValue2 were given a blank name as I just wanted to use them for temporary values. Since this blank name was used by Sum.Series to build it's description, it created the same description for both. This resulted in Sum.Series building dsValue1, but then returning the cached DataSeries for dsValue1 when the similar calculation was done for dsValue2.
I just want to point out the danger of caching all indicator calculations in the Series method. Even inventing names that are unique to my indicator such as...
QUOTE:
DataSeries dsValue1 = new DataSeries(Bars, "value1");
DataSeries dsValue2 = new DataSeries(Bars, "value2");
...doesn't guarantee that other indicators won't assign similar temporary values to a DataSeries objects.
It took me a while of scratching my head to figure out what caused this problem. I wanted to document it here as I'm sure others will stumble onto this at some point.
I believe caching all DataSeries values in this way seems dangerous as all developers have to realize that any description they assign to a DataSeries must be unique.