@Override
    public boolean isSatisfied(int index, TradingRecord tradingRecord) {
        if (tradingRecord != null && !tradingRecord.isClosed()) {
            Num entryPrice = tradingRecord.getCurrentPosition().getEntry().getNetPrice();
            Num currentPrice = this.referencePrice.getValue(index);
            Num threshold = this.stopLossThreshold.getValue(index);

            if (tradingRecord.getCurrentPosition().getEntry().isBuy()) {
                return currentPrice.isLessThan(entryPrice.minus(threshold));
            } else {
                return currentPrice.isGreaterThan(entryPrice.plus(threshold));
            }
        }
        return false;
    }
@Test
    public void testLongPositionStopLoss() {
        ZonedDateTime initialEndDateTime = ZonedDateTime.now();

        for (int i = 0; i < 10; i++) {
            series.addBar(initialEndDateTime.plusDays(i), 100, 105, 95, 100);
        }

        AverageTrueRangeStopLossRule rule = new AverageTrueRangeStopLossRule(series, 5, 1);

        // Enter long position
        TradingRecord tradingRecord = new BaseTradingRecord(Trade.TradeType.BUY, new LinearTransactionCostModel(0.01),
                new ZeroCostModel());
        tradingRecord.enter(0, series.numOf(100), series.numOf(1));

        // Price remains above stop loss
        series.addBar(series.getLastBar().getEndTime().plusDays(1), 90, 95, 85, 90);
        assertFalse(rule.isSatisfied(series.getEndIndex(), tradingRecord));

        // Price drops below stop loss
        series.addBar(series.getLastBar().getEndTime().plusDays(1), 90, 95, 85, 89);
        assertTrue(rule.isSatisfied(series.getEndIndex(), tradingRecord));
    }