progress for regression backtesting now implemented
This commit is contained in:
@@ -20,6 +20,7 @@ import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class Main {
|
||||
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("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);
|
||||
String input = in.next();
|
||||
//String input = "n";
|
||||
|
||||
if(input.equals("r")){
|
||||
regressionbacktest = true;
|
||||
|
||||
for(int x = 4; x < 30; x++){
|
||||
System.out.println("x:"+x);
|
||||
NUM_ITERATIONS_FOR_ORDER = x;
|
||||
for(int y = x-1;y < 3*x;y++){
|
||||
BUY_THRESHOLD = y;
|
||||
SELL_THRESHOLD = -y;
|
||||
|
||||
doBacktest();
|
||||
|
||||
doOrder(new Order("buy"));
|
||||
results.put(x+":"+y,funds.getValue("ETH"));
|
||||
switch(input){
|
||||
case "y":
|
||||
startBacktesting();
|
||||
break;
|
||||
case "r":
|
||||
startRegressionBacktesting();
|
||||
break;
|
||||
case "n":
|
||||
startBot();
|
||||
break;
|
||||
default:
|
||||
System.out.println("Unrecognized input");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
System.out.println(funds.getFundsStringOutput());
|
||||
System.out.println("-------Start-------");
|
||||
|
||||
//Initial rest request to get a timeframe
|
||||
try {
|
||||
HttpResponse<JsonNode> jsonResponse = Unirest.post("https://api.kraken.com/0/public/Trades")
|
||||
.header("accept", "application/json")
|
||||
@@ -141,6 +120,7 @@ public class Main {
|
||||
outputAnalyzationResults();
|
||||
|
||||
if (((counter % NUM_ITERATIONS_FOR_ORDER) == 0) && (counter != 0)) {
|
||||
|
||||
Order order = decide();
|
||||
|
||||
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) {
|
||||
Double tmpFees = 0.0;
|
||||
|
||||
switch(order.ordertype){
|
||||
case "buy":
|
||||
if (Double.compare(funds.getValue("EUR"), 0) > 0) {
|
||||
@@ -206,12 +234,12 @@ public class Main {
|
||||
newOrder.timeframe = (double)counter;
|
||||
|
||||
if(!regressionbacktest) System.out.println("------- START ORDER -------");
|
||||
if (buyindicator > BUY_THRESHOLD) {
|
||||
|
||||
if (buyindicator > BUY_THRESHOLD) {
|
||||
if(!regressionbacktest) System.out.println("BUY NOW - PRICE: " + currentPrice);
|
||||
newOrder.ordertype = "buy";
|
||||
|
||||
} else if (buyindicator > SELL_THRESHOLD) {
|
||||
} else if (buyindicator >= SELL_THRESHOLD) {
|
||||
if(!regressionbacktest) System.out.println("DO NOTHING - NEUTRAL: " + currentPrice);
|
||||
counterneutrals++;
|
||||
} 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("------- END ORDER -------");
|
||||
|
||||
return newOrder;
|
||||
@@ -418,7 +447,7 @@ public class Main {
|
||||
ma15.add(currentPrice);
|
||||
ma50.add(currentPrice);
|
||||
|
||||
graphValuesBase.add(currentPrice);
|
||||
if(!regressionbacktest) return;graphValuesBase.add(currentPrice);
|
||||
graphValues1.add(ma15.getAverage().doubleValue());
|
||||
graphValues2.add(ma50.getAverage().doubleValue());
|
||||
|
||||
@@ -444,8 +473,10 @@ public class Main {
|
||||
counter++;
|
||||
}
|
||||
|
||||
System.out.println("utils.DrawGraph -- "+graphValues1.size());
|
||||
drawGraph("Threshold: "+BUY_THRESHOLD+" Iterations: "+NUM_ITERATIONS_FOR_ORDER, graphValuesBase, graphValues1, graphValues2);
|
||||
doOrder(new Order("buy"));
|
||||
|
||||
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) {
|
||||
System.out.println("Error:" + e.getMessage());
|
||||
@@ -576,4 +607,29 @@ public class Main {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user