diff --git a/dekaBot.iml b/dekaBot.iml index 1ae8f14..cfbf201 100644 --- a/dekaBot.iml +++ b/dekaBot.iml @@ -1,12 +1,11 @@ - + - @@ -21,18 +20,9 @@ - - - - - - - - - - - - + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 5e6cf19..a630da4 100644 --- a/pom.xml +++ b/pom.xml @@ -60,6 +60,11 @@ json 20140107 + + com.googlecode.json-simple + json-simple + 1.1.1 + joda-time joda-time diff --git a/src/main/java/Backtest.java b/src/main/java/Backtest.java new file mode 100644 index 0000000..49116c0 --- /dev/null +++ b/src/main/java/Backtest.java @@ -0,0 +1,54 @@ +import org.apache.commons.math3.stat.regression.SimpleRegression; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.*; +import java.nio.charset.Charset; +import java.util.*; + +/** + * Created by denni on 09.06.2017. + */ +public class Backtest { + private static final String APP_NAME = "den_ac_deka_var2"; + private static final double VALUE_ETH = 100.0; + private static final double VALUE_EUR = 0.0; + private static final int ROUNDTRIP_TIME_MS = 20000; + private static int NUM_ITERATIONS_FOR_ORDER = 9; + private static int BUY_THRESHOLD = 9; + private static int SELL_THRESHOLD = -9; + + private static SimpleRegression srbuyvolume = new SimpleRegression(true); + private static SimpleRegression srsellvolume = new SimpleRegression(true); + private static SimpleRegression srprice = new SimpleRegression(true); + + private static OrderHistory orders = new OrderHistory(); + private static Funds funds = new Funds(); + private static TradeHistory trades; + + private static double buyvslope = 0.0; + private static double sellvslope = 0.0; + private static double priceslope = 0.0; + private static int buyindicator = 0; + private static double currentPrice = 0.0; + private static int counter = 0; + private static double fees = 0.0; + private static long sinceid = 0L; + private static String trend = ""; + + private static int counterbuys = 0; + private static int countersells = 0; + private static int counterneutrals = 0; + + private static boolean backtest = false; + + private static Map results = new HashMap(); + + public Backtest(){ + + } + + + + +} diff --git a/src/main/java/Funds.java b/src/main/java/Funds.java index f96a5bb..c9e5528 100644 --- a/src/main/java/Funds.java +++ b/src/main/java/Funds.java @@ -11,7 +11,10 @@ public class Funds { public Funds(){ } + public void clearFunds(){ + if(funds != null) funds.clear(); + } public void addFund(String name, double value){ funds.put(name, value); @@ -30,7 +33,7 @@ public class Funds { } public String getFundsStringOutput(){ - String returnValue = "Funds: "; + String returnValue = ""; for(Map.Entry entry : funds.entrySet()){ returnValue += entry.getKey()+" : "+entry.getValue()+" "; diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 9e68ce4..cfbd42b 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -6,316 +6,491 @@ import org.apache.commons.math3.stat.regression.SimpleRegression; import org.json.JSONArray; import org.json.JSONObject; -import java.util.ArrayList; -import java.util.Scanner; +import java.io.*; +import java.nio.charset.Charset; +import java.util.*; public class Main { + private static final String APP_NAME = "den_ac_deka_ct3_15_35"; + private static final double VALUE_ETH = 100.0; + private static final double VALUE_EUR = 0.0; + private static final int ROUNDTRIP_TIME_MS = 20000; + private static int NUM_ITERATIONS_FOR_ORDER = 15; + private static int BUY_THRESHOLD = 35; + private static int SELL_THRESHOLD = -35; - static SimpleRegression srbuyvolume = new SimpleRegression(true); - static SimpleRegression srsellvolume = new SimpleRegression(true); - static SimpleRegression srprice = new SimpleRegression(true); + private static SimpleRegression srbuyvolume = new SimpleRegression(true); + private static SimpleRegression srsellvolume = new SimpleRegression(true); + private static SimpleRegression srprice = new SimpleRegression(true); - static ArrayList list = new ArrayList(); - static OrderHistory orders = new OrderHistory(); - public static Funds funds = new Funds(); - public static TradeHistory trades; + private static OrderHistory orders = new OrderHistory(); + private static Funds funds = new Funds(); + private static TradeHistory trades; - public static double buyvslope = 0.0; - public static double sellvslope = 0.0; - public static double priceslope = 0.0; - public static int buyindicator = 0; - public static double currentPrice = 0.0; - static int counter = 0; + private static double buyvslope = 0.0; + private static double sellvslope = 0.0; + private static double priceslope = 0.0; + private static int buyindicator = 0; + private static double currentPrice = 0.0; + private static int counter = 0; + private static double fees = 0.0; + private static long sinceid = 0L; + private static String trend = ""; - public static int counterbuys = 0; - public static int countersells = 0; - public static int counterneutrals = 0; + private static int counterbuys = 0; + private static int countersells = 0; + private static int counterneutrals = 0; - public static void main(String [ ] args) throws InterruptedException { + private static boolean regressionbacktest = false; + private static boolean backtest = false; + + private static Map results = new HashMap<>(); + + public static void main(String[] args) throws InterruptedException { System.out.println("Starting DekaBot..."); + + System.out.println("Starting simulation... initializing Funds..."); + + funds.addFund("ETH", VALUE_ETH); + funds.addFund("EUR", VALUE_EUR); + + System.out.println("Do Backtesting? y/r/n"); Scanner in = new Scanner(System.in); + String input = in.next(); + //String input = "n"; - System.out.println("Start simulation? (y/n)"); - String s = in.next(); + if(input.equals("r")){ + regressionbacktest = true; - long sinceid = 0L; + 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; - if(s.equals("y")){ - try{ - HttpResponse jsonResponse = Unirest.post("https://api.kraken.com/0/public/Trades") - .header("accept", "application/json") - .queryString("pair", "ETHEUR") - .asJson(); + doBacktest(); - sinceid = jsonResponse.getBody().getObject().getJSONObject("result").getLong("last"); - }catch(UnirestException ue){ - System.out.println("Could not establish connection."); - return; - } - - - System.out.println("Starting simulation... initializing Funds..."); - - funds.addFund("ETH", 0.0); - funds.addFund("EUR", 3000); - - try{ - sendStats(); - }catch(UnirestException ue){ - System.out.println("Could not send stats - just continue"); - } - - - System.out.println(funds.getFundsStringOutput()); - - while(true){ - Thread.sleep(20000); - - sinceid = analyzeVolume(sinceid); - analyzePrice(); - - System.out.println("Trend: "+calculateTrend_2()+" // Price: "+currentPrice); - - if(((counter % 6) == 0) && (counter != 0)){ - Order order = decide(); - - if(order.ordertype != null) doOrder(order); - - buyindicator = 0; - System.out.println("Funds: "+funds.getFundsStringOutput()); - - srbuyvolume.clear(); - srsellvolume.clear(); - srprice.clear(); + doOrder(new Order("buy")); + results.put(x+":"+y,funds.getValue("ETH")); } - - counter++; - System.out.println("-------"+counter+"-------"); } + + System.out.println("Output of best results: "); + HashMap sortedResult = (HashMap) sortByValue(results); + + for (Map.Entry 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; + } + + sendStats(); + + System.out.println(funds.getFundsStringOutput()); + System.out.println("-------Start-------"); + + try { + HttpResponse jsonResponse = Unirest.post("https://api.kraken.com/0/public/Trades") + .header("accept", "application/json") + .queryString("pair", "ETHEUR") + .asJson(); + + sinceid = jsonResponse.getBody().getObject().getJSONObject("result").getLong("last"); + } catch (UnirestException ue) { + System.out.println("Could not establish connection: " + ue.getMessage()); + return; + } + + + while (true) { + Thread.sleep(ROUNDTRIP_TIME_MS); + + getPrice(); + getVolume(); + + trend = calculateTrend_3(); + outputAnalyzationResults(); + + if (((counter % NUM_ITERATIONS_FOR_ORDER) == 0) && (counter != 0)) { + Order order = decide(); + + if (order.ordertype != null) doOrder(order); + + buyindicator = 0; + System.out.println("Funds: " + funds.getFundsStringOutput()); + + srbuyvolume.clear(); + srsellvolume.clear(); + srprice.clear(); + } + + counter++; + System.out.println("-------" + counter + "-------"); } } - private static void doOrder(Order order){ - if(order.ordertype.equals("buy")){ - if(Double.compare(funds.getValue("EUR"),0) > 0) { - orders.addOrder(order); - funds.addValue("ETH", funds.getValue("EUR") / currentPrice); - funds.takeValue("EUR", funds.getValue("EUR")); + private static void doOrder(Order order) { + Double tmpFees = 0.0; + switch(order.ordertype){ + case "buy": + if (Double.compare(funds.getValue("EUR"), 0) > 0) { + orders.addOrder(order); - counterbuys++; - }else{ - System.out.println("Would like to buy but conditions not met (out of funds)"); - } - }else if(order.ordertype.equals("sell")){ - if(Double.compare(funds.getValue("ETH"),0) > 0){ - orders.addOrder(order); + tmpFees = funds.getValue("EUR") * 0.0026; + funds.addValue("ETH", (funds.getValue("EUR") - tmpFees) / currentPrice); + funds.takeValue("EUR", funds.getValue("EUR")); - funds.addValue("EUR", funds.getValue("ETH") * currentPrice); - funds.takeValue("ETH", funds.getValue("ETH")); + if(!regressionbacktest) System.out.printf("Fees for order: %.3f - Total Fees: %.3f",tmpFees,fees); + counterbuys++; + } else { + if(!regressionbacktest) System.out.println("Would like to buy but conditions not met (out of funds)"); + } + break; + case "sell": + if (Double.compare(funds.getValue("ETH"), 0) > 0) { + orders.addOrder(order); + + tmpFees = funds.getValue("ETH") * currentPrice * 0.0026; + funds.addValue("EUR", (funds.getValue("ETH") * currentPrice) - tmpFees); + funds.takeValue("ETH", funds.getValue("ETH")); + + if(!regressionbacktest) System.out.printf("Fees for order: %.3f - Total Fees: %.3f",tmpFees,fees); + countersells++; + } else { + if(!regressionbacktest) System.out.println("Would like to sell but conditions not met (out of funds)"); + } + break; + default: + System.out.println("ERROR: Order not recognized: " + order.type); + break; - countersells++; - }else{ - System.out.println("Would like to sell but conditions not met (out of funds)"); - } - }else{ - System.out.println("ERROR: Order not recoginized: "+order.type); } - try{ - sendStats(); - }catch(UnirestException ue){ - System.out.println("Error sending stats - just continue..."); - } - } - - private static void sendStats() throws UnirestException { - HttpResponse jsonResponse = Unirest.post("https://www.riskahead.de/api/v1/deka/updatestats/") - .header("accept", "application/json") - .queryString("app_id", "den_deka_2_6") - .field("funds_start", "EUR: 3000") - .field("funds_current", funds.getFundsStringOutput()) - .field("trades_buy", counterbuys) - .field("trades_sell", countersells) - .field("trades_neutral", counterneutrals) - .asJson(); + fees += tmpFees; + sendStats(); } private static Order decide() { Order newOrder = new Order(); newOrder.price = currentPrice; - if(buyindicator > 6){ + if(!regressionbacktest) System.out.println("------- START ORDER -------"); + if (buyindicator > BUY_THRESHOLD) { - System.out.println("BUY NOW - PRICE: "+currentPrice); - newOrder.ordertype = "buy"; + if(!regressionbacktest) System.out.println("BUY NOW - PRICE: " + currentPrice); + newOrder.ordertype = "buy"; - }else if(buyindicator > -6){ - System.out.println("DO NOTHING - NEUTRAL: "+currentPrice); + } else if (buyindicator > SELL_THRESHOLD) { + if(!regressionbacktest) System.out.println("DO NOTHING - NEUTRAL: " + currentPrice); counterneutrals++; - }else{ - if(orders.getLastOrder() == null || Double.compare(orders.getLastOrder().price,currentPrice*0.995) < 0 || Double.compare(orders.getLastOrder().price,currentPrice*0.985) > 0){ + } else { + if (orders.getLastOrder() == null || Double.compare(orders.getLastOrder().price, currentPrice * 0.995) < 0 || Double.compare(orders.getLastOrder().price, currentPrice * 0.985) > 0) { - System.out.println("SELL NOW - PRICE: "+currentPrice); + if(!regressionbacktest) System.out.println("SELL NOW - PRICE: " + currentPrice); newOrder.ordertype = "sell"; - }else{ - System.out.println("Would like to sell but percentage not reached"); + } else { + if(!regressionbacktest) System.out.println("Would like to sell but percentage not reached"); } } + if(!regressionbacktest) System.out.println("------- END ORDER -------"); return newOrder; } - private static void analyzePrice() { - try{ - HttpResponse jsonResponse; - jsonResponse = Unirest.post("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR") - .header("accept", "application/json") - .queryString("fsyn", "ETH") - .field("tsyms", "BTC,USD,EUR") - .asJson(); + private static String calculateTrend() { + if ((Double.compare(buyvslope, 0) > 0) && (Double.compare(priceslope, 0) > 0)) { + if (Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) > 0) { + buyindicator += 3; + return "strong buy"; + } else { + buyindicator += 2; + return "normal buy"; + } + } else if ((Double.compare(sellvslope, 0) > 0) && (Double.compare(priceslope, 0) < 0)) { + if (Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) < 0) { + buyindicator -= 3; + return "strong sell"; + } else { + buyindicator -= 2; + return "normal sell"; + } + } else if (Double.compare(buyvslope, sellvslope) > 0 && (Double.compare(priceslope, 0) > 0)) { + buyindicator += 1; + return "weak buy"; + } else if (Double.compare(buyvslope, sellvslope) < 0 && (Double.compare(priceslope, 0) < 0)) { + buyindicator -= 1; + return "weak sell"; + } else { + return "neutral"; + } + } + private static String calculateTrend_2() { + if (Double.isNaN(priceslope)) { + if (Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) > 0) { + buyindicator += 1; + return "weak buy"; + } else { + buyindicator -= 1; + return "weak sell"; + } + } else if ((Double.compare(priceslope, 0) > 0)) { + if ((Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) > 0) && (Double.compare(buyvslope, 0) > 0) && (Double.compare(sellvslope, 0) < 0)) { + buyindicator += 3; + return "strong buy"; + } else if ((Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) > 0) && (Double.compare(buyvslope, 0) > 0)) { + buyindicator += 2; + return "normal buy"; + } else { + buyindicator += 1; + return "weak buy"; + } + } else if ((Double.compare(priceslope, 0) < 0)) { + if ((Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) < 0) && (Double.compare(buyvslope, 0) < 0) && (Double.compare(sellvslope, 0) > 0)) { + buyindicator -= 3; + return "strong sell"; + } else if ((Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) < 0) && (Double.compare(sellvslope, 0) > 0)) { + buyindicator -= 2; + return "normal sell"; + } else { + buyindicator -= 1; + return "weak sell"; + } + } else { + return "neutral"; + } + } + + private static String calculateTrend_3() { + if (Double.isNaN(priceslope)) { + if (Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) > 0) { + buyindicator += 1; + return "weak buy"; + } else { + buyindicator -= 1; + return "weak sell"; + } + } else if ((Double.compare(priceslope, 0) > 0)) { + if ((Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) > 0)) { + buyindicator += 3; + return "strong buy"; + } else { + buyindicator += 2; + return "normal buy"; + } + } else if ((Double.compare(priceslope, 0) < 0)) { + if ((Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) < 0)) { + buyindicator -= 3; + return "strong sell"; + } else { + buyindicator -= 2; + return "normal sell"; + } + } else { + return "neutral"; + } + } + + private static void doBacktest(){ + try { + funds.clearFunds(); + orders.clearOrders(); + if(trades != null) trades.clearTrades(); + funds.addFund("ETH", VALUE_ETH); + funds.addFund("EUR", VALUE_EUR); + + String priceline = ""; + String volumeline = ""; + int counter = 0; + + InputStream fis = new FileInputStream("C:\\Development\\Projects\\DekaBot\\DekaBot\\src\\main\\resources\\price.txt"); + InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8")); + BufferedReader br = new BufferedReader(isr); + + InputStream fis2 = new FileInputStream("C:\\Development\\Projects\\DekaBot\\DekaBot\\src\\main\\resources\\volume.txt"); + InputStreamReader isr2 = new InputStreamReader(fis2, Charset.forName("UTF-8")); + BufferedReader br2 = new BufferedReader(isr2); + + while ((priceline = br.readLine()) != null) { + volumeline = br2.readLine(); + + priceline = br.readLine(); + volumeline = br2.readLine(); + + if(volumeline == null || priceline == null){ + return; + } + + if(volumeline.charAt(volumeline.length()-1) != '}'){ + volumeline += br2.readLine(); + } + + JSONObject jsonPriceObject = new JSONObject(priceline); + JSONObject jsonVolumeObject = new JSONObject(volumeline); + + jsonVolumeObject = jsonVolumeObject.getJSONObject("result"); + sinceid = jsonVolumeObject.getLong("last"); + JSONArray volumeArray = jsonVolumeObject.getJSONArray("XETHZEUR"); + + + currentPrice = jsonPriceObject.getDouble("EUR"); + srprice.addData((double) counter, currentPrice); + + trades = new TradeHistory(volumeArray, sinceid); + + srbuyvolume.addData((double) counter, trades.volumeOfBuyTrades); + srsellvolume.addData((double) counter, trades.volumeOfSellTrades); + + priceslope = srprice.getSlope(); + buyvslope = srbuyvolume.getSlope(); + sellvslope = srsellvolume.getSlope(); + + trend = calculateTrend_2(); + outputAnalyzationResults(); + + if (((counter % NUM_ITERATIONS_FOR_ORDER) == 0) && (counter != 0)) { + Order order = decide(); + + if (order.ordertype != null) doOrder(order); + + buyindicator = 0; + if(!regressionbacktest) System.out.println("Funds: " + funds.getFundsStringOutput()); + + srbuyvolume.clear(); + srsellvolume.clear(); + srprice.clear(); + } + + if(!regressionbacktest && !backtest) System.out.println("-------" + counter + "-------"); + counter++; + } + + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void getPrice() { + try { + HttpResponse jsonResponse; + jsonResponse = Unirest.post("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR") + .header("accept", "application/json") + .queryString("fsyn", "ETH") + .field("tsyms", "BTC,USD,EUR") + .asJson(); + + writeResponseToFile(jsonResponse,"price"); currentPrice = jsonResponse.getBody().getObject().getDouble("EUR"); - - srprice.addData((double)counter,currentPrice); - System.out.printf("Current price slope: %.3f \n",srprice.getSlope()); - }catch(UnirestException ue){ + srprice.addData((double) counter, currentPrice); + } catch (UnirestException ue) { System.out.println("Exception was thrown - just continue and ignore"); } } - private static String calculateTrend(){ - if((Double.compare(buyvslope, 0) > 0) && (Double.compare(priceslope, 0) > 0)){ - if(Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) > 0){ - buyindicator += 3; - return "strong buy"; - }else{ - buyindicator += 2; - return "normal buy"; - } - }else if((Double.compare(sellvslope, 0) > 0) && (Double.compare(priceslope, 0) < 0)){ - if(Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) < 0){ - buyindicator -= 3; - return "strong sell"; - }else{ - buyindicator -= 2; - return "normal sell"; - } - }else if(Double.compare(buyvslope, sellvslope) > 0 && (Double.compare(priceslope, 0) > 0)){ - buyindicator += 1; - return "weak buy"; - }else if(Double.compare(buyvslope, sellvslope) < 0 && (Double.compare(priceslope, 0) < 0)){ - buyindicator -= 1; - return "weak sell"; - }else{ - return "neutral"; - } - } - - private static String calculateTrend_2(){ - if((Double.compare(priceslope, 0.1) > 0)){ - if((Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) > 0) && (Double.compare(buyvslope, 0) > 0) && (Double.compare(sellvslope, 0) < 0)){ - buyindicator += 5; - return "strong buy"; - }else if((Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) > 0) && (Double.compare(buyvslope, 0) > 0)){ - buyindicator += 3; - return "normal buy"; - }else{ - buyindicator += 1; - return "weak buy"; - } - }else if((Double.compare(priceslope, 0.0) < 0)){ - if((Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) < 0) && (Double.compare(buyvslope, 0) < 0) && (Double.compare(sellvslope, 0) > 0)){ - buyindicator -= 5; - return "strong sell"; - }else if((Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) < 0) && (Double.compare(sellvslope, 0) > 0)){ - buyindicator -= 3; - return "normal sell"; - }else{ - buyindicator -= 1; - return "weak sell"; - } - }else{ - return "neutral"; - } - } - - private static long analyzeVolume(long sinceid) { - try{ - HttpResponse jsonResponse; - jsonResponse = Unirest.post("https://api.kraken.com/0/public/Trades") + private static void getVolume() { + try { + HttpResponse jsonResponse = Unirest.post("https://api.kraken.com/0/public/Trades") .header("accept", "application/json") .queryString("pair", "ETHEUR") .field("since", sinceid) .asJson(); + writeResponseToFile(jsonResponse,"volume"); + JSONArray array = jsonResponse.getBody().getObject().getJSONObject("result").getJSONArray("XETHZEUR"); sinceid = jsonResponse.getBody().getObject().getJSONObject("result").getLong("last"); - trades = new TradeHistory(array,sinceid); + trades = new TradeHistory(array, sinceid); - System.out.print("Number of buy trades: "+trades.numberOfBuyTrades); - System.out.printf(" - Volume of buy trades: %.3f \n",trades.volumeOfBuyTrades); - System.out.print("Number of sell trades: "+trades.numberOfSellTrades); - System.out.printf(" - Volume of sell trades: %.3f \n",trades.volumeOfSellTrades); - System.out.println("Timeframe in sec: "+trades.timeframesec); - - srbuyvolume.addData((double) counter,trades.volumeOfBuyTrades); - System.out.printf("buy volume slope: %.3f //",srbuyvolume.getSlope()); - - srsellvolume.addData((double) counter,trades.volumeOfSellTrades); - System.out.printf("sell volume slope: %.3f \n",srsellvolume.getSlope()); + srbuyvolume.addData((double) counter, trades.volumeOfBuyTrades); + srsellvolume.addData((double) counter, trades.volumeOfSellTrades); priceslope = srprice.getSlope(); buyvslope = srbuyvolume.getSlope(); sellvslope = srsellvolume.getSlope(); - }catch(UnirestException ue){ - System.out.println("Exception thrown. Just continoue.."); + } catch (UnirestException ue) { + System.out.println("Exception thrown. Just continue.."); } - - return sinceid; } - private static int calculatePercentage(Double first, Double second){ + private static void sendStats() { + if(regressionbacktest || backtest) return; + + try { + Unirest.post("https://www.riskahead.de/api/v1/deka/updatestats/") + .header("accept", "application/json") + .queryString("app_id", APP_NAME) + .field("funds_start", "ETH: " + VALUE_ETH + " EUR: " + VALUE_EUR) + .field("funds_current", funds.getFundsStringOutput()) + .field("trades_buy", counterbuys) + .field("trades_sell", countersells) + .field("trades_fees", fees) + .field("trades_neutral", counterneutrals) + .asJson(); + } catch (UnirestException ue) { + System.out.println("Error sending stats: " + ue.getMessage()); + } + } + + private static void outputAnalyzationResults() { + if(regressionbacktest || backtest) return; + System.out.printf("Price: "+ currentPrice +" => Slope: %.3f \n",srprice.getSlope()); + System.out.printf("Buy-Trades -- Amount: " + trades.numberOfBuyTrades + " Volume: %.3f => Slope: %.3f\n",trades.volumeOfBuyTrades,srbuyvolume.getSlope()); + System.out.printf("Sell-Trades -- Amount: " + trades.numberOfSellTrades + " Volume: %.3f => Slope: %.3f\n",trades.volumeOfSellTrades,srsellvolume.getSlope()); + System.out.println("Trend: " + trend); + } + + private static void writeResponseToFile(HttpResponse jsonResponse, String filename) { + try{ + PrintWriter writer = new PrintWriter(new FileOutputStream( + new File(filename+"_"+APP_NAME+".txt"), + true )); + writer.append(jsonResponse.getBody().toString()+"\r\n"); + writer.close(); + } catch (IOException e) { + // do something + } + } + + private static Map sortByValue(Map map) { + List> list = new LinkedList<>(map.entrySet()); + Collections.sort(list, new Comparator() { + @SuppressWarnings("unchecked") + public int compare(Object o2, Object o1) { + return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue()); + } + }); + + Map result = new LinkedHashMap<>(); + for (Iterator> it = list.iterator(); it.hasNext();) { + Map.Entry entry = (Map.Entry) it.next(); + result.put(entry.getKey(), entry.getValue()); + } + + return result; + } + + private static int calculatePercentage(Double first, Double second) { Double d = (1 - first / second) * 100; return Math.abs(d.intValue()); } - private static void analyzeAverage(String shortname) throws UnirestException { - HttpResponse jsonResponse = Unirest.get("https://min-api.cryptocompare.com/data/histominute") - .queryString("fsym", shortname) - .queryString("tsym", "EUR" ) - .queryString("limit","10") - .queryString("aggregate","1") - .queryString("e","Kraken") - .asJson(); - - JSONArray dataArray = jsonResponse.getBody().getObject().getJSONArray("Data"); - - double priceAvg = dataArray.getJSONObject(0).getDouble("close"); - double tmpPrice = priceAvg; - int numRising = 0; - int numFalling = 0; - - for(int i = 1; i < dataArray.length(); i++){ - JSONObject obj = dataArray.getJSONObject(i); - tmpPrice = obj.getDouble("close"); - priceAvg += tmpPrice; - } - - Integer seconds = (jsonResponse.getBody().getObject().getInt("TimeTo")-jsonResponse.getBody().getObject().getInt("TimeFrom")) / 60; - priceAvg = priceAvg / dataArray.length(); - System.out.printf("Latest price: "+currentPrice+", calculated AVG from last "+seconds+ " minutes: %.2f€ ",priceAvg); - - if(Double.compare(currentPrice,priceAvg) > 0){ - buyindicator--; - }else{ - buyindicator++; - } - } - } diff --git a/src/main/java/Order.java b/src/main/java/Order.java index 3d8351f..6bf6908 100644 --- a/src/main/java/Order.java +++ b/src/main/java/Order.java @@ -12,4 +12,8 @@ public class Order { public Order(){ } + + public Order(String ordertype){ + this.ordertype = ordertype; + } } diff --git a/src/main/java/OrderHistory.java b/src/main/java/OrderHistory.java index 3641871..bbba2ed 100644 --- a/src/main/java/OrderHistory.java +++ b/src/main/java/OrderHistory.java @@ -11,6 +11,10 @@ public class OrderHistory { } + public void clearOrders(){ + if(orders != null) orders.clear(); + } + public void addOrder(Order order){ orders.add(order); } diff --git a/src/main/java/TradeHistory.java b/src/main/java/TradeHistory.java index f2f3e04..2d74b6b 100644 --- a/src/main/java/TradeHistory.java +++ b/src/main/java/TradeHistory.java @@ -73,5 +73,9 @@ public class TradeHistory { timeframesec = trades.get(trades.size()-1).time - trades.get(0).time; } + + public void clearTrades(){ + if(trades != null) trades.clear(); + } } diff --git a/src/main/resources/META-INF/MANIFEST.MF b/src/main/resources/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5ee19cb --- /dev/null +++ b/src/main/resources/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Main + diff --git a/src/main/resources/backtestresults.txt b/src/main/resources/backtestresults.txt new file mode 100644 index 0000000..ea64e96 --- /dev/null +++ b/src/main/resources/backtestresults.txt @@ -0,0 +1,34 @@ +/* +23598,41 / 230,84 +Fees: 662.12 +round 12 +buy 20 +sell 20 + + +0 +0 +ct1: +23:38 Value: 102.254903366053 +23:39 Value: 102.05434371249746 +24:39 Value: 101.96592024993276 +15:27 Value: 101.81517198770021 +13:26 Value: 101.80589120186636 + +ct2: +15:21 Value: 104.40138708742731 +15:22 Value: 104.04880741781145 +15:23 Value: 104.04880741781145 +5:8 Value: 102.19331248328419 +12:19 Value: 102.02828394264394 + + +ct3: +15:35 Value: 103.35768030572747 +15:33 Value: 103.32193214168696 +21:50 Value: 103.05732906060597 +21:49 Value: 103.05732906060597 +10:23 Value: 102.66724122909187 + + +*/ \ No newline at end of file diff --git a/src/main/resources/simpleRule.drl b/src/main/resources/simpleRule.drl deleted file mode 100644 index 0740015..0000000 --- a/src/main/resources/simpleRule.drl +++ /dev/null @@ -1,8 +0,0 @@ -import CryptoCoin; - -rule "accountBalanceAtLeast" - when - $account : Account( balance < 100 ) - then - System.out.println("Warning! money running out!"); -end \ No newline at end of file