### Question:
AverageProfitCriterion extends AbstractAnalysisCriterion { @Override public Num calculate(BarSeries series, TradingRecord tradingRecord) { Num bars = numberOfBars.calculate(series, tradingRecord); if (bars.isEqual(series.numOf(0))) { return series.numOf(1); } return totalProfit.calculate(series, tradingRecord).pow(series.numOf(1).dividedBy(bars)); }  @Override Num calculate(BarSeries series, TradingRecord tradingRecord); @Override Num calculate(BarSeries series, Trade trade); @Override boolean betterThan(Num criterionValue1, Num criterionValue2);  }### Answer:
@Test public void calculateOnlyWithGainTrades() { series = new MockBarSeries(numFunction, 100d, 105d, 110d, 100d, 95d, 105d); TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, series), Order.sellAt(2, series), Order.buyAt(3, series), Order.sellAt(5, series)); AnalysisCriterion averageProfit = getCriterion(); assertNumEquals(1.0243, averageProfit.calculate(series, tradingRecord)); }
@Test public void calculateWithASimpleTrade() { series = new MockBarSeries(numFunction, 100d, 105d, 110d, 100d, 95d, 105d); TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, series), Order.sellAt(2, series)); AnalysisCriterion averageProfit = getCriterion(); assertNumEquals(numOf(110d / 100).pow(numOf(1d / 3)), averageProfit.calculate(series, tradingRecord)); }
@Test public void calculateOnlyWithLossTrades() { series = new MockBarSeries(numFunction, 100, 95, 100, 80, 85, 70); TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, series), Order.sellAt(1, series), Order.buyAt(2, series), Order.sellAt(5, series)); AnalysisCriterion averageProfit = getCriterion(); assertNumEquals(numOf(95d / 100 * 70d / 100).pow(numOf(1d / 6)), averageProfit.calculate(series, tradingRecord)); }
@Test public void calculateWithNoBarsShouldReturn1() { series = new MockBarSeries(numFunction, 100, 95, 100, 80, 85, 70); AnalysisCriterion averageProfit = getCriterion(); assertNumEquals(1, averageProfit.calculate(series, new BaseTradingRecord())); }
@Test public void calculateWithOneTrade() { series = new MockBarSeries(numFunction, 100, 105); Trade trade = new Trade(Order.buyAt(0, series), Order.sellAt(1, series)); AnalysisCriterion average = getCriterion(); assertNumEquals(numOf(105d / 100).pow(numOf(0.5)), average.calculate(series, trade)); }