Included MovingAverage
This commit is contained in:
@@ -22,6 +22,8 @@ public class Main {
|
||||
private static SimpleRegression srbuyvolume = new SimpleRegression(true);
|
||||
private static SimpleRegression srsellvolume = new SimpleRegression(true);
|
||||
private static SimpleRegression srprice = new SimpleRegression(true);
|
||||
private static MovingAverage ma15 = new MovingAverage(15);
|
||||
private static MovingAverage ma50 = new MovingAverage(50);
|
||||
|
||||
private static OrderHistory orders = new OrderHistory();
|
||||
private static Funds funds = new Funds();
|
||||
@@ -48,7 +50,6 @@ public class Main {
|
||||
|
||||
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);
|
||||
@@ -306,6 +307,20 @@ public class Main {
|
||||
}
|
||||
}
|
||||
|
||||
private static String calculateTrend_4() {
|
||||
if(counter < 50) return "neutral";
|
||||
|
||||
if(ma15.getAverage().compareTo(ma50.getAverage()) == 1){
|
||||
buyindicator += 2;
|
||||
return "normal buy";
|
||||
}else if(ma15.getAverage().compareTo(ma50.getAverage()) == -1){
|
||||
buyindicator -= 2;
|
||||
return "normal sell";
|
||||
}else{
|
||||
return "neutral";
|
||||
}
|
||||
}
|
||||
|
||||
private static void doBacktest(){
|
||||
try {
|
||||
funds.clearFunds();
|
||||
@@ -316,13 +331,13 @@ public class Main {
|
||||
|
||||
String priceline = "";
|
||||
String volumeline = "";
|
||||
int counter = 0;
|
||||
counter = 0;
|
||||
|
||||
InputStream fis = new FileInputStream("C:\\Development\\Projects\\DekaBot\\DekaBot\\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"));
|
||||
BufferedReader br = new BufferedReader(isr);
|
||||
|
||||
InputStream fis2 = new FileInputStream("C:\\Development\\Projects\\DekaBot\\DekaBot\\src\\main\\resources\\volume.txt");
|
||||
InputStream fis2 = new FileInputStream(System.getProperty("user.dir")+"\\src\\main\\resources\\volume.txt");
|
||||
InputStreamReader isr2 = new InputStreamReader(fis2, Charset.forName("UTF-8"));
|
||||
BufferedReader br2 = new BufferedReader(isr2);
|
||||
|
||||
@@ -351,6 +366,11 @@ public class Main {
|
||||
currentPrice = jsonPriceObject.getDouble("EUR");
|
||||
srprice.addData((double) counter, currentPrice);
|
||||
|
||||
ma15.add(currentPrice);
|
||||
ma50.add(currentPrice);
|
||||
|
||||
|
||||
|
||||
trades = new TradeHistory(volumeArray, sinceid);
|
||||
|
||||
srbuyvolume.addData((double) counter, trades.volumeOfBuyTrades);
|
||||
@@ -360,7 +380,8 @@ public class Main {
|
||||
buyvslope = srbuyvolume.getSlope();
|
||||
sellvslope = srsellvolume.getSlope();
|
||||
|
||||
trend = calculateTrend_2();
|
||||
trend = calculateTrend_4();
|
||||
|
||||
outputAnalyzationResults();
|
||||
|
||||
if (((counter % NUM_ITERATIONS_FOR_ORDER) == 0) && (counter != 0)) {
|
||||
|
||||
38
src/main/java/MovingAverage.java
Normal file
38
src/main/java/MovingAverage.java
Normal file
@@ -0,0 +1,38 @@
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
public class MovingAverage {
|
||||
|
||||
private final Queue<BigDecimal> window = new LinkedList<BigDecimal>();
|
||||
private final int period;
|
||||
private BigDecimal sum = BigDecimal.ZERO;
|
||||
|
||||
public MovingAverage(int period) {
|
||||
this.period = period;
|
||||
}
|
||||
|
||||
public void add(BigDecimal num) {
|
||||
sum = sum.add(num);
|
||||
window.add(num);
|
||||
if (window.size() > period) {
|
||||
sum = sum.subtract(window.remove());
|
||||
}
|
||||
}
|
||||
|
||||
public void add(double numd) {
|
||||
BigDecimal num = BigDecimal.valueOf(numd);
|
||||
sum = sum.add(num);
|
||||
window.add(num);
|
||||
if (window.size() > period) {
|
||||
sum = sum.subtract(window.remove());
|
||||
}
|
||||
}
|
||||
|
||||
public BigDecimal getAverage() {
|
||||
if (window.isEmpty()) return BigDecimal.ZERO;
|
||||
BigDecimal divisor = BigDecimal.valueOf(window.size());
|
||||
return sum.divide(divisor, 2, RoundingMode.HALF_UP);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user