Some reformatting.
This commit is contained in:
parent
c1e3e2162c
commit
c0676d5b03
@ -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",
|
||||||
@ -124,7 +112,7 @@ public class Exchange extends Thread {
|
|||||||
// System.out.println("Cancel BuyOrder");
|
// System.out.println("Cancel BuyOrder");
|
||||||
bid.remove(o);
|
bid.remove(o);
|
||||||
ask.remove(o);
|
ask.remove(o);
|
||||||
o.status=OrderStatus.canceled;
|
o.status = OrderStatus.canceled;
|
||||||
Unlock();
|
Unlock();
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -143,7 +131,7 @@ public class Exchange extends Thread {
|
|||||||
if (a.size == 0) {
|
if (a.size == 0) {
|
||||||
// This order is fully executed, remove
|
// This order is fully executed, remove
|
||||||
a.account.orderpending = false;
|
a.account.orderpending = false;
|
||||||
a.status=OrderStatus.executed;
|
a.status = OrderStatus.executed;
|
||||||
ask.pollFirst();
|
ask.pollFirst();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -151,7 +139,7 @@ public class Exchange extends Thread {
|
|||||||
if (b.size == 0) {
|
if (b.size == 0) {
|
||||||
//
|
//
|
||||||
b.account.orderpending = false;
|
b.account.orderpending = false;
|
||||||
b.status=OrderStatus.executed;
|
b.status = OrderStatus.executed;
|
||||||
bid.pollFirst();
|
bid.pollFirst();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -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;
|
||||||
|
@ -2,19 +2,21 @@ package StockExchange;
|
|||||||
|
|
||||||
public class Logger {
|
public class Logger {
|
||||||
|
|
||||||
static boolean dbg=true;
|
static boolean dbg = true;
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user