From 80e48e1ee0e33d8c380c90155eed26f790b8ee56 Mon Sep 17 00:00:00 2001 From: 7u83 <7u83@mail.ru> Date: Thu, 12 Jan 2017 01:00:42 +0100 Subject: [PATCH] worknig on new random trader --- src/main/java/gui/MainWin.java | 12 ++-- src/main/java/sesim/Exchange.java | 54 ++++++++++++++- src/main/java/sesim/OrderData.java | 8 +-- src/main/java/traders/RandomTrader.java | 66 +++++++++++++++---- src/main/java/traders/RandomTraderConfig.java | 4 +- src/main/java/traders/RandomTrader_old.java | 4 +- 6 files changed, 124 insertions(+), 24 deletions(-) diff --git a/src/main/java/gui/MainWin.java b/src/main/java/gui/MainWin.java index 430ddc2..ad2dcf5 100644 --- a/src/main/java/gui/MainWin.java +++ b/src/main/java/gui/MainWin.java @@ -56,16 +56,17 @@ public class MainWin extends javax.swing.JFrame { double aid1 = se.createAccount(100, 100); - double aid2 = se.createAccount(100, 100); + double aid2 = se.createAccount(1000, 100); - AccountData a1 = se.getAccountData(aid1); + /* AccountData a1 = se.getAccountData(aid1); AccountData a2 = se.getAccountData(aid2); se.createOrder(aid2, Exchange.OrderType.ASK, 20, 11.9); 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(); @@ -188,7 +189,10 @@ public class MainWin extends javax.swing.JFrame { RandomTraderConfig rcfg = new RandomTraderConfig(); - RandomTrader rt = rcfg.createTrader(se, 100, 100); + RandomTrader rt = rcfg.createTrader(se, 1000, 100); + rt.start(); + + //RandomTrader rt = new RandomTrader(); diff --git a/src/main/java/sesim/Exchange.java b/src/main/java/sesim/Exchange.java index 5b3e6e5..7e4221d 100644 --- a/src/main/java/sesim/Exchange.java +++ b/src/main/java/sesim/Exchange.java @@ -352,12 +352,36 @@ public class Exchange extends Thread { src.shares -= shares; dst.shares += shares; } + + + public boolean cancelOrder(double account_id, long order_id){ + Account a = accounts.get(account_id); + if (a==null){ + return false; + } + + boolean ret=false; + + tradelock.lock(); + Order o = a.orders.get(order_id); + if (o != null){ + order_books.get(o.type).remove(o); + a.orders.remove(o.id); + ret=true; + } + + + tradelock.unlock(); + this.updateBookReceivers(OrderType.BID); + + return ret; + } /** * * @param o */ - public void cancelOrder(Order_old o) { + public void cancelOrder_old(Order_old o) { tradelock.lock(); TreeSet book = this.selectOrderBook(o.type); book.remove(o); @@ -613,7 +637,9 @@ public class Exchange extends Thread { addOrderToBook(o); a.orders.put(o.id, o); + tradelock.lock(); this.executeOrders(); + tradelock.unlock(); this.updateBookReceivers(OrderType.ASK); this.updateBookReceivers(OrderType.BID); return o.id; @@ -645,6 +671,32 @@ public class Exchange extends Thread { ad.id = account_id; ad.money = a.money; ad.shares = a.shares; + + + ad.orders=new ArrayList(); + ad.orders.iterator(); + + a.orders.values(); + Set s =a.orders.keySet(); + Iterator it = s.iterator(); + System.out.print("Keys list"+s.size()+"\n"); + while (it.hasNext()){ + long x = (long)it.next(); + System.out.print("X"+x+"\n"); + Order o = a.orders.get(x); + System.out.print("oGot: "+o.limit+" "+o.volume+"\n"); + OrderData od = new OrderData(); + od.id=o.id; + od.limit=o.limit; + od.volume=o.volume; + ad.orders.add(od); + } + + //System.exit(0); + + //a.orders.keySet(); + //KeySet ks = a.orders.keySet(); + return ad; } diff --git a/src/main/java/sesim/OrderData.java b/src/main/java/sesim/OrderData.java index e4af64a..9aeb989 100644 --- a/src/main/java/sesim/OrderData.java +++ b/src/main/java/sesim/OrderData.java @@ -30,9 +30,9 @@ package sesim; * @author 7u83 <7u83@mail.ru> */ public class OrderData { - long id; - double limit; - double volume; - double executed; + public long id; + public double limit; + public double volume; + public double executed; } diff --git a/src/main/java/traders/RandomTrader.java b/src/main/java/traders/RandomTrader.java index d54fe99..eb45830 100644 --- a/src/main/java/traders/RandomTrader.java +++ b/src/main/java/traders/RandomTrader.java @@ -25,6 +25,7 @@ */ package traders; +import java.util.*; import java.util.Random; import java.util.Timer; import java.util.TimerTask; @@ -34,6 +35,8 @@ import sesim.AutoTrader; import sesim.Exchange; import sesim.Exchange.OrderType; +import sesim.*; + /** * * @author tobias @@ -46,6 +49,16 @@ public class RandomTrader extends AutoTrader { CANCEL, CREATE } + + + long event(){ + + System.out.print("Hello world Iam a trader\n"); + this.cancelOrders(); + // doBuy(); + return 10000; + } + class NextEvent { @@ -69,7 +82,7 @@ public class RandomTrader extends AutoTrader { @Override public void start() { - timer.schedule(new TimerTaskImpl(this), 1000); + timer.schedule(new TimerTaskImpl(this), 0); // timer.schedule(new TimerTaskImpl, date); } @@ -113,6 +126,31 @@ public class RandomTrader extends AutoTrader { double max = val * minmax[1] / 100.0; return getRandom(min, max); } + + + public long cancelOrders(){ + int n = se.getNumberOfOpenOrders(account_id); + System.out.print("Open Orders: "+n+"\n"); + if (n>0){ + System.out.print("Want to killń\n"); + AccountData ad = se.getAccountData(account_id); + Iterator it = ad.orders.iterator(); + while (it.hasNext()){ + OrderData od=it.next(); + boolean rc = se.cancelOrder(account_id, od.id); + System.out.print("killer rc "+rc+"\n"); + System.out.print("Killing: "+od.id+"\n"); + } + } + else{ + doBuy(); + } + + + + return 10000; + + } public boolean doBuy() { RandomTraderConfig myconfig = (RandomTraderConfig)this.config; @@ -120,17 +158,24 @@ public class RandomTrader extends AutoTrader { OrderType type=OrderType.BID; - double money = getRandomAmmount(ad.money, myconfig.sell_volume); - - double lp = se.getBestLimit(type); + // how much money we ant to envest? + double money = getRandomAmmount(ad.money, myconfig.buy_volume); + + double lp = 100.0; //se.getBestLimit(type); double limit; limit = lp + getRandomAmmount(lp, myconfig.buy_limit); - long volume = (int) (money / (limit * 1)); + long volume = (long) (money / (limit * 1)); if (volume <= 0) { - return false; + //return false; } + + System.out.print("Volume is:"+volume+"\n"); + System.out.print("My Ammount is: "+money+" My limit si:"+limit+ "\n"); + //System.exit(0); + + se.createOrder(account_id, OrderType.BID, volume, limit); return true; @@ -154,12 +199,11 @@ public class RandomTrader extends AutoTrader { @Override public void run() { - switch (this.nextevent.event) { - - } - + + long time = trader.event(); + this.cancel(); - timer.schedule(new TimerTaskImpl(trader), 1000); + timer.schedule(new TimerTaskImpl(trader), time); } } diff --git a/src/main/java/traders/RandomTraderConfig.java b/src/main/java/traders/RandomTraderConfig.java index 284480b..59409fe 100644 --- a/src/main/java/traders/RandomTraderConfig.java +++ b/src/main/java/traders/RandomTraderConfig.java @@ -34,12 +34,12 @@ import sesim.Exchange; */ public class RandomTraderConfig extends AutoTraderConfig { - public float[] sell_volume = {100, 100}; + public float[] sell_volume = {50, 100}; public float[] sell_limit = {-15, 15}; public int[] sell_order_wait = {15, 33}; public int[] wait_after_sell = {10, 30}; - public float[] buy_volume = {100, 100}; + public float[] buy_volume = {50, 100}; public float[] buy_limit = {-15, 15}; public int[] buy_order_wait = {15, 33}; public int[] wait_after_buy = {10, 30}; diff --git a/src/main/java/traders/RandomTrader_old.java b/src/main/java/traders/RandomTrader_old.java index fce104f..086ca9a 100644 --- a/src/main/java/traders/RandomTrader_old.java +++ b/src/main/java/traders/RandomTrader_old.java @@ -87,7 +87,7 @@ public class RandomTrader_old extends AutoTrader_old { if (account.pending.size() != 0) { Order_old o = account.pending.get(0); - account.se.cancelOrder(o); + account.se.cancelOrder_old(o); return false; } return true; @@ -139,7 +139,7 @@ public class RandomTrader_old extends AutoTrader_old { // System.out.print("RT: age is: "+age+"\n"); if (age > myconfig.maxage) { // System.out.print("MaxAge is"+myconfig.maxage+"\n"); - account.se.cancelOrder(o); + account.se.cancelOrder_old(o); // System.out.print("Age reached - canel return false\n"); return false; }