More smooze threading interface

This commit is contained in:
7u83 2017-02-26 03:01:24 +01:00
parent 6a3b814aa6
commit 5794c6de22
5 changed files with 127 additions and 82 deletions

View File

@ -110,12 +110,12 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
String cmd = evt.getActionCommand(); String cmd = evt.getActionCommand();
for (int i = 0; i < this.ctxMenuCompressionText.length; i++) { for (int i = 0; i < this.ctxMenuCompressionText.length; i++) {
if (this.ctxMenuCompressionText[i].equals(cmd)) { if (this.ctxMenuCompressionText[i].equals(cmd)) {
System.out.printf("Equality to %s\n", cmd);
this.setCompression(this.ctxMenuCompressionValues[i]); this.setCompression(this.ctxMenuCompressionValues[i]);
} }
} }
System.out.printf("ACtion %s\n", cmd);
//this.setCompression(1000 * 5);
} }
OHLCData data; OHLCData data;
@ -246,8 +246,6 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
float m = c_mm.max / c_mm.min; 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 fac = (float) c_rect.height / (float) Math.log(m);
float fmin = c_rect.height - ((float) Math.log((y / c_mm.min)) * fac); float fmin = c_rect.height - ((float) Math.log((y / c_mm.min)) * fac);

View File

@ -251,7 +251,7 @@ public final class EditStrategies extends javax.swing.JDialog {
String cfglist = Globals.prefs.get(STRATEGYPREFS, "{}"); String cfglist = Globals.prefs.get(STRATEGYPREFS, "{}");
JSONObject cfgs = new JSONObject(cfglist); JSONObject cfgs = new JSONObject(cfglist);
String item = (String) this.jComboBoxStrategySelector.getSelectedItem(); String item = (String) this.jComboBoxStrategySelector.getSelectedItem();
System.out.printf("The item selected: %s\n", item);
if (item == null) { if (item == null) {
return; return;

View File

@ -165,7 +165,7 @@ public class Exchange {
private double money; private double money;
protected AutoTraderInterface owner; protected AutoTraderInterface owner;
private final HashMap<Long, Order> orders; private final ConcurrentHashMap<Long, Order> orders;
@Override @Override
public int compareTo(Object a) { public int compareTo(Object a) {
@ -175,7 +175,7 @@ public class Exchange {
Account(double money, double shares) { Account(double money, double shares) {
id = (random.nextDouble() + (account_id.getNext())); id = (random.nextDouble() + (account_id.getNext()));
orders = new HashMap(); orders = new ConcurrentHashMap();
this.money = money; this.money = money;
this.shares = shares; this.shares = shares;
} }
@ -196,7 +196,7 @@ public class Exchange {
return owner; return owner;
} }
public HashMap<Long, Order> getOrders() { public ConcurrentHashMap<Long, Order> getOrders() {
return orders; return orders;
} }
@ -412,9 +412,9 @@ public class Exchange {
synchronized (this) { synchronized (this) {
try { try {
while (true) { 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(); this.wait();
System.out.printf("Executor runs\n");
executeOrders(); executeOrders();
updateBookReceivers(OrderType.SELLLIMIT); updateBookReceivers(OrderType.SELLLIMIT);
@ -563,6 +563,7 @@ public class Exchange {
public Quote getBestPrice_0() { public Quote getBestPrice_0() {
synchronized (executor){
SortedSet<Order> bid = order_books.get(OrderType.BUYLIMIT); SortedSet<Order> bid = order_books.get(OrderType.BUYLIMIT);
SortedSet<Order> ask = order_books.get(OrderType.SELLLIMIT); SortedSet<Order> ask = order_books.get(OrderType.SELLLIMIT);
@ -631,6 +632,7 @@ public class Exchange {
} }
return lq; return lq;
}
} }
// Class to describe an executed order // Class to describe an executed order
@ -665,15 +667,15 @@ public class Exchange {
public void addBookReceiver(OrderType t, BookReceiver br) { public void addBookReceiver(OrderType t, BookReceiver br) {
if (br == null) { if (br == null) {
System.out.printf("Br is null\n"); // System.out.printf("Br is null\n");
} else { } else {
System.out.printf("Br is not Nukk\n"); // System.out.printf("Br is not Nukk\n");
} }
ArrayList<BookReceiver> bookreceivers; ArrayList<BookReceiver> bookreceivers;
bookreceivers = selectBookReceiver(t); bookreceivers = selectBookReceiver(t);
if (bookreceivers == null) { if (bookreceivers == null) {
System.out.printf("null in bookreceivers\n"); // System.out.printf("null in bookreceivers\n");
} }
bookreceivers.add(br); bookreceivers.add(br);
} }
@ -690,7 +692,7 @@ public class Exchange {
} }
// Here we store the list of quote receivers // Here we store the list of quote receivers
private List<QuoteReceiver> qrlist; private final List<QuoteReceiver> qrlist;
/** /**
* *
@ -767,7 +769,10 @@ public class Exchange {
boolean ret = false; boolean ret = false;
Order o; Order o;
// System.out.printf("Getting executor %d\n", Thread.currentThread().getId());
synchronized (executor) { synchronized (executor) {
// System.out.printf("Have executor %d\n", Thread.currentThread().getId());
o = a.orders.get(order_id); o = a.orders.get(order_id);
// System.out.print("The Order:"+o.limit+"\n"); // System.out.print("The Order:"+o.limit+"\n");
@ -785,7 +790,7 @@ public class Exchange {
if (ret) { if (ret) {
this.updateBookReceivers(o.type); this.updateBookReceivers(o.type);
} }
// System.out.printf("Levave executor %d\n", Thread.currentThread().getId());
return ret; return ret;
} }
@ -817,13 +822,13 @@ public class Exchange {
private void removeOrderIfExecuted(Order o) { private void removeOrderIfExecuted(Order o) {
if (o.getAccount().getOwner().getName().equals("Tobias0")) { 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.volume != 0) {
if (o.getAccount().getOwner().getName().equals("Tobias0")) { 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; o.status = OrderStatus.PARTIALLY_EXECUTED;
@ -832,7 +837,7 @@ public class Exchange {
} }
if (o.getAccount().getOwner().getName().equals("Tobias0")) { 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); o.account.orders.remove(o.id);
@ -861,7 +866,7 @@ public class Exchange {
s.type = OrderType.SELL; s.type = OrderType.SELL;
addOrderToBook(s); 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() { public void executeOrders() {
System.out.printf("Exec Orders\n"); // System.out.printf("Exec Orders\n");
SortedSet<Order> bid = order_books.get(OrderType.BUYLIMIT); SortedSet<Order> bid = order_books.get(OrderType.BUYLIMIT);
SortedSet<Order> ask = order_books.get(OrderType.SELLLIMIT); SortedSet<Order> ask = order_books.get(OrderType.SELLLIMIT);
@ -1008,15 +1013,18 @@ public class Exchange {
return -1; return -1;
} }
// System.out.printf("Getting executor in create Order\n", Thread.currentThread().getId());
synchronized (executor) { synchronized (executor) {
// System.out.printf("Have executor in create Order\n", Thread.currentThread().getId());
num_orders++; num_orders++;
addOrderToBook(o); addOrderToBook(o);
a.orders.put(o.id, o); a.orders.put(o.id, o);
// System.out.printf("Calling in create Order oupdate for %s\n", o.getOrderStatus().toString());
a.update(o); a.update(o);
executor.notify(); executor.notify();
} }
// a.update(o);
return o.id; return o.id;
} }

View File

@ -32,10 +32,13 @@ import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.SortedMap; import java.util.SortedMap;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.concurrent.ConcurrentLinkedQueue;
/** /**
* *
@ -95,7 +98,7 @@ public class Scheduler extends Thread {
@Override @Override
public int compare(Object o1, Object o2) { 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); 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<TimerTaskDef> set_tasks = new LinkedList<>();
ConcurrentLinkedQueue<TimerTaskDef> set_tasks = new ConcurrentLinkedQueue<>();
/** /**
* *
* @param e * @param e
* @param time * @param time
*/ */
public void startTimerTask(TimerTask e, long time) { public void startTimerTask(TimerTask e, long time) {
long evtime = time + currentTimeMillis(); long evtime = time + currentTimeMillis();
synchronized (event_queue) { set_tasks.add(new TimerTaskDef(e, evtime));
this.addTimerTask(e, evtime);
}
synchronized (this) { synchronized (this) {
notify(); notify();
} }
} }
private boolean pause = false; private boolean pause = false;
@ -186,46 +204,44 @@ public class Scheduler extends Thread {
event_queue.put(evtime, s); event_queue.put(evtime, s);
} }
System.out.printf("Put timer task for %d %d\n",System.identityHashCode(e),evtime);
tasks.put(e, evtime); tasks.put(e, evtime);
return s.add(e); return s.add(e);
} }
private final LinkedList<TimerTask> cancel_queue = new LinkedList();
public void cancelTimerTask(TimerTask e) { public void cancelTimerTask(TimerTask e) {
synchronized (event_queue) { cancel_queue.add(e);
Long evtime = tasks.get(e); }
private void cancelMy(TimerTask e) {
Long evtime = tasks.get(e);
if (evtime == null) { if (evtime == null) {
System.out.printf("Cancel evtime is null\n",""); return;
return;
}
System.out.printf("Cancel evtime is %d\n",evtime);
SortedSet<TimerTask> 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);
}
} }
SortedSet<TimerTask> s = event_queue.get(evtime);
if (s == null) {
return;
}
Boolean rc = s.remove(e);
if (s.isEmpty()) {
event_queue.remove(evtime);
}
} }
public long runEvents() { public long runEvents() {
synchronized (event_queue) { synchronized (event_queue) {
// System.out.printf("Have Event Queue in run events %d\n", Thread.currentThread().getId());
if (event_queue.isEmpty()) { if (event_queue.isEmpty()) {
return -1; return -1;
} }
@ -247,11 +263,14 @@ public class Scheduler extends Thread {
this.addTimerTask(e, next_t + t); this.addTimerTask(e, next_t + t);
} }
// System.out.printf("Leave Event Queue in run events a %d\n", Thread.currentThread().getId());
return 0; return 0;
} else { } else {
// System.out.printf("Leave Event Queue in run events %d\n", Thread.currentThread().getId());
return (t - currentTimeMillis()) / (long) this.acceleration; return (t - currentTimeMillis()) / (long) this.acceleration;
} }
} }
} }
@ -267,6 +286,13 @@ public class Scheduler extends Thread {
while (!terminate) { while (!terminate) {
while (!set_tasks.isEmpty()) {
TimerTaskDef td = set_tasks.poll();
this.cancelMy(td.task);
this.addTimerTask(td.task, td.evtime);
}
long wtime = runEvents(); long wtime = runEvents();
if (wtime == 0) { if (wtime == 0) {

View File

@ -78,10 +78,11 @@ public class RandomTraderA extends AutoTraderBase implements AccountListener {
@Override @Override
public long timerTask() { 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); sesim.Exchange.Account a = se.getAccount(account_id);
// System.out.printf("Have Account %d\n", Thread.currentThread().getId());
long rc = this.doTrade(); 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; return rc;
} }
@ -184,16 +185,16 @@ public class RandomTraderA extends AutoTraderBase implements AccountListener {
@Override @Override
public void accountUpdated(Account a, Exchange.Order o) { public void accountUpdated(Account a, Exchange.Order o) {
System.out.printf("Order waht %s\n", o.getOrderStatus().toString()); // System.out.printf("Order what %s %d\n", o.getOrderStatus().toString(), Thread.currentThread().getId());
if (o.getOrderStatus() == OrderStatus.CLOSED && false) { 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); se.timer.cancelTimerTask(this);
System.out.printf("back from canel timer %d\n", System.identityHashCode(this)); //System.out.printf("back from canel timer %d\n", System.identityHashCode(this));
System.exit(0); //System.exit(0);
Long w = doTrade(); Long w = waitAfterOrder();
System.out.printf("We have no to wait for %d\n", w); // System.out.printf("We have now to wait for %d\n", w);
se.timer.startTimerTask(this, w); se.timer.startTimerTask(this, w);
} }
@ -244,13 +245,36 @@ System.exit(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() { long doTrade() {
// System.out.printf("Do Trader %d\n", Thread.currentThread().getId());
cancelOrders(); cancelOrders();
// System.out.printf("Orders Canceled %d\n", Thread.currentThread().getId());
Action a = getAction(); Action a = getAction();
// System.out.printf("Action is %s\n", a.toString());
if (mode == Action.RANDOM) { if (mode == Action.RANDOM) {
System.out.printf("Action: %s\n", a.toString()); // System.out.printf("Action: %s\n", a.toString());
Integer rc = doTrade1(a); Integer rc = doTrade1(a);
if (rc != null) { if (rc != null) {
return rc; return rc;
@ -264,22 +288,11 @@ System.exit(0);
if (rc != null) { if (rc != null) {
return rc; return rc;
} }
System.out.printf("All ha s failed\n"); // System.out.printf("All ha s failed\n");
return 5000; return 5000;
} }
if (mode == Action.BUY) { return waitAfterOrder();
mode = Action.RANDOM;
return getRandom(wait_after_buy);
}
if (mode == Action.SELL) {
mode = Action.RANDOM;
return getRandom(wait_after_sell);
}
return 0;
} }
/** /**
@ -349,7 +362,7 @@ System.exit(0);
volume = se.roundShares(volume); volume = se.roundShares(volume);
if (volume <= 0 || money <= 0) { if (volume <= 0 || money <= 0) {
System.out.printf("Buy Order wont work\n"); // System.out.printf("Buy Order wont work\n");
return false; return false;
} }
@ -381,10 +394,10 @@ System.exit(0);
se.roundMoney(limit); se.roundMoney(limit);
if (volume <= 0 || limit <= 0) { if (volume <= 0 || limit <= 0) {
System.out.printf("Sell wont work\n"); // System.out.printf("Sell wont work\n");
return false; 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); se.createOrder(account_id, type, volume, limit);
return true; return true;