### Question:
MaximumDrawdownCriterion extends AbstractAnalysisCriterion { @Override public Num calculate(BarSeries series, TradingRecord tradingRecord) { CashFlow cashFlow = new CashFlow(series, tradingRecord); return calculateMaximumDrawdown(series, cashFlow); }  @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 calculateWithNoTrades() { MockBarSeries series = new MockBarSeries(numFunction, 1, 2, 3, 6, 5, 20, 3); AnalysisCriterion mdd = getCriterion(); assertNumEquals(0d, mdd.calculate(series, new BaseTradingRecord())); }
@Test public void calculateWithOnlyGains() { MockBarSeries series = new MockBarSeries(numFunction, 1, 2, 3, 6, 8, 20, 3); AnalysisCriterion mdd = getCriterion(); TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, series), Order.sellAt(1, series), Order.buyAt(2, series), Order.sellAt(5, series)); assertNumEquals(0d, mdd.calculate(series, tradingRecord)); }
@Test public void calculateShouldWork() { MockBarSeries series = new MockBarSeries(numFunction, 1, 2, 3, 6, 5, 20, 3); AnalysisCriterion mdd = getCriterion(); TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, series), Order.sellAt(1, series), Order.buyAt(3, series), Order.sellAt(4, series), Order.buyAt(5, series), Order.sellAt(6, series)); assertNumEquals(.875d, mdd.calculate(series, tradingRecord)); }
@Test public void calculateWithNullSeriesSizeShouldReturn0() { MockBarSeries series = new MockBarSeries(numFunction, new double[] {}); AnalysisCriterion mdd = getCriterion(); assertNumEquals(0d, mdd.calculate(series, new BaseTradingRecord())); }
@Test public void withTradesThatSellBeforeBuying() { MockBarSeries series = new MockBarSeries(numFunction, 2, 1, 3, 5, 6, 3, 20); AnalysisCriterion mdd = getCriterion(); TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, series), Order.sellAt(1, series), Order.buyAt(3, series), Order.sellAt(4, series), Order.sellAt(5, series), Order.buyAt(6, series)); assertNumEquals(3.8d, mdd.calculate(series, tradingRecord)); }
@Test public void withSimpleTrades() { MockBarSeries series = new MockBarSeries(numFunction, 1, 10, 5, 6, 1); AnalysisCriterion mdd = getCriterion(); TradingRecord tradingRecord = new BaseTradingRecord(Order.buyAt(0, series), Order.sellAt(1, series), Order.buyAt(1, series), Order.sellAt(2, series), Order.buyAt(2, series), Order.sellAt(3, series), Order.buyAt(3, series), Order.sellAt(4, series)); assertNumEquals(.9d, mdd.calculate(series, tradingRecord)); }