diff --git a/src/main/java/gui/AskBook.java b/src/main/java/gui/AskBook.java index 4d2a6a8..92fcbdc 100644 --- a/src/main/java/gui/AskBook.java +++ b/src/main/java/gui/AskBook.java @@ -25,7 +25,7 @@ */ package gui; -import sesim.Order.*; +import sesim.Order_old.*; import java.util.ArrayList; /** @@ -36,7 +36,7 @@ public class AskBook extends OrderBook { @Override ArrayList getOrderBook() { - return MainWin.se.getOrderBook(OrderType.ask,40); + return MainWin.se.getOrderBook(OrderType_old.ask,40); } @Override @@ -48,7 +48,7 @@ public class AskBook extends OrderBook { if (MainWin.se == null) { return; } - MainWin.se.addBookReceiver(OrderType.ask, this); + MainWin.se.addBookReceiver(OrderType_old.ask, this); } diff --git a/src/main/java/gui/BidBook.java b/src/main/java/gui/BidBook.java index c05b35c..ee7b259 100644 --- a/src/main/java/gui/BidBook.java +++ b/src/main/java/gui/BidBook.java @@ -25,7 +25,7 @@ */ package gui; -import sesim.Order.*; +import sesim.Order_old.*; import java.util.ArrayList; /** @@ -36,13 +36,13 @@ public class BidBook extends OrderBook { @Override ArrayList getOrderBook() { - return MainWin.se.getOrderBook(OrderType.bid, 40); + return MainWin.se.getOrderBook(OrderType_old.bid, 40); } public BidBook() { if (MainWin.se == null) { return; } - MainWin.se.addBookReceiver(OrderType.bid, this); + MainWin.se.addBookReceiver(OrderType_old.bid, this); } } diff --git a/src/main/java/gui/MainWin.java b/src/main/java/gui/MainWin.java index cdd90b6..8cd29cf 100644 --- a/src/main/java/gui/MainWin.java +++ b/src/main/java/gui/MainWin.java @@ -27,7 +27,7 @@ package gui; import traders.RandomTraderConfig; import traders.SwitchingTraderConfig; -import sesim.Account; +import sesim.Account_old; import sesim.AutoTraderLIst; import sesim.Exchange; import sesim.BuyOrder; @@ -42,7 +42,7 @@ import javax.swing.*; public class MainWin extends javax.swing.JFrame { static public sesim.Exchange se; - static sesim.Account myAccount; + static sesim.Account_old myAccount; static traders.ManTrader myTrader; /** @@ -158,17 +158,17 @@ public class MainWin extends javax.swing.JFrame { public static void main(String args[]) { se = new Exchange(); - myAccount = new Account(se,1000,100000000.0); + myAccount = new Account_old(se,1000,100000000.0); myTrader = new traders.ManTrader(myAccount,null); -/* Account otherAccount = new Account(se,1000,1000); +/* Account_old otherAccount = new Account_old(se,1000,1000); Traders.ManTrader otherTrader = new Traders.ManTrader(otherAccount); otherTrader.sell(80, 22.70); */ /* - Account traccount = new Account(se,5500,1000000.0); + Account_old traccount = new Account_old(se,5500,1000000.0); RandomTrader rt = new RandomTrader(traccount,null); TraderRunner tr = new TraderRunner(rt); tr.start(); diff --git a/src/main/java/gui/OrderBook.java b/src/main/java/gui/OrderBook.java index 6bae44c..15a9405 100644 --- a/src/main/java/gui/OrderBook.java +++ b/src/main/java/gui/OrderBook.java @@ -145,15 +145,15 @@ public abstract class OrderBook extends javax.swing.JPanel implements Exchange.B @Override public Object getValueAt(int r, int c) { - sesim.Order o; + sesim.Order_old o; int s = list.size(); //System.out.print("Looking for Value at" + r + ":" + c + " w size:" + s + "\n"); if (!getDesc()) { - o = (sesim.Order) list.get(r); + o = (sesim.Order_old) list.get(r); } else { - o = (sesim.Order) list.get(list.size() - r - 1); + o = (sesim.Order_old) list.get(list.size() - r - 1); } Formatter f = new Formatter(); switch (c) { diff --git a/src/main/java/sesim/AccountData.java b/src/main/java/sesim/AccountData.java new file mode 100644 index 0000000..7b069a9 --- /dev/null +++ b/src/main/java/sesim/AccountData.java @@ -0,0 +1,44 @@ +/* + * 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 sesim; + +import sesim.Exchange.OrderComparator; + +import java.util.*; +import sesim.Exchange.*; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ +public class AccountData { + public double id; + public double money; + public double shares; + + public ArrayList orders; + +} diff --git a/src/main/java/sesim/Account.java b/src/main/java/sesim/Account_old.java similarity index 81% rename from src/main/java/sesim/Account.java rename to src/main/java/sesim/Account_old.java index 332c2e7..f69dc46 100644 --- a/src/main/java/sesim/Account.java +++ b/src/main/java/sesim/Account_old.java @@ -3,7 +3,7 @@ package sesim; import java.util.*; -final public class Account { +final public class Account_old { /** * Exchange this account belongs to @@ -26,19 +26,19 @@ final public class Account { public String name = ""; - public ArrayList pending; + public ArrayList pending; public boolean orderpending = false; - public Account(Exchange se, long shares, double money ) { + public Account_old(Exchange se, long shares, double money ) { this.shares=shares; this.money=money; this.se=se; pending = new ArrayList<>(); } - public Account(){ + public Account_old(){ //this(,0.0); } @@ -57,7 +57,7 @@ final public class Account { /* System.out.print( - "Account: " + "Account_old: " +money +" / " +shares @@ -67,7 +67,7 @@ final public class Account { return this.money<=se.lastprice && this.shares<=0; } - public Order sell(long volume, double limit) { + public Order_old sell(long volume, double limit) { SellOrder o = new SellOrder(); o.account = this; o.limit = limit; @@ -76,7 +76,7 @@ final public class Account { return se.SendOrder(o); } - public Order buy(long volume, double limit) { + public Order_old buy(long volume, double limit) { if (volume * limit > money) { return null; } @@ -89,7 +89,7 @@ final public class Account { } /* - public void Buy(Account a, long size, double price) { + public void Buy(Account_old a, long size, double price) { shares += size; money -= price * size; a.shares -= size; diff --git a/src/main/java/sesim/AutoTrader.java b/src/main/java/sesim/AutoTrader.java index f5b2f7d..7dc7b84 100644 --- a/src/main/java/sesim/AutoTrader.java +++ b/src/main/java/sesim/AutoTrader.java @@ -25,6 +25,7 @@ */ package sesim; +import static java.lang.Thread.sleep; import static java.lang.Thread.sleep; /** @@ -33,7 +34,7 @@ import static java.lang.Thread.sleep; */ public abstract class AutoTrader extends Trader implements Runnable { - public AutoTrader(Account account, TraderConfig config) { + public AutoTrader(Account_old account, TraderConfig config) { super(account, config); } diff --git a/src/main/java/sesim/BuyOrder.java b/src/main/java/sesim/BuyOrder.java index d4a5e86..68f0f81 100644 --- a/src/main/java/sesim/BuyOrder.java +++ b/src/main/java/sesim/BuyOrder.java @@ -1,9 +1,9 @@ package sesim; -public class BuyOrder extends Order implements Comparable { +public class BuyOrder extends Order_old implements Comparable { public BuyOrder(){ - super(OrderType.bid); + super(OrderType_old.bid); } } diff --git a/src/main/java/sesim/Exchange.java b/src/main/java/sesim/Exchange.java index c8e921e..00a3bbf 100644 --- a/src/main/java/sesim/Exchange.java +++ b/src/main/java/sesim/Exchange.java @@ -3,8 +3,8 @@ package sesim; import java.util.*; import java.util.concurrent.*; -import sesim.Order.OrderStatus; -import sesim.Order.OrderType; +import sesim.Order_old.OrderStatus; +import sesim.Order_old.OrderType_old; /** * @@ -12,36 +12,98 @@ import sesim.Order.OrderType; */ public class Exchange extends Thread { - private class iAccount implements Comparable { + public enum OrderType { + BID, ASK + } + + IDGenerator account_id = new IDGenerator(); + + private class Account implements Comparable { protected double id; protected double shares; protected double money; + protected HashMap orders; @Override public int compareTo(Object a) { - iAccount account = (iAccount) a; + Account account = (Account) a; return this.id - account.id < 0 ? -1 : 1; } - iAccount(double money, double shares) { - NEXT_ACCOUNT_ID_LOCKER.lock(); - id = (Math.random() + (next_account_id++)); - NEXT_ACCOUNT_ID_LOCKER.unlock(); - + Account(double money, double shares) { + id = (Math.random() + (account_id.getNext())); + orders = new HashMap(); } } - private static int next_account_id; - private static final Locker NEXT_ACCOUNT_ID_LOCKER = new Locker(); - TreeSet accounts; + //private TreeSet accounts = new TreeSet<>(); + HashMap accounts = new HashMap<>(); - double createAccount(double money, double shares) { - iAccount a = new iAccount(money, shares); - accounts.add(a); + public double createAccount(double money, double shares) { + Account a = new Account(money, shares); + accounts.put(a.id, a); return a.id; } + class OrderComparator implements Comparator { + + OrderType type; + + OrderComparator(OrderType type) { + this.type = type; + } + + @Override + public int compare(Order left, Order right) { + double d; + switch (this.type) { + case BID: + d = right.limit - left.limit; + break; + case ASK: + d = left.limit - right.limit; + break; + default: + d = 0; + + } + if (d != 0) { + return d > 0 ? 1 : -1; + } + + return left.id < right.id ? -1 : 1; + } + + } + + //TreeSet bid_bbook = new TreeSet <> (new OrderComperator(OrderType.BID) ); + //TreeSet ask_dbook = new TreeSet <> (new OrderComperator(OrderType.BID) ); + HashMap> order_books = new HashMap(); + + IDGenerator order_id = new IDGenerator(); + + private class Order { + + OrderType type; + double limit; + double volume; + double initial_volume; + long id; + long created; + Account account; + + Order(Account account,OrderType type, double volume, double limit) { + id = order_id.getNext(); + this.account=account; + this.type = type; + this.limit = limit; + this.volume = volume; + this.initial_volume = volume; + this.created = System.currentTimeMillis(); + } + } + /** * Histrory of quotes */ @@ -55,12 +117,20 @@ public class Exchange extends Thread { this.bid = new TreeSet<>(); this.qrlist = new ArrayList<>(); + // Create order books + for (OrderType type : OrderType.values()) { + order_books.put(type, new TreeSet(new OrderComparator(type))); + } + } - class BidBook extends TreeSet{ + + class BidBook extends TreeSet { + TreeSet t = new TreeSet(); - boolean hallo(){ + + boolean hallo() { t.comparator(); return true; } @@ -112,7 +182,7 @@ public class Exchange extends Thread { final private ArrayList ask_bookreceivers = new ArrayList<>(); final private ArrayList bid_bookreceivers = new ArrayList<>(); - private ArrayList selectBookReceiver(OrderType t) { + private ArrayList selectBookReceiver(OrderType_old t) { switch (t) { case ask: return ask_bookreceivers; @@ -122,13 +192,13 @@ public class Exchange extends Thread { return null; } - public void addBookReceiver(OrderType t, BookReceiver br) { + public void addBookReceiver(OrderType_old t, BookReceiver br) { ArrayList bookreceivers; bookreceivers = selectBookReceiver(t); bookreceivers.add(br); } - void updateBookReceivers(OrderType t) { + void updateBookReceivers(OrderType_old t) { ArrayList bookreceivers; bookreceivers = selectBookReceiver(t); @@ -170,8 +240,8 @@ public class Exchange extends Thread { double lastprice = 100.0; long lastsvolume; - public TreeSet bid; - public TreeSet ask; + public TreeSet bid; + public TreeSet ask; private Locker tradelock = new Locker(); @@ -191,7 +261,7 @@ public class Exchange extends Thread { available.release(); } */ - private TreeSet selectOrderBook(OrderType t) { + private TreeSet selectOrderBook(OrderType_old t) { switch (t) { case bid: @@ -203,20 +273,20 @@ public class Exchange extends Thread { } - public ArrayList getOrderBook(OrderType t, int depth) { + public ArrayList getOrderBook(OrderType_old t, int depth) { - TreeSet book = selectOrderBook(t); + TreeSet book = selectOrderBook(t); if (book == null) { return null; } - ArrayList ret = new ArrayList<>(); - Iterator it = book.iterator(); + ArrayList ret = new ArrayList<>(); + Iterator it = book.iterator(); for (int i = 0; i < depth && it.hasNext(); i++) { - Order o; + Order_old o; o = it.next(); ret.add(o); - //System.out.print("Order" + o.limit); + //System.out.print("Order_old" + o.limit); //System.out.println(); } return ret; @@ -225,10 +295,10 @@ public class Exchange extends Thread { public void print_current() { - Order b; - Order a; + Order_old b; + Order_old a; - //String bid; + //String BID; if (bid.isEmpty()) { b = new BuyOrder(); b.limit = -1; @@ -254,13 +324,13 @@ public class Exchange extends Thread { } - public void transferMoney(Account src, Account dst, double money) { + public void transferMoney(Account_old src, Account_old dst, double money) { src.money -= money; dst.money += money; } - protected void transferMoneyAndShares(Account src, Account dst, double money, long shares) { + private void transferMoneyAndShares(Account src, Account dst, double money, double shares) { src.money -= money; dst.money += money; src.shares -= shares; @@ -271,9 +341,9 @@ public class Exchange extends Thread { * * @param o */ - public void cancelOrder(Order o) { + public void cancelOrder(Order_old o) { tradelock.lock(); - TreeSet book = this.selectOrderBook(o.type); + TreeSet book = this.selectOrderBook(o.type); book.remove(o); this.updateBookReceivers(o.type); o.account.pending.remove(o); @@ -281,6 +351,8 @@ public class Exchange extends Thread { tradelock.unlock(); } + + /** * Transfer shares from one account to another account @@ -290,7 +362,7 @@ public class Exchange extends Thread { * @param volumen number of shares * @param price price */ - protected void transferShares(Account src, Account dst, long volume, double price) { + protected void transferShares(Account_old src, Account_old dst, long volume, double price) { dst.shares += volume; src.shares -= volume; dst.money -= price * volume; @@ -302,25 +374,35 @@ public class Exchange extends Thread { /** * */ - protected void executeOrders() { + public void executeOrders() { + TreeSet bid=order_books.get(OrderType.BID); + TreeSet ask=order_books.get(OrderType.ASK); + while (!bid.isEmpty() && !ask.isEmpty()) { Order b = bid.first(); Order a = ask.first(); - + if (b.limit < a.limit) { + System.out.print("No match\n"); // no match, nothing to do return; } // There is a match, calculate price and volume double price = b.id < a.id ? b.limit : a.limit; - long volume = b.volume >= a.volume ? a.volume : b.volume; + double volume = b.volume >= a.volume ? a.volume : b.volume; - transferMoneyAndShares(b.account, a.account, volume * price, -volume); + transferMoneyAndShares(b.account, a.account, volume * price, -volume); + + System.out.print(price+","+volume); + + + System.exit(0); - if (a.volume == 0) { + +/* if (a.volume == 0) { // This order is fully executed, remove a.account.orderpending = false; a.status = OrderStatus.executed; @@ -328,7 +410,7 @@ public class Exchange extends Thread { a.account.pending.remove(a); ask.pollFirst(); - this.updateBookReceivers(OrderType.ask); + this.updateBookReceivers(OrderType_old.ask); continue; } @@ -338,10 +420,10 @@ public class Exchange extends Thread { b.status = OrderStatus.executed; b.account.pending.remove(b); bid.pollFirst(); - this.updateBookReceivers(OrderType.bid); + this.updateBookReceivers(OrderType_old.bid); continue; } - +*/ if (b.limit >= a.limit) { price = b.id < a.id ? b.limit : a.limit; @@ -358,18 +440,18 @@ public class Exchange extends Thread { volume = b.volume; } - transferShares(a.account, b.account, volume, price); +// transferShares(a.account, b.account, volume, price); // b.account.Buy(a.account, volume, price); b.volume -= volume; a.volume -= volume; lastprice = price; - lastsvolume = volume; +// lastsvolume = volume; Quote q = new Quote(); - q.volume = volume; +// q.volume = volume; q.price = price; q.time = System.currentTimeMillis(); @@ -378,8 +460,8 @@ public class Exchange extends Thread { q.id = nextQuoteId++; this.updateQuoteReceivers(q); - this.updateBookReceivers(OrderType.bid); - this.updateBookReceivers(OrderType.ask); + this.updateBookReceivers(OrderType_old.bid); + this.updateBookReceivers(OrderType_old.ask); quoteHistory.add(q); continue; @@ -390,12 +472,12 @@ public class Exchange extends Thread { } } - private void executeOrdersX() { + private void executeOrders_old() { while (!bid.isEmpty() && !ask.isEmpty()) { - Order b = bid.first(); - Order a = ask.first(); + Order_old b = bid.first(); + Order_old a = ask.first(); if (b.limit < a.limit) { // no match, nothing to do @@ -410,7 +492,7 @@ public class Exchange extends Thread { a.account.pending.remove(a); ask.pollFirst(); - this.updateBookReceivers(OrderType.ask); + this.updateBookReceivers(OrderType_old.ask); continue; } @@ -420,7 +502,7 @@ public class Exchange extends Thread { b.status = OrderStatus.executed; b.account.pending.remove(b); bid.pollFirst(); - this.updateBookReceivers(OrderType.bid); + this.updateBookReceivers(OrderType_old.bid); continue; } @@ -463,8 +545,8 @@ public class Exchange extends Thread { q.id = nextQuoteId++; this.updateQuoteReceivers(q); - this.updateBookReceivers(OrderType.bid); - this.updateBookReceivers(OrderType.ask); + this.updateBookReceivers(OrderType_old.bid); + this.updateBookReceivers(OrderType_old.ask); /* System.out.print( "Executed: " @@ -484,27 +566,27 @@ public class Exchange extends Thread { } public void ExecuteOrder(BuyOrder o) { - // SellOrder op = ask.peek(); + // SellOrder op = ASK.peek(); } - private boolean InitOrder(Order o) { + private boolean InitOrder(Order_old o) { double moneyNeeded = o.volume * o.limit; return true; } // Add an order to the orderbook - private boolean addOrder(Order o) { + private boolean addOrder(Order_old o) { boolean ret = false; switch (o.type) { case bid: -// System.out.print("Exchange adding bid order \n"); +// System.out.print("Exchange adding BID order \n"); ret = bid.add(o); break; case ask: -// System.out.print("Exchange adding ask order \n"); +// System.out.print("Exchange adding ASK order \n"); ret = ask.add(o); break; } @@ -515,7 +597,7 @@ public class Exchange extends Thread { return ret; } - public Order SendOrder(Order o) { + public Order_old SendOrder(Order_old o) { boolean rc = InitOrder(o); if (!rc) { @@ -527,34 +609,65 @@ public class Exchange extends Thread { o.id = orderid++; addOrder(o); o.account.pending.add(o); - executeOrders(); + executeOrders_old(); tradelock.unlock(); return o; } + + private void addOrderToBook(Order o){ + order_books.get(o.type).add(o); + + } + + /** + * + * @param account_id + * @param type + * @param volume + * @param limit + * @return + */ + public long createOrder(double account_id, OrderType type, double volume, double limit) { + + Account a = accounts.get(account_id); + if (a == null) { + return -1; + } + + Order o = new Order(a,type, volume, limit); + addOrderToBook(o); + a.orders.put(o.id,o); + + return o.id; + } + + + + /* public void SendOrder(BuyOrder o) { //System.out.println("EX Buyorder"); Lock(); o.timestamp = System.currentTimeMillis(); o.id = orderid++; - bid.add(o); + BID.add(o); Unlock(); Lock(); -// executeOrders(); +// executeOrders_old(); Unlock(); } */ /* - * public void SendOrder(Order o){ + * public void SendOrder(Order_old o){ * * - * if ( o.getClass() == BuyOrder.class){ bid.add((BuyOrder)o); } + * if ( o.getClass() == BuyOrder.class){ BID.add((BuyOrder)o); } * - * if ( o.getClass() == SellOrder.class){ ask.add((SellOrder)o); } + * if ( o.getClass() == SellOrder.class){ ASK.add((SellOrder)o); } * * } */ @@ -570,7 +683,7 @@ public class Exchange extends Thread { return lastprice; } - /* public double sendOrder(Account o) { + /* public double sendOrder(Account_old o) { return 0.7; } */ diff --git a/src/main/java/sesim/IDGenerator.java b/src/main/java/sesim/IDGenerator.java new file mode 100644 index 0000000..b79b142 --- /dev/null +++ b/src/main/java/sesim/IDGenerator.java @@ -0,0 +1,52 @@ +/* + * 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 sesim; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ + class IDGenerator{ + private final Locker ID_LOCKER = new Locker(); + private long next_id; + + + public IDGenerator(long start){ + next_id=start; + } + + public IDGenerator(){ + this(0); + } + + public long getNext(){ + ID_LOCKER.lock(); + long id = next_id++; + ID_LOCKER.unlock(); + return id; + } + } + \ No newline at end of file diff --git a/src/main/java/sesim/OrderData.java b/src/main/java/sesim/OrderData.java new file mode 100644 index 0000000..bbde878 --- /dev/null +++ b/src/main/java/sesim/OrderData.java @@ -0,0 +1,34 @@ +/* + * 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 sesim; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ +public class OrderData { + +} diff --git a/src/main/java/sesim/Order.java b/src/main/java/sesim/Order_old.java similarity index 80% rename from src/main/java/sesim/Order.java rename to src/main/java/sesim/Order_old.java index 2051901..a137ea3 100644 --- a/src/main/java/sesim/Order.java +++ b/src/main/java/sesim/Order_old.java @@ -1,6 +1,6 @@ package sesim; -public abstract class Order implements Comparable { +public abstract class Order_old implements Comparable { /** * When the order was created @@ -18,19 +18,19 @@ public abstract class Order implements Comparable { public double limit; /** - * Order ID + * Order_old ID */ public long id = 0; /** * Type of order */ - public final OrderType type; + public final OrderType_old type; - public Account account = null; + public Account_old account = null; - protected int compareLimit(Order o){ + protected int compareLimit(Order_old o){ int r=0; if (o.limit < limit) { @@ -40,7 +40,7 @@ public abstract class Order implements Comparable { r=1; } - if (type==OrderType.ask) + if (type==OrderType_old.ask) return -r; return r; @@ -48,7 +48,7 @@ public abstract class Order implements Comparable { }; @Override - public int compareTo(Order o) { + public int compareTo(Order_old o) { if (o.type!=type){ System.out.print("OrderType Missmatch\n"); @@ -75,7 +75,7 @@ public abstract class Order implements Comparable { open, executed, canceled } - public enum OrderType { + public enum OrderType_old { bid,ask } @@ -99,7 +99,7 @@ public abstract class Order implements Comparable { return String.format("%d", volume); } - Order(OrderType type) { + Order_old(OrderType_old type) { this.type=type; } } diff --git a/src/main/java/sesim/SellOrder.java b/src/main/java/sesim/SellOrder.java index b6b72fe..b4f7a70 100644 --- a/src/main/java/sesim/SellOrder.java +++ b/src/main/java/sesim/SellOrder.java @@ -1,8 +1,8 @@ package sesim; -public class SellOrder extends Order { +public class SellOrder extends Order_old { public SellOrder(){ - super(OrderType.ask); + super(OrderType_old.ask); } } diff --git a/src/main/java/sesim/Trader.java b/src/main/java/sesim/Trader.java index 9816941..23ec0ef 100644 --- a/src/main/java/sesim/Trader.java +++ b/src/main/java/sesim/Trader.java @@ -25,6 +25,9 @@ */ package sesim; +import static java.lang.Thread.sleep; +import static java.lang.Thread.sleep; +import static java.lang.Thread.sleep; import static java.lang.Thread.sleep; public abstract class Trader { @@ -32,7 +35,7 @@ public abstract class Trader { public String name = null; - public Account account; + public Account_old account; public TraderConfig config; public void sell(long shares, double limit){ @@ -45,10 +48,10 @@ public abstract class Trader { /** * Construct a Trader object - * @param account Account for this trader + * @param account Account_old for this trader * @param config Configration for this trader */ - public Trader(Account account, TraderConfig config){ + public Trader(Account_old account, TraderConfig config){ this.account=account; this.config=config; } @@ -58,7 +61,7 @@ public abstract class Trader { * The Trader object shoul initialize a default config * @param account */ - public Trader(Account account){ + public Trader(Account_old account){ this(account,null); } diff --git a/src/main/java/traders/ManTrader.java b/src/main/java/traders/ManTrader.java index 3113e77..5edf62e 100644 --- a/src/main/java/traders/ManTrader.java +++ b/src/main/java/traders/ManTrader.java @@ -25,7 +25,7 @@ */ package traders; -import sesim.Account; +import sesim.Account_old; import sesim.Trader; import sesim.TraderConfig; import sesim.BuyOrder; @@ -39,7 +39,7 @@ import sesim.BuyOrder; */ public class ManTrader extends Trader{ - public ManTrader(Account account,TraderConfig config) { + public ManTrader(Account_old account,TraderConfig config) { super(account,config); } diff --git a/src/main/java/traders/RandomTrader.java b/src/main/java/traders/RandomTrader.java index 7db13f8..e66e2c9 100644 --- a/src/main/java/traders/RandomTrader.java +++ b/src/main/java/traders/RandomTrader.java @@ -25,8 +25,8 @@ */ package traders; -import sesim.Account; -import sesim.Order; +import sesim.Account_old; +import sesim.Order_old; import java.util.Random; import sesim.AutoTrader; import sesim.TraderConfig; @@ -43,7 +43,7 @@ public class RandomTrader extends AutoTrader { // object to generate random numbers final private Random rand = new Random(); - public RandomTrader(Account account, TraderConfig config) { + public RandomTrader(Account_old account, TraderConfig config) { super(account, config); if (config == null) { config = new RandomTraderConfig(); @@ -86,7 +86,7 @@ public class RandomTrader extends AutoTrader { } if (account.pending.size() != 0) { - Order o = account.pending.get(0); + Order_old o = account.pending.get(0); account.se.cancelOrder(o); return false; } @@ -133,7 +133,7 @@ public class RandomTrader extends AutoTrader { return false; } - Order o = account.pending.get(0); + Order_old o = account.pending.get(0); long age = o.getAge(); // System.out.print("RT: age is: "+age+"\n"); diff --git a/src/main/java/traders/RandomTraderConfig.java b/src/main/java/traders/RandomTraderConfig.java index d582ecc..4c26598 100644 --- a/src/main/java/traders/RandomTraderConfig.java +++ b/src/main/java/traders/RandomTraderConfig.java @@ -25,7 +25,7 @@ */ package traders; -import sesim.Account; +import sesim.Account_old; import sesim.TraderConfig; import sesim.Exchange; import sesim.AutoTrader; @@ -64,7 +64,7 @@ public class RandomTraderConfig extends TraderConfig { @Override public AutoTrader createTrader(Exchange se, long shares, double money) { - Account a = new Account(se, shares, money); + Account_old a = new Account_old(se, shares, money); return new RandomTrader(a, this); } diff --git a/src/main/java/traders/SwitchingTrader.java b/src/main/java/traders/SwitchingTrader.java index cbe8bb4..6da979c 100644 --- a/src/main/java/traders/SwitchingTrader.java +++ b/src/main/java/traders/SwitchingTrader.java @@ -25,7 +25,7 @@ */ package traders; -import sesim.Account; +import sesim.Account_old; import sesim.TraderConfig; /** @@ -37,7 +37,7 @@ public class SwitchingTrader extends RandomTrader{ private Action mode; - public SwitchingTrader(Account account, TraderConfig config) { + public SwitchingTrader(Account_old account, TraderConfig config) { super(account, config); // System.out.print("SWTrader Created\n"); diff --git a/src/main/java/traders/SwitchingTraderConfig.java b/src/main/java/traders/SwitchingTraderConfig.java index ac82ddc..ea03ef5 100644 --- a/src/main/java/traders/SwitchingTraderConfig.java +++ b/src/main/java/traders/SwitchingTraderConfig.java @@ -25,7 +25,7 @@ */ package traders; -import sesim.Account; +import sesim.Account_old; import sesim.AutoTrader; import sesim.Exchange; @@ -37,7 +37,7 @@ public class SwitchingTraderConfig extends RandomTraderConfig { @Override public AutoTrader createTrader(Exchange se, long shares, double money) { - Account a = new Account(se, shares, money); + Account_old a = new Account_old(se, shares, money); System.out.print("Returning a new sw trader\n"); return new SwitchingTrader(a, this); } diff --git a/src/test/java/sesim/ExchangeTest.java b/src/test/java/sesim/ExchangeTest.java deleted file mode 100644 index aa2918c..0000000 --- a/src/test/java/sesim/ExchangeTest.java +++ /dev/null @@ -1,300 +0,0 @@ -/* - * Copyright (c) 2017, tobias - * 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.ArrayList; -import java.util.SortedSet; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; - -/** - * - * @author tobias - */ -public class ExchangeTest { - - public ExchangeTest() { - } - - @BeforeClass - public static void setUpClass() { - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of createAccount method, of class Exchange. - */ - @org.junit.Test - public void testCreateAccount() { - System.out.println("createAccount"); - double money = 0.0; - double shares = 0.0; - Exchange instance = new Exchange(); - double expResult = 0.0; - double result = instance.createAccount(money, shares); - assertEquals(expResult, result, 0.0); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of getCurrentTimeSeconds method, of class Exchange. - */ - @org.junit.Test - public void testGetCurrentTimeSeconds() { - System.out.println("getCurrentTimeSeconds"); - long expResult = 0L; - long result = Exchange.getCurrentTimeSeconds(); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of getQuoteHistory method, of class Exchange. - */ - @org.junit.Test - public void testGetQuoteHistory() { - System.out.println("getQuoteHistory"); - long start = 0L; - Exchange instance = new Exchange(); - SortedSet expResult = null; - SortedSet result = instance.getQuoteHistory(start); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of addBookReceiver method, of class Exchange. - */ - @org.junit.Test - public void testAddBookReceiver() { - System.out.println("addBookReceiver"); - Order.OrderType t = null; - Exchange.BookReceiver br = null; - Exchange instance = new Exchange(); - instance.addBookReceiver(t, br); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of updateBookReceivers method, of class Exchange. - */ - @org.junit.Test - public void testUpdateBookReceivers() { - System.out.println("updateBookReceivers"); - Order.OrderType t = null; - Exchange instance = new Exchange(); - instance.updateBookReceivers(t); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of addQuoteReceiver method, of class Exchange. - */ - @org.junit.Test - public void testAddQuoteReceiver() { - System.out.println("addQuoteReceiver"); - Exchange.QuoteReceiver qr = null; - Exchange instance = new Exchange(); - instance.addQuoteReceiver(qr); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of getOrderBook method, of class Exchange. - */ - @org.junit.Test - public void testGetOrderBook() { - System.out.println("getOrderBook"); - Order.OrderType t = null; - int depth = 0; - Exchange instance = new Exchange(); - ArrayList expResult = null; - ArrayList result = instance.getOrderBook(t, depth); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of print_current method, of class Exchange. - */ - @org.junit.Test - public void testPrint_current() { - System.out.println("print_current"); - Exchange instance = new Exchange(); - instance.print_current(); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of transferMoney method, of class Exchange. - */ - @org.junit.Test - public void testTransferMoney() { - System.out.println("transferMoney"); - Account src = null; - Account dst = null; - double money = 0.0; - Exchange instance = new Exchange(); - instance.transferMoney(src, dst, money); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of transferMoneyAndShares method, of class Exchange. - */ - @org.junit.Test - public void testTransferMoneyAndShares() { - System.out.println("transferMoneyAndShares"); - Account src = null; - Account dst = null; - double money = 0.0; - long shares = 0L; - Exchange instance = new Exchange(); - instance.transferMoneyAndShares(src, dst, money, shares); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of cancelOrder method, of class Exchange. - */ - @org.junit.Test - public void testCancelOrder() { - System.out.println("cancelOrder"); - Order o = null; - Exchange instance = new Exchange(); - instance.cancelOrder(o); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of transferShares method, of class Exchange. - */ - @org.junit.Test - public void testTransferShares() { - System.out.println("transferShares"); - Account src = null; - Account dst = null; - long volume = 0L; - double price = 0.0; - Exchange instance = new Exchange(); - instance.transferShares(src, dst, volume, price); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of executeOrders method, of class Exchange. - */ - @org.junit.Test - public void testExecuteOrders() { - System.out.println("executeOrders"); - Exchange instance = new Exchange(); - instance.executeOrders(); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of ExecuteOrder method, of class Exchange. - */ - @org.junit.Test - public void testExecuteOrder() { - System.out.println("ExecuteOrder"); - BuyOrder o = null; - Exchange instance = new Exchange(); - instance.ExecuteOrder(o); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of SendOrder method, of class Exchange. - */ - @org.junit.Test - public void testSendOrder() { - System.out.println("SendOrder"); - Order o = null; - Exchange instance = new Exchange(); - Order expResult = null; - Order result = instance.SendOrder(o); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of getlastprice method, of class Exchange. - */ - @org.junit.Test - public void testGetlastprice() { - System.out.println("getlastprice"); - Exchange instance = new Exchange(); - double expResult = 0.0; - double result = instance.getlastprice(); - assertEquals(expResult, result, 0.0); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of run method, of class Exchange. - */ - @org.junit.Test - public void testRun() { - System.out.println("run"); - Exchange instance = new Exchange(); - instance.run(); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - -} diff --git a/src/test/java/sesim/Test.java b/src/test/java/sesim/Test.java index 9e4ca51..322cba2 100644 --- a/src/test/java/sesim/Test.java +++ b/src/test/java/sesim/Test.java @@ -36,14 +36,18 @@ public class Test { public static void main(String[] args) { Exchange se = new Exchange(); - double aid; - aid = se.createAccount(100, 110); - System.out.print(aid); - System.out.print("\n"); - - aid = se.createAccount(100, 110); - System.out.print(aid); - System.out.print("\n"); + double aid1 = se.createAccount(100, 110); + + + double aid2 = se.createAccount(100, 110); + se.createOrder(aid2, Exchange.OrderType.ASK, 50, 9); + se.createOrder(aid1, Exchange.OrderType.BID, 50, 11); + + + se.executeOrders(); + + //S/ystem.out.print(aid); + //System.out.print("\n"); }