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