diff --git a/src/main/java/gui/MainWin.java b/src/main/java/gui/MainWin.java index cdbe21e..0be9a22 100644 --- a/src/main/java/gui/MainWin.java +++ b/src/main/java/gui/MainWin.java @@ -194,6 +194,7 @@ public class MainWin extends javax.swing.JFrame { public static void main(String args[]) { se = new Exchange(); + se.timer.start(); //RandomTraderConfig rcfg = new RandomTraderConfig(); @@ -214,7 +215,7 @@ public class MainWin extends javax.swing.JFrame { // SwitchingTraderConfig cfg = new SwitchingTraderConfig(); RandomTraderConfig cfg= new RandomTraderConfig(); - for (int i=0; i<10; i++){ + for (int i=0; i<1; i++){ AutoTrader randt = cfg.createTrader(se, 100000, 0); randt.setName("Alice"); diff --git a/src/main/java/sesim/AutoTrader.java b/src/main/java/sesim/AutoTrader.java index 80a57ed..a6200f1 100644 --- a/src/main/java/sesim/AutoTrader.java +++ b/src/main/java/sesim/AutoTrader.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, tobias + * Copyright (c) 2017, 7u83 * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,36 +27,32 @@ package sesim; /** * - * @author tobias + * @author 7u83 */ -public abstract class AutoTrader { - +public abstract class AutoTrader implements Scheduler.TimerTask { + protected double account_id; protected Exchange se; protected AutoTraderConfig config; - + protected String name; - - - - - public AutoTrader(Exchange se, double money, double shares, AutoTraderConfig config){ + + public AutoTrader(Exchange se, double money, double shares, AutoTraderConfig config) { account_id = se.createAccount(money, shares); - this.se=se; - this.config=config; - this.name=""; - + this.se = se; + this.config = config; + this.name = ""; + } - - public void setName(String name){ - this.name=name; + + public void setName(String name) { + this.name = name; } - - public String getName(){ + + public String getName() { return name; } - + public abstract void start(); - - + } diff --git a/src/main/java/sesim/Exchange.java b/src/main/java/sesim/Exchange.java index c3e7c2b..f73ecca 100644 --- a/src/main/java/sesim/Exchange.java +++ b/src/main/java/sesim/Exchange.java @@ -16,7 +16,9 @@ public class Exchange { //extends Thread { } IDGenerator account_id = new IDGenerator(); - public static Timer timer = new Timer(); + //public static Timer timer = new Timer(); + + public Scheduler timer = new Scheduler(); /** * @@ -174,8 +176,21 @@ public class Exchange { //extends Thread { //SortedSet b = new TreeSet(new OrderComparator(type)); order_books.put(type, new TreeSet(new OrderComparator(type))); } + } + + + /*public interface TimerEvent { + + long timerEvent(); + } + */ + + void start(){ + timer.start(); + } + class BidBook extends TreeSet { diff --git a/src/main/java/sesim/Scheduler.java b/src/main/java/sesim/Scheduler.java index 2994b43..7cbfe1d 100644 --- a/src/main/java/sesim/Scheduler.java +++ b/src/main/java/sesim/Scheduler.java @@ -40,21 +40,27 @@ public class Scheduler extends Thread { long multiply = 1; - private final SortedMap> event_queue = new TreeMap<>(); - private boolean stop = false; + private final SortedMap> event_queue = new TreeMap<>(); + private boolean halt = false; + public interface TimerTask { + + long timerTask(); + } + + /**\ + * + */ public void halt() { - stop = true; + halt = true; synchronized (event_queue) { event_queue.notifyAll(); } } - public interface TimerEvent { - - long timerEvent(); - } - + /** + * + */ private class ObjectComparator implements Comparator { @Override @@ -63,41 +69,40 @@ public class Scheduler extends Thread { } } - public long getCurrentTimeMillies() { + public long currentTimeMillis() { return System.currentTimeMillis(); } - public void startEvent(TimerEvent e, long time) { - long evtime = time + this.getCurrentTimeMillies(); + /** + * + * @param e + * @param time + */ + public void startTimerEvent(TimerTask e, long time) { + long evtime = time + this.currentTimeMillis(); synchronized (event_queue) { - SortedSet s = event_queue.get(evtime); - if (s == null) { - s = new TreeSet<>(new ObjectComparator()); - event_queue.put(evtime, s); - } - s.add(e); + this.addEvent(e, time); } synchronized (this) { notify(); } } - public long fireEvent(TimerEvent e) { - return e.timerEvent(); + public long fireEvent(TimerTask e) { + return e.timerTask(); } - private void addEvent(TimerEvent e, long time) { + private boolean addEvent(TimerTask e, long time) { - long evtime = time + this.getCurrentTimeMillies(); + long evtime = time + this.currentTimeMillis(); - SortedSet s = event_queue.get(evtime); + SortedSet s = event_queue.get(evtime); if (s == null) { s = new TreeSet<>(new ObjectComparator()); event_queue.put(evtime, s); } - s.add(e); - + return s.add(e); } public long runEvents() { @@ -106,23 +111,20 @@ public class Scheduler extends Thread { return -1; } - long t = event_queue.firstKey(); - if (t <= this.getCurrentTimeMillies()) { + if (t <= this.currentTimeMillis()) { SortedSet s = event_queue.get(t); - event_queue.remove(t); - Iterator it = s.iterator(); + Iterator it = s.iterator(); while (it.hasNext()) { - - TimerEvent e = it.next(); + TimerTask e = it.next(); long next_t = this.fireEvent(e); this.addEvent(e, next_t); } return 0; } else { - return t - this.getCurrentTimeMillies(); + return t - this.currentTimeMillis(); } } @@ -130,7 +132,7 @@ public class Scheduler extends Thread { @Override public void run() { - while (!stop) { + while (!halt) { long wtime = runEvents(); if (wtime == 0) { continue; @@ -144,6 +146,7 @@ public class Scheduler extends Thread { wait(); } } catch (Exception e) { + } } } diff --git a/src/main/java/traders/RandomTrader.java b/src/main/java/traders/RandomTrader.java index 5a66d86..8595734 100644 --- a/src/main/java/traders/RandomTrader.java +++ b/src/main/java/traders/RandomTrader.java @@ -27,7 +27,7 @@ package traders; import java.util.*; import java.util.Random; -import java.util.TimerTask; +//import java.util.TimerTask; /*import sesim.AccountData;*/ import sesim.AutoTrader; @@ -42,33 +42,14 @@ import sesim.*; */ public class RandomTrader extends AutoTrader { - //static Timer timer = new Timer(); -/* enum Event { - CANCEL, - CREATE - } - */ - public long megaLoop(){ - long rc=0; - for (int i=0; i<1; i++){ - rc+=i; - // System.out.print(rc+"\n"); - } - return rc; - } - - long event(){ - if ("Alice".equals(this.name)){ - megaLoop(); - } + long event() { + sesim.Exchange.Account a = se.getAccount(account_id); - long rc = this.doTrade(); - // System.out.print(String.format("%s: %.0f/%.2f\n", this.getName(),a.getShares(),a.getMoney())); - return rc; - + long rc = this.doTrade(); + return rc/3; + } - public RandomTrader(Exchange se, double money, double shares, RandomTraderConfig config) { super(se, money, shares, config); @@ -77,29 +58,37 @@ public class RandomTrader extends AutoTrader { } } - - protected enum Action { - BUY,SELL,RANDOM + + @Override + public long timerTask() { + sesim.Exchange.Account a = se.getAccount(account_id); + long rc = this.doTrade(); + return rc/33; + +// return this.event(); } - - protected Action getAction() { - if (rand.nextInt(2)==0){ + + protected enum Action { + BUY, SELL, RANDOM + } + + protected Action getAction() { + if (rand.nextInt(2) == 0) { return Action.BUY; - } - else{ + } else { return Action.SELL; } - - + } - + double start = 0.1; - Timer timer=new Timer(); + //Timer timer = new Timer(); @Override public void start() { - timer.schedule(new TimerTaskImpl(this,timer), 0); + // timer.schedule(new TimerTaskImpl(this, timer), 0); + se.timer.startTimerEvent(this, 0); // timer.schedule(new TimerTaskImpl, date); } @@ -109,7 +98,6 @@ public class RandomTrader extends AutoTrader { // object to generate random numbers final private Random rand = new Random(); - /** * Get a (long) random number between min an max * @@ -137,46 +125,43 @@ public class RandomTrader extends AutoTrader { double max = val * minmax[1] / 100.0; return getRandom(min, max); } - - - public long cancelOrders(){ + + public long cancelOrders() { int n = se.getNumberOfOpenOrders(account_id); // System.out.print("Open Orders: "+n+"\n"); - if (n>0){ + if (n > 0) { // System.out.print("Want to killń\n"); AccountData ad = se.getAccountData(account_id); - Iterator it = ad.orders.iterator(); - while (it.hasNext()){ - OrderData od=it.next(); + Iterator it = ad.orders.iterator(); + while (it.hasNext()) { + OrderData od = it.next(); boolean rc = se.cancelOrder(account_id, od.id); // System.out.print("killer rc "+rc+"\n"); - // System.out.print("Killing: "+od.id+"\n"); + // System.out.print("Killing: "+od.id+"\n"); } } - + return n; - + } public long doBuy() { - RandomTraderConfig myconfig = (RandomTraderConfig)this.config; + RandomTraderConfig myconfig = (RandomTraderConfig) this.config; AccountData ad = this.se.getAccountData(account_id); - - OrderType type=OrderType.BID; -if (ad==null || myconfig==null) { - System.out.print(ad+"\n"); - return 0; - -} + OrderType type = OrderType.BID; + + if (ad == null || myconfig == null) { + System.out.print(ad + "\n"); + return 0; + + } // how much money we ant to envest? double money = getRandomAmmount(ad.money, myconfig.buy_volume); - - Quote q = se.getCurrentPrice(); - double lp = q == null ? start : q.price; - - + Quote q = se.getCurrentPrice(); + double lp = q == null ? start : q.price; + double limit; limit = lp + getRandomAmmount(lp, myconfig.buy_limit); @@ -184,37 +169,26 @@ if (ad==null || myconfig==null) { if (volume <= 0) { return 0; } - -// System.out.print("Volume is:"+volume+"\n"); -// System.out.print("My Ammount is: "+money+" My limit si:"+limit+ "\n"); - se.createOrder(account_id, type, volume, limit); - return getRandom(myconfig.buy_order_wait); - + return getRandom(myconfig.buy_order_wait); } - public long doSell() { - RandomTraderConfig myconfig = (RandomTraderConfig)this.config; + RandomTraderConfig myconfig = (RandomTraderConfig) this.config; AccountData ad = this.se.getAccountData(account_id); - - OrderType type=OrderType.ASK; - + OrderType type = OrderType.ASK; // how much money we ant to envest? - double volume = (long)getRandomAmmount(ad.shares, myconfig.sell_volume); - + double volume = (long) getRandomAmmount(ad.shares, myconfig.sell_volume); - // double lp = 100.0; //se.getBestLimit(type); - + // double lp = 100.0; //se.getBestLimit(type); Quote q = se.getCurrentPrice(); - double lp = q == null ? start : q.price; + double lp = q == null ? start : q.price; - double limit; limit = lp + getRandomAmmount(lp, myconfig.sell_limit); @@ -223,37 +197,30 @@ if (ad==null || myconfig==null) { System.out.print("SellVolume 0\n"); return 0; } - + // System.out.print("Volume is:"+volume+"\n"); - // System.out.print("My Ammount is: "+volume+" My limit si:"+limit+ "\n"); - - + // System.out.print("My Ammount is: "+volume+" My limit si:"+limit+ "\n"); se.createOrder(account_id, type, volume, limit); return getRandom(myconfig.sell_order_wait); } - - - - - long doTrade(){ + long doTrade() { cancelOrders(); Action a = getAction(); - switch (a){ + switch (a) { case BUY: return doBuy(); case SELL: return doSell(); - - + } return 0; - + } - + /* private static class TimerTaskImpl extends TimerTask { RandomTrader trader; @@ -261,20 +228,21 @@ if (ad==null || myconfig==null) { public TimerTaskImpl(RandomTrader trader, Timer timer) { this.trader = trader; - this.timer=timer; + this.timer = timer; } @Override public void run() { - + long time = trader.event(); - time/=1; - + time /= 100; + this.cancel(); - timer.schedule(new TimerTaskImpl(trader,timer), time); + timer.schedule(new TimerTaskImpl(trader, timer), time); } } - +*/ + } diff --git a/src/test/java/sesim/Test.java b/src/test/java/sesim/Test.java index 1338010..ff4d2a5 100644 --- a/src/test/java/sesim/Test.java +++ b/src/test/java/sesim/Test.java @@ -70,10 +70,10 @@ public class Test { Scheduler s = new Scheduler(); s.start(); - class Ev implements Scheduler.TimerEvent{ + class Ev implements Scheduler.TimerTask{ @Override - public long timerEvent() { + public long timerTask() { System.out.printf("Timer Event Occured %s\n",name); if ("Ev1".equals(this.name)) return 2000; @@ -92,8 +92,8 @@ public class Test { Ev e2 = new Ev("Eb2"); - s.startEvent(e1, 0); - s.startEvent(e2, 0); + s.startTimerEvent(e1, 0); + s.startTimerEvent(e2, 0); try { @@ -110,7 +110,7 @@ public class Test { System.out.print("All isstopped\n"); - // s.startEvent(e2, 100); + // s.startTimerEvent(e2, 100);