commit 0f3913b7a19df6d3e2b6b0c6441a703b512cd99f Author: Dennis Thiessen Date: Thu Jun 1 21:05:02 2017 +0200 Initial Commit diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..bbf2bd3 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_protobuf_protobuf_java_2_6_0.xml b/.idea/libraries/Maven__com_google_protobuf_protobuf_java_2_6_0.xml new file mode 100644 index 0000000..7ced7e4 --- /dev/null +++ b/.idea/libraries/Maven__com_google_protobuf_protobuf_java_2_6_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_mashape_unirest_unirest_java_1_4_9.xml b/.idea/libraries/Maven__com_mashape_unirest_unirest_java_1_4_9.xml new file mode 100644 index 0000000..d7792cc --- /dev/null +++ b/.idea/libraries/Maven__com_mashape_unirest_unirest_java_1_4_9.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_thoughtworks_xstream_xstream_1_4_9.xml b/.idea/libraries/Maven__com_thoughtworks_xstream_xstream_1_4_9.xml new file mode 100644 index 0000000..1922b11 --- /dev/null +++ b/.idea/libraries/Maven__com_thoughtworks_xstream_xstream_1_4_9.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__commons_codec_commons_codec_1_6.xml b/.idea/libraries/Maven__commons_codec_commons_codec_1_6.xml new file mode 100644 index 0000000..e8a6a9f --- /dev/null +++ b/.idea/libraries/Maven__commons_codec_commons_codec_1_6.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__commons_logging_commons_logging_1_1_3.xml b/.idea/libraries/Maven__commons_logging_commons_logging_1_1_3.xml new file mode 100644 index 0000000..01c7b8e --- /dev/null +++ b/.idea/libraries/Maven__commons_logging_commons_logging_1_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__joda_time_joda_time_2_9_9.xml b/.idea/libraries/Maven__joda_time_joda_time_2_9_9.xml new file mode 100644 index 0000000..a468f58 --- /dev/null +++ b/.idea/libraries/Maven__joda_time_joda_time_2_9_9.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_antlr_antlr_runtime_3_5.xml b/.idea/libraries/Maven__org_antlr_antlr_runtime_3_5.xml new file mode 100644 index 0000000..6d3a0f6 --- /dev/null +++ b/.idea/libraries/Maven__org_antlr_antlr_runtime_3_5.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_commons_commons_math3_3_6_1.xml b/.idea/libraries/Maven__org_apache_commons_commons_math3_3_6_1.xml new file mode 100644 index 0000000..ebfe0a8 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_commons_commons_math3_3_6_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_httpcomponents_httpasyncclient_4_0_2.xml b/.idea/libraries/Maven__org_apache_httpcomponents_httpasyncclient_4_0_2.xml new file mode 100644 index 0000000..8664f17 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_httpcomponents_httpasyncclient_4_0_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_httpcomponents_httpclient_4_3_6.xml b/.idea/libraries/Maven__org_apache_httpcomponents_httpclient_4_3_6.xml new file mode 100644 index 0000000..04cad1e --- /dev/null +++ b/.idea/libraries/Maven__org_apache_httpcomponents_httpclient_4_3_6.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_httpcomponents_httpcore_4_3_3.xml b/.idea/libraries/Maven__org_apache_httpcomponents_httpcore_4_3_3.xml new file mode 100644 index 0000000..a821fc2 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_httpcomponents_httpcore_4_3_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_httpcomponents_httpcore_nio_4_3_2.xml b/.idea/libraries/Maven__org_apache_httpcomponents_httpcore_nio_4_3_2.xml new file mode 100644 index 0000000..f8b72d4 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_httpcomponents_httpcore_nio_4_3_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_apache_httpcomponents_httpmime_4_3_6.xml b/.idea/libraries/Maven__org_apache_httpcomponents_httpmime_4_3_6.xml new file mode 100644 index 0000000..ef74cf0 --- /dev/null +++ b/.idea/libraries/Maven__org_apache_httpcomponents_httpmime_4_3_6.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_drools_drools_compiler_6_5_0_Final.xml b/.idea/libraries/Maven__org_drools_drools_compiler_6_5_0_Final.xml new file mode 100644 index 0000000..3432243 --- /dev/null +++ b/.idea/libraries/Maven__org_drools_drools_compiler_6_5_0_Final.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_drools_drools_core_6_5_0_Final.xml b/.idea/libraries/Maven__org_drools_drools_core_6_5_0_Final.xml new file mode 100644 index 0000000..4fd4384 --- /dev/null +++ b/.idea/libraries/Maven__org_drools_drools_core_6_5_0_Final.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_eclipse_jdt_core_compiler_ecj_4_4_2.xml b/.idea/libraries/Maven__org_eclipse_jdt_core_compiler_ecj_4_4_2.xml new file mode 100644 index 0000000..98a6916 --- /dev/null +++ b/.idea/libraries/Maven__org_eclipse_jdt_core_compiler_ecj_4_4_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_json_json_20140107.xml b/.idea/libraries/Maven__org_json_json_20140107.xml new file mode 100644 index 0000000..0092a09 --- /dev/null +++ b/.idea/libraries/Maven__org_json_json_20140107.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_kie_kie_api_6_5_0_Final.xml b/.idea/libraries/Maven__org_kie_kie_api_6_5_0_Final.xml new file mode 100644 index 0000000..f844c06 --- /dev/null +++ b/.idea/libraries/Maven__org_kie_kie_api_6_5_0_Final.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_kie_kie_internal_6_5_0_Final.xml b/.idea/libraries/Maven__org_kie_kie_internal_6_5_0_Final.xml new file mode 100644 index 0000000..f697a0b --- /dev/null +++ b/.idea/libraries/Maven__org_kie_kie_internal_6_5_0_Final.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_mvel_mvel2_2_2_8_Final.xml b/.idea/libraries/Maven__org_mvel_mvel2_2_2_8_Final.xml new file mode 100644 index 0000000..465d3dc --- /dev/null +++ b/.idea/libraries/Maven__org_mvel_mvel2_2_2_8_Final.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_2.xml b/.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_2.xml new file mode 100644 index 0000000..7d29533 --- /dev/null +++ b/.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__xmlpull_xmlpull_1_1_3_1.xml b/.idea/libraries/Maven__xmlpull_xmlpull_1_1_3_1.xml new file mode 100644 index 0000000..0f2d9ef --- /dev/null +++ b/.idea/libraries/Maven__xmlpull_xmlpull_1_1_3_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__xpp3_xpp3_min_1_1_4c.xml b/.idea/libraries/Maven__xpp3_xpp3_min_1_1_4c.xml new file mode 100644 index 0000000..6726a2d --- /dev/null +++ b/.idea/libraries/Maven__xpp3_xpp3_min_1_1_4c.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e8942bd --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..548dd52 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..15f8787 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,850 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + doOrder + calculateTrend + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1495743885678 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No facets are configured + + + + + + + + + + + + + + + 1.8 + + + + + + + + dekaBot + + + + + + + + Maven: com.mashape.unirest:unirest-java:1.4.9 + + + + + + + + \ No newline at end of file diff --git a/dekaBot.iml b/dekaBot.iml new file mode 100644 index 0000000..1ae8f14 --- /dev/null +++ b/dekaBot.iml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5e6cf19 --- /dev/null +++ b/pom.xml @@ -0,0 +1,86 @@ + + + 4.0.0 + + my + dekaBot + 1.0-SNAPSHOT + + + 6.5.0.Final + + + + + jboss-public-repository-group + JBoss Public Repository Group + http://repository.jboss.org/nexus/content/groups/public/ + + true + never + + + true + daily + + + + + + + com.mashape.unirest + unirest-java + 1.4.9 + + + + org.apache.httpcomponents + httpclient + 4.3.6 + + + org.apache.httpcomponents + httpasyncclient + 4.0.2 + + + org.apache.httpcomponents + httpmime + 4.3.6 + + + org.apache.commons + commons-math3 + 3.6.1 + + + org.json + json + 20140107 + + + org.drools + drools-core + ${runtime.version} + + + org.drools + drools-compiler + ${runtime.version} + + + joda-time + joda-time + 2.9.9 + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/CryptoCoin.java b/src/main/java/CryptoCoin.java new file mode 100644 index 0000000..a1e3979 --- /dev/null +++ b/src/main/java/CryptoCoin.java @@ -0,0 +1,11 @@ +public class CryptoCoin { + public String shortname; + public double price; + public double change5Min; + + public CryptoCoin(String shortname, Double price){ + this.shortname = shortname; + this.price = price; + } + +} diff --git a/src/main/java/Funds.java b/src/main/java/Funds.java new file mode 100644 index 0000000..f96a5bb --- /dev/null +++ b/src/main/java/Funds.java @@ -0,0 +1,41 @@ +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by dennis on 28.05.2017. + */ +public class Funds { + Map funds = new HashMap(); + + public Funds(){ + + } + + + public void addFund(String name, double value){ + funds.put(name, value); + } + + public double getValue(String name){ + return funds.get(name); + } + + public void takeValue(String name, double value){ + funds.put(name, funds.get(name)-value); + } + + public void addValue(String name, double value){ + funds.put(name, funds.get(name)+value); + } + + public String getFundsStringOutput(){ + String returnValue = "Funds: "; + + for(Map.Entry entry : funds.entrySet()){ + returnValue += entry.getKey()+" : "+entry.getValue()+" "; + } + + return returnValue; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..9e68ce4 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,321 @@ +import com.mashape.unirest.http.HttpResponse; +import com.mashape.unirest.http.JsonNode; +import com.mashape.unirest.http.Unirest; +import com.mashape.unirest.http.exceptions.UnirestException; +import org.apache.commons.math3.stat.regression.SimpleRegression; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.Scanner; + +public class Main { + + static SimpleRegression srbuyvolume = new SimpleRegression(true); + static SimpleRegression srsellvolume = new SimpleRegression(true); + 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; + + 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; + + public static int counterbuys = 0; + public static int countersells = 0; + public static int counterneutrals = 0; + + public static void main(String [ ] args) throws InterruptedException { + System.out.println("Starting DekaBot..."); + Scanner in = new Scanner(System.in); + + System.out.println("Start simulation? (y/n)"); + String s = in.next(); + + long sinceid = 0L; + + if(s.equals("y")){ + 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."); + 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(); + } + + 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")); + + 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); + + funds.addValue("EUR", funds.getValue("ETH") * currentPrice); + funds.takeValue("ETH", funds.getValue("ETH")); + + 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(); + } + + private static Order decide() { + Order newOrder = new Order(); + newOrder.price = currentPrice; + + if(buyindicator > 6){ + + System.out.println("BUY NOW - PRICE: "+currentPrice); + newOrder.ordertype = "buy"; + + }else if(buyindicator > -6){ + 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){ + + System.out.println("SELL NOW - PRICE: "+currentPrice); + newOrder.ordertype = "sell"; + + }else{ + System.out.println("Would like to sell but percentage not reached"); + } + } + + 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(); + + + currentPrice = jsonResponse.getBody().getObject().getDouble("EUR"); + + srprice.addData((double)counter,currentPrice); + System.out.printf("Current price slope: %.3f \n",srprice.getSlope()); + }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") + .header("accept", "application/json") + .queryString("pair", "ETHEUR") + .field("since", sinceid) + .asJson(); + + JSONArray array = jsonResponse.getBody().getObject().getJSONObject("result").getJSONArray("XETHZEUR"); + sinceid = jsonResponse.getBody().getObject().getJSONObject("result").getLong("last"); + 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()); + + priceslope = srprice.getSlope(); + buyvslope = srbuyvolume.getSlope(); + sellvslope = srsellvolume.getSlope(); + + }catch(UnirestException ue){ + System.out.println("Exception thrown. Just continoue.."); + } + + return sinceid; + } + + 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 new file mode 100644 index 0000000..3d8351f --- /dev/null +++ b/src/main/java/Order.java @@ -0,0 +1,15 @@ +/** + * Created by dennis on 28.05.2017. + */ +public class Order { + public String pair; + public String type; + public String ordertype; + public Double price; + public Double volume; + public String leverage = "none"; + + public Order(){ + + } +} diff --git a/src/main/java/OrderHistory.java b/src/main/java/OrderHistory.java new file mode 100644 index 0000000..3641871 --- /dev/null +++ b/src/main/java/OrderHistory.java @@ -0,0 +1,25 @@ +import java.util.ArrayList; +import java.util.List; + +/** + * Created by dennis on 28.05.2017. + */ +public class OrderHistory { + public ArrayList orders = new ArrayList(); + + public OrderHistory(){ + + } + + public void addOrder(Order order){ + orders.add(order); + } + + public Order getLastOrder(){ + if(orders.size() > 0){ + return orders.get(orders.size()-1); + }else{ + return null; + } + } +} diff --git a/src/main/java/Trade.java b/src/main/java/Trade.java new file mode 100644 index 0000000..9ca98ac --- /dev/null +++ b/src/main/java/Trade.java @@ -0,0 +1,15 @@ +public class Trade { + public double price; + public double volume; + public int time; + public String buyorsell; + public String marketorlimit; + + public Trade(double price, double volume, int time, String buyorsell, String marketorlimit){ + this.price = price; + this.volume = volume; + this.time = time; + this.buyorsell = buyorsell; + this.marketorlimit = marketorlimit; + } +} diff --git a/src/main/java/TradeHistory.java b/src/main/java/TradeHistory.java new file mode 100644 index 0000000..f2f3e04 --- /dev/null +++ b/src/main/java/TradeHistory.java @@ -0,0 +1,77 @@ +import java.util.ArrayList; + +import org.json.JSONArray; + +public class TradeHistory { + public ArrayList trades = new ArrayList(); + public int numberOfBuyTrades = 0; + public int numberOfSellTrades = 0; + public double volumeOfBuyTrades = 0.0; + public double volumeOfSellTrades = 0.0; + public int timeframesec = 0; + public long sinceid = 0; + + public TradeHistory(JSONArray array, long sinceid){ + if(array.length() < 1) return; + + double price = 0.0; + double value = 0.0; + int timestamp = 0; + String buyorsell = ""; + String marketorlimit = ""; + + for(int i = 0;i < array.length();i++){ + price = array.getJSONArray(i).getDouble(0); + value = array.getJSONArray(i).getDouble(1); + timestamp = array.getJSONArray(i).getInt(2); + buyorsell = array.getJSONArray(i).getString(3); + marketorlimit = array.getJSONArray(i).getString(4); + + trades.add(new Trade(price,value,timestamp,buyorsell,marketorlimit)); + + if(buyorsell.equals("b")){ + this.numberOfBuyTrades++; + this.volumeOfBuyTrades += price; + }else if(buyorsell.equals("s")){ + this.numberOfSellTrades++; + this.volumeOfSellTrades += price; + } + } + + this.sinceid = sinceid; + this.timeframesec = array.getJSONArray(array.length()-1).getInt(2) - array.getJSONArray(0).getInt(2); + } + + public TradeHistory(){ + } + + public TradeHistory getNumberAndVolumeOfBuyTradesInPastSec(int timeframe){ + int currenttimestamp = (int) (System.currentTimeMillis() / 1000L); + TradeHistory returnHistory = new TradeHistory(); + + for(Trade trade : trades){ + if(trade.time > (currenttimestamp-timeframe)){ + returnHistory.trades.add(trade); + } + } + + returnHistory.calculateNumberAndVolume(); + + return returnHistory; + } + + public void calculateNumberAndVolume(){ + for(Trade trade : trades){ + if(trade.buyorsell.equals("b")){ + this.numberOfBuyTrades++; + this.volumeOfBuyTrades += trade.price; + }else if(trade.buyorsell.equals("s")){ + this.numberOfSellTrades++; + this.volumeOfSellTrades += trade.price; + } + } + + timeframesec = trades.get(trades.size()-1).time - trades.get(0).time; + } + +} diff --git a/src/main/resources/simpleRule.drl b/src/main/resources/simpleRule.drl new file mode 100644 index 0000000..0740015 --- /dev/null +++ b/src/main/resources/simpleRule.drl @@ -0,0 +1,8 @@ +import CryptoCoin; + +rule "accountBalanceAtLeast" + when + $account : Account( balance < 100 ) + then + System.out.println("Warning! money running out!"); +end \ No newline at end of file diff --git a/target/classes/CryptoCoin.class b/target/classes/CryptoCoin.class new file mode 100644 index 0000000..593da4c Binary files /dev/null and b/target/classes/CryptoCoin.class differ diff --git a/target/classes/Funds.class b/target/classes/Funds.class new file mode 100644 index 0000000..e1bbd45 Binary files /dev/null and b/target/classes/Funds.class differ diff --git a/target/classes/Main.class b/target/classes/Main.class new file mode 100644 index 0000000..d5fb77f Binary files /dev/null and b/target/classes/Main.class differ diff --git a/target/classes/Order.class b/target/classes/Order.class new file mode 100644 index 0000000..4280f88 Binary files /dev/null and b/target/classes/Order.class differ diff --git a/target/classes/OrderHistory.class b/target/classes/OrderHistory.class new file mode 100644 index 0000000..39dc9dc Binary files /dev/null and b/target/classes/OrderHistory.class differ diff --git a/target/classes/Trade.class b/target/classes/Trade.class new file mode 100644 index 0000000..604c4e6 Binary files /dev/null and b/target/classes/Trade.class differ diff --git a/target/classes/TradeHistory.class b/target/classes/TradeHistory.class new file mode 100644 index 0000000..add998d Binary files /dev/null and b/target/classes/TradeHistory.class differ diff --git a/target/classes/simpleRule.drl b/target/classes/simpleRule.drl new file mode 100644 index 0000000..0740015 --- /dev/null +++ b/target/classes/simpleRule.drl @@ -0,0 +1,8 @@ +import CryptoCoin; + +rule "accountBalanceAtLeast" + when + $account : Account( balance < 100 ) + then + System.out.println("Warning! money running out!"); +end \ No newline at end of file