Nice orderbook

This commit is contained in:
7u83
2016-12-27 20:16:56 +01:00
parent 7dde10a9f6
commit 1de297da36
13 changed files with 242 additions and 103 deletions

View File

@ -2,18 +2,21 @@ package SeSim;
public class BuyOrder extends Order implements Comparable<Order> {
@Override
public int compareTo(Order o) {
/* @Override
public int compareLimit(Order o) {
if (o.limit < limit) {
//System.out.println("return 1");
return -1;
}
if (o.limit > limit) {
//System.out.println("return -1");
return +1;
}
// System.out.println("0000000000000000000000");
return 0;
}
*/
public BuyOrder(){
type=OrderType.buy;
}
}

View File

@ -74,8 +74,8 @@ public class Exchange extends Thread {
double lastprice = 300.0;
long lastsize;
public TreeSet<BuyOrder> bid;
public TreeSet<SellOrder> ask;
public TreeSet<Order> bid;
public TreeSet<Order> ask;
private final Semaphore available = new Semaphore(1, true);
@ -93,7 +93,7 @@ public class Exchange extends Thread {
}
public ArrayList geAskBook(int n){
ArrayList ret = new ArrayList();
ArrayList <Order> ret= new ArrayList<>();
Iterator it=ask.iterator();
for(int i=0;i<n && it.hasNext();i++){
SellOrder o;
@ -106,7 +106,7 @@ public class Exchange extends Thread {
}
public ArrayList geBidBook(int n){
ArrayList ret = new ArrayList();
ArrayList <Order> ret = new ArrayList<>();
Iterator it=bid.iterator();
for(int i=0;i<n && it.hasNext();i++){
BuyOrder o;
@ -122,8 +122,8 @@ public class Exchange extends Thread {
public void print_current() {
BuyOrder b;
SellOrder a;
Order b;
Order a;
//String bid;
if (bid.isEmpty()) {
@ -176,8 +176,8 @@ public class Exchange extends Thread {
return;
}
BuyOrder b = bid.first();
SellOrder a = ask.first();
Order b = bid.first();
Order a = ask.first();
if (a.volume == 0) {
// This order is fully executed, remove
@ -251,6 +251,7 @@ public class Exchange extends Thread {
return true;
}
/*
public void SendOrder(SellOrder o) {
// System.out.println("EX Sellorder");
Lock();
@ -263,11 +264,42 @@ public class Exchange extends Thread {
Unlock();
Lock();
OrderMatching();
// OrderMatching();
Unlock();
}
*/
private void addOrder(Order o){
switch (o.type){
case buy:
bid.add(o);
break;
case sell:
ask.add(o);
break;
default:
return;
}
}
public void SendOrder(Order o){
Lock();
o.timestamp = System.currentTimeMillis();
boolean rc = InitOrder(o);
System.out.print(o.timestamp+" TS:\n");
o.id = orderid++;
addOrder(o);
Unlock();
}
/*
public void SendOrder(BuyOrder o) {
//System.out.println("EX Buyorder");
Lock();
@ -277,11 +309,11 @@ public class Exchange extends Thread {
Unlock();
Lock();
OrderMatching();
// OrderMatching();
Unlock();
}
*/
/*
* public void SendOrder(Order o){
*
@ -308,6 +340,10 @@ public class Exchange extends Thread {
return 0.7;
}
/**
*
*/
@Override
public void run() {
while (true) {
try {

View File

@ -17,14 +17,74 @@ public abstract class Order implements Comparable<Order> {
*/
public double limit;
// double money = 0;
/**
* Order ID
*/
public long id = 0;
/**
* Type of order
*/
public OrderType type;
public Account account = null;
protected int compareLimit(Order o){
int r=0;
if (o.limit < limit) {
r=-1;
}
if (o.limit > limit) {
r=1;
}
if (r==0)
return 0;
if (type==OrderType.sell)
return 1-r;
return r;
};
@Override
public int compareTo(Order o) {
if (o.type!=type){
System.out.print("OrderType Missmatch\n");
return -1;
}
int r = compareLimit(o);
if (r!=0)
return r;
if (o.timestamp< timestamp)
return -1;
if (o.timestamp>timestamp)
return 1;
if (o.id>id)
return -1;
if (o.id<id)
return 1;
return 0;
}
enum OrderStatus {
open, executed, canceled
}
enum OrderType {
buy,sell
}
OrderStatus status = OrderStatus.open;

View File

@ -1,16 +1,20 @@
package SeSim;
public class SellOrder extends Order {
/*
@Override
public int compareTo(Order o) {
if (o.limit < limit) {
return 1;
}
if (o.limit > limit) {
return -1;
}
return 0;
return super.compareTo(o);
}
public SellOrder(){
type=OrderType.buy;
}
*/
public SellOrder(){
type=OrderType.sell;
}
}