### Question:
ExpectedShortfallCriterion extends AbstractAnalysisCriterion { @Override public Num calculate(BarSeries series, TradingRecord tradingRecord) { Returns returns = new Returns(series, tradingRecord, Returns.ReturnType.LOG); return calculateES(returns, confidence); } ExpectedShortfallCriterion(Double confidence); @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, 106d, 107d, 108d, 115d); TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, series), Order.sellAt(2, series), Order.buyAt(3, series), Order.sellAt(5, series)); AnalysisCriterion varCriterion = getCriterion(); assertNumEquals(numOf(0.0), varCriterion.calculate(series, tradingRecord)); }
@Test public void calculateWithASimpleTrade() { series = new MockBarSeries(numFunction, 100d, 104d, 90d, 100d, 95d, 105d); TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, series), Order.sellAt(2, series)); AnalysisCriterion esCriterion = getCriterion(); assertNumEquals(numOf(Math.log(90d / 104)), esCriterion.calculate(series, tradingRecord)); }
@Test public void calculateOnlyWithLossTrade() { List<Double> prices = IntStream.rangeClosed(1, 100).asDoubleStream().boxed().sorted(Collections.reverseOrder()) .collect(Collectors.toList()); series = new MockBarSeries(numFunction, prices); Trade trade = new Trade(Order.buyAt(series.getBeginIndex(), series), Order.sellAt(series.getEndIndex(), series)); AnalysisCriterion esCriterion = getCriterion(); assertNumEquals(numOf(-0.35835189384561106), esCriterion.calculate(series, trade)); }
@Test public void calculateWithNoBarsShouldReturn0() { series = new MockBarSeries(numFunction, 100d, 95d, 100d, 80d, 85d, 70d); AnalysisCriterion varCriterion = getCriterion(); assertNumEquals(numOf(0), varCriterion.calculate(series, new BaseTradingRecord())); }
@Test public void calculateWithBuyAndHold() { series = new MockBarSeries(numFunction, 100d, 99d); Trade trade = new Trade(Order.buyAt(0, series), Order.sellAt(1, series)); AnalysisCriterion varCriterion = getCriterion(); assertNumEquals(numOf(Math.log(99d / 100)), varCriterion.calculate(series, trade)); }