IBLevel2QuoteProcessor extends IBQuoteProcessor<Level2QuoteData> { @Override protected void processData(Level2QuoteData data) { double price = data.getPrice(); Ticker ticker = data.getTicker(); int operation = data.getOperation(); int side = data.getSide(); int position = data.getPosition(); int size = data.getSize(); IMarketDepthBook book; MarketDepthBook.Side depthSide; if (side == SIDE_BID) { book = bidBookMap.get(data.getTicker()); depthSide = MarketDepthBook.Side.BID; if (book == null) { book = MarketDepthBook.newInstance(); book.setSide(MarketDepthBook.Side.BID); bidBookMap.put(ticker, book); } } else { book = askBookMap.get(data.getTicker()); depthSide = MarketDepthBook.Side.ASK; if (book == null) { book = MarketDepthBook.newInstance(); book.setSide(MarketDepthBook.Side.ASK); askBookMap.put(ticker, book); } } BigDecimal bdPrice = QuoteUtil.getBigDecimalValue(ticker, price); MarketDepthLevel level = new MarketDepthLevel(depthSide, bdPrice, size); if( operation == OP_INSERT ) { book.insertLevel(position, level); } else if ( operation == OP_UPDATE ) { book.updateLevel( position, level ); } else if( operation == OP_DELETE ) { book.deleteLevel(position); } else { throw new IllegalStateException( "Unknown operation: " + operation ); } buildAndFireEvent(ticker, book); } IBLevel2QuoteProcessor(BlockingQueue<Level2QuoteData> queue, IQuoteEngine quoteEngine); void removeTicker( Ticker ticker );  }
@Test public void testProcessData_BidSide_EmptyBook() { MockIBLevel2QuoteProcessor processor = buildQuoteProcessor(); Ticker ticker = getCurrencyTicker(); BigDecimal price = new BigDecimal(1.3553).setScale(4, RoundingMode.HALF_UP); Level2QuoteData data = new Level2QuoteData(ticker, 0, processor.OP_INSERT, processor.SIDE_BID, price.doubleValue(), 100); processor.processData(data); Ticker actualTicker = processor.buildAndFireEventTicker; IMarketDepthBook book = processor.buildAndFireEventBook; assertEquals(ticker, actualTicker); assertEquals(MarketDepthBook.Side.BID, book.getSide()); assertEquals(1, book.getLevelCount()); assertEquals(100, book.getTotalSize().intValue()); MarketDepthLevel level = book.getLevelAt(0); assertEquals(MarketDepthBook.Side.BID, level.getSide()); assertEquals(100, level.getSize().intValue()); assertEquals(price, level.getPrice()); }
@Test public void testProcessData_BidSide_NonEmptyBook() { MockIBLevel2QuoteProcessor processor = buildQuoteProcessor(); Ticker ticker = getCurrencyTicker(); IMarketDepthBook book = MarketDepthBook.newInstance(); book.setSide(MarketDepthBook.Side.BID); processor.bidBookMap.put(ticker, book); BigDecimal price = new BigDecimal(1.3553).setScale(4, RoundingMode.HALF_UP); Level2QuoteData data = new Level2QuoteData(ticker, 0, processor.OP_INSERT, processor.SIDE_BID, price.doubleValue(), 100); processor.processData(data); Ticker actualTicker = processor.buildAndFireEventTicker; IMarketDepthBook actualBook = processor.buildAndFireEventBook; assertEquals(ticker, actualTicker); assertEquals(MarketDepthBook.Side.BID, book.getSide()); assertEquals(1, book.getLevelCount()); assertEquals(100, book.getTotalSize().intValue()); MarketDepthLevel level = book.getLevelAt(0); assertEquals(MarketDepthBook.Side.BID, level.getSide()); assertEquals(100, level.getSize().intValue()); assertEquals(price, level.getPrice()); }
@Test public void testProcessData_AskSide_EmptyBook() { MockIBLevel2QuoteProcessor processor = buildQuoteProcessor(); Ticker ticker = getCurrencyTicker(); BigDecimal price = new BigDecimal(1.3553).setScale(4, RoundingMode.HALF_UP); Level2QuoteData data = new Level2QuoteData(ticker, 0, processor.OP_INSERT, processor.SIDE_ASK, price.doubleValue(), 100); processor.processData(data); Ticker actualTicker = processor.buildAndFireEventTicker; IMarketDepthBook book = processor.buildAndFireEventBook; assertEquals(ticker, actualTicker); assertEquals(MarketDepthBook.Side.ASK, book.getSide()); assertEquals(1, book.getLevelCount()); assertEquals(100, book.getTotalSize().intValue()); MarketDepthLevel level = book.getLevelAt(0); assertEquals(MarketDepthBook.Side.ASK, level.getSide()); assertEquals(100, level.getSize().intValue()); assertEquals(price, level.getPrice()); }
@Test public void testProcessData_AskSide_NonEmptyBook() { MockIBLevel2QuoteProcessor processor = buildQuoteProcessor(); Ticker ticker = getCurrencyTicker(); IMarketDepthBook book = MarketDepthBook.newInstance(); book.setSide(MarketDepthBook.Side.ASK); processor.askBookMap.put(ticker, book); BigDecimal price = new BigDecimal(1.3553).setScale(4, RoundingMode.HALF_UP); Level2QuoteData data = new Level2QuoteData(ticker, 0, processor.OP_INSERT, processor.SIDE_ASK, price.doubleValue(), 100); processor.processData(data); Ticker actualTicker = processor.buildAndFireEventTicker; IMarketDepthBook actualBook = processor.buildAndFireEventBook; assertEquals(ticker, actualTicker); assertEquals(MarketDepthBook.Side.ASK, book.getSide()); assertEquals(1, book.getLevelCount()); assertEquals(100, book.getTotalSize().intValue()); MarketDepthLevel level = book.getLevelAt(0); assertEquals(MarketDepthBook.Side.ASK, level.getSide()); assertEquals(100, level.getSize().intValue()); assertEquals(price, level.getPrice()); }
@Test public void testUpdateLevel() { MockIBLevel2QuoteProcessor processor = buildQuoteProcessor(); final IMarketDepthBook mockBook = mockery.mock(IMarketDepthBook.class); final int index = 0; MarketDepthBook.setTestInstance(mockBook); Ticker ticker = getCurrencyTicker(); processor.askBookMap.put(ticker, mockBook); double price = 1.3551; BigDecimal bdPrice = QuoteUtil.getBigDecimalValue(ticker, price); Level2QuoteData data = new Level2QuoteData(ticker, 0, processor.OP_UPDATE, processor.SIDE_BID, bdPrice.doubleValue(), 500); final MarketDepthLevel level = new MarketDepthLevel(MarketDepthBook.Side.BID, bdPrice, 500); mockery.checking(new Expectations() { { one(mockBook).setSide(MarketDepthBook.Side.BID); one(mockBook).updateLevel(index, level); } }); processor.processData(data); mockery.assertIsSatisfied(); }
@Test public void testDeleteLevel() { MockIBLevel2QuoteProcessor processor = buildQuoteProcessor(); final IMarketDepthBook mockBook = mockery.mock(IMarketDepthBook.class); final int index = 0; MarketDepthBook.setTestInstance(mockBook); Ticker ticker = getCurrencyTicker(); processor.askBookMap.put(ticker, mockBook); double price = 1.3551; BigDecimal bdPrice = QuoteUtil.getBigDecimalValue(ticker, price); Level2QuoteData data = new Level2QuoteData(ticker, 0, processor.OP_DELETE, processor.SIDE_BID, bdPrice.doubleValue(), 500); mockery.checking(new Expectations() { { one(mockBook).setSide(MarketDepthBook.Side.BID); one(mockBook).deleteLevel(index); } }); processor.processData(data); mockery.assertIsSatisfied(); }
@Test public void testUnknownOperation() { MockIBLevel2QuoteProcessor processor = buildQuoteProcessor(); final IMarketDepthBook mockBook = mockery.mock(IMarketDepthBook.class); final int index = 0; MarketDepthBook.setTestInstance(mockBook); Ticker ticker = getCurrencyTicker(); processor.askBookMap.put(ticker, mockBook); double price = 1.3551; BigDecimal bdPrice = QuoteUtil.getBigDecimalValue(ticker, price); Level2QuoteData data = new Level2QuoteData(ticker, 0, 99, processor.SIDE_BID, bdPrice.doubleValue(), 500); mockery.checking(new Expectations() { { one(mockBook).setSide(MarketDepthBook.Side.BID); } }); try { processor.processData(data); fail(); } catch (IllegalStateException ex) { } mockery.assertIsSatisfied(); }