2017-01-05 19:20:44 +01:00
|
|
|
/*
|
2017-01-11 14:42:40 +01:00
|
|
|
* Copyright (c) 2017, tobias
|
2017-01-05 19:20:44 +01:00
|
|
|
* 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 traders;
|
|
|
|
|
2017-01-12 01:00:42 +01:00
|
|
|
import java.util.*;
|
2017-01-11 19:11:17 +01:00
|
|
|
import java.util.Random;
|
2017-01-11 14:42:40 +01:00
|
|
|
import java.util.TimerTask;
|
2017-01-11 19:11:17 +01:00
|
|
|
import sesim.AccountData;
|
2017-01-05 19:20:44 +01:00
|
|
|
|
2017-01-11 14:42:40 +01:00
|
|
|
import sesim.AutoTrader;
|
2017-01-11 19:11:17 +01:00
|
|
|
import sesim.Exchange;
|
|
|
|
import sesim.Exchange.OrderType;
|
2017-01-05 19:20:44 +01:00
|
|
|
|
2017-01-12 01:00:42 +01:00
|
|
|
import sesim.*;
|
|
|
|
|
2017-01-11 14:42:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author tobias
|
|
|
|
*/
|
2017-01-11 19:11:17 +01:00
|
|
|
public class RandomTrader extends AutoTrader {
|
|
|
|
|
2017-01-12 09:07:08 +01:00
|
|
|
//static Timer timer = new Timer();
|
2017-01-11 19:11:17 +01:00
|
|
|
|
|
|
|
enum Event {
|
|
|
|
CANCEL,
|
|
|
|
CREATE
|
|
|
|
}
|
2017-01-12 01:00:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
long event(){
|
|
|
|
|
|
|
|
System.out.print("Hello world Iam a trader\n");
|
2017-01-12 09:07:08 +01:00
|
|
|
return this.doTrade();
|
2017-01-12 01:00:42 +01:00
|
|
|
// doBuy();
|
2017-01-12 01:44:50 +01:00
|
|
|
|
2017-01-12 01:00:42 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 19:11:17 +01:00
|
|
|
|
|
|
|
class NextEvent {
|
|
|
|
|
|
|
|
public Event event;
|
|
|
|
public long time;
|
|
|
|
|
|
|
|
NextEvent(Event e, long time) {
|
|
|
|
this.event = e;
|
|
|
|
this.time = time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public RandomTrader(Exchange se, double money, double shares, RandomTraderConfig config) {
|
|
|
|
super(se, money, shares, config);
|
|
|
|
if (this.config == null) {
|
|
|
|
this.config = new RandomTraderConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-01-05 19:20:44 +01:00
|
|
|
|
2017-01-11 14:42:40 +01:00
|
|
|
@Override
|
|
|
|
public void start() {
|
2017-01-11 19:11:17 +01:00
|
|
|
|
2017-01-12 09:07:08 +01:00
|
|
|
Exchange.timer.schedule(new TimerTaskImpl(this), 0);
|
2017-01-11 19:11:17 +01:00
|
|
|
|
|
|
|
// timer.schedule(new TimerTaskImpl, date);
|
|
|
|
}
|
|
|
|
|
|
|
|
// config for this trader
|
|
|
|
//final private RandomTraderConfig_old myconfig;
|
|
|
|
// object to generate random numbers
|
|
|
|
final private Random rand = new Random();
|
|
|
|
|
|
|
|
/*public RandomTrader(Exchange se, double money,shares,) {
|
|
|
|
//super(account, config);
|
|
|
|
if (config == null) {
|
|
|
|
config = new RandomTraderConfig_old();
|
|
|
|
}
|
|
|
|
myconfig = (RandomTraderConfig_old) config;
|
|
|
|
}*/
|
|
|
|
/**
|
|
|
|
* Get a (long) random number between min an max
|
|
|
|
*
|
|
|
|
* @param min minimum value
|
|
|
|
* @param max maximeum value
|
|
|
|
* @return the number
|
|
|
|
*/
|
|
|
|
protected double getRandom(double min, double max) {
|
|
|
|
double r = rand.nextDouble();
|
|
|
|
return (max - min) * r + min;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int getRandom(int[] minmax) {
|
|
|
|
return (int) Math.round(getRandom(minmax[0], minmax[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param val
|
|
|
|
* @param minmax
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
protected double getRandomAmmount(double val, float[] minmax) {
|
|
|
|
double min = val * minmax[0] / 100.0;
|
|
|
|
double max = val * minmax[1] / 100.0;
|
|
|
|
return getRandom(min, max);
|
|
|
|
}
|
2017-01-12 01:00:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
2017-01-12 01:44:50 +01:00
|
|
|
|
2017-01-12 09:07:08 +01:00
|
|
|
return n;
|
2017-01-12 01:00:42 +01:00
|
|
|
|
|
|
|
}
|
2017-01-11 19:11:17 +01:00
|
|
|
|
2017-01-12 09:07:08 +01:00
|
|
|
public long doBuy() {
|
2017-01-11 19:11:17 +01:00
|
|
|
RandomTraderConfig myconfig = (RandomTraderConfig)this.config;
|
|
|
|
AccountData ad = this.se.getAccountData(account_id);
|
2017-01-12 09:07:08 +01:00
|
|
|
|
2017-01-11 19:11:17 +01:00
|
|
|
OrderType type=OrderType.BID;
|
|
|
|
|
2017-01-12 01:00:42 +01:00
|
|
|
// how much money we ant to envest?
|
|
|
|
double money = getRandomAmmount(ad.money, myconfig.buy_volume);
|
2017-01-05 19:20:44 +01:00
|
|
|
|
2017-01-12 01:00:42 +01:00
|
|
|
|
|
|
|
double lp = 100.0; //se.getBestLimit(type);
|
2017-01-11 19:11:17 +01:00
|
|
|
double limit;
|
|
|
|
limit = lp + getRandomAmmount(lp, myconfig.buy_limit);
|
|
|
|
|
2017-01-12 01:00:42 +01:00
|
|
|
long volume = (long) (money / (limit * 1));
|
2017-01-11 19:11:17 +01:00
|
|
|
if (volume <= 0) {
|
2017-01-12 09:07:08 +01:00
|
|
|
return 0;
|
2017-01-11 19:11:17 +01:00
|
|
|
}
|
2017-01-12 01:00:42 +01:00
|
|
|
|
|
|
|
System.out.print("Volume is:"+volume+"\n");
|
|
|
|
System.out.print("My Ammount is: "+money+" My limit si:"+limit+ "\n");
|
|
|
|
|
2017-01-11 19:11:17 +01:00
|
|
|
|
2017-01-12 09:07:08 +01:00
|
|
|
se.createOrder(account_id, type, volume, limit);
|
|
|
|
|
|
|
|
return getRandom(myconfig.buy_order_wait)*1000;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public long doSell() {
|
|
|
|
RandomTraderConfig myconfig = (RandomTraderConfig)this.config;
|
|
|
|
AccountData ad = this.se.getAccountData(account_id);
|
|
|
|
|
|
|
|
OrderType type=OrderType.ASK;
|
2017-01-11 19:11:17 +01:00
|
|
|
|
2017-01-12 09:07:08 +01:00
|
|
|
// how much money we ant to envest?
|
|
|
|
double volume = (long)getRandomAmmount(ad.shares, myconfig.sell_volume);
|
|
|
|
|
2017-01-11 19:11:17 +01:00
|
|
|
|
2017-01-12 09:07:08 +01:00
|
|
|
double lp = 100.0; //se.getBestLimit(type);
|
|
|
|
double limit;
|
|
|
|
limit = lp + getRandomAmmount(lp, myconfig.sell_limit);
|
|
|
|
|
|
|
|
// long volume = (long) (money / (limit * 1));
|
|
|
|
// if (volume <= 0) {
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
|
|
|
|
System.out.print("Volume is:"+volume+"\n");
|
|
|
|
System.out.print("My Ammount is: "+volume+" My limit si:"+limit+ "\n");
|
|
|
|
|
|
|
|
|
|
|
|
se.createOrder(account_id, type, volume, limit);
|
|
|
|
|
|
|
|
return getRandom(myconfig.sell_order_wait)*1000;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long doTrade(){
|
|
|
|
cancelOrders();
|
|
|
|
int what = rand.nextInt(2);
|
|
|
|
if (what==0)
|
|
|
|
return doBuy();
|
|
|
|
else
|
|
|
|
return doSell();
|
|
|
|
|
|
|
|
|
2017-01-11 19:11:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected NextEvent createOrder() {
|
|
|
|
|
|
|
|
return new NextEvent(Event.CANCEL, 3000);
|
2017-01-05 19:20:44 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 14:42:40 +01:00
|
|
|
private static class TimerTaskImpl extends TimerTask {
|
2017-01-11 19:11:17 +01:00
|
|
|
|
2017-01-11 14:42:40 +01:00
|
|
|
RandomTrader trader;
|
2017-01-11 19:11:17 +01:00
|
|
|
NextEvent nextevent;
|
2017-01-05 19:20:44 +01:00
|
|
|
|
2017-01-11 14:42:40 +01:00
|
|
|
public TimerTaskImpl(RandomTrader trader) {
|
2017-01-11 19:11:17 +01:00
|
|
|
this.trader = trader;
|
|
|
|
|
2017-01-05 19:20:44 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 14:42:40 +01:00
|
|
|
@Override
|
2017-01-11 19:11:17 +01:00
|
|
|
public void run() {
|
2017-01-12 01:00:42 +01:00
|
|
|
|
|
|
|
long time = trader.event();
|
|
|
|
|
2017-01-11 19:11:17 +01:00
|
|
|
this.cancel();
|
2017-01-12 09:07:08 +01:00
|
|
|
Exchange.timer.schedule(new TimerTaskImpl(trader), time);
|
2017-01-11 19:11:17 +01:00
|
|
|
|
2017-01-11 14:42:40 +01:00
|
|
|
}
|
2017-01-05 19:20:44 +01:00
|
|
|
}
|
2017-01-11 19:11:17 +01:00
|
|
|
|
2017-01-05 19:20:44 +01:00
|
|
|
}
|