EODTradingStrategy implements Level1QuoteListener, OrderEventListener, BrokerErrorListener { public synchronized void placeMOCOrders(Ticker longTicker, Ticker shortTicker, ZonedDateTime lastReportedTime) { String correlationId = getUUID(); int longSize = getOrderSize(longTicker); int shortSize = getOrderSize(shortTicker); logger.info("Placing long orders for: " + longSize + " shares of: " + longTicker ); logger.info("Placing short orders for: " + shortSize + " shares of: " + shortTicker ); TradeOrder longOrder = new TradeOrder(ibClient.getNextOrderId(), longTicker, longSize, TradeDirection.BUY); longOrder.setType(entryOrderType); longOrder.setReference("EOD-Pair-Strategy:" + correlationId + ":Entry:Long*"); TradeOrder longExitOrder = new TradeOrder(ibClient.getNextOrderId(), longTicker, longSize, TradeDirection.SELL); longExitOrder.setType(exitOrderType); longExitOrder.setGoodAfterTime(getNextBusinessDay(lastReportedTime)); longExitOrder.setReference("EOD-Pair-Strategy:" + correlationId + ":Exit:Long*"); logger.info( "Placing long orders: " + longOrder); longOrder.addChildOrder(longExitOrder); TradeOrder shortOrder = new TradeOrder(ibClient.getNextOrderId(), shortTicker, shortSize, TradeDirection.SELL_SHORT); shortOrder.setType(entryOrderType); shortOrder.setReference("EOD-Pair-Strategy:" + correlationId + ":Entry:Short*"); TradeOrder shortExitOrder = new TradeOrder(ibClient.getNextOrderId(), shortTicker, shortSize, TradeDirection.BUY_TO_COVER); shortExitOrder.setType(exitOrderType); shortExitOrder.setGoodAfterTime(getNextBusinessDay(lastReportedTime)); shortExitOrder.setReference("EOD-Pair-Strategy:" + correlationId + ":Exit:Short*"); logger.info( "Placing short orders: " + shortOrder); shortOrder.addChildOrder(shortExitOrder); ibClient.placeOrder(longOrder); ibClient.placeOrder(shortOrder); }  void start(String propFile); synchronized void placeMOCOrders(Ticker longTicker, Ticker shortTicker, ZonedDateTime lastReportedTime); @Override void orderEvent(OrderEvent event); @Override void quoteRecieved(ILevel1Quote quote); ZonedDateTime getNextBusinessDay(ZonedDateTime date); @Override void brokerErrorFired(BrokerError error); static void main(String[] args);  }
@Test public void testPlaceMOCOrders() { String corrId = "123"; int longSize = 100; int shortSize = 200; ZonedDateTime currentDateTime = ZonedDateTime.of(2016, 3, 3, 5, 5, 1, 0, ZoneId.systemDefault()); ZonedDateTime orderTime = ZonedDateTime.now(); strategy.ibClient = mockIbClient; doReturn(corrId).when(strategy).getUUID(); doReturn(longSize).when(strategy).getOrderSize(longTicker); doReturn(shortSize).when(strategy).getOrderSize(shortTicker); doReturn(orderTime).when(strategy).getNextBusinessDay(currentDateTime); when(mockIbClient.getNextOrderId()).thenReturn("100", "101", "102", "103", "104", "105"); TradeOrder expectedLongOrder = new TradeOrder("100", longTicker, longSize, TradeDirection.BUY); expectedLongOrder.setType(TradeOrder.Type.MARKET_ON_CLOSE); expectedLongOrder.setReference("EOD-Pair-Strategy:123:Entry:Long*"); TradeOrder expectedLongExitOrder = new TradeOrder("101", longTicker, longSize, TradeDirection.SELL); expectedLongExitOrder.setType(TradeOrder.Type.MARKET_ON_OPEN); expectedLongExitOrder.setGoodAfterTime(orderTime); expectedLongExitOrder.setReference("EOD-Pair-Strategy:123:Exit:Long*"); expectedLongOrder.addChildOrder(expectedLongExitOrder); TradeOrder expectedShortOrder = new TradeOrder("102", shortTicker, shortSize, TradeDirection.SELL_SHORT); expectedShortOrder.setType(TradeOrder.Type.MARKET_ON_CLOSE); expectedShortOrder.setReference("EOD-Pair-Strategy:123:Entry:Short*"); TradeOrder expectedShortExitOrder = new TradeOrder("103", shortTicker, shortSize, TradeDirection.BUY_TO_COVER); expectedShortExitOrder.setType(TradeOrder.Type.MARKET_ON_OPEN); expectedShortExitOrder.setGoodAfterTime(orderTime); expectedShortExitOrder.setReference("EOD-Pair-Strategy:123:Exit:Short*"); expectedShortOrder.addChildOrder(expectedShortExitOrder); strategy.placeMOCOrders(longTicker, shortTicker, currentDateTime); verify(mockIbClient).placeOrder(expectedLongOrder); verify(mockIbClient).placeOrder(expectedShortOrder); }