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