Code refactoring and some improvements.

This commit is contained in:
7u83 2016-12-27 23:36:18 +01:00
parent aa67324462
commit da871d1a09
13 changed files with 152 additions and 192 deletions

View File

@ -35,7 +35,7 @@ public class AskBook extends OrderBook {
@Override @Override
ArrayList getArrayList() { ArrayList getArrayList() {
return MainWin.se.geAskBook(10); return MainWin.se.getAskBook(10);
} }
@Override @Override

View File

@ -35,7 +35,7 @@ public class BidBook extends OrderBook{
@Override @Override
ArrayList getArrayList() { ArrayList getArrayList() {
return MainWin.se.geBidBook(10); return MainWin.se.getBidBook(10);
} }
} }

View File

@ -85,7 +85,7 @@ public class ControlPanel extends javax.swing.JPanel {
private void SellButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SellButtonActionPerformed private void SellButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SellButtonActionPerformed
System.out.print("SellPressed\n"); System.out.print("SellPressed\n");
MainWin.myAccount.Sell(100, 520.0, MainWin.se); // MainWin.myAccount.Sell(100, 520.0, MainWin.se);
}//GEN-LAST:event_SellButtonActionPerformed }//GEN-LAST:event_SellButtonActionPerformed
private void BuyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuyButtonActionPerformed private void BuyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuyButtonActionPerformed

View File

@ -30,6 +30,7 @@ import SeSim.BuyOrder;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.*; import javax.swing.*;
import SeSim.*; import SeSim.*;
import Traders.*;
/** /**
@ -40,6 +41,7 @@ public class MainWin extends javax.swing.JFrame {
static SeSim.Exchange se; static SeSim.Exchange se;
static SeSim.Account myAccount; static SeSim.Account myAccount;
static Traders.ManTrader myTrader;
/** /**
* Creates new form MainWin * Creates new form MainWin
@ -156,7 +158,10 @@ public class MainWin extends javax.swing.JFrame {
public static void main(String args[]) { public static void main(String args[]) {
se = new Exchange(); se = new Exchange();
myAccount = new Account(1000,100000000.0); myAccount = new Account(se,1000,100000000.0);
myTrader = new Traders.ManTrader(myAccount);
SeSim.SellOrder so = new SeSim.SellOrder(); SeSim.SellOrder so = new SeSim.SellOrder();
so.limit = 20.0; so.limit = 20.0;
@ -164,29 +169,6 @@ public class MainWin extends javax.swing.JFrame {
so.timestamp = 12; so.timestamp = 12;
se.SendOrder(so); se.SendOrder(so);
/*
SeSim.BuyOrder bo = new SeSim.BuyOrder();
bo.limit = 20.0;
bo.volume = 12;
bo.timestamp = 12;
se.SendOrder(bo);
SeSim.BuyOrder bo1 = new SeSim.BuyOrder();
bo1.limit = 27.0;
bo1.volume = 123;
bo1.timestamp = 922;
se.SendOrder(bo1);
for (int i = 0; i < 130; i++) {
BuyOrder o = new BuyOrder();
o.volume = 90 + i;
o.limit = 80 + i;
se.SendOrder(o);
}
*/
try { try {
// Set cross-platform Java L&F (also called "Metal") // Set cross-platform Java L&F (also called "Metal")
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

View File

@ -42,35 +42,6 @@ public class OrderBookPanel extends javax.swing.JPanel {
public OrderBookPanel() { public OrderBookPanel() {
this.se = MainWin.se; this.se = MainWin.se;
MainWin.myAccount.Sell(100, 820.0, MainWin.se);
MainWin.myAccount.Sell(100, 90.0, MainWin.se);
MainWin.myAccount.Sell(310, 112.156, MainWin.se);
MainWin.myAccount.Sell(3, 112.156, MainWin.se);
MainWin.myAccount.Sell(9, 1112.156, MainWin.se);
MainWin.myAccount.Sell(17, 122.156, MainWin.se);
MainWin.myAccount.Sell(100, 120.0, MainWin.se);
MainWin.myAccount.Sell(100, 19.5, MainWin.se);
MainWin.myAccount.Sell(100, 19.5, MainWin.se);
MainWin.myAccount.Sell(100, 19.3, MainWin.se);
try {
sleep(1000);
} catch (InterruptedException e) {
System.out.println("Interrupted");
return;
}
MainWin.myAccount.Sell(310, 6112.156, MainWin.se);
MainWin.myAccount.Sell(3, 7112.156, MainWin.se);
MainWin.myAccount.Buy(100, 2.0, MainWin.se);
MainWin.myAccount.Buy(100, 3.0, MainWin.se);
MainWin.myAccount.Buy(100, 2.0, MainWin.se);
MainWin.myAccount.Buy(100, 1.0, MainWin.se);
MainWin.myAccount.Buy(100, 3.0, MainWin.se);
MainWin.myAccount.Buy(100, 5.0, MainWin.se);
initComponents(); initComponents();

View File

@ -1,7 +1,13 @@
package SeSim; package SeSim;
import java.util.*;
final public class Account { final public class Account {
/**
* Exchange this account belongs to
*/
protected Exchange se;
/** /**
* Number of shares in this account * Number of shares in this account
@ -21,13 +27,15 @@ final public class Account {
public boolean orderpending = false; public boolean orderpending = false;
public Account(long shares, double money ) { public Account(Exchange se, long shares, double money ) {
this.shares=shares; this.shares=shares;
this.money=money; this.money=money;
this.se=se;
pending = new TreeSet();
} }
public Account(){ public Account(){
this(0,0.0); //this(,0.0);
} }
// private double bound_money; // private double bound_money;
@ -40,13 +48,15 @@ final public class Account {
); );
} }
public SellOrder Sell(long volume, double limit, Exchange ex) { TreeSet pending;
public SellOrder sell(long volume, double limit) {
SellOrder o = new SellOrder(); SellOrder o = new SellOrder();
o.account = this; o.account = this;
o.limit = limit; o.limit = limit;
o.volume = volume; o.volume = volume;
orderpending = true; orderpending = true;
ex.SendOrder(o); se.SendOrder(o);
return o; return o;
} }
@ -66,11 +76,13 @@ final public class Account {
} }
/*
public void Buy(Account a, long size, double price) { public void Buy(Account a, long size, double price) {
shares += size; shares += size;
money -= price * size; money -= price * size;
a.shares -= size; a.shares -= size;
a.money += price * size; a.money += price * size;
} }
*/
} }

View File

@ -2,19 +2,6 @@ package SeSim;
public class BuyOrder extends Order implements Comparable<Order> { public class BuyOrder extends Order implements Comparable<Order> {
/* @Override
public int compareLimit(Order o) {
if (o.limit < limit) {
return -1;
}
if (o.limit > limit) {
return +1;
}
return 0;
}
*/
public BuyOrder(){ public BuyOrder(){
type=OrderType.buy; type=OrderType.buy;
} }

View File

@ -3,7 +3,6 @@ package SeSim;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import SeSim.Order.OrderStatus; import SeSim.Order.OrderStatus;
/** /**
@ -12,14 +11,6 @@ import SeSim.Order.OrderStatus;
*/ */
public class Exchange extends Thread { public class Exchange extends Thread {
// Here we store the list of quote receivers
private final TreeSet<QuoteReceiver> qrlist;
/** /**
* Histrory of quotes * Histrory of quotes
*/ */
@ -55,7 +46,22 @@ public class Exchange extends Thread {
void UpdateQuote(Quote q); void UpdateQuote(Quote q);
} }
public void AddQuoteReceiver(QuoteReceiver qr) { /**
*
*/
public interface BookReceiver {
void UpdateOrderBook();
}
// Here we store the list of quote receivers
private final TreeSet<QuoteReceiver> qrlist;
/**
*
* @param qr
*/
public void addQuoteReceiver(QuoteReceiver qr) {
qrlist.add(qr); qrlist.add(qr);
} }
@ -68,11 +74,11 @@ public class Exchange extends Thread {
} }
// long time = 0; // long time = 0;
double price = 12.9; double theprice = 12.9;
long orderid = 1; long orderid = 1;
double lastprice = 300.0; double lastprice = 300.0;
long lastsize; long lastsvolume;
public TreeSet<Order> bid; public TreeSet<Order> bid;
public TreeSet<Order> ask; public TreeSet<Order> ask;
@ -92,33 +98,40 @@ public class Exchange extends Thread {
available.release(); available.release();
} }
public ArrayList geAskBook(int n){ private ArrayList<Order> getBook(TreeSet<Order> book, int depth) {
ArrayList<Order> ret = new ArrayList<>(); ArrayList<Order> ret = new ArrayList<>();
Iterator it=ask.iterator(); Iterator<Order> it = book.iterator();
for(int i=0;i<n && it.hasNext();i++){ for (int i = 0; i < depth && it.hasNext(); i++) {
SellOrder o; Order o;
o = (SellOrder)it.next(); o = it.next();
ret.add(o); ret.add(o);
System.out.print("Order" + o.limit); System.out.print("Order" + o.limit);
System.out.println(); System.out.println();
} }
return ret; return ret;
} }
public ArrayList geBidBook(int n){ /**
ArrayList <Order> ret = new ArrayList<>(); * Get the "ask" orderbook
Iterator it=bid.iterator(); *
for(int i=0;i<n && it.hasNext();i++){ * @param depth Number oder Orders to retrieve from orderbook
BuyOrder o; * @return Orderbook
o = (BuyOrder)it.next(); */
ret.add(o); public ArrayList<Order> getAskBook(int depth) {
System.out.print("Order"+o.limit); return getBook(ask, depth);
System.out.println();
}
return ret;
} }
/**
* Get the "bid" oderbook
*
* @param depth Number oder Orders to retrieve from orderbook
* @return Orderbook
*/
public ArrayList<Order> getBidBook(int depth) {
return getBook(bid, depth);
}
public void print_current() { public void print_current() {
@ -143,10 +156,9 @@ public class Exchange extends Thread {
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",
b.format_limit(), b.format_volume(), b.format_limit(), b.format_volume(),
lastprice, lastsize, lastprice, lastsvolume,
a.format_limit(), a.format_volume()) a.format_limit(), a.format_volume())
); );
@ -168,9 +180,24 @@ public class Exchange extends Thread {
} }
/**
* Transfer shares from one account to another account
* @param src source account
* @param dst destination account
* @param volumen number of shares
* @param price price
*/
protected void transferShares(Account src, Account dst, long volume, double price){
dst.shares += volume;
src.shares -= volume;
dst.money -= price * volume;
src.money += price * volume;
}
public void OrderMatching() { public void OrderMatching() {
while (true) { while (true) {
if (bid.isEmpty() || ask.isEmpty()) { if (bid.isEmpty() || ask.isEmpty()) {
// nothing to do // nothing to do
return; return;
@ -188,7 +215,7 @@ public class Exchange extends Thread {
} }
if (b.volume == 0) { if (b.volume == 0) {
// // This order is fully executed, remove
b.account.orderpending = false; b.account.orderpending = false;
b.status = OrderStatus.executed; b.status = OrderStatus.executed;
bid.pollFirst(); bid.pollFirst();
@ -209,24 +236,28 @@ public class Exchange extends Thread {
price = a.limit; price = a.limit;
} }
long size = 0; long volume;
if (b.volume >= a.volume) { if (b.volume >= a.volume) {
size = a.volume; volume = a.volume;
} else { } else {
size = b.volume; volume = b.volume;
} }
b.account.Buy(a.account, size, price); transferShares(a.account,b.account,volume,price);
b.volume -= size;
a.volume -= size; // b.account.Buy(a.account, volume, price);
b.volume -= volume;
a.volume -= volume;
lastprice = price; lastprice = price;
lastsize = size; lastsvolume = volume;
Quote q = new Quote(); Quote q = new Quote();
q.size = size; q.size = volume;
q.price = price; q.price = price;
q.time = System.currentTimeMillis(); q.time = System.currentTimeMillis();
@ -251,54 +282,31 @@ public class Exchange extends Thread {
return true; return true;
} }
/* private boolean addOrder(Order o) {
public void SendOrder(SellOrder o) {
// System.out.println("EX Sellorder");
Lock();
boolean rc = InitOrder(o);
o.timestamp = System.currentTimeMillis();
o.id = orderid++;
ask.add(o);
Unlock();
Lock();
// OrderMatching();
Unlock();
}
*/
private void addOrder(Order o){
switch (o.type) { switch (o.type) {
case buy: case buy:
bid.add(o); return bid.add(o);
break;
case sell: case sell:
ask.add(o); return ask.add(o);
break;
default:
return;
} }
return false;
} }
public void SendOrder(Order o) { public void SendOrder(Order o) {
Lock(); Lock();
o.timestamp = System.currentTimeMillis(); o.timestamp = System.currentTimeMillis();
boolean rc = InitOrder(o); boolean rc = InitOrder(o);
System.out.print(o.timestamp + " TS:\n"); System.out.print(o.timestamp + " TS:\n");
o.id = orderid++; o.id = orderid++;
addOrder(o); addOrder(o);
OrderMatching();
Unlock(); Unlock();
}
}
/* /*
public void SendOrder(BuyOrder o) { public void SendOrder(BuyOrder o) {
//System.out.println("EX Buyorder"); //System.out.println("EX Buyorder");
@ -333,7 +341,7 @@ public class Exchange extends Thread {
* SendOrder(bo); * SendOrder(bo);
*/ */
return price; return theprice;
} }
public double sendOrder(Account o) { public double sendOrder(Account o) {

View File

@ -7,13 +7,17 @@ public class MTrader extends Trader {
Exchange ex; Exchange ex;
Random rand; Random rand;
public MTrader(Exchange ex1, long shares, double money) { public MTrader(Account account) {
super(account);
}
/* public MTrader(Exchange ex1, long shares, double money) {
account.money = money; account.money = money;
account.shares = shares; account.shares = shares;
this.ex = ex; this.ex = ex;
rand = new Random(); rand = new Random();
} }
*/
public void DoBuy() { public void DoBuy() {
// System.out.println("AAA"); // System.out.println("AAA");
@ -33,7 +37,7 @@ public class MTrader extends Trader {
long size = (int) (account.money / limit); long size = (int) (account.money / limit);
account.Buy(size, limit, ex); account.Buy(size, limit, ex);
return;
} }
public void DoSell() { public void DoSell() {
@ -53,7 +57,7 @@ public class MTrader extends Trader {
long size = (int) (account.shares); long size = (int) (account.shares);
account.Sell(size, limit, ex); account.sell(size, limit);
return; return;
} }
@ -77,7 +81,7 @@ public class MTrader extends Trader {
return; return;
} }
//System.out.printf("MyPrice: %.2f\n",price); //System.out.printf("MyPrice: %.2f\n",theprice);
} }
} }

View File

@ -13,12 +13,16 @@ public class RandomTrader extends Trader {
// my current order // my current order
private Order myorder = null; private Order myorder = null;
public RandomTrader(Exchange ex, long shares, double money) { public RandomTrader(Account account) {
super(account);
}
/* public RandomTrader(Exchange ex, long shares, double money) {
account.money = money; account.money = money;
account.shares = shares; account.shares = shares;
this.ex = ex; this.ex = ex;
} }
*/
public void DoBuy() { public void DoBuy() {
if (myorder != null) { if (myorder != null) {
@ -54,7 +58,7 @@ public class RandomTrader extends Trader {
long size = (int) (account.shares); long size = (int) (account.shares);
myorder = account.Sell(size, limit, ex); myorder = account.sell(size, limit);
} }
public void trade() { public void trade() {

View File

@ -1,18 +1,6 @@
package SeSim; package SeSim;
public class SellOrder extends Order { public class SellOrder extends Order {
/*
@Override
public int compareTo(Order o) {
return super.compareTo(o);
}
public SellOrder(){
type=OrderType.buy;
}
*/
public SellOrder(){ public SellOrder(){
type=OrderType.sell; type=OrderType.sell;

View File

@ -30,13 +30,14 @@ public abstract class Trader {
public String name = null; public String name = null;
public abstract void trade(); public abstract void trade();
public Account account; // = new Account(); public Account account;
/** /**
* Construct a Trader object * Construct a Trader object
*/ */
public Trader(){ public Trader(Account account){
this.account=account;
} }
}; };

View File

@ -25,6 +25,7 @@
*/ */
package Traders; package Traders;
import SeSim.Account;
import SeSim.Trader; import SeSim.Trader;
import SeSim.BuyOrder; import SeSim.BuyOrder;
@ -35,10 +36,12 @@ import SeSim.BuyOrder;
*/ */
public class ManTrader extends Trader{ public class ManTrader extends Trader{
public void ManTrader(){ public ManTrader(Account account) {
this.name = "ManTrader"; super(account);
} }
@Override @Override
public void trade(){ public void trade(){