PreOrderValidationService { public boolean checkLimitsForCcy(TradeableInstrument<N> instrument, TradingSignal signal) { String currencies[] = TradingUtils.splitInstrumentPair(instrument.getInstrument()); for (String currency : currencies) { int positionCount = this.tradeInfoService.findNetPositionCountForCurrency(currency) + this.orderInfoService.findNetPositionCountForCurrency(currency); int sign = TradingUtils.getSign(instrument.getInstrument(), signal, currency); int newPositionCount = positionCount + sign; if (Math.abs(newPositionCount) > this.baseTradingConfig.getMaxAllowedNetContracts() && Integer.signum(sign) == Integer.signum(positionCount)) { LOG.warn(String.format("Cannot place trade %s because max limit exceeded. max allowed=%d and " + "future net positions=%d for currency %s if trade executed", instrument.getInstrument(), this.baseTradingConfig .getMaxAllowedNetContracts(), newPositionCount, currency)); return false; } } return true; } PreOrderValidationService(TradeInfoService<M, N, K> tradeInfoService,
			MovingAverageCalculationService<N> movingAverageCalculationService, BaseTradingConfig baseTradingConfig,
			OrderInfoService<M, N, K> orderInfoService); boolean isInSafeZone(TradingSignal signal, double price, TradeableInstrument<N> instrument); boolean checkInstrumentNotAlreadyTraded(TradeableInstrument<N> instrument); boolean checkLimitsForCcy(TradeableInstrument<N> instrument, TradingSignal signal);  }
@Test public void limitsForCcyTest() { final String AUD = "AUD"; final String NZD = "NZD"; final String CAD = "CAD"; final String CHF = "CHF"; TradeInfoService<M, N, Long> tradeInfoService = mock(TradeInfoService.class); OrderInfoService<M, N, Long> orderInfoService = mock(OrderInfoService.class); BaseTradingConfig baseTradingCfg = mock(BaseTradingConfig.class); PreOrderValidationService<M, N, Long> service = new PreOrderValidationService<M, N, Long>(tradeInfoService, null, baseTradingCfg, orderInfoService); TradeableInstrument<N> audnzd = new TradeableInstrument<N>(AUD + TradingConstants.CURRENCY_PAIR_SEP_UNDERSCORE + NZD); when(baseTradingCfg.getMaxAllowedNetContracts()).thenReturn(4); when(tradeInfoService.findNetPositionCountForCurrency(AUD)).thenReturn(4); when(tradeInfoService.findNetPositionCountForCurrency(NZD)).thenReturn(3); when(orderInfoService.findNetPositionCountForCurrency(AUD)).thenReturn(-1); when(orderInfoService.findNetPositionCountForCurrency(NZD)).thenReturn(1); assertFalse(service.checkLimitsForCcy(audnzd, TradingSignal.SHORT)); TradeableInstrument<N> cadchf = new TradeableInstrument<N>(CAD + TradingConstants.CURRENCY_PAIR_SEP_UNDERSCORE + CHF); when(tradeInfoService.findNetPositionCountForCurrency(CAD)).thenReturn(5); when(tradeInfoService.findNetPositionCountForCurrency(CHF)).thenReturn(1); when(orderInfoService.findNetPositionCountForCurrency(CAD)).thenReturn(1); when(orderInfoService.findNetPositionCountForCurrency(CHF)).thenReturn(1); assertTrue(service.checkLimitsForCcy(cadchf, TradingSignal.SHORT)); }