diff --git a/src/StockExchange/Exchange.java b/src/StockExchange/Exchange.java index b410079..97482ff 100644 --- a/src/StockExchange/Exchange.java +++ b/src/StockExchange/Exchange.java @@ -7,241 +7,221 @@ import StockExchange.Order.OrderStatus; public class Exchange extends Thread { - // Class to describe an executed order - public class Quote { - double bid; - double bid_size; - double ask; - double ask_size; - - public double price; - public long size; - public long time; - } - - - + // Class to describe an executed order + public class Quote { - // QuoteReceiver has to be implemented by objects that wants - // to receive quote updates - public interface QuoteReceiver - { - void UpdateQuote(Quote q); - } - - // Here we store the list of quote receivers - TreeSet qrlist=new TreeSet(); - public void AddQuoteReceiver(QuoteReceiver qr) - { - qrlist.add(qr); - } - - // send updated quotes to all quote receivers - void UpdateQuoteReceivers(Quote q) - { - Iterator i = qrlist.iterator(); - while(i.hasNext()){ - i.next().UpdateQuote(q); - } - } - + double bid; + double bid_size; + double ask; + double ask_size; + public double price; + public long size; + public long time; + } - public ArrayList quoteHistory = new ArrayList(); + // QuoteReceiver has to be implemented by objects that wants + // to receive quote updates + public interface QuoteReceiver { - // long time = 0; - double price = 12.9; - long orderid = 1; + void UpdateQuote(Quote q); + } - double lastprice = 300.0; - long lastsize; + // Here we store the list of quote receivers + TreeSet qrlist = new TreeSet(); - // Order orderlist[]; + public void AddQuoteReceiver(QuoteReceiver qr) { + qrlist.add(qr); + } - TreeSet bid = new TreeSet(); - TreeSet ask = new TreeSet(); + // send updated quotes to all quote receivers + void UpdateQuoteReceivers(Quote q) { + Iterator i = qrlist.iterator(); + while (i.hasNext()) { + i.next().UpdateQuote(q); + } + } - private final Semaphore available = new Semaphore(1, true); + public ArrayList quoteHistory = new ArrayList(); - private void Lock() { - try { - available.acquire(); - } catch (InterruptedException e) { - System.out.println("Interrupted"); - } + // long time = 0; + double price = 12.9; + long orderid = 1; - } + double lastprice = 300.0; + long lastsize; - private void Unlock() { - available.release(); - } - - - + // Order orderlist[]; + TreeSet bid = new TreeSet(); + TreeSet ask = new TreeSet(); - public void print_current() { - - - BuyOrder b; - SellOrder a; - - - //String bid; - + private final Semaphore available = new Semaphore(1, true); - if (bid.isEmpty()) { - b = new BuyOrder(); - b.limit = -1; - b.size = 0; - } else - b = bid.first(); + private void Lock() { + try { + available.acquire(); + } catch (InterruptedException e) { + System.out.println("Interrupted"); + } - if (ask.isEmpty()) { - a = new SellOrder(); - a.limit = -1; - a.size = 0; + } - } else - a = ask.first(); - - - Logger.info( - String.format("BID: %s(%s) LAST: %.2f(%d) ASK: %s(%s)\n", - b.format_limit(), b.format_size(), - lastprice, lastsize, - a.format_limit(), a.format_size()) - ); + private void Unlock() { + available.release(); + } - } + public void print_current() { - public void TransferMoney(Account src, Account dst, double money) { - src.money -= money; - dst.money += money; - } + BuyOrder b; + SellOrder a; - public void CancelOrder(Order o) { - Lock(); + //String bid; + if (bid.isEmpty()) { + b = new BuyOrder(); + b.limit = -1; + b.size = 0; + } else { + b = bid.first(); + } + + if (ask.isEmpty()) { + a = new SellOrder(); + a.limit = -1; + a.size = 0; + + } else { + a = ask.first(); + } + + Logger.info( + String.format("BID: %s(%s) LAST: %.2f(%d) ASK: %s(%s)\n", + b.format_limit(), b.format_size(), + lastprice, lastsize, + a.format_limit(), a.format_size()) + ); + + } + + public void TransferMoney(Account src, Account dst, double money) { + src.money -= money; + dst.money += money; + } + + public void CancelOrder(Order o) { + Lock(); // System.out.println("Cancel BuyOrder"); - bid.remove(o); - ask.remove(o); - o.status=OrderStatus.canceled; - Unlock(); + bid.remove(o); + ask.remove(o); + o.status = OrderStatus.canceled; + Unlock(); - } + } - public void OrderMatching() { + public void OrderMatching() { - while (true) { - if (bid.isEmpty() || ask.isEmpty()) { - // nothing to do - return; - } + while (true) { + if (bid.isEmpty() || ask.isEmpty()) { + // nothing to do + return; + } - BuyOrder b = bid.first(); - SellOrder a = ask.first(); + BuyOrder b = bid.first(); + SellOrder a = ask.first(); - if (a.size == 0) { - // This order is fully executed, remove - a.account.orderpending = false; - a.status=OrderStatus.executed; - ask.pollFirst(); - continue; - } + if (a.size == 0) { + // This order is fully executed, remove + a.account.orderpending = false; + a.status = OrderStatus.executed; + ask.pollFirst(); + continue; + } - if (b.size == 0) { - // - b.account.orderpending = false; - b.status=OrderStatus.executed; - bid.pollFirst(); - continue; - } + if (b.size == 0) { + // + b.account.orderpending = false; + b.status = OrderStatus.executed; + bid.pollFirst(); + continue; + } - if (b.limit < a.limit) { - // no match, nothing to do - return; - } + if (b.limit < a.limit) { + // no match, nothing to do + return; + } - if (b.limit >= a.limit) { - double price; + if (b.limit >= a.limit) { + double price; - + if (b.id < a.id) { + price = b.limit; + } else { + price = a.limit; + } - - - - - if (b.id < a.id) - price = b.limit; - else - price = a.limit; + long size = 0; + if (b.size >= a.size) { + size = a.size; + } else { + size = b.size; + } + b.account.Buy(a.account, size, price); + b.size -= size; + a.size -= size; - long size = 0; + lastprice = price; + lastsize = size; - if (b.size >= a.size) { - size = a.size; - } else { - size = b.size; - } + Quote q = new Quote(); - b.account.Buy(a.account, size, price); - b.size -= size; - a.size -= size; + q.size = size; + q.price = price; + q.time = System.currentTimeMillis(); - lastprice = price; - lastsize = size; + this.UpdateQuoteReceivers(q); - Quote q = new Quote(); + //quoteHistory.add(q); + continue; - q.size = size; - q.price = price; - q.time = System.currentTimeMillis(); - - this.UpdateQuoteReceivers(q); - - //quoteHistory.add(q); + } - continue; + return; + } + } - } + public void ExecuteOrder(BuyOrder o) { + // SellOrder op = ask.peek(); - return; - } - } + } - public void ExecuteOrder(BuyOrder o) { - // SellOrder op = ask.peek(); - - } - - public void SendOrder(SellOrder o) { + public void SendOrder(SellOrder o) { // System.out.println("EX Sellorder"); - Lock(); - o.timestamp = System.currentTimeMillis(); - o.id = orderid++; - ask.add(o); - Unlock(); - Lock(); - OrderMatching(); - Unlock(); - - } + Lock(); + o.timestamp = System.currentTimeMillis(); + o.id = orderid++; + ask.add(o); + Unlock(); + Lock(); + OrderMatching(); + Unlock(); - public void SendOrder(BuyOrder o) { - //System.out.println("EX Buyorder"); - Lock(); - o.timestamp = System.currentTimeMillis(); - o.id = orderid++; - bid.add(o); - Unlock(); - Lock(); - OrderMatching(); - Unlock(); + } - } + public void SendOrder(BuyOrder o) { + //System.out.println("EX Buyorder"); + Lock(); + o.timestamp = System.currentTimeMillis(); + o.id = orderid++; + bid.add(o); + Unlock(); + Lock(); + OrderMatching(); + Unlock(); - /* + } + + /* * public void SendOrder(Order o){ * * @@ -250,34 +230,33 @@ public class Exchange extends Thread { * if ( o.getClass() == SellOrder.class){ ask.add((SellOrder)o); } * * } - */ - - public double getlastprice() { - /* + */ + public double getlastprice() { + /* * SellOrder so = new SellOrder(); so.limit=1000.0; so.size=500; * SendOrder(so); * * BuyOrder bo = new BuyOrder(); bo.limit=1001.0; bo.size=300; * SendOrder(bo); - */ + */ - return price; - } + return price; + } - public double sendOrder(Account o) { - return 0.7; - } + public double sendOrder(Account o) { + return 0.7; + } - public void run() { - while (true) { - try { - sleep(1500); - } catch (InterruptedException e) { - System.out.println("Interrupted"); - } - print_current(); + public void run() { + while (true) { + try { + sleep(1500); + } catch (InterruptedException e) { + System.out.println("Interrupted"); + } + print_current(); - } - } + } + } -} \ No newline at end of file +} diff --git a/src/StockExchange/Logger.java b/src/StockExchange/Logger.java index c52c982..2c9f100 100644 --- a/src/StockExchange/Logger.java +++ b/src/StockExchange/Logger.java @@ -1,21 +1,23 @@ package StockExchange; public class Logger { - - static boolean dbg=true; - static boolean info=true; - - static void dbg(String s){ - if (!dbg) - return; - System.out.print("DBG: "); - System.out.println(s); - } - - static void info(String s){ - if (!info) - return; - System.out.print("INFO: "); - System.out.println(s); - } + + static boolean dbg = true; + static boolean info = true; + + static void dbg(String s) { + if (!dbg) { + return; + } + System.out.print("DBG: "); + System.out.println(s); + } + + static void info(String s) { + if (!info) { + return; + } + System.out.print("INFO: "); + System.out.println(s); + } } diff --git a/src/StockExchange/SellOrder.java b/src/StockExchange/SellOrder.java index 1cb14eb..fb9ac25 100644 --- a/src/StockExchange/SellOrder.java +++ b/src/StockExchange/SellOrder.java @@ -2,6 +2,7 @@ package StockExchange; public class SellOrder extends Order { + @Override public int compareTo(Order o) { if (o.limit < limit) {