From f0888a55d07c00e8ccdc61aca76a8b75a11dbc81 Mon Sep 17 00:00:00 2001 From: 7u83 <7u83@maiol.ru> Date: Mon, 10 Apr 2017 20:03:51 +0200 Subject: [PATCH] Statistics are working with low/high etc. --- nbproject/project.properties | 2 +- src/gui/Statistics.java | 16 ++++++ src/sesim/Exchange.java | 96 ++++++++++++++++++++++++++++++------ 3 files changed, 98 insertions(+), 16 deletions(-) diff --git a/nbproject/project.properties b/nbproject/project.properties index 1017e25..857e006 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,4 +1,4 @@ -#Sun, 09 Apr 2017 23:54:11 +0200 +#Mon, 10 Apr 2017 19:52:24 +0200 annotation.processing.enabled=true annotation.processing.enabled.in.editor=false annotation.processing.processors.list= diff --git a/src/gui/Statistics.java b/src/gui/Statistics.java index 427047d..b4a8331 100644 --- a/src/gui/Statistics.java +++ b/src/gui/Statistics.java @@ -48,6 +48,22 @@ public class Statistics extends javax.swing.JPanel { Exchange.Statistics s = Globals.se.getStatistics(); tradesLabel.setText(String.format("%d", s.trades)); + + + if (s.heigh == null){ + labelHigh.setText("--"); + } + else { + labelHigh.setText(Globals.se.getMoneyFormatter().format(s.heigh)); + } + + if (s.low == null){ + labelLow.setText("--"); + } + else { + labelLow.setText(Globals.se.getMoneyFormatter().format(s.low)); + } + } }, 1000, 1000); diff --git a/src/sesim/Exchange.java b/src/sesim/Exchange.java index 69d847a..4f6fcf7 100644 --- a/src/sesim/Exchange.java +++ b/src/sesim/Exchange.java @@ -1,5 +1,6 @@ package sesim; +import java.text.DecimalFormat; import java.util.*; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentHashMap; @@ -19,7 +20,9 @@ public class Exchange { ConcurrentLinkedQueue order_queue = new ConcurrentLinkedQueue(); private double money_df = 10000; - + private int money_decimals=2; + DecimalFormat money_formatter; + /** * Set the number of decimals used with money * @@ -27,9 +30,13 @@ public class Exchange { */ public void setMoneyDecimals(int n) { money_df = Math.pow(10, n); + money_decimals=n; + money_formatter=getFormatter(n); } private double shares_df = 1; + private double shares_decimals=0; + private DecimalFormat shares_formatter; /** * Set the number of decimals for shares @@ -38,6 +45,8 @@ public class Exchange { */ public void setSharesDecimals(int n) { shares_df = Math.pow(10, n); + shares_decimals=n; + shares_formatter = getFormatter(n); } public double roundToDecimals(double val, double f) { @@ -51,6 +60,28 @@ public class Exchange { public double roundMoney(double money) { return roundToDecimals(money, money_df); } + + public DecimalFormat getFormatter(int n){ + DecimalFormat formatter; + String s = "#0."; + if (n == 0) { + s = "#"; + } else { + for (int i = 0; i < n; i++) { + s = s + "0"; + } + } + return new DecimalFormat(s); + } + + public DecimalFormat getMoneyFormatter(){ + return money_formatter; + } + + public DecimalFormat getSharesFormatter(){ + return shares_formatter; + } + /** * Definition of order types @@ -389,7 +420,8 @@ public class Exchange { traders = new ArrayList(); - num_trades = 0; + statistics = new Statistics(); + //num_trades = 0; this.ohlc_data = new HashMap(); @@ -416,28 +448,34 @@ public class Exchange { public long trades; public long orders; + public Double heigh; + public Double low; - void reset() { + public final void reset() { trades = 0; + heigh=null; + low=null; } Statistics() { - + reset(); } }; Statistics statistics; - long num_trades = 0; - long num_orders = 0; +// long num_trades = 0; +// long num_orders = 0; public Statistics getStatistics() { - Statistics s = new Statistics(); + return statistics; +/* Statistics s = new Statistics(); s.trades = num_trades; s.orders = num_orders; return s; +*/ } @@ -759,8 +797,8 @@ public class Exchange { // long time = 0; //double theprice = 12.9; // long orderid = 1; - double lastprice = 100.0; - long lastsvolume; + //double lastprice = 100.0; + // long lastsvolume; // private final Locker tradelock = new Locker(); public ArrayList getOrderBook(OrderType type, int depth) { @@ -935,6 +973,29 @@ public class Exchange { removeOrderIfExecuted(b); } + + + + void addQuoteToHistory(Quote q){ + if (statistics.heigh==null){ + statistics.heigh=q.price; + } + else if (statistics.heighq.price){ + statistics.low=q.price; + } + + quoteHistory.add(q); + updateOHLCData(q); + updateQuoteReceivers(q); + } + + /** * */ @@ -1016,7 +1077,8 @@ public class Exchange { volume_total += volume; money_total += price * volume; - num_trades++; +// num_trades++; + statistics.trades++; this.checkSLOrders(price); @@ -1030,10 +1092,12 @@ public class Exchange { q.volume = volume_total; q.time = timer.currentTimeMillis(); - this.quoteHistory.add(q); - this.updateOHLCData(q); - - this.updateQuoteReceivers(q); + + addQuoteToHistory(q); + + //this.quoteHistory.add(q); + //this.updateOHLCData(q); + //this.updateQuoteReceivers(q); } @@ -1099,7 +1163,9 @@ public class Exchange { // System.out.printf("Getting executor in create Order\n", Thread.currentThread().getId()); synchronized (executor) { - num_orders++; + //num_orders++; + statistics.orders++; + addOrderToBook(o); a.orders.put(o.id, o); a.update(o);