From fd90846f04b82cba15b38d115e8e87554fd1eea0 Mon Sep 17 00:00:00 2001 From: 7u83 <7u83@mail.ru> Date: Sun, 10 Dec 2017 09:33:25 +0100 Subject: [PATCH] Moved order books out of exchange to Stock class. using getDefaultStock to be compatible --- nbproject/project.properties | 2 +- src/chart/MChart.form | 55 +++++++++++++++ src/chart/MChart.java | 123 ++++++++++++++++++++++++++++++++++ src/chart/MMChart.form | 19 ++++++ src/chart/MMChart.java | 113 +++++++++++++++++++++++++++++++ src/chart/NewPanel.form | 56 ++++++++++++++++ src/chart/NewPanel.java | 96 ++++++++++++++++++++++++++ src/gui/SeSimApplication.form | 7 ++ src/gui/SeSimApplication.java | 5 +- src/sesim/Exchange.java | 114 +++++++++++++++++++++---------- src/sesim/Stock.java | 33 +++++++-- 11 files changed, 578 insertions(+), 45 deletions(-) create mode 100644 src/chart/MChart.form create mode 100644 src/chart/MChart.java create mode 100644 src/chart/MMChart.form create mode 100644 src/chart/MMChart.java create mode 100644 src/chart/NewPanel.form create mode 100644 src/chart/NewPanel.java diff --git a/nbproject/project.properties b/nbproject/project.properties index b99eec0..171ff64 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,4 +1,4 @@ -#Thu, 07 Dec 2017 16:22:48 +0100 +#Sun, 10 Dec 2017 09:32:20 +0100 annotation.processing.enabled=true annotation.processing.enabled.in.editor=false annotation.processing.processors.list= diff --git a/src/chart/MChart.form b/src/chart/MChart.form new file mode 100644 index 0000000..f5af8b0 --- /dev/null +++ b/src/chart/MChart.form @@ -0,0 +1,55 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/chart/MChart.java b/src/chart/MChart.java new file mode 100644 index 0000000..66c920d --- /dev/null +++ b/src/chart/MChart.java @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2017, 7u83 <7u83@mail.ru> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package chart; + +import javax.swing.JPanel; +import javax.swing.border.Border; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ +public class MChart extends java.awt.Panel { + + private javax.swing.JButton jButton1; + private javax.swing.JToggleButton jToggleButton1; + + /** + * Creates new form MChart + */ + public MChart() { + initComponents(); + /* java.awt.GridBagConstraints gridBagConstraints; + + jButton1 = new javax.swing.JButton(); + jToggleButton1 = new javax.swing.JToggleButton(); + + java.awt.GridBagLayout layout = new java.awt.GridBagLayout(); + layout.columnWidths = new int[]{0, 5, 0, 5, 0}; + layout.rowHeights = new int[]{0}; + setLayout(layout); + */ + this.setupLeyout(); + + } + + void setupLeyout() { + Border border = javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)); + + jButton1 = new javax.swing.JButton(); + jToggleButton1 = new javax.swing.JToggleButton(); + java.awt.GridBagLayout layout = new java.awt.GridBagLayout(); + setLayout(layout); + java.awt.GridBagConstraints gridBagConstraints; + jButton1.setText("jButton1"); + + JPanel p = new JPanel(); + //Border border; + //border = javax.swing.border.LineBorder; + + p.setBorder(border); + + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 0; + gridBagConstraints.gridwidth = 1; + gridBagConstraints.gridheight = java.awt.GridBagConstraints.RELATIVE; + gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; + gridBagConstraints.weightx = 1.0; + gridBagConstraints.weighty = 0.7; + add(p, gridBagConstraints); + + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + // //GEN-BEGIN:initComponents + private void initComponents() { + + jPanel1 = new javax.swing.JPanel(); + + setCursor(new java.awt.Cursor(java.awt.Cursor.CROSSHAIR_CURSOR)); + + jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); + this.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(149, 149, 149) + .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap(390, Short.MAX_VALUE)) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(97, 97, 97) + .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap(174, Short.MAX_VALUE)) + ); + }// //GEN-END:initComponents + + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JPanel jPanel1; + // End of variables declaration//GEN-END:variables +} diff --git a/src/chart/MMChart.form b/src/chart/MMChart.form new file mode 100644 index 0000000..518aed6 --- /dev/null +++ b/src/chart/MMChart.form @@ -0,0 +1,19 @@ + + +
+ + + + + + + + + + + + + + + +
diff --git a/src/chart/MMChart.java b/src/chart/MMChart.java new file mode 100644 index 0000000..c788f46 --- /dev/null +++ b/src/chart/MMChart.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2017, 7u83 <7u83@mail.ru> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package chart; + +import java.awt.Color; +import java.awt.Dimension; +import javax.swing.JButton; +import javax.swing.JPanel; +import javax.swing.border.Border; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ +public class MMChart extends javax.swing.JPanel { + + /** + * Creates new form MMChart + */ + public MMChart() { + initComponents(); + setupLayout(); + } + void setupLayout() { + Border redborder = javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)); + Border blueborder = javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 255)); + + // jButton1 = new javax.swing.JButton(); + // jToggleButton1 = new javax.swing.JToggleButton(); + java.awt.GridBagLayout layout = new java.awt.GridBagLayout(); + setLayout(layout); + java.awt.GridBagConstraints gridBagConstraints; + // jButton1.setText("jButton1"); + JButton but = new JButton(); + but.setText("Hello world"); + JPanel p; + + p = new JPanel(); + p.setBorder(redborder); + p.setBackground(Color.blue); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 0; + gridBagConstraints.gridwidth = 1; + gridBagConstraints.gridheight = java.awt.GridBagConstraints.RELATIVE; + gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; + gridBagConstraints.weightx = 1.0; + gridBagConstraints.weighty = 0.7; + add(p, gridBagConstraints); + + + + p = new JPanel(); + p.setBorder(redborder); + p.setBackground(Color.white); + p.setPreferredSize(new Dimension(100,100)); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 1; + gridBagConstraints.gridy = 0; + gridBagConstraints.gridwidth = 1; + gridBagConstraints.gridheight = java.awt.GridBagConstraints.RELATIVE; + gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; + gridBagConstraints.weightx = 0.0; + gridBagConstraints.weighty = 0.7; + add(p, gridBagConstraints); + + + + + p.setLayout(layout); + + + + } + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + setLayout(null); + }// //GEN-END:initComponents + + + // Variables declaration - do not modify//GEN-BEGIN:variables + // End of variables declaration//GEN-END:variables +} diff --git a/src/chart/NewPanel.form b/src/chart/NewPanel.form new file mode 100644 index 0000000..67e12aa --- /dev/null +++ b/src/chart/NewPanel.form @@ -0,0 +1,56 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/chart/NewPanel.java b/src/chart/NewPanel.java new file mode 100644 index 0000000..0da4814 --- /dev/null +++ b/src/chart/NewPanel.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2017, 7u83 <7u83@mail.ru> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package chart; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ +public class NewPanel extends java.awt.Panel { + + /** + * Creates new form NewPanel + */ + public NewPanel() { + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + // //GEN-BEGIN:initComponents + private void initComponents() { + java.awt.GridBagConstraints gridBagConstraints; + + jButton1 = new javax.swing.JButton(); + jButton2 = new javax.swing.JButton(); + jButton3 = new javax.swing.JButton(); + + setLayout(new java.awt.GridBagLayout()); + + jButton1.setText("jButton1"); + jButton1.setPreferredSize(new java.awt.Dimension(0, 0)); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 0; + gridBagConstraints.gridwidth = 80; + gridBagConstraints.gridheight = java.awt.GridBagConstraints.RELATIVE; + gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; + gridBagConstraints.weightx = 50.0; + gridBagConstraints.weighty = 0.7; + add(jButton1, gridBagConstraints); + + jButton2.setText("jButton2"); + jButton2.setPreferredSize(new java.awt.Dimension(0, 0)); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridy = 1; + gridBagConstraints.gridwidth = 80; + gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; + gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; + gridBagConstraints.weightx = 0.5; + gridBagConstraints.weighty = 0.3; + add(jButton2, gridBagConstraints); + + jButton3.setText("jButton3"); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 80; + gridBagConstraints.gridy = 0; + gridBagConstraints.gridwidth = 20; + gridBagConstraints.gridheight = java.awt.GridBagConstraints.RELATIVE; + gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; + gridBagConstraints.weighty = 0.7; + add(jButton3, gridBagConstraints); + }// //GEN-END:initComponents + + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton jButton1; + private javax.swing.JButton jButton2; + private javax.swing.JButton jButton3; + // End of variables declaration//GEN-END:variables +} diff --git a/src/gui/SeSimApplication.form b/src/gui/SeSimApplication.form index f1422d4..3521071 100644 --- a/src/gui/SeSimApplication.form +++ b/src/gui/SeSimApplication.form @@ -469,6 +469,13 @@ + + + + + + + diff --git a/src/gui/SeSimApplication.java b/src/gui/SeSimApplication.java index f469de8..33a8860 100644 --- a/src/gui/SeSimApplication.java +++ b/src/gui/SeSimApplication.java @@ -187,6 +187,7 @@ public class SeSimApplication extends javax.swing.JFrame { quoteVertical1 = new gui.orderbook.QuoteVertical(); jSplitPane5 = new javax.swing.JSplitPane(); statistics1 = new gui.Statistics(); + mMChart2 = new chart.MMChart(); menuBar = new javax.swing.JMenuBar(); fileMenu = new javax.swing.JMenu(); openMenuItem = new javax.swing.JMenuItem(); @@ -325,6 +326,7 @@ public class SeSimApplication extends javax.swing.JFrame { jSplitPane5.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); jSplitPane5.setTopComponent(statistics1); + jSplitPane5.setRightComponent(mMChart2); jSplitPane3.setLeftComponent(jSplitPane5); @@ -861,7 +863,7 @@ public class SeSimApplication extends javax.swing.JFrame { //hs.setVisible(true); SeSimHelp h = new SeSimHelp(); - + h.setVisible(true); }//GEN-LAST:event_jMenuItem1ActionPerformed @@ -943,6 +945,7 @@ public class SeSimApplication extends javax.swing.JFrame { private javax.swing.JSplitPane jSplitPane4; private javax.swing.JSplitPane jSplitPane5; private javax.swing.JTextArea jTextArea1; + private chart.MMChart mMChart2; private javax.swing.JMenuBar menuBar; private javax.swing.JMenuItem openMenuItem; private gui.orderbook.OrderBook orderBookNew1; diff --git a/src/sesim/Exchange.java b/src/sesim/Exchange.java index 93d889c..50a140b 100644 --- a/src/sesim/Exchange.java +++ b/src/sesim/Exchange.java @@ -43,12 +43,20 @@ import sesim.Order.OrderType; * @author 7u83 */ public class Exchange { - +//private HashMap> order_books; + + HashMap stocks; - Stock getStock(String symbol) { + final String DEFAULT_STOCK="def"; + + public Stock getStock(String symbol) { return stocks.get(symbol); } + + public Stock getDefaultStock(){ + return getStock(DEFAULT_STOCK); + } ConcurrentLinkedQueue order_queue = new ConcurrentLinkedQueue(); @@ -240,7 +248,7 @@ public class Exchange { return a.id; } - class OrderComparator implements Comparator { + static class OrderComparator implements Comparator { OrderType type; @@ -288,7 +296,6 @@ public class Exchange { } - HashMap> order_books; IDGenerator order_id_generator = new IDGenerator(); @@ -315,11 +322,15 @@ public class Exchange { this.ohlc_data = new HashMap(); // Create order books - order_books = new HashMap(); + +/* order_books = new HashMap(); for (OrderType type : OrderType.values()) { order_books.put(type, new TreeSet(new OrderComparator(type))); } - +*/ + Stock defstock = new Stock(DEFAULT_STOCK); + stocks = new HashMap(); + stocks.put(defstock.getSymbol(), defstock); } /** @@ -383,7 +394,7 @@ public class Exchange { Account a = o.account; a.orders.put(o.id, o); a.update(o); - executeOrders(); + executeOrders(getDefaultStock()); } updateBookReceivers(OrderType.SELLLIMIT); @@ -428,7 +439,7 @@ public class Exchange { } } */ - public SortedSet getQuoteHistory(long start) { +/* public SortedSet getQuoteHistory(long start) { Quote s = new Quote(); s.time = start * 1000; @@ -440,7 +451,8 @@ public class Exchange { return result; } - +*/ + public final String CFG_MONEY_DECIMALS = "money_decimals"; public final String CFG_SHARES_DECIMALS = "shares_decimals"; @@ -454,10 +466,10 @@ public class Exchange { } - public Double getBestPrice() { - System.out.printf("Get BP\n"); - SortedSet bid = order_books.get(OrderType.BUYLIMIT); - SortedSet ask = order_books.get(OrderType.SELLLIMIT); + public Double getBestPrice(Stock stock) { + + SortedSet bid = stock.order_books.get(OrderType.BUYLIMIT); + SortedSet ask = stock.order_books.get(OrderType.SELLLIMIT); Quote lq = this.getLastQuoete(); Order b = null, a = null; @@ -533,11 +545,11 @@ public class Exchange { return lq.price; } - public Quote getBestPrice_0() { + public Quote getBestPrice_0(Stock stock) { synchronized (executor) { - SortedSet bid = order_books.get(OrderType.BUYLIMIT); - SortedSet ask = order_books.get(OrderType.SELLLIMIT); + SortedSet bid = stock.order_books.get(OrderType.BUYLIMIT); + SortedSet ask = stock.order_books.get(OrderType.SELLLIMIT); Quote lq = this.getLastQuoete(); Order b = null, a = null; @@ -607,6 +619,10 @@ public class Exchange { } } + public Quote getBestPrice_0(){ + return getBestPrice_0(getDefaultStock()); + } + // Class to describe an executed order // QuoteReceiver has to be implemented by objects that wants // to receive quote updates @@ -688,9 +704,9 @@ public class Exchange { //double lastprice = 100.0; // long lastsvolume; // private final Locker tradelock = new Locker(); - public ArrayList getOrderBook(OrderType type, int depth) { + public ArrayList getOrderBook(Stock stock, OrderType type, int depth) { - SortedSet book = order_books.get(type); + SortedSet book = stock.order_books.get(type); if (book == null) { return null; } @@ -715,6 +731,11 @@ public class Exchange { return ret; } + public ArrayList getOrderBook(OrderType type, int depth){ + return getOrderBook(getDefaultStock(),type,depth); + } + + public Quote getLastQuoete() { if (this.quoteHistory.isEmpty()) { return null; @@ -737,7 +758,7 @@ public class Exchange { } - public boolean cancelOrder(double account_id, long order_id) { + public boolean cancelOrder(Stock stock,double account_id, long order_id) { Account a = accounts.get(account_id); if (a == null) { return false; @@ -754,7 +775,7 @@ public class Exchange { // System.out.print("The Order:"+o.limit+"\n"); if (o != null) { - SortedSet ob = order_books.get(o.type); + SortedSet ob = stock.order_books.get(o.type); boolean rc = ob.remove(o); @@ -770,6 +791,10 @@ public class Exchange { // System.out.printf("Levave executor %d\n", Thread.currentThread().getId()); return ret; } + + public boolean cancelOrder(double account_id, long order_id){ + return cancelOrder(getDefaultStock(),account_id,order_id); + } Random random; @@ -797,7 +822,7 @@ public class Exchange { public double fairValue = 0; - private void removeOrderIfExecuted(Order o) { + private void removeOrderIfExecuted(Stock stock,Order o) { if (o.getAccount().getOwner().getName().equals("Tobias0")) { // System.out.printf("Tobias 0 test\n"); } @@ -819,7 +844,7 @@ public class Exchange { o.account.orders.remove(o.id); - SortedSet book = order_books.get(o.type); + SortedSet book = stock.order_books.get(o.type); book.remove(book.first()); @@ -827,10 +852,14 @@ public class Exchange { o.account.update(o); } + + private void removeOrderIfExecuted(Order o){ + removeOrderIfExecuted(getDefaultStock(),o); + } - void checkSLOrders(double price) { - SortedSet sl = order_books.get(OrderType.STOPLOSS); - SortedSet ask = order_books.get(OrderType.SELLLIMIT); + void checkSLOrders(Stock stock, double price) { + SortedSet sl = stock.order_books.get(OrderType.STOPLOSS); + SortedSet ask = stock.order_books.get(OrderType.SELLLIMIT); if (sl.isEmpty()) { return; @@ -846,6 +875,11 @@ public class Exchange { // System.out.printf("Stoploss hit %f %f\n", s.volume, s.limit); } } + + void checkSLOrders(double price){ + checkSLOrders(getDefaultStock(),price); + } + public void executeUnlimitedOrders() { @@ -886,14 +920,14 @@ public class Exchange { /** * */ - public void executeOrders() { + public void executeOrders(Stock stock) { // System.out.printf("Exec Orders\n"); - SortedSet bid = order_books.get(OrderType.BUYLIMIT); - SortedSet ask = order_books.get(OrderType.SELLLIMIT); + SortedSet bid = stock.order_books.get(OrderType.BUYLIMIT); + SortedSet ask = stock.order_books.get(OrderType.SELLLIMIT); - SortedSet ul_buy = order_books.get(OrderType.BUY); - SortedSet ul_sell = order_books.get(OrderType.SELL); + SortedSet ul_buy = stock.order_books.get(OrderType.BUY); + SortedSet ul_sell = stock.order_books.get(OrderType.SELL); double volume_total = 0; double money_total = 0; @@ -905,7 +939,7 @@ public class Exchange { Order a = ul_sell.first(); Order b = ul_buy.first(); - Double price = getBestPrice(); + Double price = getBestPrice(stock); if (price == null) { break; } @@ -989,8 +1023,8 @@ public class Exchange { long buy_orders = 0; long sell_orders = 0; - private void addOrderToBook(Order o) { - order_books.get(o.type).add(o); + private void addOrderToBook(Stock stock, Order o) { + stock.order_books.get(o.type).add(o); switch (o.type) { case BUY: case BUYLIMIT: @@ -1004,6 +1038,10 @@ public class Exchange { } // System.out.printf("B/S %d/%d Failed B/S: %d/%d\n", buy_orders, sell_orders,buy_failed,sell_failed); } + + private void addOrderToBook(Order o){ + addOrderToBook(getDefaultStock(),o); + } long buy_failed = 0; long sell_failed = 0; @@ -1054,7 +1092,7 @@ public class Exchange { a.orders.put(o.id, o); a.update(o); - executeOrders(); + executeOrders(getDefaultStock()); updateBookReceivers(OrderType.SELLLIMIT); updateBookReceivers(OrderType.BUYLIMIT); @@ -1066,13 +1104,17 @@ public class Exchange { return o.getID(); } - public double getBestLimit(OrderType type) { - Order o = order_books.get(type).first(); + public double getBestLimit(Stock stock,OrderType type) { + Order o = stock.order_books.get(type).first(); if (o == null) { return -1; } return o.limit; } + public double getBestLimit(OrderType type){ + return getBestLimit(getDefaultStock(),type); + } + public int getNumberOfOpenOrders(double account_id) { Account a = accounts.get(account_id); diff --git a/src/sesim/Stock.java b/src/sesim/Stock.java index df3ce07..85656e7 100644 --- a/src/sesim/Stock.java +++ b/src/sesim/Stock.java @@ -25,21 +25,40 @@ */ package sesim; +import java.util.HashMap; +import java.util.SortedSet; +import java.util.TreeSet; + +import sesim.Exchange.OrderComparator; + /** * * @author 7u83 <7u83@mail.ru> */ public class Stock { - private String symbol; + + private final String symbol; private String name; - - String getSymbol(){ + + Stock(String symbol) { + this.symbol=symbol; + + order_books = new HashMap(); + + // Create an order book for each order type + for (Order.OrderType type : Order.OrderType.values()) { + this.order_books.put(type, new TreeSet(new Exchange.OrderComparator(type))); + } + } + + String getSymbol() { return symbol; } - - String getName(){ + + String getName() { return name; } - - + + public final HashMap> order_books; + }