|
1 |
| -package orderbook; |
| 1 | +import java.util.*; |
2 | 2 |
|
3 |
| -import java.util.ArrayList; |
4 |
| -import java.util.Collections; |
5 |
| -import java.util.HashMap; |
6 |
| -import java.util.List; |
7 |
| -import java.util.Map; |
8 |
| -import java.util.TreeMap; |
| 3 | +class Order { |
| 4 | + int quantity; |
| 5 | + double price; |
| 6 | + String side; |
9 | 7 |
|
10 |
| -public class OrderBook { |
11 |
| - |
12 |
| - TreeMap<Double, List<Order>> buyOrders = new TreeMap<>(Collections.reverseOrder()); |
13 |
| - TreeMap<Double, List<Order>> sellOrders = new TreeMap<>(); |
14 |
| - HashMap<String, Order> orderMap = new HashMap<>(); |
| 8 | + Order(int quantity, double price, String side) { |
| 9 | + this.quantity = quantity; |
| 10 | + this.price = price; |
| 11 | + this.side = side; |
| 12 | + } |
| 13 | +} |
15 | 14 |
|
16 |
| - public void addOrder(Order order) { |
17 |
| - TreeMap<Double, List<Order>> refTreeMap = order.getIsBuyOrder() ? buyOrders : sellOrders; |
18 |
| - refTreeMap.putIfAbsent(order.getPrice(), new ArrayList<>()); |
19 |
| - refTreeMap.get(order.getPrice()).add(order); |
20 |
| - orderMap.put(order.getId(), order); |
| 15 | +final class Orderbook { |
| 16 | + private final TreeMap<Double, List<Order>> bids = new TreeMap<>(Collections.reverseOrder()); |
| 17 | + private final TreeMap<Double, List<Order>> asks = new TreeMap<>(); |
| 18 | + |
| 19 | + public Orderbook(boolean generateDummies) { |
| 20 | + if (generateDummies) { |
| 21 | + Random random = new Random(12); |
| 22 | + |
| 23 | + for (int i = 0; i < 10; i++) { |
| 24 | + double randomPrice = 90.0 + random.nextInt(1001) / 100.0; |
| 25 | + addOrder(random.nextInt(100) + 1, randomPrice, "BUY"); |
| 26 | + addOrder(random.nextInt(100) + 1, randomPrice, "BUY"); |
| 27 | + } |
| 28 | + |
| 29 | + for (int i = 0; i < 10; i++) { |
| 30 | + double randomPrice = 100.0 + random.nextInt(1001) / 100.0; |
| 31 | + addOrder(random.nextInt(100) + 1, randomPrice, "SELL"); |
| 32 | + addOrder(random.nextInt(100) + 1, randomPrice, "SELL"); |
| 33 | + } |
| 34 | + |
| 35 | + } |
21 | 36 | }
|
22 |
| - |
23 |
| - public boolean removeOrders(String orderId) { |
24 |
| - Order order = orderMap.remove(orderId); |
25 |
| - if(order==null) { |
26 |
| - return false; |
| 37 | + |
| 38 | + public void addOrder(int qty, double price, String side) { |
| 39 | + Order order = new Order(qty, price, side.equals("BUY") ? "BUY" : "SELL"); |
| 40 | + TreeMap<Double, List<Order>> book = side.equals("BUY") ? bids : asks; |
| 41 | + |
| 42 | + book.computeIfAbsent(price, k -> new ArrayList<>()).add(order); |
| 43 | + } |
| 44 | + |
| 45 | + public void print() { |
| 46 | + System.out.println("========== Orderbook ========="); |
| 47 | + printLeg(asks, "ASK"); |
| 48 | + |
| 49 | + double bestAsk = bestQuote("SELL"); |
| 50 | + double bestBid = bestQuote("BUY"); |
| 51 | + System.out.println("====== " + String.format("%.2f", (bestAsk - bestBid) / bestBid * 10000) + "bps ======"); |
| 52 | + |
| 53 | + printLeg(bids, "BUY"); |
| 54 | + System.out.println("==============================\n\n"); |
| 55 | + } |
| 56 | + |
| 57 | + private void printLeg(TreeMap<Double, List<Order>> book, String side) { |
| 58 | + if (side.equals("ASK")) { |
| 59 | + for (Map.Entry<Double, List<Order>> entry : book.entrySet()) { |
| 60 | + printPriceLevel(entry.getKey(), entry.getValue(), "31"); |
| 61 | + } |
| 62 | + } else { |
| 63 | + NavigableSet<Double> descendingKeys = book.navigableKeySet().descendingSet(); |
| 64 | + for (Double key : descendingKeys) { |
| 65 | + printPriceLevel(key, book.get(key), "32"); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private void printPriceLevel(double price, List<Order> orders, String color) { |
| 71 | + int totalQuantity = 0; |
| 72 | + for (Order order : orders) { |
| 73 | + totalQuantity += order.quantity; |
27 | 74 | }
|
28 |
| - TreeMap<Double, List<Order>> refTreeMap = order.getIsBuyOrder() ? buyOrders : sellOrders; |
29 |
| - List<Order> ordersAtPrice = refTreeMap.get(order.getPrice()); |
30 |
| - ordersAtPrice.remove(order); |
31 |
| - if(ordersAtPrice.isEmpty()) { |
32 |
| - refTreeMap.remove(order.getPrice()); |
| 75 | + System.out.printf("\t\033[1;%sm\u20B9%6.2f%5d\033[0m ", color, price, totalQuantity); |
| 76 | + for (int i = 0; i < totalQuantity / 10; i++) { |
| 77 | + System.out.print("█"); |
33 | 78 | }
|
34 |
| - return true; |
| 79 | + System.out.println(); |
| 80 | + } |
| 81 | + |
| 82 | + private double bestQuote(String side) { |
| 83 | + TreeMap<Double, List<Order>> book = side.equals("BUY") ? bids : asks; |
| 84 | + return book.isEmpty() ? 0.0 : book.firstKey(); |
35 | 85 | }
|
| 86 | + |
36 | 87 |
|
37 |
| - public void viewOrders() { |
38 |
| - System.out.println("Buy Orders are: "); |
39 |
| - if(!buyOrders.isEmpty()) { |
40 |
| - for(Map.Entry<Double, List<Order>> entry : buyOrders.entrySet()) { |
41 |
| - System.out.println("Price: "+entry.getKey()+" -> "+entry.getValue()); |
| 88 | + |
| 89 | + public OrderFill handleOrder(String type, int orderQuantity, String side, double price) { |
| 90 | + int unitsTransacted = 0; |
| 91 | + double totalValue = 0.0; |
| 92 | + |
| 93 | + TreeMap<Double, List<Order>> book = side.equals("BUY") ? asks : bids; |
| 94 | + |
| 95 | + Iterator<Map.Entry<Double, List<Order>>> iterator = book.entrySet().iterator(); |
| 96 | + while (iterator.hasNext() && orderQuantity > 0) { |
| 97 | + Map.Entry<Double, List<Order>> entry = iterator.next(); |
| 98 | + double priceLevel = entry.getKey(); |
| 99 | + |
| 100 | + if (type.equals("LIMIT") && ((side.equals("BUY") && priceLevel > price) |
| 101 | + || (side.equals("SELL") && priceLevel < price))) { |
| 102 | + break; |
42 | 103 | }
|
43 |
| - } else { |
44 |
| - System.out.println("No buy orders."); |
45 |
| - } |
46 | 104 |
|
47 |
| - System.out.println("Sell Orders are: "); |
48 |
| - if(!sellOrders.isEmpty()) { |
49 |
| - for(Map.Entry<Double, List<Order>> entry : sellOrders.entrySet()) { |
50 |
| - System.out.println("Price: "+entry.getKey()+" -> "+entry.getValue()); |
| 105 | + List<Order> orders = entry.getValue(); |
| 106 | + Iterator<Order> orderIterator = orders.iterator(); |
| 107 | + while (orderIterator.hasNext() && orderQuantity > 0) { |
| 108 | + Order order = orderIterator.next(); |
| 109 | + |
| 110 | + if (order.quantity > orderQuantity) { |
| 111 | + unitsTransacted += orderQuantity; |
| 112 | + totalValue += orderQuantity * priceLevel; |
| 113 | + order.quantity -= orderQuantity; |
| 114 | + orderQuantity = 0; |
| 115 | + } else { |
| 116 | + unitsTransacted += order.quantity; |
| 117 | + totalValue += order.quantity * priceLevel; |
| 118 | + orderQuantity -= order.quantity; |
| 119 | + orderIterator.remove(); |
| 120 | + } |
51 | 121 | }
|
52 |
| - } else { |
53 |
| - System.out.println("No sell orders."); |
| 122 | + |
| 123 | + if (orders.isEmpty()) { |
| 124 | + iterator.remove(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if (type.equals("LIMIT") && orderQuantity > 0) { |
| 129 | + addOrder(orderQuantity, price, side.equals("BUY") ? "BUY" : "SELL"); |
54 | 130 | }
|
| 131 | + |
| 132 | + return new OrderFill(unitsTransacted, totalValue); |
55 | 133 | }
|
| 134 | +} |
56 | 135 |
|
| 136 | +class OrderFill { |
| 137 | + int unitsTransacted; |
| 138 | + double totalValue; |
| 139 | + |
| 140 | + OrderFill(int unitsTransacted, double totalValue) { |
| 141 | + this.unitsTransacted = unitsTransacted; |
| 142 | + this.totalValue = totalValue; |
| 143 | + } |
57 | 144 | }
|
0 commit comments