Initial Commit
This commit is contained in:
11
src/main/java/CryptoCoin.java
Normal file
11
src/main/java/CryptoCoin.java
Normal file
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
41
src/main/java/Funds.java
Normal file
41
src/main/java/Funds.java
Normal file
@@ -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<String, Double> funds = new HashMap<String, Double>();
|
||||
|
||||
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<String, Double> entry : funds.entrySet()){
|
||||
returnValue += entry.getKey()+" : "+entry.getValue()+" ";
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
}
|
||||
321
src/main/java/Main.java
Normal file
321
src/main/java/Main.java
Normal file
@@ -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<Double> list = new ArrayList<Double>();
|
||||
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<JsonNode> 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<JsonNode> 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<JsonNode> 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<JsonNode> 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<JsonNode> 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++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
15
src/main/java/Order.java
Normal file
15
src/main/java/Order.java
Normal file
@@ -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(){
|
||||
|
||||
}
|
||||
}
|
||||
25
src/main/java/OrderHistory.java
Normal file
25
src/main/java/OrderHistory.java
Normal file
@@ -0,0 +1,25 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by dennis on 28.05.2017.
|
||||
*/
|
||||
public class OrderHistory {
|
||||
public ArrayList<Order> orders = new ArrayList<Order>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
15
src/main/java/Trade.java
Normal file
15
src/main/java/Trade.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
77
src/main/java/TradeHistory.java
Normal file
77
src/main/java/TradeHistory.java
Normal file
@@ -0,0 +1,77 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.json.JSONArray;
|
||||
|
||||
public class TradeHistory {
|
||||
public ArrayList<Trade> trades = new ArrayList<Trade>();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
8
src/main/resources/simpleRule.drl
Normal file
8
src/main/resources/simpleRule.drl
Normal file
@@ -0,0 +1,8 @@
|
||||
import CryptoCoin;
|
||||
|
||||
rule "accountBalanceAtLeast"
|
||||
when
|
||||
$account : Account( balance < 100 )
|
||||
then
|
||||
System.out.println("Warning! money running out!");
|
||||
end
|
||||
Reference in New Issue
Block a user