diff --git a/src/main/java/chart/Chart.java b/src/main/java/chart/Chart.java index 7778c06..e83921d 100644 --- a/src/main/java/chart/Chart.java +++ b/src/main/java/chart/Chart.java @@ -37,13 +37,19 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab protected Rectangle clip_bounds = new Rectangle(); protected Dimension gdim; - protected int first_bar, last_bar; + private int first_bar, last_bar; + + + public final void initChart(){ + data = new OHLCData(60000*30); + } /** * Creates new form Chart */ public Chart() { initComponents(); + initChart(); if (Globals.se == null) { return; } @@ -52,16 +58,15 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab } - // int item_width = 10; - //int items = 350; - //long ntime = 0; - OHLCData data = new OHLCData(60000*30); + + OHLCData data; + OHLCDataItem current = null; - void drawCandle(Graphics2D g, OHLCData d, int x, int y) { - - } + //void drawCandle(Graphics2D g, OHLCData d, int x, int y) { +// + // } @Override public Dimension getPreferredScrollableViewportSize() { diff --git a/src/main/java/gui/NewMDIApplication.form b/src/main/java/gui/NewMDIApplication.form index e825d6d..acef6ff 100644 --- a/src/main/java/gui/NewMDIApplication.form +++ b/src/main/java/gui/NewMDIApplication.form @@ -26,6 +26,15 @@ + + + + + + + + + @@ -110,6 +119,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -201,9 +238,9 @@ - + - + @@ -217,8 +254,8 @@ - - + + @@ -226,13 +263,13 @@ - + - + - + @@ -297,6 +334,9 @@ + + + @@ -335,7 +375,7 @@ - + diff --git a/src/main/java/gui/NewMDIApplication.java b/src/main/java/gui/NewMDIApplication.java index c2ae5a0..f6a5a1c 100644 --- a/src/main/java/gui/NewMDIApplication.java +++ b/src/main/java/gui/NewMDIApplication.java @@ -98,32 +98,13 @@ public class NewMDIApplication extends javax.swing.JFrame { Globals.se.fairValue=moneyTotal/sharesTotal; + Globals.se.fairValue=1.0; + for (int i=0; i getTraderList() { - + if (se.traders==null) + return new ArrayList<>(); + sesim.Quote q = se.getLastQuoete(); double price = q == null ? 0 : q.price; Iterator it = se.traders.iterator(); diff --git a/src/main/java/sesim/Exchange.java b/src/main/java/sesim/Exchange.java index 5f9c4c9..a3c73f0 100644 --- a/src/main/java/sesim/Exchange.java +++ b/src/main/java/sesim/Exchange.java @@ -56,12 +56,12 @@ public class Exchange { IDGenerator account_id = new IDGenerator(); //public static Timer timer = new Timer(); - public Scheduler timer = new Scheduler(); + public Scheduler timer; // = new Scheduler(); - public ArrayList traders = new ArrayList(); + public ArrayList traders; /** * Implements a trading account @@ -113,8 +113,8 @@ public class Exchange { } // private final ConcurrentHashMap accounts = new ConcurrentHashMap<>(); - private final ConcurrentHashMap accounts = new ConcurrentHashMap<>(); - + private ConcurrentHashMap accounts; + public double createAccount(double money, double shares) { Account a = new Account(money, shares); @@ -167,8 +167,8 @@ public class Exchange { } - HashMap> order_books = new HashMap(); - + HashMap> order_books; + IDGenerator order_id = new IDGenerator(); public class Order { @@ -224,21 +224,38 @@ public class Exchange { /** * Histrory of quotes */ - public TreeSet quoteHistory = new TreeSet<>(); + public TreeSet quoteHistory; // = new TreeSet<>(); + + final void initExchange(){ + timer = new Scheduler(); // timer = new Scheduler(); + random = new Random(12); + quoteHistory = new TreeSet(); + accounts = new ConcurrentHashMap<>(); + + traders = new ArrayList(); + + num_trades=0; + + + // Create order books + order_books= new HashMap(); + for (OrderType type : OrderType.values()) { + order_books.put(type, new TreeSet(new OrderComparator(type))); + } + + } + /** * Constructor */ public Exchange() { - this.random = new java.util.Random(12); + qrlist = (new CopyOnWriteArrayList<>()); - this.qrlist = (new CopyOnWriteArrayList<>()); + initExchange(); + - // Create order books - for (OrderType type : OrderType.values()) { - //SortedSet b = new TreeSet(new OrderComparator(type)); - order_books.put(type, new TreeSet(new OrderComparator(type))); - } + } @@ -262,10 +279,26 @@ public class Exchange { } - void start() { + + + /** + * Start the exchange + */ + public void start() { timer.start(); } - + + public void reset(){ + initExchange(); + } + + public void terminate() { + timer.terminate(); + } + + + +/* class BidBook extends TreeSet { TreeSet t = new TreeSet(); @@ -275,7 +308,8 @@ public class Exchange { return true; } } - +*/ + public SortedSet getQuoteHistory(long start) { Quote s = new Quote(); @@ -366,7 +400,7 @@ public class Exchange { } // Here we store the list of quote receivers - private final List qrlist; + private List qrlist; /** * @@ -455,7 +489,7 @@ public class Exchange { return ret; } - final Random random; + Random random; public int randNextInt() { System.out.printf("Next int: %d\n", random.nextInt()); @@ -463,7 +497,7 @@ public class Exchange { } public int randNextInt(int bounds) { - System.out.printf("Next doub: %f\n", random.nextDouble()); + return random.nextInt(bounds); } @@ -492,7 +526,9 @@ public class Exchange { book.remove(book.first()); } - + + + /** * */ diff --git a/src/main/java/sesim/Scheduler.java b/src/main/java/sesim/Scheduler.java index 579913e..0ae9c47 100644 --- a/src/main/java/sesim/Scheduler.java +++ b/src/main/java/sesim/Scheduler.java @@ -25,7 +25,7 @@ */ package sesim; -import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date; +//import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Comparator; @@ -42,38 +42,51 @@ import java.util.TreeSet; */ public class Scheduler extends Thread { - private double multiplier = 0.0; - - public void setMultiply(double val){ - this.multiplier=val; + private double multiplier = 0.0; + + public void setMultiply(double val) { + + this.multiplier = val; + synchronized(this){ + this.notify(); + } } - - public double getMultiply(){ + + public double getMultiply() { return this.multiplier; } private final SortedMap> event_queue = new TreeMap<>(); - private boolean halt = false; public interface TimerTask { long timerTask(); } + private boolean terminate = false; + /** - * \ - * + * Terminate the scheduler */ - public void halt() { - halt = true; + public void terminate() { + terminate = true; synchronized (event_queue) { event_queue.notifyAll(); } + + } + + @Override + public void start() { + if (this.isAlive()) { + return; + } + this.initScheduler(); + super.start(); + + } - /** - * - */ private class ObjectComparator implements Comparator { @Override @@ -90,18 +103,18 @@ public class Scheduler extends Thread { * @return */ public long currentTimeMillis1() { - + long diff = System.currentTimeMillis() - last_time_millis; - // diff=121L; + // diff=121L; last_time_millis += diff; this.current_time_millis += diff * this.multiplier; return (long) this.current_time_millis; } - - public long currentTimeMillis(){ - return (long)this.current_time_millis; + + public long currentTimeMillis() { + return (long) this.current_time_millis; } static public String formatTimeMillis(long t) { @@ -109,10 +122,10 @@ public class Scheduler extends Thread { DateFormat formatter = new SimpleDateFormat("HH:mm:ss"); String dateFormatted = formatter.format(date); - long seconds = (t/1000) % 60; - long minutes = (t/1000/60) % 60; - long hours = (t/1000)/(60*60); - return String.format("%02d:%02d:%02d",hours,minutes, seconds); + long seconds = (t / 1000) % 60; + long minutes = (t / 1000 / 60) % 60; + long hours = (t / 1000) / (60 * 60); + return String.format("%02d:%02d:%02d", hours, minutes, seconds); } @@ -147,8 +160,7 @@ public class Scheduler extends Thread { private boolean addEvent(TimerTask e, long evtime) { - // long evtime = time + currentTimeMillis(); - + // long evtime = time + currentTimeMillis(); SortedSet s = event_queue.get(evtime); if (s == null) { s = new TreeSet<>(new ObjectComparator()); @@ -165,19 +177,18 @@ public class Scheduler extends Thread { long t = event_queue.firstKey(); long ct = currentTimeMillis1(); - + //System.out.printf("Current CMP %d %d\n", ct, t); - if (t <= ct) { - this.current_time_millis=t; + this.current_time_millis = t; SortedSet s = event_queue.get(t); event_queue.remove(t); Iterator it = s.iterator(); while (it.hasNext()) { TimerTask e = it.next(); long next_t = this.fireEvent(e); - - this.addEvent(e, next_t+t); + + this.addEvent(e, next_t + t); } return 0; @@ -189,10 +200,16 @@ public class Scheduler extends Thread { } + void initScheduler() { + current_time_millis = 0.0; + terminate = false; + + } + @Override public void run() { - // this.current_time_millis=0; - while (!halt) { + + while (!terminate) { long wtime = runEvents(); @@ -202,7 +219,7 @@ public class Scheduler extends Thread { synchronized (this) { try { - + if (wtime != -1 && !pause) { wait(wtime); } else { @@ -214,6 +231,8 @@ public class Scheduler extends Thread { } } + this.event_queue.clear(); + } } diff --git a/src/main/java/traders/RandomTrader.java b/src/main/java/traders/RandomTrader.java index 7cd7a56..ef7d757 100644 --- a/src/main/java/traders/RandomTrader.java +++ b/src/main/java/traders/RandomTrader.java @@ -121,7 +121,7 @@ public class RandomTrader extends AutoTrader { return (max - min) * r + min; } - protected int getRandom(Integer[] minmax) { + protected int getRandom(Long[] minmax) { return (int) Math.round(getRandom(minmax[0], minmax[1])); } diff --git a/src/main/java/traders/RandomTraderConfig.java b/src/main/java/traders/RandomTraderConfig.java index 5948d4a..b6c5fba 100644 --- a/src/main/java/traders/RandomTraderConfig.java +++ b/src/main/java/traders/RandomTraderConfig.java @@ -44,13 +44,13 @@ public class RandomTraderConfig extends AutoTraderBase implements AutoTraderConf public Float[] sell_volume = {100f, 100f}; public Float[] sell_limit = {-0.1f, 0.10101f}; - public Integer[] sell_wait = {10000, 50000}; - public Integer[] wait_after_sell = {1000, 30000}; + public Long[] sell_wait = {10000L, 50000L}; + public Long[] wait_after_sell = {1000L, 30000L}; public Float[] buy_volume = {100f, 100f}; public Float[] buy_limit = {-0.1f, 0.10101f}; - public Integer[] buy_wait = {10000, 50000}; - public Integer[] wait_after_buy = {10, 30}; + public Long[] buy_wait = {10000L, 50000L}; + public Long[] wait_after_buy = {10L, 30L}; @Override public AutoTrader createTrader(Exchange se, JSONObject cfg, double money, double shares) { @@ -121,6 +121,17 @@ public class RandomTraderConfig extends AutoTraderBase implements AutoTraderConf return ret; } + + private Long[] to_long(JSONArray a) { + Long[] ret = new Long[a.length()]; + for (int i = 0; i < a.length(); i++) { + ret[i] = a.getLong(i); + + } + return ret; + + } + private Number[] to_arn(JSONArray a) { Number[] ret = new Number[a.length()]; @@ -148,11 +159,11 @@ public class RandomTraderConfig extends AutoTraderBase implements AutoTraderConf buy_volume = to_float(cfg.getJSONArray(BUY_VOLUME)); sell_limit = to_float(cfg.getJSONArray(SELL_LIMIT)); buy_limit = to_float(cfg.getJSONArray(BUY_LIMIT)); - sell_wait = to_integer(cfg.getJSONArray(SELL_WAIT)); - buy_wait = to_integer(cfg.getJSONArray(BUY_WAIT)); + sell_wait = to_long(cfg.getJSONArray(SELL_WAIT)); + buy_wait = to_long(cfg.getJSONArray(BUY_WAIT)); - wait_after_sell = to_integer(cfg.getJSONArray(WAIT_AFTER_SELL)); - wait_after_buy = to_integer(cfg.getJSONArray(WAIT_AFTER_BUY)); + wait_after_sell = to_long(cfg.getJSONArray(WAIT_AFTER_SELL)); + wait_after_buy = to_long(cfg.getJSONArray(WAIT_AFTER_BUY)); } diff --git a/src/main/java/traders/RandomTraderGui.form b/src/main/java/traders/RandomTraderGui.form index 7f171f3..4b30196 100644 --- a/src/main/java/traders/RandomTraderGui.form +++ b/src/main/java/traders/RandomTraderGui.form @@ -220,28 +220,28 @@ - + - + - + - + diff --git a/src/main/java/traders/RandomTraderGui.java b/src/main/java/traders/RandomTraderGui.java index 41499af..e46035e 100644 --- a/src/main/java/traders/RandomTraderGui.java +++ b/src/main/java/traders/RandomTraderGui.java @@ -79,15 +79,15 @@ public class RandomTraderGui extends AutoTraderGui { cfg.buy_limit[1] = (Float) this.buyLimitMax.getValue(); cfg.sell_limit[0] = (Float) this.sellLimitMin.getValue(); cfg.sell_limit[1] = (Float) this.sellLimitMax.getValue(); - cfg.buy_wait[0] = (Integer) this.buyWaitMin.getValue(); - cfg.buy_wait[1] = (Integer) this.buyWaitMax.getValue(); - cfg.sell_wait[0] = (Integer) this.sellWaitMin.getValue(); - cfg.sell_wait[1] = (Integer) this.sellWaitMax.getValue(); + cfg.buy_wait[0] = (Long) this.buyWaitMin.getValue(); + cfg.buy_wait[1] = (Long) this.buyWaitMax.getValue(); + cfg.sell_wait[0] = (Long) this.sellWaitMin.getValue(); + cfg.sell_wait[1] = (Long) this.sellWaitMax.getValue(); - cfg.wait_after_buy[0] = (Integer) this.waitAfterBuyMin.getValue(); - cfg.wait_after_buy[1] = (Integer) this.waitAfterBuyMax.getValue(); - cfg.wait_after_sell[0] = (Integer) this.waitAfterSellMin.getValue(); - cfg.wait_after_sell[1] = (Integer) this.waitAfterSellMax.getValue(); + cfg.wait_after_buy[0] = (Long) this.waitAfterBuyMin.getValue(); + cfg.wait_after_buy[1] = (Long) this.waitAfterBuyMax.getValue(); + cfg.wait_after_sell[0] = (Long) this.waitAfterSellMin.getValue(); + cfg.wait_after_sell[1] = (Long) this.waitAfterSellMax.getValue(); // cfg.buy_volume[0] = Float.parseFloat(this.buyVolMin.getText()); @@ -153,13 +153,13 @@ public class RandomTraderGui extends AutoTraderGui { setBorder(null); - buyWaitMin.setModel(new javax.swing.SpinnerNumberModel(0, 0, null, 1000)); + buyWaitMin.setModel(new javax.swing.SpinnerNumberModel(0L, 0L, null, 1000L)); - sellWaitMin.setModel(new javax.swing.SpinnerNumberModel(0, 0, null, 1000)); + sellWaitMin.setModel(new javax.swing.SpinnerNumberModel(0L, 0L, null, 1000L)); - sellWaitMax.setModel(new javax.swing.SpinnerNumberModel(0, 0, null, 1000)); + sellWaitMax.setModel(new javax.swing.SpinnerNumberModel(0L, 0L, null, 1000L)); - buyWaitMax.setModel(new javax.swing.SpinnerNumberModel(0, 0, null, 1000)); + buyWaitMax.setModel(new javax.swing.SpinnerNumberModel(0L, 0L, null, 1000L)); buyLimitMin.setModel(new javax.swing.SpinnerNumberModel(0.0f, -100.0f, null, 1.0f)); diff --git a/src/main/java/traders/SwitchingTraderConfig.java b/src/main/java/traders/SwitchingTraderConfig.java index 642e02e..217df52 100644 --- a/src/main/java/traders/SwitchingTraderConfig.java +++ b/src/main/java/traders/SwitchingTraderConfig.java @@ -47,13 +47,13 @@ public class SwitchingTraderConfig extends RandomTraderConfig implements AutoTra sell_volume = new Float[]{17f, 100f}; sell_limit = new Float[]{-30f, 1f}; - sell_wait = new Integer[]{1, 5}; - wait_after_sell = new Integer[]{1, 5}; + sell_wait = new Long[]{1L, 5L}; + wait_after_sell = new Long[]{1L, 5L}; buy_volume = new Float[]{18f, 100f}; buy_limit = new Float[]{-1f, 30f}; - buy_wait = new Integer[]{1, 5}; - wait_after_buy = new Integer[]{1, 5}; + buy_wait = new Long[]{1L, 5L}; + wait_after_buy = new Long[]{1L, 5L}; } @Override