SwitchingTrader + Chart
This commit is contained in:
@ -32,6 +32,10 @@ import SeSim.AutoTrader;
|
||||
import SeSim.TraderConfig;
|
||||
|
||||
public class RandomTrader extends AutoTrader {
|
||||
|
||||
protected enum Action {
|
||||
sell,buy
|
||||
}
|
||||
|
||||
// config for this trader
|
||||
final private RandomTraderConfig myconfig;
|
||||
@ -58,9 +62,9 @@ public class RandomTrader extends AutoTrader {
|
||||
double r = rand.nextDouble();
|
||||
return (max - min) * r + min;
|
||||
}
|
||||
|
||||
protected int getRandom(int[] minmax){
|
||||
return (int)Math.round(getRandom(minmax[0],minmax[1]));
|
||||
|
||||
protected int getRandom(int[] minmax) {
|
||||
return (int) Math.round(getRandom(minmax[0], minmax[1]));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -74,42 +78,37 @@ public class RandomTrader extends AutoTrader {
|
||||
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++ ){
|
||||
|
||||
public boolean waitForOrder(long seconds) {
|
||||
|
||||
for (int i = 0; (i < seconds) && (0 != account.pending.size()); i++) {
|
||||
doSleep(1);
|
||||
}
|
||||
|
||||
if (account.pending.size()!=0){
|
||||
Order o=account.pending.get(0);
|
||||
account.se.CancelOrder(o);
|
||||
|
||||
if (account.pending.size() != 0) {
|
||||
Order 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 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)
|
||||
if (volume <= 0) {
|
||||
return false;
|
||||
|
||||
|
||||
}
|
||||
|
||||
buy(volume, limit);
|
||||
return waitForOrder(getRandom(myconfig.buy_order_wait));
|
||||
|
||||
|
||||
}
|
||||
|
||||
public boolean doSell() {
|
||||
@ -123,10 +122,10 @@ public class RandomTrader extends AutoTrader {
|
||||
|
||||
sell(volume, limit);
|
||||
return waitForOrder(getRandom(myconfig.sell_order_wait));
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* private boolean monitorTrades() {
|
||||
/* private boolean monitorTrades() {
|
||||
|
||||
int numpending = account.pending.size();
|
||||
if (numpending == 0) {
|
||||
@ -148,21 +147,20 @@ public class RandomTrader extends AutoTrader {
|
||||
//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(
|
||||
/* System.out.print(
|
||||
"Random:"
|
||||
+ x
|
||||
+ "\n"
|
||||
);
|
||||
|
||||
*/
|
||||
/*
|
||||
*/
|
||||
/*
|
||||
// System.out.print("RT: Now trading\n");
|
||||
if (monitorTrades()) {
|
||||
return;
|
||||
@ -187,52 +185,65 @@ public class RandomTrader extends AutoTrader {
|
||||
*/
|
||||
}
|
||||
|
||||
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? (0=sell, 1=buy)
|
||||
int action = rand.nextInt(2);
|
||||
|
||||
if (account.isRuined()){
|
||||
// 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 0: //sell
|
||||
if(account.shares<=0){
|
||||
// action=1;
|
||||
switch (action) {
|
||||
|
||||
case sell:
|
||||
if (account.shares <= 0) {
|
||||
// we have no shares
|
||||
continue;
|
||||
}
|
||||
// System.out.print("Sell\n");
|
||||
rc = doSell();
|
||||
if (!rc)
|
||||
if (!rc) {
|
||||
continue;
|
||||
}
|
||||
// System.out.print("Sold\n");
|
||||
doSleep(getRandom(myconfig.wait_after_sell));
|
||||
// System.out.print("Next\n");
|
||||
break;
|
||||
case 1: //sell
|
||||
if(account.money<=0){
|
||||
case buy:
|
||||
if (account.money <= 0) {
|
||||
// we have no money
|
||||
continue;
|
||||
}
|
||||
// System.out.print("Sell\n");
|
||||
rc = doBuy();
|
||||
if (!rc)
|
||||
if (!rc) {
|
||||
continue;
|
||||
}
|
||||
// System.out.print("Bought\n");
|
||||
doSleep(getRandom(myconfig.wait_after_buy));
|
||||
// System.out.print("Next\n");
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
// doSleep(1);
|
||||
|
||||
|
||||
// doSleep(1);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -50,12 +50,12 @@ public class RandomTraderConfig extends TraderConfig {
|
||||
public float[] sell_volume= {100,100};
|
||||
public float[] sell_limit = {-15,100};
|
||||
public int[] sell_order_wait = {5,33};
|
||||
public int[] wait_after_sell = {20,33};
|
||||
public int[] wait_after_sell = {2,10};
|
||||
|
||||
|
||||
public float[] buy_volume={100,100};
|
||||
public float[] buy_limit = {-50,5};
|
||||
public int[] buy_order_wait = {5,33};
|
||||
public float[] buy_limit = {-5,115};
|
||||
public int[] buy_order_wait = {15,33};
|
||||
public int[] wait_after_buy = {20,33};
|
||||
|
||||
|
||||
|
107
src/Traders/SwitchingTrader.java
Normal file
107
src/Traders/SwitchingTrader.java
Normal file
@ -0,0 +1,107 @@
|
||||
/*
|
||||
* 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;
|
||||
import SeSim.TraderConfig;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class SwitchingTrader extends RandomTrader{
|
||||
|
||||
|
||||
private Action mode;
|
||||
|
||||
public SwitchingTrader(Account account, TraderConfig config) {
|
||||
|
||||
super(account, config);
|
||||
System.out.print("SWTrader Created\n");
|
||||
|
||||
if (account.shares>0)
|
||||
mode=Action.sell;
|
||||
else
|
||||
mode=Action.buy;
|
||||
printstartus();
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void printstartus(){
|
||||
|
||||
System.out.print("SWTrader:");
|
||||
switch (mode){
|
||||
case buy:
|
||||
System.out.print("buy"
|
||||
+account.shares
|
||||
+" "
|
||||
+account.money
|
||||
);
|
||||
break;
|
||||
case sell:
|
||||
System.out.print("sell"
|
||||
+account.shares
|
||||
+" "
|
||||
+account.money
|
||||
);
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
System.out.print("\n");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Action getAction(){
|
||||
|
||||
|
||||
|
||||
|
||||
if ( (account.shares>0) && (mode==Action.sell)){
|
||||
printstartus();
|
||||
return mode;
|
||||
}
|
||||
if ( (account.shares<=0 && mode==Action.sell)){
|
||||
mode=Action.buy;
|
||||
printstartus();
|
||||
return mode;
|
||||
}
|
||||
if (account.money>100.0 && mode==Action.buy){
|
||||
printstartus();
|
||||
return mode;
|
||||
}
|
||||
if (account.money<=100.0 && mode==Action.buy){
|
||||
mode=Action.sell;
|
||||
printstartus();
|
||||
return mode;
|
||||
}
|
||||
printstartus();
|
||||
return mode;
|
||||
}
|
||||
|
||||
}
|
57
src/Traders/SwitchingTraderConfig.java
Normal file
57
src/Traders/SwitchingTraderConfig.java
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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;
|
||||
import SeSim.AutoTrader;
|
||||
import SeSim.Exchange;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class SwitchingTraderConfig extends RandomTraderConfig {
|
||||
|
||||
@Override
|
||||
public AutoTrader createTrader(Exchange se, long shares, double money) {
|
||||
Account a = new Account(se, shares, money);
|
||||
System.out.print("Returning a new sw trader\n");
|
||||
return new SwitchingTrader(a, this);
|
||||
}
|
||||
|
||||
public SwitchingTraderConfig() {
|
||||
|
||||
sell_volume = new float[]{100, 100};
|
||||
sell_limit = new float[]{-15, 1};
|
||||
sell_order_wait = new int[]{5, 10};
|
||||
wait_after_sell = new int[]{2, 10};
|
||||
|
||||
buy_volume = new float[]{100, 100};
|
||||
buy_limit = new float[]{-5, 115};
|
||||
buy_order_wait = new int[]{15, 33};
|
||||
wait_after_buy = new int[]{20, 33};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user