### Question:
ProfitLossCriterion extends AbstractAnalysisCriterion { @Override public Num calculate(BarSeries series, TradingRecord tradingRecord) { return tradingRecord.getTrades().stream().filter(Trade::isClosed).map(trade -> calculate(series, trade)) .reduce(series.numOf(0), Num::plus); }  @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() { MockBarSeries series = new MockBarSeries(numFunction, 100, 105, 110, 100, 95, 105); TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, series, series.numOf(50)), Order.sellAt(2, series, series.numOf(50)), Order.buyAt(3, series, series.numOf(50)), Order.sellAt(5, series, series.numOf(50))); AnalysisCriterion profit = getCriterion(); assertNumEquals(500 + 250, profit.calculate(series, tradingRecord)); }
@Test public void calculateOnlyWithLossTrades() { MockBarSeries series = new MockBarSeries(numFunction, 100, 95, 100, 80, 85, 70); TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, series, series.numOf(50)), Order.sellAt(1, series, series.numOf(50)), Order.buyAt(2, series, series.numOf(50)), Order.sellAt(5, series, series.numOf(50))); AnalysisCriterion profit = getCriterion(); assertNumEquals(-250 - 1500, profit.calculate(series, tradingRecord)); }
@Test public void calculateShortOnlyWithGainTrades() { MockBarSeries series = new MockBarSeries(numFunction, 100, 105, 110, 100, 95, 105); TradingRecord tradingRecord = new BaseTradingRecord(Order.sellAt(0, series, series.numOf(50)), Order.buyAt(2, series, series.numOf(50)), Order.sellAt(3, series, series.numOf(50)), Order.buyAt(5, series, series.numOf(50))); AnalysisCriterion profit = getCriterion(); assertNumEquals(-(500 + 250), profit.calculate(series, tradingRecord)); }
@Test public void calculateShortOnlyWithLossTrades() { MockBarSeries series = new MockBarSeries(numFunction, 100, 95, 100, 80, 85, 70); TradingRecord tradingRecord = new BaseTradingRecord(Order.sellAt(0, series, series.numOf(50)), Order.buyAt(1, series, series.numOf(50)), Order.sellAt(2, series, series.numOf(50)), Order.buyAt(5, series, series.numOf(50))); AnalysisCriterion profit = getCriterion(); assertNumEquals(250 + 1500, profit.calculate(series, tradingRecord)); }