progress for regression backtesting now implemented

This commit is contained in:
Dennis Thiessen
2017-07-14 16:06:24 +02:00
parent 0a50e8babb
commit 1ac35c871a

View File

@@ -20,6 +20,7 @@ import java.io.*;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.*; import java.util.*;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
public class Main { public class Main {
private static final String APP_NAME = "den_ac_deka_ct3_15_35"; private static final String APP_NAME = "den_ac_deka_ct3_15_35";
@@ -69,55 +70,33 @@ public class Main {
funds.addFund("ETH", VALUE_ETH); funds.addFund("ETH", VALUE_ETH);
funds.addFund("EUR", VALUE_EUR); funds.addFund("EUR", VALUE_EUR);
System.out.println("Do Backtesting? y/r/n"); System.out.println("Do backtesting? y (yes)/r (yes - regression)/n (no)");
Scanner in = new Scanner(System.in); Scanner in = new Scanner(System.in);
String input = in.next(); String input = in.next();
//String input = "n";
if(input.equals("r")){ switch(input){
regressionbacktest = true; case "y":
startBacktesting();
for(int x = 4; x < 30; x++){ break;
System.out.println("x:"+x); case "r":
NUM_ITERATIONS_FOR_ORDER = x; startRegressionBacktesting();
for(int y = x-1;y < 3*x;y++){ break;
BUY_THRESHOLD = y; case "n":
SELL_THRESHOLD = -y; startBot();
break;
doBacktest(); default:
System.out.println("Unrecognized input");
doOrder(new Order("buy")); break;
results.put(x+":"+y,funds.getValue("ETH"));
}
}
System.out.println("Output of best results: ");
HashMap<String, Double> sortedResult = (HashMap<String, Double>) sortByValue(results);
for (Map.Entry<String, Double> entry : sortedResult.entrySet()) {
System.out.println(entry.getKey() + " Value: "+entry.getValue());
}
return;
}else if(input.equals("y")){
backtest = true;
doBacktest();
doOrder(new Order("buy"));
System.out.println("");
System.out.println("Final ETH count: "+funds.getValue("ETH"));
System.out.println("Buys: "+counterbuys+" Sells: "+countersells+" Neutral: "+counterneutrals+" Fees: "+fees);
return;
} }
}
private static void startBot() throws InterruptedException {
sendStats(); sendStats();
System.out.println(funds.getFundsStringOutput()); System.out.println(funds.getFundsStringOutput());
System.out.println("-------Start-------"); System.out.println("-------Start-------");
//Initial rest request to get a timeframe
try { try {
HttpResponse<JsonNode> jsonResponse = Unirest.post("https://api.kraken.com/0/public/Trades") HttpResponse<JsonNode> jsonResponse = Unirest.post("https://api.kraken.com/0/public/Trades")
.header("accept", "application/json") .header("accept", "application/json")
@@ -141,6 +120,7 @@ public class Main {
outputAnalyzationResults(); outputAnalyzationResults();
if (((counter % NUM_ITERATIONS_FOR_ORDER) == 0) && (counter != 0)) { if (((counter % NUM_ITERATIONS_FOR_ORDER) == 0) && (counter != 0)) {
Order order = decide(); Order order = decide();
if (order.ordertype != null) doOrder(order); if (order.ordertype != null) doOrder(order);
@@ -158,9 +138,57 @@ public class Main {
} }
} }
private static void startBacktesting() {
backtest = true;
doBacktest();
//Buy ETH from remaining funds so the testing will end only with ETH
doOrder(new Order("buy"));
System.out.println("Final ETH count: "+funds.getValue("ETH"));
System.out.println("Buys: "+counterbuys+" Sells: "+countersells+" Neutral: "+counterneutrals+" Fees: "+fees);
}
private static void startRegressionBacktesting() {
regressionbacktest = true;
long startTime = System.currentTimeMillis();
int numofiters = 0;
for(int x = 4; x < 30; x++){
numofiters += 3 * x - (x-1);
}
int iter = 0;
for(int x = 4; x < 30; x++){
NUM_ITERATIONS_FOR_ORDER = x;
for(int y = x-1;y < 3*x;y++){
iter++;
printProgress(startTime,numofiters,iter);
BUY_THRESHOLD = y;
SELL_THRESHOLD = -y;
doBacktest();
results.put(x+":"+y,funds.getValue("ETH"));
}
}
System.out.println("Output of best results: ");
HashMap<String, Double> sortedResult = (HashMap<String, Double>) sortByValue(results);
for (Map.Entry<String, Double> entry : sortedResult.entrySet()) {
System.out.println(entry.getKey() + " Value: "+entry.getValue());
}
}
private static void doOrder(Order order) { private static void doOrder(Order order) {
Double tmpFees = 0.0; Double tmpFees = 0.0;
switch(order.ordertype){ switch(order.ordertype){
case "buy": case "buy":
if (Double.compare(funds.getValue("EUR"), 0) > 0) { if (Double.compare(funds.getValue("EUR"), 0) > 0) {
@@ -206,12 +234,12 @@ public class Main {
newOrder.timeframe = (double)counter; newOrder.timeframe = (double)counter;
if(!regressionbacktest) System.out.println("------- START ORDER -------"); if(!regressionbacktest) System.out.println("------- START ORDER -------");
if (buyindicator > BUY_THRESHOLD) {
if (buyindicator > BUY_THRESHOLD) {
if(!regressionbacktest) System.out.println("BUY NOW - PRICE: " + currentPrice); if(!regressionbacktest) System.out.println("BUY NOW - PRICE: " + currentPrice);
newOrder.ordertype = "buy"; newOrder.ordertype = "buy";
} else if (buyindicator > SELL_THRESHOLD) { } else if (buyindicator >= SELL_THRESHOLD) {
if(!regressionbacktest) System.out.println("DO NOTHING - NEUTRAL: " + currentPrice); if(!regressionbacktest) System.out.println("DO NOTHING - NEUTRAL: " + currentPrice);
counterneutrals++; counterneutrals++;
} else { } else {
@@ -224,6 +252,7 @@ public class Main {
if(!regressionbacktest) System.out.println("Would like to sell but percentage not reached"); if(!regressionbacktest) System.out.println("Would like to sell but percentage not reached");
} }
} }
if(!regressionbacktest) System.out.println("------- END ORDER -------"); if(!regressionbacktest) System.out.println("------- END ORDER -------");
return newOrder; return newOrder;
@@ -418,7 +447,7 @@ public class Main {
ma15.add(currentPrice); ma15.add(currentPrice);
ma50.add(currentPrice); ma50.add(currentPrice);
graphValuesBase.add(currentPrice); if(!regressionbacktest) return;graphValuesBase.add(currentPrice);
graphValues1.add(ma15.getAverage().doubleValue()); graphValues1.add(ma15.getAverage().doubleValue());
graphValues2.add(ma50.getAverage().doubleValue()); graphValues2.add(ma50.getAverage().doubleValue());
@@ -444,8 +473,10 @@ public class Main {
counter++; counter++;
} }
System.out.println("utils.DrawGraph -- "+graphValues1.size()); doOrder(new Order("buy"));
drawGraph("Threshold: "+BUY_THRESHOLD+" Iterations: "+NUM_ITERATIONS_FOR_ORDER, graphValuesBase, graphValues1, graphValues2);
if(!regressionbacktest) System.out.println("utils.DrawGraph -- "+graphValues1.size());
if(!regressionbacktest) drawGraph("Threshold: "+BUY_THRESHOLD+" Iterations: "+NUM_ITERATIONS_FOR_ORDER, graphValuesBase, graphValues1, graphValues2);
} catch (IOException e) { } catch (IOException e) {
System.out.println("Error:" + e.getMessage()); System.out.println("Error:" + e.getMessage());
@@ -576,4 +607,29 @@ public class Main {
frame.setVisible(true); frame.setVisible(true);
} }
private static void printProgress(long startTime, long total, long current) {
long eta = current == 0 ? 0 :
(total - current) * (System.currentTimeMillis() - startTime) / current;
String etaHms = current == 0 ? "N/A" :
String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(eta),
TimeUnit.MILLISECONDS.toMinutes(eta) % TimeUnit.HOURS.toMinutes(1),
TimeUnit.MILLISECONDS.toSeconds(eta) % TimeUnit.MINUTES.toSeconds(1));
StringBuilder string = new StringBuilder(140);
int percent = (int) (current * 100 / total);
string
.append('\r')
.append(String.join("", Collections.nCopies(percent == 0 ? 2 : 2 - (int) (Math.log10(percent)), " ")))
.append(String.format(" %d%% [", percent))
.append(String.join("", Collections.nCopies(percent, "=")))
.append('>')
.append(String.join("", Collections.nCopies(100 - percent, " ")))
.append(']')
.append(String.join("", Collections.nCopies((int) (Math.log10(total)) - (int) (Math.log10(current)), " ")))
.append(String.format(" %d/%d, ETA: %s", current, total, etaHms));
System.out.print(string);
}
} }