@Override
    public Num calculate(BarSeries series, Position position) {
        if (position.isClosed()) {
            Num loss = excludeCosts ? position.getGrossProfit() : position.getProfit();
            return loss.isNegative() ? loss : series.zero();
        }
        return series.zero();

    }
@Test
    public void calculateComparingIncludingVsExcludingCosts() {
        MockBarSeries series = new MockBarSeries(numFunction, 100, 95, 100, 80, 85, 70);
        LinearTransactionCostModel transactionCost = new LinearTransactionCostModel(0.01);
        ZeroCostModel holdingCost = new ZeroCostModel();
        TradingRecord tradingRecord = new BaseTradingRecord(Trade.TradeType.BUY, transactionCost, holdingCost);

        // entry price = 100 (cost = 100*0.01 = 1) => netPrice = 101, grossPrice = 100
        tradingRecord.enter(0, series.getBar(0).getClosePrice(), numOf(1));
        // exit price = 95 (cost = 95*0.01 = 0.95) => netPrice = 94.05, grossPrice = 95
        tradingRecord.exit(1, series.getBar(1).getClosePrice(),
                tradingRecord.getCurrentPosition().getEntry().getAmount());

        // entry price = 100 (cost = 100*0.01 = 1) => netPrice = 101, grossPrice = 100
        tradingRecord.enter(2, series.getBar(2).getClosePrice(), numOf(1));
        // exit price = 70 (cost = 70*0.01 = 0.70) => netPrice = 69.3, grossPrice = 70
        tradingRecord.exit(5, series.getBar(5).getClosePrice(),
                tradingRecord.getCurrentPosition().getEntry().getAmount());

        // include costs, i.e. loss - costs:
        // [(94.05 - 101)] + [(69.3 - 101)] = -6.95 + (-31.7) = -38.65 loss
        // [(95 - 100)] + [(70 - 100)] = -5 + (-30) = -35 loss - 3.65 = -38.65 loss
        AnalysisCriterion lossIncludingCosts = getCriterion(false);
        assertNumEquals(-38.65, lossIncludingCosts.calculate(series, tradingRecord));

        // exclude costs, i.e. costs are not contained:
        // [(95 - 100)] + [(70 - 100)] = -5 + (-30) = -35 loss
        AnalysisCriterion lossExcludingCosts = getCriterion(true);
        assertNumEquals(-35, lossExcludingCosts.calculate(series, tradingRecord));
    }