PreOrderValidationService { public boolean isInSafeZone(TradingSignal signal, double price, TradeableInstrument<N> instrument) { double wma10yr = this.movingAverageCalculationService.calculateWMA(instrument, FIVE_YRS_IN_MTHS, CandleStickGranularity.M); final double max10yrWmaOffset = baseTradingConfig.getMax10yrWmaOffset(); double minPrice = (1.0 - max10yrWmaOffset) * wma10yr; double maxPrice = (1.0 + max10yrWmaOffset) * wma10yr; if ((signal == TradingSignal.SHORT && price > minPrice) || (signal == TradingSignal.LONG && price < maxPrice)) { return true; } else { LOG.info(String.format( "Rejecting %s %s because price %2.5f is 10pct on either side of wma 10yr price of %2.5f", signal, instrument.getInstrument(), price, wma10yr)); return false; } } 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 priceInSafeZoneTest() { MovingAverageCalculationService<N> movingAvgCalcService = mock(MovingAverageCalculationService.class); BaseTradingConfig baseTradingCfg = mock(BaseTradingConfig.class); PreOrderValidationService<M, N, K> service = new PreOrderValidationService<M, N, K>(null, movingAvgCalcService, baseTradingCfg, null); when(baseTradingCfg.getMax10yrWmaOffset()).thenReturn(0.1); TradeableInstrument<N> eurusd = new TradeableInstrument<N>("EUR_USD"); when( movingAvgCalcService.calculateWMA(eq(eurusd), eq(PreOrderValidationService.FIVE_YRS_IN_MTHS), eq(CandleStickGranularity.M))).thenReturn(1.22); assertTrue(service.isInSafeZone(TradingSignal.LONG, 1.3, eurusd)); assertTrue(service.isInSafeZone(TradingSignal.SHORT, 1.11, eurusd)); assertFalse(service.isInSafeZone(TradingSignal.LONG, 1.36, eurusd)); assertFalse(service.isInSafeZone(TradingSignal.SHORT, 1.05, eurusd)); }