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,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();

View File

@ -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<Order_old> 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<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;
}

View File

@ -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;
}

View File

@ -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 <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() {
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);
}
}

View File

@ -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};

View File

@ -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;
}