### Question:
AverageProfitableTradesCriterion extends AbstractAnalysisCriterion { @Override public double calculate(TimeSeries series, Trade trade) { int entryIndex = trade.getEntry().getIndex(); int exitIndex = trade.getExit().getIndex(); Decimal result; if (trade.getEntry().isBuy()) { result = series.getTick(exitIndex).getClosePrice().dividedBy(series.getTick(entryIndex).getClosePrice()); } else { result = series.getTick(entryIndex).getClosePrice().dividedBy(series.getTick(exitIndex).getClosePrice()); } return (result.isGreaterThan(Decimal.ONE)) ? 1d : 0d; }  @Override double calculate(TimeSeries series, Trade trade); @Override double calculate(TimeSeries series, TradingRecord tradingRecord); @Override boolean betterThan(double criterionValue1, double criterionValue2);  }### Answer:
@Test public void calculate() { TimeSeries series = new MockTimeSeries(100d, 95d, 102d, 105d, 97d, 113d); TradingRecord tradingRecord = new BaseTradingRecord( Order.buyAt(0), Order.sellAt(1), Order.buyAt(2), Order.sellAt(3), Order.buyAt(4), Order.sellAt(5)); AverageProfitableTradesCriterion average = new AverageProfitableTradesCriterion(); assertEquals(2d/3, average.calculate(series, tradingRecord), TATestsUtils.TA_OFFSET); }
@Test public void calculateWithOneTrade() { TimeSeries series = new MockTimeSeries(100d, 95d, 102d, 105d, 97d, 113d); Trade trade = new Trade(Order.buyAt(0), Order.sellAt(1)); AverageProfitableTradesCriterion average = new AverageProfitableTradesCriterion(); assertEquals(0d, average.calculate(series, trade), TATestsUtils.TA_OFFSET); trade = new Trade(Order.buyAt(1), Order.sellAt(2)); assertEquals(1d, average.calculate(series, trade), TATestsUtils.TA_OFFSET); }
@Test public void calculate() { TimeSeries series = new MockTimeSeries(100d, 95d, 102d, 105d, 97d, 113d); TradingRecord tradingRecord = new TradingRecord( Order.buyAt(0), Order.sellAt(1), Order.buyAt(2), Order.sellAt(3), Order.buyAt(4), Order.sellAt(5)); AverageProfitableTradesCriterion average = new AverageProfitableTradesCriterion(); assertEquals(2d/3, average.calculate(series, tradingRecord), TATestsUtils.TA_OFFSET); }