diff --git a/src/main/java/gui/MainWin.java b/src/main/java/gui/MainWin.java index 5183344..cdbe21e 100644 --- a/src/main/java/gui/MainWin.java +++ b/src/main/java/gui/MainWin.java @@ -25,12 +25,8 @@ */ package gui; -import javax.swing.UIManager; -import javax.swing.UnsupportedLookAndFeelException; import sesim.AutoTrader; import sesim.Exchange; -import traders.RandomTrader; -import traders.RandomTraderConfig; import traders.*; @@ -205,7 +201,8 @@ public class MainWin extends javax.swing.JFrame { //rt.start(); // SwitchingTraderConfig rcfg1 = new SwitchingTraderConfig(); - SwitchingTraderConfig rcfg1 = new SwitchingTraderConfig(); + // SwitchingTraderConfig rcfg1 = new SwitchingTraderConfig(); + RandomTraderConfig rcfg1 = new RandomTraderConfig(); AutoTrader rt1 = rcfg1.createTrader(se, 0, 1000000); rt1.setName("Bob"); @@ -214,9 +211,10 @@ public class MainWin extends javax.swing.JFrame { //rt2.start(); - SwitchingTraderConfig cfg = new SwitchingTraderConfig(); + // SwitchingTraderConfig cfg = new SwitchingTraderConfig(); + RandomTraderConfig cfg= new RandomTraderConfig(); - for (int i=0; i<1000; i++){ + for (int i=0; i<10; i++){ AutoTrader randt = cfg.createTrader(se, 100000, 0); randt.setName("Alice"); @@ -263,10 +261,8 @@ public class MainWin extends javax.swing.JFrame { // /* Create and display the form */ - java.awt.EventQueue.invokeLater(new Runnable() { - public void run() { - new MainWin().setVisible(true); - } + java.awt.EventQueue.invokeLater(() -> { + new MainWin().setVisible(true); }); } diff --git a/src/main/java/gui/NewMDIApplication.form b/src/main/java/gui/NewMDIApplication.form index 0be9b45..86552c2 100644 --- a/src/main/java/gui/NewMDIApplication.form +++ b/src/main/java/gui/NewMDIApplication.form @@ -2,12 +2,6 @@
- - - - - - @@ -124,13 +118,39 @@ - + + + + + + + - + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/gui/NewMDIApplication.java b/src/main/java/gui/NewMDIApplication.java index 72e49af..d7de99c 100644 --- a/src/main/java/gui/NewMDIApplication.java +++ b/src/main/java/gui/NewMDIApplication.java @@ -47,7 +47,9 @@ public class NewMDIApplication extends javax.swing.JFrame { // //GEN-BEGIN:initComponents private void initComponents() { + bidBook1 = new gui.BidBook(); desktopPane = new javax.swing.JDesktopPane(); + orderBookPanel1 = new gui.OrderBookPanel(); menuBar = new javax.swing.JMenuBar(); fileMenu = new javax.swing.JMenu(); openMenuItem = new javax.swing.JMenuItem(); @@ -132,11 +134,22 @@ public class NewMDIApplication extends javax.swing.JFrame { getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGap(0, 400, Short.MAX_VALUE) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addComponent(orderBookPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(bidBook1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap()) + .addComponent(desktopPane) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGap(0, 279, Short.MAX_VALUE) + .addGroup(layout.createSequentialGroup() + .addComponent(desktopPane, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(orderBookPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(bidBook1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addContainerGap()) ); pack(); @@ -183,6 +196,7 @@ public class NewMDIApplication extends javax.swing.JFrame { // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JMenuItem aboutMenuItem; + private gui.BidBook bidBook1; private javax.swing.JMenuItem contentMenuItem; private javax.swing.JMenuItem copyMenuItem; private javax.swing.JMenuItem cutMenuItem; @@ -194,6 +208,7 @@ public class NewMDIApplication extends javax.swing.JFrame { private javax.swing.JMenu helpMenu; private javax.swing.JMenuBar menuBar; private javax.swing.JMenuItem openMenuItem; + private gui.OrderBookPanel orderBookPanel1; private javax.swing.JMenuItem pasteMenuItem; private javax.swing.JMenuItem saveAsMenuItem; private javax.swing.JMenuItem saveMenuItem; diff --git a/src/main/java/sesim/Account_old.java b/src/main/java/sesim/Account_old.java deleted file mode 100644 index 9896a53..0000000 --- a/src/main/java/sesim/Account_old.java +++ /dev/null @@ -1,101 +0,0 @@ -package sesim; - -import java.util.*; - - -final public class Account_old { - - /** - * Exchange this account belongs to - */ - public Exchange se; - - /** - * Number of shares in this account - */ - public long shares = 0; - - /** - * Ammount of money in this account - */ - public double money = 0; - - /** - * Name of this account - */ - public String name = ""; - - - public ArrayList pending; - - public boolean orderpending = false; - - - public Account_old(Exchange se, long shares, double money ) { - this.shares=shares; - this.money=money; - this.se=se; - pending = new ArrayList<>(); - } - - public Account_old(){ - //this(,0.0); - } - - // private double bound_money; - - - - public void print_current() { - System.out.printf("%s shares: %d credit: %.2f\n", - name, shares, money - ); - } - - - public boolean isRuined(){ - - -/* System.out.print( - "Account_old: " - +money - +" / " - +shares - +"\n" - ); - */ - return this.money<=se.lastprice && this.shares<=0; - } - - /* - public Order_old sell(long volume, double limit) { - SellOrder o = new SellOrder(); - o.account = this; - o.limit = limit; - o.volume = volume; - orderpending = true; - return se.SendOrder(o); - } -*/ - /* public Order_old buy(long volume, double limit) { - if (volume * limit > money) { - return null; - } - BuyOrder o = new BuyOrder(); - o.limit = limit; - o.volume = volume; - o.account = this; - orderpending = true; - return se.SendOrder(o); - } -/* - /* - public void Buy(Account_old a, long size, double price) { - shares += size; - money -= price * size; - a.shares -= size; - a.money += price * size; - } -*/ - -} diff --git a/src/main/java/sesim/AutoTraderLIst.java b/src/main/java/sesim/AutoTraderList.java similarity index 83% rename from src/main/java/sesim/AutoTraderLIst.java rename to src/main/java/sesim/AutoTraderList.java index 82cfa3f..29094f0 100644 --- a/src/main/java/sesim/AutoTraderLIst.java +++ b/src/main/java/sesim/AutoTraderList.java @@ -25,11 +25,26 @@ */ package sesim; +import java.util.SortedSet; +import java.util.List; +import java.util.ArrayList; + /** * * @author 7u83 <7u83@mail.ru> */ -public class AutoTraderLIst { +public class AutoTraderList { + + SortedSet traders; - + AutoTraderList(){ + + } + + List getTraders(){ + + + return new ArrayList(); + } + } diff --git a/src/main/java/sesim/BuyOrder.java b/src/main/java/sesim/BuyOrder.java deleted file mode 100644 index 68f0f81..0000000 --- a/src/main/java/sesim/BuyOrder.java +++ /dev/null @@ -1,9 +0,0 @@ -package sesim; - -public class BuyOrder extends Order_old implements Comparable { - - public BuyOrder(){ - super(OrderType_old.bid); - } - -} diff --git a/src/main/java/sesim/Exchange.java b/src/main/java/sesim/Exchange.java index 308a094..c3e7c2b 100644 --- a/src/main/java/sesim/Exchange.java +++ b/src/main/java/sesim/Exchange.java @@ -2,10 +2,8 @@ package sesim; import java.util.*; import java.util.concurrent.ConcurrentHashMap; - import java.util.concurrent.CopyOnWriteArrayList; -import sesim.Order_old.OrderType_old; /** * @@ -168,9 +166,7 @@ public class Exchange { //extends Thread { * Constructor */ public Exchange() { - this.ask = new TreeSet<>(); - this.bid = new TreeSet<>(); - // this.qrlist = new ArrayList<>(); + this.qrlist = (new CopyOnWriteArrayList<>()); // Create order books @@ -316,23 +312,12 @@ public class Exchange { //extends Thread { double lastprice = 100.0; long lastsvolume; - public TreeSet bid; - public TreeSet ask; + private final Locker tradelock = new Locker(); - private TreeSet selectOrderBook(OrderType_old t) { - - switch (t) { - case bid: - return this.bid; - case ask: - return this.ask; - } - return null; - - } + public ArrayList getOrderBook(OrderType type, int depth) { @@ -356,43 +341,9 @@ public class Exchange { //extends Thread { return this.quoteHistory.first(); } - public void print_current() { - - Order_old b; - Order_old a; - - //String BID; - if (bid.isEmpty()) { - b = new BuyOrder(); - b.limit = -1; - b.volume = 0; - } else { - b = bid.first(); - } - - if (ask.isEmpty()) { - a = new SellOrder(); - a.limit = -1; - a.volume = 0; - - } else { - a = ask.first(); - } - - Logger.info(String.format("BID: %s(%s) LAST: %.2f(%d) ASK: %s(%s)\n", - b.format_limit(), b.format_volume(), - lastprice, lastsvolume, - a.format_limit(), a.format_volume()) - ); - - } - - public void transferMoney(Account_old src, Account_old dst, double money) { - src.money -= money; - dst.money += money; - - } + + private void transferMoneyAndShares(Account src, Account dst, double money, double shares) { src.money -= money; dst.money += money; @@ -431,33 +382,9 @@ public class Exchange { //extends Thread { * * @param o */ - public void cancelOrder_old(Order_old o) { - tradelock.lock(); - TreeSet book = this.selectOrderBook(o.type); - book.remove(o); - /* this.updateBookReceivers(o.type); - o.account.pending.remove(o); - o.status = OrderStatus.canceled; - tradelock.unlock(); - */ - - } - - /** - * Transfer shares from one account to another account - * - * @param src source account - * @param dst destination account - * @param volumen number of shares - * @param price price - */ - protected void transferShares(Account_old src, Account_old dst, long volume, double price) { - dst.shares += volume; - src.shares -= volume; - dst.money -= price * volume; - src.money += price * volume; - } + + long nextQuoteId = 0; private void removeOrderIfExecuted(Order o) { diff --git a/src/main/java/sesim/Locker.java b/src/main/java/sesim/Locker.java index bf59db3..22799e0 100644 --- a/src/main/java/sesim/Locker.java +++ b/src/main/java/sesim/Locker.java @@ -27,7 +27,9 @@ package sesim; import java.util.concurrent.Semaphore; + /** + * A locker object * * @author 7u83 <7u83@mail.ru> */ @@ -37,6 +39,7 @@ public class Locker { /** * Acquire a lock + * * @return */ public boolean lock() { @@ -48,6 +51,9 @@ public class Locker { return true; } + /** + * Release a lock + */ public void unlock() { AVAIL.release(); } diff --git a/src/main/java/sesim/Order_old.java b/src/main/java/sesim/Order_old.java deleted file mode 100644 index a137ea3..0000000 --- a/src/main/java/sesim/Order_old.java +++ /dev/null @@ -1,105 +0,0 @@ -package sesim; - -public abstract class Order_old implements Comparable { - - /** - * When the order was created - */ - public long timestamp = 0; - - /** - * Number of shares - */ - public long volume; - - /** - * Limit price - */ - public double limit; - - /** - * Order_old ID - */ - public long id = 0; - - /** - * Type of order - */ - public final OrderType_old type; - - public Account_old account = null; - - - protected int compareLimit(Order_old o){ - - int r=0; - if (o.limit < limit) { - r=-1; - } - if (o.limit > limit) { - r=1; - } - - if (type==OrderType_old.ask) - return -r; - - return r; - - }; - - @Override - public int compareTo(Order_old o) { - - if (o.type!=type){ - System.out.print("OrderType Missmatch\n"); - return -1; - } - - int r = compareLimit(o); - if (r!=0) - return r; - - - if (o.id>id) - return -1; - - if (o.id + * 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 sesim; + +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.concurrent.ConcurrentSkipListSet; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ +public class Scheduler extends Thread { + + long multiply = 1; + final SortedMap> event_queue = new TreeMap<>(); + + private boolean stop = false; + + public class Runner extends Thread { + + Runner() { + + } + + @Override + public void run() { + while (!stop) { + synchronized (event_queue) { + try { + event_queue.wait(5000); + } catch (Exception e) { + + } + System.out.printf("500 mllies waited\n"); + } + + } + + } + + } + + private class Event implements Comparable { + + long time; + + @Override + public int compareTo(Object o) { + if (((Event) o).time < time) { + return -1; + } + if (((Event) o).time > time) { + return 1; + } + return 0; + + } + + } + + // Runner queues[]; + Runner runners[]; + + /** + * + * @param num_runners + */ + public Scheduler(int num_runners) { + // event_queue = new ConcurrentSkipListSet(new TreeSet<>()); + runners = new Runner[num_runners]; + + for (int i = 0; i < num_runners; i++) { + Runner r = new Runner(); + System.out.print("Making runner: " + r + "\n"); + + runners[i] = new Runner(); + } + + } + + /** + * + */ + @Override + public void start() { + + /* for (Runner runner : runners) { + runner.start(); + } + */ + super.start(); + } + + /* + public boolean isAlive() { + + for (Runner runner : runners) { + if (runner.isAlive()) { + return true; + } + } + return false; + } + */ + public void halt() { + stop = true; + synchronized (event_queue) { + event_queue.notifyAll(); + } + } + + public interface TimerEvent { + + long timerEvent(); + + } + + class ObjectComparator implements Comparator { + + @Override + public int compare(Object o1, Object o2) { + return System.identityHashCode(o1) - System.identityHashCode(o1); + } + + } + + public long getCurrentTimeMillies() { + return System.currentTimeMillis(); + + } + + public void startEvent(TimerEvent e, long time) { + long evtime = time + this.getCurrentTimeMillies(); + synchronized (event_queue) { + SortedSet s = event_queue.get(evtime); + if (s == null) { + s = new TreeSet<>(new ObjectComparator()); + event_queue.put(evtime, s); + } + s.add(e); + } + synchronized(this){ + notify(); + } + } + + public long fireEvent(TimerEvent e) { + return e.timerEvent(); + } + + private void addEvent(TimerEvent e, long time) { + long evtime = time + this.getCurrentTimeMillies(); + SortedSet s = event_queue.get(evtime); + if (s == null) { + s = new TreeSet<>(new ObjectComparator()); + event_queue.put(evtime, s); + } + s.add(e); + } + + public long runEvents() { + synchronized (event_queue) { + if (event_queue.isEmpty()) { + return -1; + } + + long t = event_queue.firstKey(); + if (t >= this.getCurrentTimeMillies()) { + SortedSet s = event_queue.get(t); + event_queue.remove(t); + Iterator it = s.iterator(); + while (it.hasNext()) { + long next_t = this.fireEvent(it.next()); + + } + + } + } + return 1; + } + + @Override + public void run() { + while (!stop) { + long wtime = runEvents(); + + synchronized (this) { + try { + if (wtime != -1) { + wait(wtime); + } else { + wait(); + } + } catch (Exception e) { + } + } + } + System.out.print("Running the scheduler\n"); + } + +} diff --git a/src/main/java/sesim/SellOrder.java b/src/main/java/sesim/SellOrder.java deleted file mode 100644 index b4f7a70..0000000 --- a/src/main/java/sesim/SellOrder.java +++ /dev/null @@ -1,8 +0,0 @@ -package sesim; - -public class SellOrder extends Order_old { - - public SellOrder(){ - super(OrderType_old.ask); - } -} diff --git a/src/test/java/sesim/Test.java b/src/test/java/sesim/Test.java index 41449d6..d992b65 100644 --- a/src/test/java/sesim/Test.java +++ b/src/test/java/sesim/Test.java @@ -25,6 +25,9 @@ */ package sesim; +import java.util.SortedMap; +import java.util.TreeMap; + @@ -63,46 +66,48 @@ public class Test { * @param args the command line arguments */ public static void main(String[] args) { - Exchange se = new Exchange(); + + Scheduler s = new Scheduler(10); + s.start(); + + class Ev implements Scheduler.TimerEvent{ - double aid1 = se.createAccount(100, 100); - double aid2 = se.createAccount(100, 100); + @Override + public long timerEvent() { + System.out.print("Timer Event Occured"); + return 3000; + } + + } + + Ev e1 = new Ev(); + Ev e2 = new Ev(); - sesim.Exchange.Account a = se.getAccount(aid1); - System.out.print(a.getMoney()); + s.startEvent(e1, 0); - tube(); + while(true){} + // s.startEvent(e2, 100); + + + + + /* long starttime=System.currentTimeMillis(); + while (s.isAlive()){ + if (System.currentTimeMillis()>starttime+6650){ + s.stop(); + break; + } + } + System.out.print("Waiting fpor Stop\n"); + while (s.isAlive()){ + + } + + System.out.print("All isstopped\n"); + */ + - System.exit(0); - - /* - - AccountData a1 = se.getAccountData(aid1); - AccountData a2 = se.getAccountData(aid2); - Test.print_account(a1); - Test.print_account(a2); - - se.createOrder(aid2, Exchange.OrderType.ASK, 20, 11); - se.createOrder(aid2, Exchange.OrderType.ASK, 10, 10); - se.createOrder(aid2, Exchange.OrderType.ASK, 10, 9); - se.createOrder(aid1, Exchange.OrderType.BID, 50, 11); - - System.out.print("Exec Orders\n"); - se.executeOrders(); - System.out.print("Executed Orders\n"); - - a1 = se.getAccountData(aid1); - a2 = se.getAccountData(aid2); - Test.print_account(a1); - Test.print_account(a2); - - - - - //S/ystem.out.print(aid); - //System.out.print("\n"); -*/ } }