minor refactoring

This commit is contained in:
Dennis Thiessen
2017-07-17 16:20:10 +02:00
parent 1ac35c871a
commit b34621ac3a

View File

@@ -46,7 +46,7 @@ public class Main {
private static double priceslope = 0.0;
private static int buyindicator = 0;
private static double currentPrice = 0.0;
private static int counter = 0;
private static int currentIteration = 0;
private static double fees = 0.0;
private static long sinceid = 0L;
private static String trend = "";
@@ -70,28 +70,28 @@ public class Main {
funds.addFund("ETH", VALUE_ETH);
funds.addFund("EUR", VALUE_EUR);
System.out.println("Do backtesting? y (yes)/r (yes - regression)/n (no)");
System.out.println("Do backtesting? (n = no || y = yes || r = regression-backtest)");
Scanner in = new Scanner(System.in);
String input = in.next();
switch(input){
case "y":
startBacktesting();
startSingleBacktesting();
break;
case "r":
startRegressionBacktesting();
break;
case "n":
startBot();
startLiveTrading();
break;
default:
System.out.println("Unrecognized input");
System.out.println("Unrecognized input. Run again...");
break;
}
}
private static void startBot() throws InterruptedException {
sendStats();
private static void startLiveTrading() throws InterruptedException {
sendCurrentStatsToLogServer();
System.out.println(funds.getFundsStringOutput());
System.out.println("-------Start-------");
@@ -109,17 +109,20 @@ public class Main {
return;
}
//Bot will run forever - kill task to end trading
while (true) {
Thread.sleep(ROUNDTRIP_TIME_MS);
getPrice();
getVolume();
getCurrentPrice();
getCurrentVolume();
// decide which trend calculation should be used
trend = calculateTrend_3();
outputAnalyzationResults();
if (((counter % NUM_ITERATIONS_FOR_ORDER) == 0) && (counter != 0)) {
//every NUM_ITERATIONS_FOR_ORDER start decision making process
if (((currentIteration % NUM_ITERATIONS_FOR_ORDER) == 0) && (currentIteration != 0)) {
Order order = decide();
@@ -133,12 +136,12 @@ public class Main {
srprice.clear();
}
counter++;
System.out.println("-------" + counter + "-------");
currentIteration++;
System.out.println("-------" + currentIteration + "-------");
}
}
private static void startBacktesting() {
private static void startSingleBacktesting() {
backtest = true;
doBacktest();
@@ -225,13 +228,13 @@ public class Main {
}
fees += tmpFees;
sendStats();
sendCurrentStatsToLogServer();
}
private static Order decide() {
Order newOrder = new Order();
newOrder.price = currentPrice;
newOrder.timeframe = (double)counter;
newOrder.timeframe = (double) currentIteration;
if(!regressionbacktest) System.out.println("------- START ORDER -------");
@@ -261,7 +264,7 @@ public class Main {
private static Order decide2() {
Order newOrder = new Order();
newOrder.price = currentPrice;
newOrder.timeframe = (double)counter;
newOrder.timeframe = (double) currentIteration;
if(!regressionbacktest) System.out.println("------- START ORDER -------");
if (buyindicator > BUY_THRESHOLD) {
@@ -379,7 +382,7 @@ public class Main {
}
private static String calculateTrend_4() {
if(counter < 500) return "neutral";
if(currentIteration < 500) return "neutral";
if(ma15.getAverage().compareTo(ma50.getAverage()) == 1){
buyindicator += 3;
@@ -402,9 +405,9 @@ public class Main {
funds.addFund("ETH", VALUE_ETH);
funds.addFund("EUR", VALUE_EUR);
String priceline = "";
String volumeline = "";
counter = 0;
String priceline;
String volumeline;
currentIteration = 0;
InputStream fis = new FileInputStream(System.getProperty("user.dir")+"\\src\\main\\resources\\price.txt");
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
@@ -432,9 +435,9 @@ public class Main {
trades = new TradeHistory(volumeArray, sinceid);
srbuyvolume.addData((double) counter, trades.volumeOfBuyTrades);
srsellvolume.addData((double) counter, trades.volumeOfSellTrades);
srprice.addData((double) counter, currentPrice);
srbuyvolume.addData((double) currentIteration, trades.volumeOfBuyTrades);
srsellvolume.addData((double) currentIteration, trades.volumeOfSellTrades);
srprice.addData((double) currentIteration, currentPrice);
priceslope = srprice.getSlope();
buyvslope = srbuyvolume.getSlope();
@@ -453,7 +456,7 @@ public class Main {
outputAnalyzationResults();
if (((counter % NUM_ITERATIONS_FOR_ORDER) == 0) && (counter != 0)) {
if (((currentIteration % NUM_ITERATIONS_FOR_ORDER) == 0) && (currentIteration != 0)) {
/* DECIDE DECIDE CALCULATOR */
Order order = decide2();
@@ -469,8 +472,8 @@ public class Main {
srprice.clear();
}
if(!regressionbacktest && !backtest) System.out.println("-------" + counter + "-------");
counter++;
if(!regressionbacktest && !backtest) System.out.println("-------" + currentIteration + "-------");
currentIteration++;
}
doOrder(new Order("buy"));
@@ -483,7 +486,7 @@ public class Main {
}
}
private static void getPrice() {
private static void getCurrentPrice() {
try {
HttpResponse<JsonNode> jsonResponse;
jsonResponse = Unirest.post("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR")
@@ -495,13 +498,13 @@ public class Main {
writeResponseToFile(jsonResponse,"price");
currentPrice = jsonResponse.getBody().getObject().getDouble("EUR");
srprice.addData((double) counter, currentPrice);
srprice.addData((double) currentIteration, currentPrice);
} catch (UnirestException ue) {
System.out.println("Exception was thrown - just continue and ignore");
}
}
private static void getVolume() {
private static void getCurrentVolume() {
try {
HttpResponse<JsonNode> jsonResponse = Unirest.post("https://api.kraken.com/0/public/Trades")
.header("accept", "application/json")
@@ -515,8 +518,8 @@ public class Main {
sinceid = jsonResponse.getBody().getObject().getJSONObject("result").getLong("last");
trades = new TradeHistory(array, sinceid);
srbuyvolume.addData((double) counter, trades.volumeOfBuyTrades);
srsellvolume.addData((double) counter, trades.volumeOfSellTrades);
srbuyvolume.addData((double) currentIteration, trades.volumeOfBuyTrades);
srsellvolume.addData((double) currentIteration, trades.volumeOfSellTrades);
priceslope = srprice.getSlope();
buyvslope = srbuyvolume.getSlope();
@@ -527,7 +530,7 @@ public class Main {
}
}
private static void sendStats() {
private static void sendCurrentStatsToLogServer() {
if(regressionbacktest || backtest) return;
try {
@@ -568,7 +571,7 @@ public class Main {
private static <K, V> Map<K, V> sortByValue(Map<K, V> map) {
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, new Comparator<Object>() {
list.sort( new Comparator<Object>() {
@SuppressWarnings("unchecked")
public int compare(Object o2, Object o1) {
return ((Comparable<V>) ((Map.Entry<K, V>) (o1)).getValue()).compareTo(((Map.Entry<K, V>) (o2)).getValue());
@@ -577,7 +580,7 @@ public class Main {
Map<K, V> result = new LinkedHashMap<>();
for (Iterator<Map.Entry<K, V>> it = list.iterator(); it.hasNext();) {
Map.Entry<K, V> entry = (Map.Entry<K, V>) it.next();
Map.Entry<K, V> entry = it.next();
result.put(entry.getKey(), entry.getValue());
}