Addad Autotrades object to let thousands of trades trade

This commit is contained in:
7u83
2016-12-28 20:08:55 +01:00
parent 2b575d87dd
commit 811cc0636d
8 changed files with 216 additions and 94 deletions

View File

@ -7,6 +7,13 @@ import SeSim.Trader;
import SeSim.TraderConfig;
public class RandomTrader extends Trader {
public TraderConfig getTraderConfig(){
return new RandomTraderConfig();
}
private RandomTraderConfig myconfig;
Random rand = new Random();
@ -14,36 +21,43 @@ public class RandomTrader extends Trader {
//private Order myorder = null;
public RandomTrader(Account account,TraderConfig config) {
super(account,config);
if (config==null){
config = new RandomTraderConfig();
}
myconfig = (RandomTraderConfig)config;
}
public void doBuy() {
// if (account.money <= 0) {
// return;
// }
if (account.money <= 0) {
return;
}
double perc = rand.nextDouble() * 1.0;
double perc = 5.0-rand.nextDouble() * 10.0;
double lp = account.se.getlastprice();
double limit = lp / 100 * perc + lp;
long size = (int) (account.money / (limit * 1));
long volume = (int) (account.money / (limit * 1));
if (volume ==0 ){
System.out.print("Can't buy shares = 0 my money="+account.money+" shares="+account.shares+"\n");
return;
}
buy(size, limit);
buy(volume, limit);
return;
}
public void doSell() {
/* if (myorder != null) {
return;
}
if (account.shares <= 0) {
return;
}
*/
double perc = rand.nextDouble() * 1.0;
double perc = 5.0-rand.nextDouble() * 10.0;
double lp = account.se.getlastprice();
double limit = lp - lp / 100 * perc;
@ -57,7 +71,7 @@ public class RandomTrader extends Trader {
// System.out.print("RT: Monitoring trades - Pending: "+numpending+"\n");
if (numpending == 0) {
System.out.print("RT: pending = 0 - return false\n");
// System.out.print("RT: pending = 0 - return false\n");
return false;
}
@ -66,9 +80,10 @@ public class RandomTrader extends Trader {
// System.out.print("RT: age is: "+age+"\n");
if (age > 10000) {
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");
// System.out.print("Age reached - canel return false\n");
return false;
}
@ -87,7 +102,11 @@ public class RandomTrader extends Trader {
// What next to do?
int action = rand.nextInt(3);
int action = rand.nextInt(5);
if (account.money<10 && account.shares<5){
System.out.print("I'm almost ruined\n");
}
if (action == 1) {
doBuy();
@ -101,22 +120,5 @@ public class RandomTrader extends Trader {
}
/* public void run(){
while (true)
{
try{
sleep(200);
}
catch(InterruptedException e) {
System.out.println("Interrupted");
}
// System.out.println("Trader has slept");
trade();
}
}
*/
}

View File

@ -0,0 +1,53 @@
/*
* 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.*;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class RandomTraderConfig extends TraderConfig {
public long maxage = 1000 * 10 * 1;
/**
*
* @param se
* @param shares
* @param money
* @return
*/
@Override
public Trader createTrader(Exchange se, long shares, double money) {
Account a = new Account(se, shares, money);
return new RandomTrader(a, this);
}
}