@Test public void getValueWithOldResultsRemoval() { double[] data = new double[20]; Arrays.fill(data, 1); BarSeries barSeries = new MockBarSeries(numFunction, data); SMAIndicator sma = new SMAIndicator(new ClosePriceIndicator(barSeries), 10); assertNumEquals(1, sma.getValue(5)); assertNumEquals(1, sma.getValue(10)); barSeries.setMaximumBarCount(12); assertNumEquals(1, sma.getValue(19)); }
@Override public T getValue(int index) { BarSeries series = getBarSeries(); if (series == null) { return calculate(index); } final int removedBarsCount = series.getRemovedBarsCount(); final int maximumResultCount = series.getMaximumBarCount(); T result; if (index < removedBarsCount) { log.trace("{}: result from bar {} already removed from cache, use {}-th instead", getClass().getSimpleName(), index, removedBarsCount); increaseLengthTo(removedBarsCount, maximumResultCount); highestResultIndex = removedBarsCount; result = results.get(0); if (result == null) { result = calculate(0); results.set(0, result); } } else { if (index == series.getEndIndex()) { result = calculate(index); } else { increaseLengthTo(index, maximumResultCount); if (index > highestResultIndex) { highestResultIndex = index; result = calculate(index); results.set(results.size() - 1, result); } else { int resultInnerIndex = results.size() - 1 - (highestResultIndex - index); result = results.get(resultInnerIndex); if (result == null) { result = calculate(index); results.set(resultInnerIndex, result); } } } } return result; }
CachedIndicator extends AbstractIndicator<T> { @Override public T getValue(int index) { BarSeries series = getBarSeries(); if (series == null) { return calculate(index); } final int removedBarsCount = series.getRemovedBarsCount(); final int maximumResultCount = series.getMaximumBarCount(); T result; if (index < removedBarsCount) { log.trace("{}: result from bar {} already removed from cache, use {}-th instead", getClass().getSimpleName(), index, removedBarsCount); increaseLengthTo(removedBarsCount, maximumResultCount); highestResultIndex = removedBarsCount; result = results.get(0); if (result == null) { result = calculate(0); results.set(0, result); } } else { if (index == series.getEndIndex()) { result = calculate(index); } else { increaseLengthTo(index, maximumResultCount); if (index > highestResultIndex) { highestResultIndex = index; result = calculate(index); results.set(results.size() - 1, result); } else { int resultInnerIndex = results.size() - 1 - (highestResultIndex - index); result = results.get(resultInnerIndex); if (result == null) { result = calculate(index); results.set(resultInnerIndex, result); } } } } return result; } }
CachedIndicator extends AbstractIndicator<T> { @Override public T getValue(int index) { BarSeries series = getBarSeries(); if (series == null) { return calculate(index); } final int removedBarsCount = series.getRemovedBarsCount(); final int maximumResultCount = series.getMaximumBarCount(); T result; if (index < removedBarsCount) { log.trace("{}: result from bar {} already removed from cache, use {}-th instead", getClass().getSimpleName(), index, removedBarsCount); increaseLengthTo(removedBarsCount, maximumResultCount); highestResultIndex = removedBarsCount; result = results.get(0); if (result == null) { result = calculate(0); results.set(0, result); } } else { if (index == series.getEndIndex()) { result = calculate(index); } else { increaseLengthTo(index, maximumResultCount); if (index > highestResultIndex) { highestResultIndex = index; result = calculate(index); results.set(results.size() - 1, result); } else { int resultInnerIndex = results.size() - 1 - (highestResultIndex - index); result = results.get(resultInnerIndex); if (result == null) { result = calculate(index); results.set(resultInnerIndex, result); } } } } return result; } CachedIndicator(BarSeries series);  CachedIndicator(Indicator<?> indicator); }
CachedIndicator extends AbstractIndicator<T> { @Override public T getValue(int index) { BarSeries series = getBarSeries(); if (series == null) { return calculate(index); } final int removedBarsCount = series.getRemovedBarsCount(); final int maximumResultCount = series.getMaximumBarCount(); T result; if (index < removedBarsCount) { log.trace("{}: result from bar {} already removed from cache, use {}-th instead", getClass().getSimpleName(), index, removedBarsCount); increaseLengthTo(removedBarsCount, maximumResultCount); highestResultIndex = removedBarsCount; result = results.get(0); if (result == null) { result = calculate(0); results.set(0, result); } } else { if (index == series.getEndIndex()) { result = calculate(index); } else { increaseLengthTo(index, maximumResultCount); if (index > highestResultIndex) { highestResultIndex = index; result = calculate(index); results.set(results.size() - 1, result); } else { int resultInnerIndex = results.size() - 1 - (highestResultIndex - index); result = results.get(resultInnerIndex); if (result == null) { result = calculate(index); results.set(resultInnerIndex, result); } } } } return result; } CachedIndicator(BarSeries series);  CachedIndicator(Indicator<?> indicator); @Override T getValue(int index); }
CachedIndicator extends AbstractIndicator<T> { @Override public T getValue(int index) { BarSeries series = getBarSeries(); if (series == null) { return calculate(index); } final int removedBarsCount = series.getRemovedBarsCount(); final int maximumResultCount = series.getMaximumBarCount(); T result; if (index < removedBarsCount) { log.trace("{}: result from bar {} already removed from cache, use {}-th instead", getClass().getSimpleName(), index, removedBarsCount); increaseLengthTo(removedBarsCount, maximumResultCount); highestResultIndex = removedBarsCount; result = results.get(0); if (result == null) { result = calculate(0); results.set(0, result); } } else { if (index == series.getEndIndex()) { result = calculate(index); } else { increaseLengthTo(index, maximumResultCount); if (index > highestResultIndex) { highestResultIndex = index; result = calculate(index); results.set(results.size() - 1, result); } else { int resultInnerIndex = results.size() - 1 - (highestResultIndex - index); result = results.get(resultInnerIndex); if (result == null) { result = calculate(index); results.set(resultInnerIndex, result); } } } } return result; } CachedIndicator(BarSeries series);  CachedIndicator(Indicator<?> indicator); @Override T getValue(int index);  }