Some reformatting.

This commit is contained in:
7u83 2016-12-25 21:26:30 +01:00
parent c1e3e2162c
commit c0676d5b03
3 changed files with 210 additions and 228 deletions

View File

@ -9,6 +9,7 @@ public class Exchange extends Thread {
// Class to describe an executed order // Class to describe an executed order
public class Quote { public class Quote {
double bid; double bid;
double bid_size; double bid_size;
double ask; double ask;
@ -19,35 +20,29 @@ public class Exchange extends Thread {
public long time; public long time;
} }
// QuoteReceiver has to be implemented by objects that wants // QuoteReceiver has to be implemented by objects that wants
// to receive quote updates // to receive quote updates
public interface QuoteReceiver public interface QuoteReceiver {
{
void UpdateQuote(Quote q); void UpdateQuote(Quote q);
} }
// Here we store the list of quote receivers // Here we store the list of quote receivers
TreeSet<QuoteReceiver> qrlist = new TreeSet<QuoteReceiver>(); TreeSet<QuoteReceiver> qrlist = new TreeSet<QuoteReceiver>();
public void AddQuoteReceiver(QuoteReceiver qr)
{ public void AddQuoteReceiver(QuoteReceiver qr) {
qrlist.add(qr); qrlist.add(qr);
} }
// send updated quotes to all quote receivers // send updated quotes to all quote receivers
void UpdateQuoteReceivers(Quote q) void UpdateQuoteReceivers(Quote q) {
{
Iterator<QuoteReceiver> i = qrlist.iterator(); Iterator<QuoteReceiver> i = qrlist.iterator();
while (i.hasNext()) { while (i.hasNext()) {
i.next().UpdateQuote(q); i.next().UpdateQuote(q);
} }
} }
public ArrayList<Quote> quoteHistory = new ArrayList();
public ArrayList<Quote> quoteHistory = new ArrayList<Quote>();
// long time = 0; // long time = 0;
double price = 12.9; double price = 12.9;
@ -57,7 +52,6 @@ public class Exchange extends Thread {
long lastsize; long lastsize;
// Order orderlist[]; // Order orderlist[];
TreeSet<BuyOrder> bid = new TreeSet<BuyOrder>(); TreeSet<BuyOrder> bid = new TreeSet<BuyOrder>();
TreeSet<SellOrder> ask = new TreeSet<SellOrder>(); TreeSet<SellOrder> ask = new TreeSet<SellOrder>();
@ -76,34 +70,28 @@ public class Exchange extends Thread {
available.release(); available.release();
} }
public void print_current() { public void print_current() {
BuyOrder b; BuyOrder b;
SellOrder a; SellOrder a;
//String bid; //String bid;
if (bid.isEmpty()) { if (bid.isEmpty()) {
b = new BuyOrder(); b = new BuyOrder();
b.limit = -1; b.limit = -1;
b.size = 0; b.size = 0;
} else } else {
b = bid.first(); b = bid.first();
}
if (ask.isEmpty()) { if (ask.isEmpty()) {
a = new SellOrder(); a = new SellOrder();
a.limit = -1; a.limit = -1;
a.size = 0; a.size = 0;
} else } else {
a = ask.first(); a = ask.first();
}
Logger.info( Logger.info(
String.format("BID: %s(%s) LAST: %.2f(%d) ASK: %s(%s)\n", String.format("BID: %s(%s) LAST: %.2f(%d) ASK: %s(%s)\n",
@ -164,18 +152,11 @@ public class Exchange extends Thread {
if (b.limit >= a.limit) { if (b.limit >= a.limit) {
double price; double price;
if (b.id < a.id) {
if (b.id < a.id)
price = b.limit; price = b.limit;
else } else {
price = a.limit; price = a.limit;
}
long size = 0; long size = 0;
@ -201,7 +182,6 @@ public class Exchange extends Thread {
this.UpdateQuoteReceivers(q); this.UpdateQuoteReceivers(q);
//quoteHistory.add(q); //quoteHistory.add(q);
continue; continue;
} }
@ -251,7 +231,6 @@ public class Exchange extends Thread {
* *
* } * }
*/ */
public double getlastprice() { public double getlastprice() {
/* /*
* SellOrder so = new SellOrder(); so.limit=1000.0; so.size=500; * SellOrder so = new SellOrder(); so.limit=1000.0; so.size=500;

View File

@ -6,15 +6,17 @@ public class Logger {
static boolean info = true; static boolean info = true;
static void dbg(String s) { static void dbg(String s) {
if (!dbg) if (!dbg) {
return; return;
}
System.out.print("DBG: "); System.out.print("DBG: ");
System.out.println(s); System.out.println(s);
} }
static void info(String s) { static void info(String s) {
if (!info) if (!info) {
return; return;
}
System.out.print("INFO: "); System.out.print("INFO: ");
System.out.println(s); System.out.println(s);
} }

View File

@ -2,6 +2,7 @@ package StockExchange;
public class SellOrder extends Order { public class SellOrder extends Order {
@Override
public int compareTo(Order o) { public int compareTo(Order o) {
if (o.limit < limit) { if (o.limit < limit) {