worknig on new random trader
This commit is contained in:
parent
5e49190e8c
commit
d866f33fea
@ -25,15 +25,16 @@
|
||||
*/
|
||||
package gui;
|
||||
|
||||
import traders.RandomTraderConfig;
|
||||
import traders.RandomTraderConfig_old;
|
||||
import traders.SwitchingTraderConfig;
|
||||
import sesim.Account_old;
|
||||
import sesim.AutoTraderLIst;
|
||||
import sesim.Exchange;
|
||||
import sesim.BuyOrder;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.*;
|
||||
import sesim.AccountData;
|
||||
import traders.RandomTrader;
|
||||
import traders.RandomTraderConfig;
|
||||
|
||||
|
||||
/**
|
||||
@ -59,11 +60,11 @@ public class MainWin extends javax.swing.JFrame {
|
||||
|
||||
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, 7);
|
||||
se.createOrder(aid1, Exchange.OrderType.BID, 50, 11);
|
||||
|
||||
/*
|
||||
System.out.print("Exec Orders\n");
|
||||
@ -78,7 +79,7 @@ public class MainWin extends javax.swing.JFrame {
|
||||
|
||||
/*
|
||||
AutoTraderLIst at = new AutoTraderLIst();
|
||||
// RandomTraderConfig rcfg = new RandomTraderConfig();
|
||||
// RandomTraderConfig_old rcfg = new RandomTraderConfig_old();
|
||||
SwitchingTraderConfig rcfg = new SwitchingTraderConfig();
|
||||
at.add(1000, rcfg, se, 100, 0);
|
||||
at.add(1000, rcfg, se, 0, 10000);
|
||||
@ -184,8 +185,17 @@ public class MainWin extends javax.swing.JFrame {
|
||||
public static void main(String args[]) {
|
||||
|
||||
se = new Exchange();
|
||||
myAccount = new Account_old(se,1000,100000000.0);
|
||||
myTrader = new traders.ManTrader(myAccount,null);
|
||||
|
||||
|
||||
RandomTraderConfig rcfg = new RandomTraderConfig();
|
||||
RandomTrader rt = rcfg.createTrader(se, 100, 100);
|
||||
|
||||
|
||||
//RandomTrader rt = new RandomTrader();
|
||||
//rt.start();
|
||||
|
||||
// myAccount = new Account_old(se,1000,100000000.0);
|
||||
// myTrader = new traders.ManTrader(myAccount,null);
|
||||
|
||||
/* Account_old otherAccount = new Account_old(se,1000,1000);
|
||||
Traders.ManTrader otherTrader = new Traders.ManTrader(otherAccount);
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 7u83 <7u83@mail.ru>
|
||||
* Copyright (c) 2017, tobias
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@ -25,41 +25,31 @@
|
||||
*/
|
||||
package sesim;
|
||||
|
||||
import static java.lang.Thread.sleep;
|
||||
import static java.lang.Thread.sleep;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
* @author tobias
|
||||
*/
|
||||
public abstract class AutoTrader extends Trader implements Runnable {
|
||||
|
||||
public AutoTrader(Account_old account, TraderConfig config) {
|
||||
super(account, config);
|
||||
public abstract class AutoTrader {
|
||||
|
||||
protected double account_id;
|
||||
protected Exchange se;
|
||||
protected AutoTraderConfig config;
|
||||
|
||||
|
||||
|
||||
|
||||
public AutoTrader(Exchange se, double money, double shares, AutoTraderConfig config){
|
||||
account_id = se.createAccount(money, shares);
|
||||
this.se=se;
|
||||
this.config=config;
|
||||
}
|
||||
|
||||
protected void doSleep(int seconds) {
|
||||
try {
|
||||
sleep(seconds * 1000);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
|
||||
/*public AutoTrader(Exchange se, double money, double shares){
|
||||
this(se,money,shares,null);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
System.out.print("Starting AutoTrader\n");
|
||||
class Runner extends Thread {
|
||||
|
||||
AutoTrader trader;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
trader.run();
|
||||
}
|
||||
}
|
||||
Runner r = new Runner();
|
||||
r.trader = this;
|
||||
r.start();
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
public abstract void start();
|
||||
|
||||
|
||||
}
|
||||
|
34
src/main/java/sesim/AutoTraderConfig.java
Normal file
34
src/main/java/sesim/AutoTraderConfig.java
Normal file
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author tobias
|
||||
*/
|
||||
public abstract class AutoTraderConfig {
|
||||
public abstract AutoTrader createTrader(Exchange se, double money, double shares);
|
||||
}
|
@ -31,10 +31,10 @@ package sesim;
|
||||
*/
|
||||
public class AutoTraderLIst {
|
||||
|
||||
public void add(int n, TraderConfig config, Exchange se, long shares, double money) {
|
||||
public void add(int n, TraderConfig_old config, Exchange se, long shares, double money) {
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
AutoTrader trader = config.createTrader(se, shares, money);
|
||||
AutoTrader_old trader = config.createTrader(se, shares, money);
|
||||
// TraderRunner tr = new TraderRunner(trader);
|
||||
|
||||
trader.start();
|
||||
|
64
src/main/java/sesim/AutoTrader_old.java
Normal file
64
src/main/java/sesim/AutoTrader_old.java
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 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 static java.lang.Thread.sleep;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public abstract class AutoTrader_old extends Trader_old implements Runnable {
|
||||
|
||||
public AutoTrader_old(Account_old account, TraderConfig_old config) {
|
||||
super(account, config);
|
||||
}
|
||||
|
||||
protected void doSleep(int seconds) {
|
||||
try {
|
||||
sleep(seconds * 1000);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void start() {
|
||||
System.out.print("Starting AutoTrader\n");
|
||||
class Runner extends Thread {
|
||||
|
||||
AutoTrader_old trader;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
trader.run();
|
||||
}
|
||||
}
|
||||
Runner r = new Runner();
|
||||
r.trader = this;
|
||||
r.start();
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -15,6 +15,8 @@ public class Exchange extends Thread {
|
||||
public enum OrderType {
|
||||
BID, ASK
|
||||
}
|
||||
|
||||
|
||||
|
||||
IDGenerator account_id = new IDGenerator();
|
||||
|
||||
@ -612,11 +614,27 @@ public class Exchange extends Thread {
|
||||
a.orders.put(o.id, o);
|
||||
|
||||
this.executeOrders();
|
||||
this.updateBookReceivers(type);
|
||||
|
||||
this.updateBookReceivers(OrderType.ASK);
|
||||
this.updateBookReceivers(OrderType.BID);
|
||||
return o.id;
|
||||
}
|
||||
|
||||
|
||||
public double getBestLimit(OrderType type){
|
||||
Order o = order_books.get(type).first();
|
||||
if (o==null){
|
||||
return -1;
|
||||
}
|
||||
return o.limit;
|
||||
}
|
||||
|
||||
public int getNumberOfOpenOrders(double account_id){
|
||||
Account a=accounts.get(account_id);
|
||||
if (a==null)
|
||||
return 0;
|
||||
return a.orders.size();
|
||||
}
|
||||
|
||||
public AccountData getAccountData(double account_id) {
|
||||
Account a = accounts.get(account_id);
|
||||
if (a == null) {
|
||||
|
@ -29,8 +29,8 @@ package sesim;
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public abstract class TraderConfig{
|
||||
public abstract class TraderConfig_old{
|
||||
String name;
|
||||
|
||||
public abstract AutoTrader createTrader(Exchange se, long shares, double money);
|
||||
public abstract AutoTrader_old createTrader(Exchange se, long shares, double money);
|
||||
}
|
@ -25,18 +25,13 @@
|
||||
*/
|
||||
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 {
|
||||
public abstract class Trader_old {
|
||||
|
||||
public String name = null;
|
||||
|
||||
|
||||
public Account_old account;
|
||||
public TraderConfig config;
|
||||
public TraderConfig_old config;
|
||||
|
||||
public void sell(long shares, double limit){
|
||||
account.sell(shares, limit);
|
||||
@ -51,7 +46,7 @@ public abstract class Trader {
|
||||
* @param account Account_old for this trader
|
||||
* @param config Configration for this trader
|
||||
*/
|
||||
public Trader(Account_old account, TraderConfig config){
|
||||
public Trader_old(Account_old account, TraderConfig_old config){
|
||||
this.account=account;
|
||||
this.config=config;
|
||||
}
|
||||
@ -61,7 +56,7 @@ public abstract class Trader {
|
||||
* The Trader object shoul initialize a default config
|
||||
* @param account
|
||||
*/
|
||||
public Trader(Account_old account){
|
||||
public Trader_old(Account_old account){
|
||||
this(account,null);
|
||||
}
|
||||
|
@ -26,8 +26,8 @@
|
||||
package traders;
|
||||
|
||||
import sesim.Account_old;
|
||||
import sesim.Trader;
|
||||
import sesim.TraderConfig;
|
||||
import sesim.Trader_old;
|
||||
import sesim.TraderConfig_old;
|
||||
import sesim.BuyOrder;
|
||||
|
||||
|
||||
@ -37,9 +37,9 @@ import sesim.BuyOrder;
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class ManTrader extends Trader{
|
||||
public class ManTrader extends Trader_old{
|
||||
|
||||
public ManTrader(Account_old account,TraderConfig config) {
|
||||
public ManTrader(Account_old account,TraderConfig_old config) {
|
||||
super(account,config);
|
||||
}
|
||||
|
||||
|
@ -25,47 +25,143 @@
|
||||
*/
|
||||
package traders;
|
||||
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import sesim.AccountData;
|
||||
|
||||
import sesim.AutoTrader;
|
||||
import sesim.Exchange;
|
||||
import sesim.Exchange.OrderType;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author tobias
|
||||
*/
|
||||
public class RandomTrader extends AutoTrader{
|
||||
public class RandomTrader extends AutoTrader {
|
||||
|
||||
static Timer timer = new Timer();
|
||||
|
||||
enum Event {
|
||||
CANCEL,
|
||||
CREATE
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static Timer timer= new Timer();
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
|
||||
timer.schedule(new TimerTaskImpl(this), 1000);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
public boolean doBuy() {
|
||||
RandomTraderConfig myconfig = (RandomTraderConfig)this.config;
|
||||
AccountData ad = this.se.getAccountData(account_id);
|
||||
|
||||
timer.schedule (new TimerTaskImpl(this), 1000 );
|
||||
|
||||
// timer.schedule(new TimerTaskImpl, date);
|
||||
OrderType type=OrderType.BID;
|
||||
|
||||
double money = getRandomAmmount(ad.money, myconfig.sell_volume);
|
||||
|
||||
double lp = se.getBestLimit(type);
|
||||
|
||||
double limit;
|
||||
limit = lp + getRandomAmmount(lp, myconfig.buy_limit);
|
||||
|
||||
long volume = (int) (money / (limit * 1));
|
||||
if (volume <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected NextEvent createOrder() {
|
||||
|
||||
return new NextEvent(Event.CANCEL, 3000);
|
||||
}
|
||||
|
||||
private static class TimerTaskImpl extends TimerTask {
|
||||
|
||||
RandomTrader trader;
|
||||
|
||||
NextEvent nextevent;
|
||||
|
||||
public TimerTaskImpl(RandomTrader trader) {
|
||||
this.trader=trader;
|
||||
|
||||
this.trader = trader;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
System.out.print("Timer \n");
|
||||
// timer.schedule (this, 1000);
|
||||
|
||||
//this.wait(1000);
|
||||
this.cancel();
|
||||
timer.schedule(new TimerTaskImpl(trader), 1000);
|
||||
|
||||
public void run() {
|
||||
switch (this.nextevent.event) {
|
||||
|
||||
}
|
||||
|
||||
this.cancel();
|
||||
timer.schedule(new TimerTaskImpl(trader), 1000);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 7u83 <7u83@mail.ru>
|
||||
* Copyright (c) 2017, tobias
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@ -25,49 +25,27 @@
|
||||
*/
|
||||
package traders;
|
||||
|
||||
import sesim.Account_old;
|
||||
import sesim.TraderConfig;
|
||||
import sesim.AutoTraderConfig;
|
||||
import sesim.Exchange;
|
||||
import sesim.AutoTrader;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
* @author tobias
|
||||
*/
|
||||
public class RandomTraderConfig extends TraderConfig {
|
||||
public class RandomTraderConfig extends AutoTraderConfig {
|
||||
|
||||
//public long maxage = 1000 * 10 * 1;
|
||||
|
||||
|
||||
/*public long hold_shares_min = 10;
|
||||
|
||||
public long hold_shares_max = 30;
|
||||
public float[] sell_volume = {100, 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_min = 10;
|
||||
*/
|
||||
|
||||
/**
|
||||
* If shares are selled, this specifies
|
||||
* the minimum and maximum volume to be selled
|
||||
*/
|
||||
public float[] sell_volume= {100,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_limit = {-15, 15};
|
||||
public int[] buy_order_wait = {15, 33};
|
||||
public int[] wait_after_buy = {10, 30};
|
||||
|
||||
|
||||
public float[] buy_volume={100,100};
|
||||
public float[] buy_limit = {-15,15};
|
||||
public int[] buy_order_wait = {15,33};
|
||||
public int[] wait_after_buy = {10,30};
|
||||
|
||||
|
||||
@Override
|
||||
public AutoTrader createTrader(Exchange se, long shares, double money) {
|
||||
Account_old a = new Account_old(se, shares, money);
|
||||
return new RandomTrader(a, this);
|
||||
public RandomTrader createTrader(Exchange se, double money, double shares) {
|
||||
return new traders.RandomTrader(se, money, shares, this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
73
src/main/java/traders/RandomTraderConfig_old.java
Normal file
73
src/main/java/traders/RandomTraderConfig_old.java
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 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 traders;
|
||||
|
||||
import sesim.Account_old;
|
||||
import sesim.TraderConfig_old;
|
||||
import sesim.Exchange;
|
||||
import sesim.AutoTrader_old;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class RandomTraderConfig_old extends TraderConfig_old {
|
||||
|
||||
//public long maxage = 1000 * 10 * 1;
|
||||
|
||||
|
||||
/*public long hold_shares_min = 10;
|
||||
|
||||
public long hold_shares_max = 30;
|
||||
|
||||
public float buy_volume_min = 10;
|
||||
*/
|
||||
|
||||
/**
|
||||
* If shares are selled, this specifies
|
||||
* the minimum and maximum volume to be selled
|
||||
*/
|
||||
public float[] sell_volume= {100,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_limit = {-15,15};
|
||||
public int[] buy_order_wait = {15,33};
|
||||
public int[] wait_after_buy = {10,30};
|
||||
|
||||
|
||||
@Override
|
||||
public AutoTrader_old createTrader(Exchange se, long shares, double money) {
|
||||
Account_old a = new Account_old(se, shares, money);
|
||||
return new RandomTrader_old(a, this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
251
src/main/java/traders/RandomTrader_old.java
Normal file
251
src/main/java/traders/RandomTrader_old.java
Normal file
@ -0,0 +1,251 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 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 traders;
|
||||
|
||||
import sesim.Account_old;
|
||||
import sesim.Order_old;
|
||||
import java.util.Random;
|
||||
import sesim.AutoTrader_old;
|
||||
import sesim.TraderConfig_old;
|
||||
|
||||
public class RandomTrader_old extends AutoTrader_old {
|
||||
|
||||
protected enum Action {
|
||||
sell,buy
|
||||
}
|
||||
|
||||
// config for this trader
|
||||
final private RandomTraderConfig_old myconfig;
|
||||
|
||||
// object to generate random numbers
|
||||
final private Random rand = new Random();
|
||||
|
||||
public RandomTrader_old(Account_old account, TraderConfig_old config) {
|
||||
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);
|
||||
}
|
||||
|
||||
public boolean waitForOrder(long seconds) {
|
||||
|
||||
for (int i = 0; (i < seconds) && (0 != account.pending.size()); i++) {
|
||||
doSleep(1);
|
||||
}
|
||||
|
||||
if (account.pending.size() != 0) {
|
||||
Order_old o = account.pending.get(0);
|
||||
account.se.cancelOrder(o);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean doBuy() {
|
||||
|
||||
double money = getRandomAmmount(account.money, myconfig.sell_volume);
|
||||
|
||||
double lp = account.se.getlastprice();
|
||||
double limit;
|
||||
limit = lp + getRandomAmmount(lp, myconfig.buy_limit);
|
||||
|
||||
long volume = (int) (money / (limit * 1));
|
||||
if (volume <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
buy(volume, limit);
|
||||
return waitForOrder(getRandom(myconfig.buy_order_wait));
|
||||
|
||||
}
|
||||
|
||||
public boolean doSell() {
|
||||
|
||||
long volume;
|
||||
volume = (long) Math.round(getRandomAmmount(account.shares, myconfig.sell_volume));
|
||||
|
||||
double lp = account.se.getlastprice();
|
||||
double limit;
|
||||
limit = lp + getRandomAmmount(lp, myconfig.sell_limit);
|
||||
|
||||
sell(volume, limit);
|
||||
return waitForOrder(getRandom(myconfig.sell_order_wait));
|
||||
|
||||
}
|
||||
|
||||
/* private boolean monitorTrades() {
|
||||
|
||||
int numpending = account.pending.size();
|
||||
if (numpending == 0) {
|
||||
// System.out.print("RT: pending = 0 - return false\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
Order_old o = account.pending.get(0);
|
||||
long age = o.getAge();
|
||||
|
||||
// System.out.print("RT: age is: "+age+"\n");
|
||||
if (age > myconfig.maxage) {
|
||||
// System.out.print("MaxAge is"+myconfig.maxage+"\n");
|
||||
account.se.cancelOrder(o);
|
||||
// System.out.print("Age reached - canel return false\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
//System.out.print("RT: monitor return true\n");
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
public void trade() {
|
||||
|
||||
float am[] = {-10, 200};
|
||||
|
||||
double x = Math.round(this.getRandomAmmount(1000, am));
|
||||
/* System.out.print(
|
||||
"Random:"
|
||||
+ x
|
||||
+ "\n"
|
||||
);
|
||||
|
||||
*/
|
||||
/*
|
||||
// System.out.print("RT: Now trading\n");
|
||||
if (monitorTrades()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// What next to do?
|
||||
int action = rand.nextInt(5);
|
||||
|
||||
if (account.money < 10 && account.shares < 5) {
|
||||
System.out.print("I'm almost ruined\n");
|
||||
}
|
||||
|
||||
if (action == 1) {
|
||||
doBuy();
|
||||
return;
|
||||
}
|
||||
|
||||
if (action == 2) {
|
||||
doSell();
|
||||
return;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
protected Action getAction() {
|
||||
if (rand.nextInt(2)==0){
|
||||
return Action.buy;
|
||||
}
|
||||
else{
|
||||
return Action.sell;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.print("Starting Random Trader\n");
|
||||
while (true) {
|
||||
|
||||
// What next to do?
|
||||
Action action = getAction();
|
||||
|
||||
if (account.isRuined()) {
|
||||
// System.out.print("I'm ruined\n");
|
||||
// System.exit(0);
|
||||
}
|
||||
boolean rc;
|
||||
// action=1;
|
||||
switch (action) {
|
||||
|
||||
case sell:
|
||||
if (account.shares <= 0) {
|
||||
// we have no shares
|
||||
continue;
|
||||
}
|
||||
// System.out.print("Sell\n");
|
||||
rc = doSell();
|
||||
if (!rc) {
|
||||
continue;
|
||||
}
|
||||
// System.out.print("Sold\n");
|
||||
doSleep(getRandom(myconfig.wait_after_sell));
|
||||
// System.out.print("Next\n");
|
||||
break;
|
||||
case buy:
|
||||
if (account.money <= 0) {
|
||||
// we have no money
|
||||
continue;
|
||||
}
|
||||
// System.out.print("Sell\n");
|
||||
rc = doBuy();
|
||||
if (!rc) {
|
||||
continue;
|
||||
}
|
||||
// System.out.print("Bought\n");
|
||||
doSleep(getRandom(myconfig.wait_after_buy));
|
||||
// System.out.print("Next\n");
|
||||
break;
|
||||
|
||||
}
|
||||
// doSleep(1);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -26,18 +26,18 @@
|
||||
package traders;
|
||||
|
||||
import sesim.Account_old;
|
||||
import sesim.TraderConfig;
|
||||
import sesim.TraderConfig_old;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class SwitchingTrader extends RandomTrader{
|
||||
public class SwitchingTrader extends RandomTrader_old{
|
||||
|
||||
|
||||
private Action mode;
|
||||
|
||||
public SwitchingTrader(Account_old account, TraderConfig config) {
|
||||
public SwitchingTrader(Account_old account, TraderConfig_old config) {
|
||||
|
||||
super(account, config);
|
||||
// System.out.print("SWTrader Created\n");
|
||||
|
@ -26,17 +26,17 @@
|
||||
package traders;
|
||||
|
||||
import sesim.Account_old;
|
||||
import sesim.AutoTrader;
|
||||
import sesim.AutoTrader_old;
|
||||
import sesim.Exchange;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class SwitchingTraderConfig extends RandomTraderConfig {
|
||||
public class SwitchingTraderConfig extends RandomTraderConfig_old {
|
||||
|
||||
@Override
|
||||
public AutoTrader createTrader(Exchange se, long shares, double money) {
|
||||
public AutoTrader_old createTrader(Exchange se, long shares, double money) {
|
||||
Account_old a = new Account_old(se, shares, money);
|
||||
System.out.print("Returning a new sw trader\n");
|
||||
return new SwitchingTrader(a, this);
|
||||
|
Loading…
Reference in New Issue
Block a user