Code cleaning

This commit is contained in:
7u83 2017-01-15 09:28:26 +01:00
parent 7bf55870b1
commit cee076162c
8 changed files with 90 additions and 268 deletions

View File

@ -56,6 +56,9 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
void drawXLegend(Graphics2D g) {
g = (Graphics2D)g.create();
int xl_height = 30;
Dimension dim = this.getSize();
@ -173,7 +176,7 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
// }
ArrayList<OHLCDataItem> od = data.data;
System.out.print("OD S: " + od.size() + "\n");
// System.out.print("OD S: " + od.size() + "\n");
g.setColor(Color.BLUE);
g.setStroke(new BasicStroke(3));

View File

@ -205,7 +205,8 @@ public class MainWin extends javax.swing.JFrame {
// SwitchingTraderConfig rcfg1 = new SwitchingTraderConfig();
RandomTraderConfig rcfg1 = new RandomTraderConfig();
AutoTrader rt1 = rcfg1.createTrader(se, 1000000, 0);
AutoTrader rt1 = rcfg1.createTrader(se, 0, 1000000);
rt1.setName("Bob");
rt1.start();
//AutoTrader rt2 = rcfg1.createTrader(se, 1, 100);
@ -213,8 +214,24 @@ public class MainWin extends javax.swing.JFrame {
RandomTraderConfig cfg = new RandomTraderConfig();
for (int i=0; i<10000; i++){
AutoTrader randt = cfg.createTrader(se, 100, 100);
/* cfg.sell_limit[0]=0.5f;
cfg.sell_limit[1]=1.0f;
cfg.buy_limit[0]=-0.5f;
cfg.buy_limit[1]=-1.0f;
*/
// cfg.buy_order_wait[0]=20;
// cfg.buy_order_wait[1]=30;
// cfg.sell_order_wait[0]=20;
// cfg.sell_order_wait[1]=30;
for (int i=0; i<1000; i++){
AutoTrader randt = cfg.createTrader(se, 1000000, 0);
randt.setName("Alice");
randt.start();
}

View File

@ -67,6 +67,7 @@ final public class Account_old {
return this.money<=se.lastprice && this.shares<=0;
}
/*
public Order_old sell(long volume, double limit) {
SellOrder o = new SellOrder();
o.account = this;
@ -75,8 +76,8 @@ final public class Account_old {
orderpending = true;
return se.SendOrder(o);
}
public Order_old buy(long volume, double limit) {
*/
/* public Order_old buy(long volume, double limit) {
if (volume * limit > money) {
return null;
}
@ -87,7 +88,7 @@ final public class Account_old {
orderpending = true;
return se.SendOrder(o);
}
/*
/*
public void Buy(Account_old a, long size, double price) {
shares += size;

View File

@ -1,8 +1,10 @@
package sesim;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import sesim.Order_old.OrderStatus;
import sesim.Order_old.OrderType_old;
/**
@ -56,10 +58,11 @@ public class Exchange { //extends Thread {
}
//private TreeSet<Account> accounts = new TreeSet<>();
HashMap<Double, Account> accounts = new HashMap<>();
// private final ConcurrentHashMap<Double, Account> accounts = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Double, Account> accounts = new ConcurrentHashMap<>();
public double createAccount(double money, double shares) {
Account a = new Account(money, shares);
accounts.put(a.id, a);
return a.id;
@ -105,9 +108,8 @@ public class Exchange { //extends Thread {
}
//TreeSet <Order> bid_bbook = new TreeSet <> (new OrderComperator(OrderType.BID) );
//TreeSet <Order> ask_dbook = new TreeSet <> (new OrderComperator(OrderType.BID) );
HashMap<OrderType, TreeSet<Order>> order_books = new HashMap();
HashMap<OrderType, SortedSet<Order>> order_books = new HashMap();
IDGenerator order_id = new IDGenerator();
@ -168,10 +170,12 @@ public class Exchange { //extends Thread {
public Exchange() {
this.ask = new TreeSet<>();
this.bid = new TreeSet<>();
this.qrlist = new ArrayList<>();
// this.qrlist = new ArrayList<>();
this.qrlist = (new CopyOnWriteArrayList<>());
// Create order books
for (OrderType type : OrderType.values()) {
//SortedSet b = new TreeSet(new OrderComparator(type));
order_books.put(type, new TreeSet(new OrderComparator(type)));
}
@ -211,8 +215,8 @@ public class Exchange { //extends Thread {
public Quote getCurrentPrice() {
TreeSet<Order> bid = order_books.get(OrderType.BID);
TreeSet<Order> ask = order_books.get(OrderType.ASK);
SortedSet<Order> bid = order_books.get(OrderType.BID);
SortedSet<Order> ask = order_books.get(OrderType.ASK);
Quote q = null;
@ -239,11 +243,7 @@ public class Exchange { //extends Thread {
}
/* public SortedSet<Quote> getQuoteHistory(int seconds) {
Quote last = quoteHistory.last();
return this.getQuoteHistory(seconds, last.time);
}
*/
// Class to describe an executed order
// QuoteReceiver has to be implemented by objects that wants
// to receive quote updates
@ -287,17 +287,11 @@ public class Exchange { //extends Thread {
while (i.hasNext()) {
i.next().UpdateOrderBook();
}
/* try {
sleep(10);
} catch (InterruptedException e) {
System.out.println("I was Interrupted");
}
*/
}
// Here we store the list of quote receivers
private final ArrayList<QuoteReceiver> qrlist;
private final List<QuoteReceiver> qrlist;
/**
*
@ -327,22 +321,7 @@ public class Exchange { //extends Thread {
private final Locker tradelock = new Locker();
/*
private final Semaphore available = new Semaphore(1, true);
private void Lock() {
try {
available.acquire();
} catch (InterruptedException s) {
System.out.println("Interrupted\n");
}
}
private void Unlock() {
available.release();
}
*/
private TreeSet<Order_old> selectOrderBook(OrderType_old t) {
switch (t) {
@ -357,11 +336,11 @@ public class Exchange { //extends Thread {
public ArrayList<Order> getOrderBook(OrderType type, int depth) {
TreeSet<Order> book = order_books.get(type);
SortedSet<Order> book = order_books.get(type);
if (book == null) {
return null;
}
tradelock.lock();
ArrayList<Order> ret = new ArrayList<>();
Iterator<Order> it = book.iterator();
@ -369,6 +348,7 @@ public class Exchange { //extends Thread {
for (int i = 0; i < depth && it.hasNext(); i++) {
ret.add(it.next());
}
tradelock.unlock();
return ret;
}
@ -433,7 +413,7 @@ public class Exchange { //extends Thread {
// System.out.print("The Order:"+o.limit+"\n");
if (o != null) {
TreeSet ob = order_books.get(o.type);
SortedSet ob = order_books.get(o.type);
boolean rc = ob.remove(o);
@ -485,8 +465,11 @@ public class Exchange { //extends Thread {
return;
}
o.account.orders.remove(o.id);
order_books.get(o.type).pollFirst();
SortedSet book = order_books.get(o.type);
book.remove(book.first());
//pollFirst();
}
/**
@ -494,8 +477,8 @@ public class Exchange { //extends Thread {
*/
public void executeOrders() {
TreeSet<Order> bid = order_books.get(OrderType.BID);
TreeSet<Order> ask = order_books.get(OrderType.ASK);
SortedSet<Order> bid = order_books.get(OrderType.BID);
SortedSet<Order> ask = order_books.get(OrderType.ASK);
double volume_total = 0;
double money_total = 0;
@ -537,155 +520,12 @@ public class Exchange { //extends Thread {
q.time = System.currentTimeMillis();
// System.out.print("There was a trade:"+q.price+"\n");
this.quoteHistory.add(q);
this.updateQuoteReceivers(q);
}
private void executeOrders_old() {
while (!bid.isEmpty() && !ask.isEmpty()) {
Order_old b = bid.first();
Order_old a = ask.first();
if (b.limit < a.limit) {
// no match, nothing to do
return;
}
if (a.volume == 0) {
// This order is fully executed, remove
a.account.orderpending = false;
a.status = OrderStatus.executed;
a.account.pending.remove(a);
ask.pollFirst();
// this.updateBookReceivers(OrderType_old.ask);
continue;
}
if (b.volume == 0) {
// This order is fully executed, remove
b.account.orderpending = false;
b.status = OrderStatus.executed;
b.account.pending.remove(b);
bid.pollFirst();
// this.updateBookReceivers(OrderType_old.bid);
continue;
}
if (b.limit >= a.limit) {
double price;
price = b.id < a.id ? b.limit : a.limit;
/* if (b.id < a.id) {
price = b.limit;
} else {
price = a.limit;
}
*/
long volume;
if (b.volume >= a.volume) {
volume = a.volume;
} else {
volume = b.volume;
}
transferShares(a.account, b.account, volume, price);
// b.account.Buy(a.account, volume, price);
b.volume -= volume;
a.volume -= volume;
lastprice = price;
lastsvolume = volume;
Quote q = new Quote();
q.volume = volume;
q.price = price;
q.time = System.currentTimeMillis();
q.ask = a.limit;
q.bid = b.limit;
q.id = nextQuoteId++;
this.updateQuoteReceivers(q);
// this.updateBookReceivers(OrderType_old.bid);
// this.updateBookReceivers(OrderType_old.ask);
/* System.out.print(
"Executed: "
+ q.price
+ " / "
+ q.volume
+ "\n"
);
*/
quoteHistory.add(q);
continue;
}
return;
}
}
public void ExecuteOrder(BuyOrder o) {
// SellOrder op = ASK.peek();
}
private boolean InitOrder(Order_old o) {
double moneyNeeded = o.volume * o.limit;
return true;
}
// Add an order to the orderbook
private boolean addOrder(Order_old o) {
boolean ret = false;
switch (o.type) {
case bid:
// System.out.print("Exchange adding BID order \n");
ret = bid.add(o);
break;
case ask:
// System.out.print("Exchange adding ASK order \n");
ret = ask.add(o);
break;
}
if (ret) {
// this.updateBookReceivers(o.type);
}
return ret;
}
public Order_old SendOrder(Order_old o) {
boolean rc = InitOrder(o);
if (!rc) {
return null;
}
tradelock.lock();
o.timestamp = System.currentTimeMillis();
o.id = orderid++;
addOrder(o);
o.account.pending.add(o);
executeOrders_old();
tradelock.unlock();
return o;
}
private void addOrderToBook(Order o) {
order_books.get(o.type).add(o);
@ -740,7 +580,9 @@ public class Exchange { //extends Thread {
}
public AccountData getAccountData(double account_id) {
tradelock.lock();
Account a = accounts.get(account_id);
tradelock.unlock();
if (a == null) {
return null;
}
@ -798,63 +640,4 @@ public class Exchange { //extends Thread {
return al;
}
/*
public void SendOrder(BuyOrder o) {
//System.out.println("EX Buyorder");
Lock();
o.timestamp = System.currentTimeMillis();
o.id = orderid++;
BID.add(o);
Unlock();
Lock();
// executeOrders_old();
Unlock();
}
*/
/*
* public void SendOrder(Order_old o){
*
*
* if ( o.getClass() == BuyOrder.class){ BID.add((BuyOrder)o); }
*
* if ( o.getClass() == SellOrder.class){ ASK.add((SellOrder)o); }
*
* }
*/
public double getlastprice() {
/*
* SellOrder so = new SellOrder(); so.limit=1000.0; so.volume=500;
* SendOrder(so);
*
* BuyOrder bo = new BuyOrder(); bo.limit=1001.0; bo.volume=300;
* SendOrder(bo);
*/
return lastprice;
}
/* public double sendOrder(Account_old o) {
return 0.7;
}
*/
/**
*
*/
// @Override
public void run() {
/* while (true) {
try {
sleep(1500);
} catch (InterruptedException e) {
System.out.println("I was Interrupted");
}
print_current();
}
*/
}
}

View File

@ -33,14 +33,14 @@ public abstract class Trader_old {
public Account_old account;
public TraderConfig_old config;
public void sell(long shares, double limit){
/* public void sell(long shares, double limit){
account.sell(shares, limit);
}
public void buy(long shares, double limit){
account.buy(shares, limit);
}
*/
/**
* Construct a Trader object
* @param account Account_old for this trader

View File

@ -109,13 +109,7 @@ public class RandomTrader extends AutoTrader {
// object to generate random numbers
final private Random rand = new Random();
/*public RandomTrader(Exchange se, double money,shares,) {
//super(account, config);
if (config == null) {
config = new RandomTraderConfig_old();
}
myconfig = (RandomTraderConfig_old) config;
}*/
/**
* Get a (long) random number between min an max
*
@ -170,6 +164,10 @@ public class RandomTrader extends AutoTrader {
OrderType type=OrderType.BID;
if (ad==null || myconfig==null) {
System.out.print(ad+"\n");
}
// how much money we ant to envest?
double money = getRandomAmmount(ad.money, myconfig.buy_volume);
@ -192,7 +190,7 @@ public class RandomTrader extends AutoTrader {
se.createOrder(account_id, type, volume, limit);
return getRandom(myconfig.buy_order_wait)*1000;
return getRandom(myconfig.buy_order_wait);
}
@ -204,6 +202,8 @@ public class RandomTrader extends AutoTrader {
OrderType type=OrderType.ASK;
// how much money we ant to envest?
double volume = (long)getRandomAmmount(ad.shares, myconfig.sell_volume);
@ -228,7 +228,7 @@ public class RandomTrader extends AutoTrader {
se.createOrder(account_id, type, volume, limit);
return getRandom(myconfig.sell_order_wait)*1000;
return getRandom(myconfig.sell_order_wait);
}

View File

@ -36,13 +36,13 @@ import sesim.Exchange;
public class RandomTraderConfig extends AutoTraderConfig {
public float[] sell_volume = {100, 100};
public float[] sell_limit = {-1.0f, 1.0f};
public int[] sell_order_wait = {1, 5};
public float[] sell_limit = {-10f, 10f};
public int[] sell_order_wait = {1000, 5000};
public int[] wait_after_sell = {10, 30};
public float[] buy_volume = {100, 100};
public float[] buy_limit = {-1.0f, 1f};
public int[] buy_order_wait = {1, 5};
public float[] buy_limit = {-10, 10f};
public int[] buy_order_wait = {1000, 5000};
public int[] wait_after_buy = {10, 30};
@Override

View File

@ -25,11 +25,27 @@
*/
package sesim;
/**
*
* @author tobias
*/
public class Test {
static void tube(){
try{
System.out.printf("Hello %s\n", "args");
if (0==0)
return;
}
finally {
System.out.printf("Always %s\n", "the end");
}
System.out.print("haha\n");
}
static void print_account(AccountData ad) {
System.out.print(
@ -55,6 +71,8 @@ public class Test {
sesim.Exchange.Account a = se.getAccount(aid1);
System.out.print(a.getMoney());
tube();
System.exit(0);