@Test public void buy() { book.enter(1, Side.SELL, 1000, 100); book.enter(2, Side.BUY, 1000, 100); Event ask = new Add(1, Side.SELL, 1000, 100); Event match = new Match(1, 2, Side.BUY, 1000, 100, 0); assertEquals(asList(ask, match), events.collect()); }
private void buy(long incomingId, long incomingPrice, long incomingQuantity) { while (!asks.isEmpty()) { Order resting = asks.first(); long restingPrice = resting.getPrice(); if (restingPrice > incomingPrice) break; long restingId = resting.getId(); long restingQuantity = resting.getRemainingQuantity(); if (restingQuantity > incomingQuantity) { resting.reduce(incomingQuantity); listener.match(restingId, incomingId, Side.BUY, restingPrice, incomingQuantity, resting.getRemainingQuantity()); return; } asks.remove(resting); orders.remove(restingId); listener.match(restingId, incomingId, Side.BUY, restingPrice, restingQuantity, 0); incomingQuantity -= restingQuantity; if (incomingQuantity == 0) return; } add(incomingId, Side.BUY, incomingPrice, incomingQuantity, bids); }
OrderBook { private void buy(long incomingId, long incomingPrice, long incomingQuantity) { while (!asks.isEmpty()) { Order resting = asks.first(); long restingPrice = resting.getPrice(); if (restingPrice > incomingPrice) break; long restingId = resting.getId(); long restingQuantity = resting.getRemainingQuantity(); if (restingQuantity > incomingQuantity) { resting.reduce(incomingQuantity); listener.match(restingId, incomingId, Side.BUY, restingPrice, incomingQuantity, resting.getRemainingQuantity()); return; } asks.remove(resting); orders.remove(restingId); listener.match(restingId, incomingId, Side.BUY, restingPrice, restingQuantity, 0); incomingQuantity -= restingQuantity; if (incomingQuantity == 0) return; } add(incomingId, Side.BUY, incomingPrice, incomingQuantity, bids); } }
OrderBook { private void buy(long incomingId, long incomingPrice, long incomingQuantity) { while (!asks.isEmpty()) { Order resting = asks.first(); long restingPrice = resting.getPrice(); if (restingPrice > incomingPrice) break; long restingId = resting.getId(); long restingQuantity = resting.getRemainingQuantity(); if (restingQuantity > incomingQuantity) { resting.reduce(incomingQuantity); listener.match(restingId, incomingId, Side.BUY, restingPrice, incomingQuantity, resting.getRemainingQuantity()); return; } asks.remove(resting); orders.remove(restingId); listener.match(restingId, incomingId, Side.BUY, restingPrice, restingQuantity, 0); incomingQuantity -= restingQuantity; if (incomingQuantity == 0) return; } add(incomingId, Side.BUY, incomingPrice, incomingQuantity, bids); } OrderBook(OrderBookListener listener); }
OrderBook { private void buy(long incomingId, long incomingPrice, long incomingQuantity) { while (!asks.isEmpty()) { Order resting = asks.first(); long restingPrice = resting.getPrice(); if (restingPrice > incomingPrice) break; long restingId = resting.getId(); long restingQuantity = resting.getRemainingQuantity(); if (restingQuantity > incomingQuantity) { resting.reduce(incomingQuantity); listener.match(restingId, incomingId, Side.BUY, restingPrice, incomingQuantity, resting.getRemainingQuantity()); return; } asks.remove(resting); orders.remove(restingId); listener.match(restingId, incomingId, Side.BUY, restingPrice, restingQuantity, 0); incomingQuantity -= restingQuantity; if (incomingQuantity == 0) return; } add(incomingId, Side.BUY, incomingPrice, incomingQuantity, bids); } OrderBook(OrderBookListener listener); void enter(long orderId, Side side, long price, long size); void cancel(long orderId, long size); }
OrderBook { private void buy(long incomingId, long incomingPrice, long incomingQuantity) { while (!asks.isEmpty()) { Order resting = asks.first(); long restingPrice = resting.getPrice(); if (restingPrice > incomingPrice) break; long restingId = resting.getId(); long restingQuantity = resting.getRemainingQuantity(); if (restingQuantity > incomingQuantity) { resting.reduce(incomingQuantity); listener.match(restingId, incomingId, Side.BUY, restingPrice, incomingQuantity, resting.getRemainingQuantity()); return; } asks.remove(resting); orders.remove(restingId); listener.match(restingId, incomingId, Side.BUY, restingPrice, restingQuantity, 0); incomingQuantity -= restingQuantity; if (incomingQuantity == 0) return; } add(incomingId, Side.BUY, incomingPrice, incomingQuantity, bids); } OrderBook(OrderBookListener listener); void enter(long orderId, Side side, long price, long size); void cancel(long orderId, long size);  }