public override T GetValue(int index) { ITimeSeries series = TimeSeries; if (series != null) { int seriesEndIndex = series.GetEndIndex(); if (index <= seriesEndIndex) { // We are not after the end of the series int removedBarsCount = series.GetRemovedBarsCount(); int startIndex = Math.Max(removedBarsCount, HighestResultIndex); if (index - startIndex > RECURSION_THRESHOLD) { // Too many unCalculated values; the risk for a StackOverflowError becomes high. // Calculating the previous values iteratively for (int prevIdx = startIndex; prevIdx < index; prevIdx++) { base.GetValue(prevIdx); } } } } return(base.GetValue(index)); }
public override T GetValue(int index) { ITimeSeries series = TimeSeries; if (series == null) { // Series is null; the indicator doesn't need cache. // (e.g. simple computation of the value) // --> Calculating the value return(Calculate(index)); } // Series is not null int removedTicksCount = series.GetRemovedBarsCount(); int maximumResultCount = series.GetMaximumBarCount(); T result; if (index < removedTicksCount) { // Result already removed from cache increaseLengthTo(removedTicksCount, maximumResultCount); HighestResultIndex = removedTicksCount; result = _results[0]; if (Equals(result, default(T))) { // It should be "result = calculate(removedTicksCount);". // We use "result = calculate(0);" as a workaround // to fix issue #120 (https://github.com/mdeverdelhan/ta4j/issues/120). result = Calculate(0); _results[0] = result; } } else { increaseLengthTo(index, maximumResultCount); if (index > HighestResultIndex) { // Result not calculated yet HighestResultIndex = index; result = Calculate(index); _results[_results.Count - 1] = result; } else { // Result covered by current cache int resultInnerIndex = _results.Count - 1 - (HighestResultIndex - index); result = _results[resultInnerIndex]; if (Equals(result, default(T))) { result = Calculate(index); } _results[resultInnerIndex] = result; } } return(result); }