diff --git a/src/main/java/chart/Chart.java b/src/main/java/chart/Chart.java index c9731ba..6489a6a 100644 --- a/src/main/java/chart/Chart.java +++ b/src/main/java/chart/Chart.java @@ -110,12 +110,12 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab String cmd = evt.getActionCommand(); for (int i = 0; i < this.ctxMenuCompressionText.length; i++) { if (this.ctxMenuCompressionText[i].equals(cmd)) { - System.out.printf("Equality to %s\n", cmd); + this.setCompression(this.ctxMenuCompressionValues[i]); } } - System.out.printf("ACtion %s\n", cmd); - //this.setCompression(1000 * 5); + + } OHLCData data; @@ -246,8 +246,6 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab float m = c_mm.max / c_mm.min; - //System.out.printf("Min: %f Max: %f M: %f\n",c_mm.min,c_mm.max,m); - //float fac = (float) c_rect.height /(float) Math.log(c_mm.max * c_yscaling); float fac = (float) c_rect.height / (float) Math.log(m); float fmin = c_rect.height - ((float) Math.log((y / c_mm.min)) * fac); diff --git a/src/main/java/gui/EditStrategies.java b/src/main/java/gui/EditStrategies.java index e475f54..3f902f0 100644 --- a/src/main/java/gui/EditStrategies.java +++ b/src/main/java/gui/EditStrategies.java @@ -251,7 +251,7 @@ public final class EditStrategies extends javax.swing.JDialog { String cfglist = Globals.prefs.get(STRATEGYPREFS, "{}"); JSONObject cfgs = new JSONObject(cfglist); String item = (String) this.jComboBoxStrategySelector.getSelectedItem(); - System.out.printf("The item selected: %s\n", item); + if (item == null) { return; diff --git a/src/main/java/sesim/Exchange.java b/src/main/java/sesim/Exchange.java index bbce3ea..194c74b 100644 --- a/src/main/java/sesim/Exchange.java +++ b/src/main/java/sesim/Exchange.java @@ -165,7 +165,7 @@ public class Exchange { private double money; protected AutoTraderInterface owner; - private final HashMap orders; + private final ConcurrentHashMap orders; @Override public int compareTo(Object a) { @@ -175,7 +175,7 @@ public class Exchange { Account(double money, double shares) { id = (random.nextDouble() + (account_id.getNext())); - orders = new HashMap(); + orders = new ConcurrentHashMap(); this.money = money; this.shares = shares; } @@ -196,7 +196,7 @@ public class Exchange { return owner; } - public HashMap getOrders() { + public ConcurrentHashMap getOrders() { return orders; } @@ -412,9 +412,9 @@ public class Exchange { synchronized (this) { try { while (true) { - System.out.printf("Executor waits -0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0--0-0-00--0-0\n"); + this.wait(); - System.out.printf("Executor runs\n"); + executeOrders(); updateBookReceivers(OrderType.SELLLIMIT); @@ -563,6 +563,7 @@ public class Exchange { public Quote getBestPrice_0() { + synchronized (executor){ SortedSet bid = order_books.get(OrderType.BUYLIMIT); SortedSet ask = order_books.get(OrderType.SELLLIMIT); @@ -631,6 +632,7 @@ public class Exchange { } return lq; + } } // Class to describe an executed order @@ -665,15 +667,15 @@ public class Exchange { public void addBookReceiver(OrderType t, BookReceiver br) { if (br == null) { - System.out.printf("Br is null\n"); +// System.out.printf("Br is null\n"); } else { - System.out.printf("Br is not Nukk\n"); + // System.out.printf("Br is not Nukk\n"); } ArrayList bookreceivers; bookreceivers = selectBookReceiver(t); if (bookreceivers == null) { - System.out.printf("null in bookreceivers\n"); +// System.out.printf("null in bookreceivers\n"); } bookreceivers.add(br); } @@ -690,7 +692,7 @@ public class Exchange { } // Here we store the list of quote receivers - private List qrlist; + private final List qrlist; /** * @@ -767,7 +769,10 @@ public class Exchange { boolean ret = false; Order o; + +// System.out.printf("Getting executor %d\n", Thread.currentThread().getId()); synchronized (executor) { +// System.out.printf("Have executor %d\n", Thread.currentThread().getId()); o = a.orders.get(order_id); // System.out.print("The Order:"+o.limit+"\n"); @@ -785,7 +790,7 @@ public class Exchange { if (ret) { this.updateBookReceivers(o.type); } - +// System.out.printf("Levave executor %d\n", Thread.currentThread().getId()); return ret; } @@ -817,13 +822,13 @@ public class Exchange { private void removeOrderIfExecuted(Order o) { if (o.getAccount().getOwner().getName().equals("Tobias0")) { - System.out.printf("Tobias 0 test\n"); +// System.out.printf("Tobias 0 test\n"); } if (o.volume != 0) { if (o.getAccount().getOwner().getName().equals("Tobias0")) { - System.out.printf("Patially remove tobias\n"); +// System.out.printf("Patially remove tobias\n"); } o.status = OrderStatus.PARTIALLY_EXECUTED; @@ -832,7 +837,7 @@ public class Exchange { } if (o.getAccount().getOwner().getName().equals("Tobias0")) { - System.out.printf("Fully remove tobias\n"); +// System.out.printf("Fully remove tobias\n"); } o.account.orders.remove(o.id); @@ -861,7 +866,7 @@ public class Exchange { s.type = OrderType.SELL; addOrderToBook(s); - System.out.printf("Stoploss hit %f %f\n", s.volume, s.limit); +// System.out.printf("Stoploss hit %f %f\n", s.volume, s.limit); } } @@ -886,7 +891,7 @@ public class Exchange { */ public void executeOrders() { - System.out.printf("Exec Orders\n"); +// System.out.printf("Exec Orders\n"); SortedSet bid = order_books.get(OrderType.BUYLIMIT); SortedSet ask = order_books.get(OrderType.SELLLIMIT); @@ -1008,15 +1013,18 @@ public class Exchange { return -1; } +// System.out.printf("Getting executor in create Order\n", Thread.currentThread().getId()); synchronized (executor) { +// System.out.printf("Have executor in create Order\n", Thread.currentThread().getId()); num_orders++; addOrderToBook(o); a.orders.put(o.id, o); +// System.out.printf("Calling in create Order oupdate for %s\n", o.getOrderStatus().toString()); a.update(o); executor.notify(); } - + // a.update(o); return o.id; } diff --git a/src/main/java/sesim/Scheduler.java b/src/main/java/sesim/Scheduler.java index 57bbdd2..aa31509 100644 --- a/src/main/java/sesim/Scheduler.java +++ b/src/main/java/sesim/Scheduler.java @@ -32,10 +32,13 @@ import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedList; +import java.util.Queue; import java.util.SortedMap; import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; +import java.util.concurrent.ConcurrentLinkedQueue; /** * @@ -95,7 +98,7 @@ public class Scheduler extends Thread { @Override public int compare(Object o1, Object o2) { - //return (((TimerTask) o1).getID() - ((TimerTask) o2).getID()) < 0 ? -1 : 1; + //return (((TimerTask) o1).getID() - ((TimerTask) o2).getID()) < 0 ? -1 : 1; return System.identityHashCode(o1) - System.identityHashCode(o2); } } @@ -137,19 +140,34 @@ public class Scheduler extends Thread { } + class TimerTaskDef { + + TimerTask task; + long evtime; + + TimerTaskDef(TimerTask e, long t) { + task = e; + evtime = t; + } + + } + + //LinkedList set_tasks = new LinkedList<>(); + ConcurrentLinkedQueue set_tasks = new ConcurrentLinkedQueue<>(); /** * * @param e * @param time */ public void startTimerTask(TimerTask e, long time) { + long evtime = time + currentTimeMillis(); - synchronized (event_queue) { - this.addTimerTask(e, evtime); - } + set_tasks.add(new TimerTaskDef(e, evtime)); + synchronized (this) { notify(); } + } private boolean pause = false; @@ -185,47 +203,45 @@ public class Scheduler extends Thread { s = new TreeSet<>(new ObjectComparator()); event_queue.put(evtime, s); } - - System.out.printf("Put timer task for %d %d\n",System.identityHashCode(e),evtime); + tasks.put(e, evtime); return s.add(e); } + private final LinkedList cancel_queue = new LinkedList(); + public void cancelTimerTask(TimerTask e) { - synchronized (event_queue) { - Long evtime = tasks.get(e); - - - - if (evtime == null) { - System.out.printf("Cancel evtime is null\n",""); - return; - } - - System.out.printf("Cancel evtime is %d\n",evtime); - - SortedSet s = event_queue.get(evtime); - if (s == null) { - System.out.printf("Task was null\n", ""); - return; - } - - System.out.printf("Remove now\n", ""); - - Boolean rc = s.remove(e); - - - System.out.printf("Remove RC %s",rc.toString()); - if (s.isEmpty()) { - System.out.printf("Complete removed\n"); - event_queue.remove(evtime); - } + cancel_queue.add(e); + } + + private void cancelMy(TimerTask e) { + + Long evtime = tasks.get(e); + + if (evtime == null) { + return; } + + SortedSet s = event_queue.get(evtime); + if (s == null) { + + return; + } + + Boolean rc = s.remove(e); + + if (s.isEmpty()) { + + event_queue.remove(evtime); + } + } public long runEvents() { synchronized (event_queue) { +// System.out.printf("Have Event Queue in run events %d\n", Thread.currentThread().getId()); + if (event_queue.isEmpty()) { return -1; } @@ -247,11 +263,14 @@ public class Scheduler extends Thread { this.addTimerTask(e, next_t + t); } +// System.out.printf("Leave Event Queue in run events a %d\n", Thread.currentThread().getId()); return 0; } else { +// System.out.printf("Leave Event Queue in run events %d\n", Thread.currentThread().getId()); return (t - currentTimeMillis()) / (long) this.acceleration; } + } } @@ -267,6 +286,13 @@ public class Scheduler extends Thread { while (!terminate) { + while (!set_tasks.isEmpty()) { + TimerTaskDef td = set_tasks.poll(); + this.cancelMy(td.task); + this.addTimerTask(td.task, td.evtime); + + } + long wtime = runEvents(); if (wtime == 0) { diff --git a/src/main/java/traders/RandomTraderA.java b/src/main/java/traders/RandomTraderA.java index f968bb8..d5689bb 100644 --- a/src/main/java/traders/RandomTraderA.java +++ b/src/main/java/traders/RandomTraderA.java @@ -78,10 +78,11 @@ public class RandomTraderA extends AutoTraderBase implements AccountListener { @Override public long timerTask() { - System.out.printf("Enter TimerTask for %d\n", System.identityHashCode(this)); +// System.out.printf("Enter TimerTask for %d / %d\n", System.identityHashCode(this), Thread.currentThread().getId()); sesim.Exchange.Account a = se.getAccount(account_id); +// System.out.printf("Have Account %d\n", Thread.currentThread().getId()); long rc = this.doTrade(); - System.out.printf("Exit TimerTask for %d\n", System.identityHashCode(this)); +// System.out.printf("Exit TimerTask for %d / %d\n", System.identityHashCode(this), Thread.currentThread().getId()); return rc; } @@ -184,16 +185,16 @@ public class RandomTraderA extends AutoTraderBase implements AccountListener { @Override public void accountUpdated(Account a, Exchange.Order o) { - System.out.printf("Order waht %s\n", o.getOrderStatus().toString()); - if (o.getOrderStatus() == OrderStatus.CLOSED && false) { +// System.out.printf("Order what %s %d\n", o.getOrderStatus().toString(), Thread.currentThread().getId()); + if (o.getOrderStatus() == OrderStatus.CLOSED ) { - System.out.printf("Enteter canel timer\n"); +// System.out.printf("Enteter canel timer %d\n", Thread.currentThread().getId()); se.timer.cancelTimerTask(this); -System.out.printf("back from canel timer %d\n", System.identityHashCode(this)); -System.exit(0); +//System.out.printf("back from canel timer %d\n", System.identityHashCode(this)); +//System.exit(0); - Long w = doTrade(); - System.out.printf("We have no to wait for %d\n", w); + Long w = waitAfterOrder(); +// System.out.printf("We have now to wait for %d\n", w); se.timer.startTimerTask(this, w); } @@ -243,14 +244,37 @@ System.exit(0); return 0; } + + + long waitAfterOrder(){ + if (mode == Action.BUY) { + mode = Action.RANDOM; + return getRandom(wait_after_buy); + } + + if (mode == Action.SELL) { + mode = Action.RANDOM; + return getRandom(wait_after_sell); + } + +// System.out.printf("Return action 0\n"); + return 0; + + } + long doTrade() { + +// System.out.printf("Do Trader %d\n", Thread.currentThread().getId()); cancelOrders(); +// System.out.printf("Orders Canceled %d\n", Thread.currentThread().getId()); Action a = getAction(); +// System.out.printf("Action is %s\n", a.toString()); + if (mode == Action.RANDOM) { - System.out.printf("Action: %s\n", a.toString()); +// System.out.printf("Action: %s\n", a.toString()); Integer rc = doTrade1(a); if (rc != null) { return rc; @@ -264,22 +288,11 @@ System.exit(0); if (rc != null) { return rc; } - System.out.printf("All ha s failed\n"); +// System.out.printf("All ha s failed\n"); return 5000; } - if (mode == Action.BUY) { - mode = Action.RANDOM; - return getRandom(wait_after_buy); - } - - if (mode == Action.SELL) { - mode = Action.RANDOM; - return getRandom(wait_after_sell); - } - - return 0; - + return waitAfterOrder(); } /** @@ -349,7 +362,7 @@ System.exit(0); volume = se.roundShares(volume); if (volume <= 0 || money <= 0) { - System.out.printf("Buy Order wont work\n"); +// System.out.printf("Buy Order wont work\n"); return false; } @@ -381,10 +394,10 @@ System.exit(0); se.roundMoney(limit); if (volume <= 0 || limit <= 0) { - System.out.printf("Sell wont work\n"); +// System.out.printf("Sell wont work\n"); return false; } - System.out.printf("Create a Sell Order %f %f!!!!\n", volume, limit); +// System.out.printf("Create a Sell Order %f %f!!!!\n", volume, limit); se.createOrder(account_id, type, volume, limit); return true;