@Test public void upscaledTo5DayBars() { final DurationBarAggregator barAggregator = new DurationBarAggregator(Duration.ofDays(5), true); final List<Bar> bars = barAggregator.aggregate(getOneDayBars()); assertEquals(3, bars.size()); final Bar bar1 = bars.get(0); final Num num1 = bar1.getOpenPrice(); TestUtils.assertNumEquals(num1.numOf(1), bar1.getOpenPrice()); TestUtils.assertNumEquals(num1.numOf(6), bar1.getHighPrice()); TestUtils.assertNumEquals(num1.numOf(4), bar1.getLowPrice()); TestUtils.assertNumEquals(num1.numOf(9), bar1.getClosePrice()); TestUtils.assertNumEquals(num1.numOf(33), bar1.getVolume()); final Bar bar2 = bars.get(1); final Num num2 = bar2.getOpenPrice(); TestUtils.assertNumEquals(num2.numOf(6), bar2.getOpenPrice()); TestUtils.assertNumEquals(num2.numOf(91), bar2.getHighPrice()); TestUtils.assertNumEquals(num2.numOf(4), bar2.getLowPrice()); TestUtils.assertNumEquals(num2.numOf(10), bar2.getClosePrice()); TestUtils.assertNumEquals(num2.numOf(260), bar2.getVolume()); Bar bar3 = bars.get(2); Num num3 = bar3.getOpenPrice(); TestUtils.assertNumEquals(num3.numOf(4), bar3.getOpenPrice()); TestUtils.assertNumEquals(num3.numOf(991), bar3.getHighPrice()); TestUtils.assertNumEquals(num3.numOf(43), bar3.getLowPrice()); TestUtils.assertNumEquals(num3.numOf(10), bar3.getClosePrice()); TestUtils.assertNumEquals(num3.numOf(1010), bar3.getVolume()); }
@Override public List<Bar> aggregate(List<Bar> bars) { final List<Bar> aggregated = new ArrayList<>(); if (bars.isEmpty()) { return aggregated; } final Duration actualDur = bars.iterator().next().getTimePeriod(); final boolean isMultiplication = timePeriod.getSeconds() % actualDur.getSeconds() == 0; if (!isMultiplication) { throw new IllegalArgumentException( "Cannot aggregate bars: the new timePeriod must be a multiplication of the actual timePeriod."); } int i = 0; final Num zero = bars.iterator().next().getOpenPrice().numOf(0); while (i < bars.size()) { Bar bar = bars.get(i); final ZonedDateTime beginTime = bar.getBeginTime(); final Num open = bar.getOpenPrice(); Num high = bar.getHighPrice(); Num low = bar.getLowPrice(); Num close = null; Num volume = zero; Num amount = zero; Duration sumDur = Duration.ZERO; while (sumDur.compareTo(timePeriod) < 0) { if (i < bars.size()) { bar = bars.get(i); if (high == null || bar.getHighPrice().isGreaterThan(high)) { high = bar.getHighPrice(); } if (low == null || bar.getLowPrice().isLessThan(low)) { low = bar.getLowPrice(); } close = bar.getClosePrice(); volume = volume.plus(bar.getVolume()); amount = amount.plus(bar.getAmount()); } sumDur = sumDur.plus(actualDur); i++; } if (!onlyFinalBars || i <= bars.size()) { final Bar aggregatedBar = new BaseBar(timePeriod, beginTime.plus(timePeriod), open, high, low, close, volume, amount); aggregated.add(aggregatedBar); } } return aggregated; }
DurationBarAggregator implements BarAggregator { @Override public List<Bar> aggregate(List<Bar> bars) { final List<Bar> aggregated = new ArrayList<>(); if (bars.isEmpty()) { return aggregated; } final Duration actualDur = bars.iterator().next().getTimePeriod(); final boolean isMultiplication = timePeriod.getSeconds() % actualDur.getSeconds() == 0; if (!isMultiplication) { throw new IllegalArgumentException( "Cannot aggregate bars: the new timePeriod must be a multiplication of the actual timePeriod."); } int i = 0; final Num zero = bars.iterator().next().getOpenPrice().numOf(0); while (i < bars.size()) { Bar bar = bars.get(i); final ZonedDateTime beginTime = bar.getBeginTime(); final Num open = bar.getOpenPrice(); Num high = bar.getHighPrice(); Num low = bar.getLowPrice(); Num close = null; Num volume = zero; Num amount = zero; Duration sumDur = Duration.ZERO; while (sumDur.compareTo(timePeriod) < 0) { if (i < bars.size()) { bar = bars.get(i); if (high == null || bar.getHighPrice().isGreaterThan(high)) { high = bar.getHighPrice(); } if (low == null || bar.getLowPrice().isLessThan(low)) { low = bar.getLowPrice(); } close = bar.getClosePrice(); volume = volume.plus(bar.getVolume()); amount = amount.plus(bar.getAmount()); } sumDur = sumDur.plus(actualDur); i++; } if (!onlyFinalBars || i <= bars.size()) { final Bar aggregatedBar = new BaseBar(timePeriod, beginTime.plus(timePeriod), open, high, low, close, volume, amount); aggregated.add(aggregatedBar); } } return aggregated; } }
DurationBarAggregator implements BarAggregator { @Override public List<Bar> aggregate(List<Bar> bars) { final List<Bar> aggregated = new ArrayList<>(); if (bars.isEmpty()) { return aggregated; } final Duration actualDur = bars.iterator().next().getTimePeriod(); final boolean isMultiplication = timePeriod.getSeconds() % actualDur.getSeconds() == 0; if (!isMultiplication) { throw new IllegalArgumentException( "Cannot aggregate bars: the new timePeriod must be a multiplication of the actual timePeriod."); } int i = 0; final Num zero = bars.iterator().next().getOpenPrice().numOf(0); while (i < bars.size()) { Bar bar = bars.get(i); final ZonedDateTime beginTime = bar.getBeginTime(); final Num open = bar.getOpenPrice(); Num high = bar.getHighPrice(); Num low = bar.getLowPrice(); Num close = null; Num volume = zero; Num amount = zero; Duration sumDur = Duration.ZERO; while (sumDur.compareTo(timePeriod) < 0) { if (i < bars.size()) { bar = bars.get(i); if (high == null || bar.getHighPrice().isGreaterThan(high)) { high = bar.getHighPrice(); } if (low == null || bar.getLowPrice().isLessThan(low)) { low = bar.getLowPrice(); } close = bar.getClosePrice(); volume = volume.plus(bar.getVolume()); amount = amount.plus(bar.getAmount()); } sumDur = sumDur.plus(actualDur); i++; } if (!onlyFinalBars || i <= bars.size()) { final Bar aggregatedBar = new BaseBar(timePeriod, beginTime.plus(timePeriod), open, high, low, close, volume, amount); aggregated.add(aggregatedBar); } } return aggregated; } DurationBarAggregator(Duration timePeriod);  DurationBarAggregator(Duration timePeriod, boolean onlyFinalBars); }
DurationBarAggregator implements BarAggregator { @Override public List<Bar> aggregate(List<Bar> bars) { final List<Bar> aggregated = new ArrayList<>(); if (bars.isEmpty()) { return aggregated; } final Duration actualDur = bars.iterator().next().getTimePeriod(); final boolean isMultiplication = timePeriod.getSeconds() % actualDur.getSeconds() == 0; if (!isMultiplication) { throw new IllegalArgumentException( "Cannot aggregate bars: the new timePeriod must be a multiplication of the actual timePeriod."); } int i = 0; final Num zero = bars.iterator().next().getOpenPrice().numOf(0); while (i < bars.size()) { Bar bar = bars.get(i); final ZonedDateTime beginTime = bar.getBeginTime(); final Num open = bar.getOpenPrice(); Num high = bar.getHighPrice(); Num low = bar.getLowPrice(); Num close = null; Num volume = zero; Num amount = zero; Duration sumDur = Duration.ZERO; while (sumDur.compareTo(timePeriod) < 0) { if (i < bars.size()) { bar = bars.get(i); if (high == null || bar.getHighPrice().isGreaterThan(high)) { high = bar.getHighPrice(); } if (low == null || bar.getLowPrice().isLessThan(low)) { low = bar.getLowPrice(); } close = bar.getClosePrice(); volume = volume.plus(bar.getVolume()); amount = amount.plus(bar.getAmount()); } sumDur = sumDur.plus(actualDur); i++; } if (!onlyFinalBars || i <= bars.size()) { final Bar aggregatedBar = new BaseBar(timePeriod, beginTime.plus(timePeriod), open, high, low, close, volume, amount); aggregated.add(aggregatedBar); } } return aggregated; } DurationBarAggregator(Duration timePeriod);  DurationBarAggregator(Duration timePeriod, boolean onlyFinalBars); @Override List<Bar> aggregate(List<Bar> bars); }
DurationBarAggregator implements BarAggregator { @Override public List<Bar> aggregate(List<Bar> bars) { final List<Bar> aggregated = new ArrayList<>(); if (bars.isEmpty()) { return aggregated; } final Duration actualDur = bars.iterator().next().getTimePeriod(); final boolean isMultiplication = timePeriod.getSeconds() % actualDur.getSeconds() == 0; if (!isMultiplication) { throw new IllegalArgumentException( "Cannot aggregate bars: the new timePeriod must be a multiplication of the actual timePeriod."); } int i = 0; final Num zero = bars.iterator().next().getOpenPrice().numOf(0); while (i < bars.size()) { Bar bar = bars.get(i); final ZonedDateTime beginTime = bar.getBeginTime(); final Num open = bar.getOpenPrice(); Num high = bar.getHighPrice(); Num low = bar.getLowPrice(); Num close = null; Num volume = zero; Num amount = zero; Duration sumDur = Duration.ZERO; while (sumDur.compareTo(timePeriod) < 0) { if (i < bars.size()) { bar = bars.get(i); if (high == null || bar.getHighPrice().isGreaterThan(high)) { high = bar.getHighPrice(); } if (low == null || bar.getLowPrice().isLessThan(low)) { low = bar.getLowPrice(); } close = bar.getClosePrice(); volume = volume.plus(bar.getVolume()); amount = amount.plus(bar.getAmount()); } sumDur = sumDur.plus(actualDur); i++; } if (!onlyFinalBars || i <= bars.size()) { final Bar aggregatedBar = new BaseBar(timePeriod, beginTime.plus(timePeriod), open, high, low, close, volume, amount); aggregated.add(aggregatedBar); } } return aggregated; } DurationBarAggregator(Duration timePeriod);  DurationBarAggregator(Duration timePeriod, boolean onlyFinalBars); @Override List<Bar> aggregate(List<Bar> bars);  }