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
ArrayList getArrayList() {
return MainWin.se.geAskBook(10);
return MainWin.se.getAskBook(10);
}
@Override

View File

@ -35,7 +35,7 @@ public class BidBook extends OrderBook{
@Override
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
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
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.*;
import SeSim.*;
import Traders.*;
/**
@ -40,6 +41,7 @@ public class MainWin extends javax.swing.JFrame {
static SeSim.Exchange se;
static SeSim.Account myAccount;
static Traders.ManTrader myTrader;
/**
* Creates new form MainWin
@ -156,7 +158,10 @@ public class MainWin extends javax.swing.JFrame {
public static void main(String args[]) {
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();
so.limit = 20.0;
@ -164,29 +169,6 @@ public class MainWin extends javax.swing.JFrame {
so.timestamp = 12;
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 {
// Set cross-platform Java L&F (also called "Metal")
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

View File

@ -42,35 +42,6 @@ public class OrderBookPanel extends javax.swing.JPanel {
public OrderBookPanel() {
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();

View File

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

View File

@ -2,19 +2,6 @@ package SeSim;
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(){
type=OrderType.buy;
}

View File

@ -3,7 +3,6 @@ package SeSim;
import java.util.*;
import java.util.concurrent.*;
import SeSim.Order.OrderStatus;
/**
@ -12,14 +11,6 @@ import SeSim.Order.OrderStatus;
*/
public class Exchange extends Thread {
// Here we store the list of quote receivers
private final TreeSet<QuoteReceiver> qrlist;
/**
* Histrory of quotes
*/
@ -55,7 +46,22 @@ public class Exchange extends Thread {
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);
}
@ -68,12 +74,12 @@ public class Exchange extends Thread {
}
// long time = 0;
double price = 12.9;
double theprice = 12.9;
long orderid = 1;
double lastprice = 300.0;
long lastsize;
long lastsvolume;
public TreeSet<Order> bid;
public TreeSet<Order> ask;
@ -91,34 +97,41 @@ public class Exchange extends Thread {
private void Unlock() {
available.release();
}
public ArrayList geAskBook(int n){
ArrayList <Order> ret= new ArrayList<>();
Iterator it=ask.iterator();
for(int i=0;i<n && it.hasNext();i++){
SellOrder o;
o = (SellOrder)it.next();
private ArrayList<Order> getBook(TreeSet<Order> book, int depth) {
ArrayList<Order> ret = new ArrayList<>();
Iterator<Order> it = book.iterator();
for (int i = 0; i < depth && it.hasNext(); i++) {
Order o;
o = it.next();
ret.add(o);
System.out.print("Order"+o.limit);
System.out.print("Order" + o.limit);
System.out.println();
}
return ret;
}
public ArrayList geBidBook(int n){
ArrayList <Order> ret = new ArrayList<>();
Iterator it=bid.iterator();
for(int i=0;i<n && it.hasNext();i++){
BuyOrder o;
o = (BuyOrder)it.next();
ret.add(o);
System.out.print("Order"+o.limit);
System.out.println();
}
return ret;
/**
* Get the "ask" orderbook
*
* @param depth Number oder Orders to retrieve from orderbook
* @return Orderbook
*/
public ArrayList<Order> getAskBook(int depth) {
return getBook(ask, depth);
}
/**
* 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() {
@ -143,11 +156,10 @@ public class Exchange extends Thread {
a = ask.first();
}
Logger.info(
String.format("BID: %s(%s) LAST: %.2f(%d) ASK: %s(%s)\n",
b.format_limit(), b.format_volume(),
lastprice, lastsize,
a.format_limit(), a.format_volume())
Logger.info(String.format("BID: %s(%s) LAST: %.2f(%d) ASK: %s(%s)\n",
b.format_limit(), b.format_volume(),
lastprice, lastsvolume,
a.format_limit(), a.format_volume())
);
}
@ -155,7 +167,7 @@ public class Exchange extends Thread {
public void TransferMoney(Account src, Account dst, double money) {
src.money -= money;
dst.money += money;
}
public void CancelOrder(Order o) {
@ -167,10 +179,25 @@ public class Exchange extends Thread {
Unlock();
}
/**
* 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() {
while (true) {
if (bid.isEmpty() || ask.isEmpty()) {
// nothing to do
return;
@ -188,7 +215,7 @@ public class Exchange extends Thread {
}
if (b.volume == 0) {
//
// This order is fully executed, remove
b.account.orderpending = false;
b.status = OrderStatus.executed;
bid.pollFirst();
@ -209,24 +236,28 @@ public class Exchange extends Thread {
price = a.limit;
}
long size = 0;
long volume;
if (b.volume >= a.volume) {
size = a.volume;
volume = a.volume;
} else {
size = b.volume;
volume = b.volume;
}
b.account.Buy(a.account, size, price);
b.volume -= size;
a.volume -= size;
transferShares(a.account,b.account,volume,price);
// b.account.Buy(a.account, volume, price);
b.volume -= volume;
a.volume -= volume;
lastprice = price;
lastsize = size;
lastsvolume = volume;
Quote q = new Quote();
q.size = size;
q.size = volume;
q.price = price;
q.time = System.currentTimeMillis();
@ -251,55 +282,32 @@ public class Exchange extends Thread {
return true;
}
/*
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){
private boolean addOrder(Order o) {
switch (o.type) {
case buy:
bid.add(o);
break;
return bid.add(o);
case sell:
ask.add(o);
break;
default:
return;
return ask.add(o);
}
return false;
}
public void SendOrder(Order o){
public void SendOrder(Order o) {
Lock();
o.timestamp = System.currentTimeMillis();
boolean rc = InitOrder(o);
System.out.print(o.timestamp+" TS:\n");
o.id = orderid++;
System.out.print(o.timestamp + " TS:\n");
o.id = orderid++;
addOrder(o);
Unlock();
OrderMatching();
Unlock();
}
/*
/*
public void SendOrder(BuyOrder o) {
//System.out.println("EX Buyorder");
Lock();
@ -313,8 +321,8 @@ public class Exchange extends Thread {
Unlock();
}
*/
/*
*/
/*
* public void SendOrder(Order o){
*
*
@ -333,7 +341,7 @@ public class Exchange extends Thread {
* SendOrder(bo);
*/
return price;
return theprice;
}
public double sendOrder(Account o) {

View File

@ -7,13 +7,17 @@ public class MTrader extends Trader {
Exchange ex;
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.shares = shares;
this.ex = ex;
rand = new Random();
}
*/
public void DoBuy() {
// System.out.println("AAA");
@ -33,7 +37,7 @@ public class MTrader extends Trader {
long size = (int) (account.money / limit);
account.Buy(size, limit, ex);
return;
}
public void DoSell() {
@ -53,7 +57,7 @@ public class MTrader extends Trader {
long size = (int) (account.shares);
account.Sell(size, limit, ex);
account.sell(size, limit);
return;
}
@ -77,7 +81,7 @@ public class MTrader extends Trader {
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
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.shares = shares;
this.ex = ex;
}
*/
public void DoBuy() {
if (myorder != null) {
@ -54,7 +58,7 @@ public class RandomTrader extends Trader {
long size = (int) (account.shares);
myorder = account.Sell(size, limit, ex);
myorder = account.sell(size, limit);
}
public void trade() {

View File

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

View File

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

View File

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