worknig on new random trader

This commit is contained in:
7u83 2017-01-12 01:00:42 +01:00
parent d866f33fea
commit 80e48e1ee0
6 changed files with 124 additions and 24 deletions

View File

@ -56,15 +56,16 @@ public class MainWin extends javax.swing.JFrame {
double aid1 = se.createAccount(100, 100); 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); AccountData a2 = se.getAccountData(aid2);
se.createOrder(aid2, Exchange.OrderType.ASK, 20, 11.9); se.createOrder(aid2, Exchange.OrderType.ASK, 20, 11.9);
se.createOrder(aid2, Exchange.OrderType.ASK, 20, 11); se.createOrder(aid2, Exchange.OrderType.ASK, 20, 11);
se.createOrder(aid2, Exchange.OrderType.ASK, 10, 10); se.createOrder(aid2, Exchange.OrderType.ASK, 10, 10);
se.createOrder(aid2, Exchange.OrderType.ASK, 10, 9); se.createOrder(aid2, Exchange.OrderType.ASK, 10, 9);
se.createOrder(aid1, Exchange.OrderType.BID, 50, 11); se.createOrder(aid1, Exchange.OrderType.BID, 50, 11);
*/
/* /*
System.out.print("Exec Orders\n"); System.out.print("Exec Orders\n");
@ -188,7 +189,10 @@ public class MainWin extends javax.swing.JFrame {
RandomTraderConfig rcfg = new RandomTraderConfig(); RandomTraderConfig rcfg = new RandomTraderConfig();
RandomTrader rt = rcfg.createTrader(se, 100, 100); RandomTrader rt = rcfg.createTrader(se, 1000, 100);
rt.start();
//RandomTrader rt = new RandomTrader(); //RandomTrader rt = new RandomTrader();

View File

@ -353,11 +353,35 @@ public class Exchange extends Thread {
dst.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 * @param o
*/ */
public void cancelOrder(Order_old o) { public void cancelOrder_old(Order_old o) {
tradelock.lock(); tradelock.lock();
TreeSet<Order_old> book = this.selectOrderBook(o.type); TreeSet<Order_old> book = this.selectOrderBook(o.type);
book.remove(o); book.remove(o);
@ -613,7 +637,9 @@ public class Exchange extends Thread {
addOrderToBook(o); addOrderToBook(o);
a.orders.put(o.id, o); a.orders.put(o.id, o);
tradelock.lock();
this.executeOrders(); this.executeOrders();
tradelock.unlock();
this.updateBookReceivers(OrderType.ASK); this.updateBookReceivers(OrderType.ASK);
this.updateBookReceivers(OrderType.BID); this.updateBookReceivers(OrderType.BID);
return o.id; return o.id;
@ -645,6 +671,32 @@ public class Exchange extends Thread {
ad.id = account_id; ad.id = account_id;
ad.money = a.money; ad.money = a.money;
ad.shares = a.shares; ad.shares = a.shares;
ad.orders=new ArrayList<OrderData>();
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; return ad;
} }

View File

@ -30,9 +30,9 @@ package sesim;
* @author 7u83 <7u83@mail.ru> * @author 7u83 <7u83@mail.ru>
*/ */
public class OrderData { public class OrderData {
long id; public long id;
double limit; public double limit;
double volume; public double volume;
double executed; public double executed;
} }

View File

@ -25,6 +25,7 @@
*/ */
package traders; package traders;
import java.util.*;
import java.util.Random; import java.util.Random;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
@ -34,6 +35,8 @@ import sesim.AutoTrader;
import sesim.Exchange; import sesim.Exchange;
import sesim.Exchange.OrderType; import sesim.Exchange.OrderType;
import sesim.*;
/** /**
* *
* @author tobias * @author tobias
@ -47,6 +50,16 @@ public class RandomTrader extends AutoTrader {
CREATE CREATE
} }
long event(){
System.out.print("Hello world Iam a trader\n");
this.cancelOrders();
// doBuy();
return 10000;
}
class NextEvent { class NextEvent {
public Event event; public Event event;
@ -69,7 +82,7 @@ public class RandomTrader extends AutoTrader {
@Override @Override
public void start() { public void start() {
timer.schedule(new TimerTaskImpl(this), 1000); timer.schedule(new TimerTaskImpl(this), 0);
// timer.schedule(new TimerTaskImpl, date); // timer.schedule(new TimerTaskImpl, date);
} }
@ -114,24 +127,56 @@ public class RandomTrader extends AutoTrader {
return getRandom(min, max); 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 <OrderData> 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() { public boolean doBuy() {
RandomTraderConfig myconfig = (RandomTraderConfig)this.config; RandomTraderConfig myconfig = (RandomTraderConfig)this.config;
AccountData ad = this.se.getAccountData(account_id); AccountData ad = this.se.getAccountData(account_id);
OrderType type=OrderType.BID; OrderType type=OrderType.BID;
double money = getRandomAmmount(ad.money, myconfig.sell_volume); // how much money we ant to envest?
double money = getRandomAmmount(ad.money, myconfig.buy_volume);
double lp = se.getBestLimit(type);
double lp = 100.0; //se.getBestLimit(type);
double limit; double limit;
limit = lp + getRandomAmmount(lp, myconfig.buy_limit); limit = lp + getRandomAmmount(lp, myconfig.buy_limit);
long volume = (int) (money / (limit * 1)); long volume = (long) (money / (limit * 1));
if (volume <= 0) { 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; return true;
@ -154,12 +199,11 @@ public class RandomTrader extends AutoTrader {
@Override @Override
public void run() { public void run() {
switch (this.nextevent.event) {
} long time = trader.event();
this.cancel(); this.cancel();
timer.schedule(new TimerTaskImpl(trader), 1000); timer.schedule(new TimerTaskImpl(trader), time);
} }
} }

View File

@ -34,12 +34,12 @@ import sesim.Exchange;
*/ */
public class RandomTraderConfig extends AutoTraderConfig { public class RandomTraderConfig extends AutoTraderConfig {
public float[] sell_volume = {100, 100}; public float[] sell_volume = {50, 100};
public float[] sell_limit = {-15, 15}; public float[] sell_limit = {-15, 15};
public int[] sell_order_wait = {15, 33}; public int[] sell_order_wait = {15, 33};
public int[] wait_after_sell = {10, 30}; 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 float[] buy_limit = {-15, 15};
public int[] buy_order_wait = {15, 33}; public int[] buy_order_wait = {15, 33};
public int[] wait_after_buy = {10, 30}; public int[] wait_after_buy = {10, 30};

View File

@ -87,7 +87,7 @@ public class RandomTrader_old extends AutoTrader_old {
if (account.pending.size() != 0) { if (account.pending.size() != 0) {
Order_old o = account.pending.get(0); Order_old o = account.pending.get(0);
account.se.cancelOrder(o); account.se.cancelOrder_old(o);
return false; return false;
} }
return true; return true;
@ -139,7 +139,7 @@ public class RandomTrader_old extends AutoTrader_old {
// System.out.print("RT: age is: "+age+"\n"); // System.out.print("RT: age is: "+age+"\n");
if (age > myconfig.maxage) { if (age > myconfig.maxage) {
// System.out.print("MaxAge is"+myconfig.maxage+"\n"); // 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"); // System.out.print("Age reached - canel return false\n");
return false; return false;
} }