Exponential Moving Average implemented

This commit is contained in:
Dennis Thiessen
2017-07-20 15:28:34 +02:00
parent b34621ac3a
commit e6e9b86541
3 changed files with 60 additions and 8 deletions

View File

@@ -10,7 +10,8 @@ import storage.Order;
import storage.OrderHistory; import storage.OrderHistory;
import storage.TradeHistory; import storage.TradeHistory;
import utils.DrawGraph; import utils.DrawGraph;
import utils.MovingAverage; import utils.ExponentialMovingAverage;
import utils.SimpleMovingAverage;
import javax.swing.*; import javax.swing.*;
@@ -34,8 +35,10 @@ public class Main {
private static SimpleRegression srbuyvolume = new SimpleRegression(true); private static SimpleRegression srbuyvolume = new SimpleRegression(true);
private static SimpleRegression srsellvolume = new SimpleRegression(true); private static SimpleRegression srsellvolume = new SimpleRegression(true);
private static SimpleRegression srprice = new SimpleRegression(true); private static SimpleRegression srprice = new SimpleRegression(true);
private static MovingAverage ma15 = new MovingAverage(200); private static SimpleMovingAverage ma15 = new SimpleMovingAverage(200);
private static MovingAverage ma50 = new MovingAverage(1000); private static SimpleMovingAverage ma50 = new SimpleMovingAverage(1000);
private static ExponentialMovingAverage ema15 = new ExponentialMovingAverage(50);
private static ExponentialMovingAverage ema50 = new ExponentialMovingAverage(100);
private static OrderHistory orders = new OrderHistory(); private static OrderHistory orders = new OrderHistory();
private static Funds funds = new Funds(); private static Funds funds = new Funds();
@@ -62,6 +65,8 @@ public class Main {
private static ArrayList<Double> graphValuesBase = new ArrayList<>(); private static ArrayList<Double> graphValuesBase = new ArrayList<>();
private static ArrayList<Double> graphValues1 = new ArrayList<>(); private static ArrayList<Double> graphValues1 = new ArrayList<>();
private static ArrayList<Double> graphValues2 = new ArrayList<>(); private static ArrayList<Double> graphValues2 = new ArrayList<>();
private static ArrayList<Double> graphValues3 = new ArrayList<>();
private static ArrayList<Double> graphValues4 = new ArrayList<>();
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
System.out.println("Starting DekaBot..."); System.out.println("Starting DekaBot...");
@@ -383,7 +388,7 @@ public class Main {
private static String calculateTrend_4() { private static String calculateTrend_4() {
if(currentIteration < 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;
return "normal buy"; return "normal buy";
@@ -393,9 +398,21 @@ public class Main {
}else{ }else{
return "neutral"; return "neutral";
} }
*/
if(ema15.getAverage() > ema50.getAverage()){
buyindicator += 3;
return "normal buy";
}else if(ema15.getAverage() < ema50.getAverage()){
buyindicator -= 3;
return "normal sell";
}else{
return "neutral";
}
} }
private static void doBacktest(){ private static void doBacktest(){
System.out.println("Start backtest");
try { try {
graphValues1.clear(); graphValues1.clear();
graphValues2.clear(); graphValues2.clear();
@@ -450,9 +467,13 @@ public class Main {
ma15.add(currentPrice); ma15.add(currentPrice);
ma50.add(currentPrice); ma50.add(currentPrice);
if(!regressionbacktest) return;graphValuesBase.add(currentPrice); // if(!regressionbacktest) return;
graphValuesBase.add(currentPrice);
graphValues1.add(ma15.getAverage().doubleValue()); graphValues1.add(ma15.getAverage().doubleValue());
graphValues2.add(ma50.getAverage().doubleValue()); graphValues2.add(ma50.getAverage().doubleValue());
graphValues3.add(ema15.average(currentPrice));
graphValues4.add(ema50.average(currentPrice));
outputAnalyzationResults(); outputAnalyzationResults();
@@ -481,8 +502,13 @@ public class Main {
if(!regressionbacktest) System.out.println("utils.DrawGraph -- "+graphValues1.size()); if(!regressionbacktest) System.out.println("utils.DrawGraph -- "+graphValues1.size());
if(!regressionbacktest) drawGraph("Threshold: "+BUY_THRESHOLD+" Iterations: "+NUM_ITERATIONS_FOR_ORDER, graphValuesBase, graphValues1, graphValues2); if(!regressionbacktest) drawGraph("Threshold: "+BUY_THRESHOLD+" Iterations: "+NUM_ITERATIONS_FOR_ORDER, graphValuesBase, graphValues1, graphValues2);
if(!regressionbacktest) drawGraph("Threshold: "+BUY_THRESHOLD+" Iterations: "+NUM_ITERATIONS_FOR_ORDER, graphValuesBase, graphValues3, graphValues4);
} catch (IOException e) { } catch (IOException e) {
System.out.println("Error:" + e.getMessage()); System.out.println("Error:" + e.getMessage());
} catch (Exception e) {
System.out.println("Exception occured:" + e.getMessage());
} }
} }
@@ -588,7 +614,7 @@ public class Main {
} }
private static void drawGraph(String title, ArrayList<Double> valuesBase, ArrayList<Double> values1, ArrayList<Double> values2){ private static void drawGraph(String title, ArrayList<Double> valuesBase, ArrayList<Double> values1, ArrayList<Double> values2){
JFrame frame = new JFrame("Graph"); JFrame frame = new JFrame(title);
frame.setSize(700, 400); frame.setSize(700, 400);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

View File

@@ -0,0 +1,26 @@
package utils;
public class ExponentialMovingAverage {
private double alpha;
private Double oldValue;
public ExponentialMovingAverage(double alpha) {
this.alpha = alpha;
}
public double average(double value) {
if (oldValue == null) {
oldValue = value;
return value;
}
//double newValue = oldValue + alpha * (value - oldValue);
double k = 2 / (alpha +1);
double newValue = (value * k) + (oldValue * (1 - k));
oldValue = newValue;
return newValue;
}
public double getAverage(){
return oldValue;
}
}

View File

@@ -5,13 +5,13 @@ import java.math.RoundingMode;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
public class MovingAverage { public class SimpleMovingAverage {
private final Queue<BigDecimal> window = new LinkedList<BigDecimal>(); private final Queue<BigDecimal> window = new LinkedList<BigDecimal>();
private final int period; private final int period;
private BigDecimal sum = BigDecimal.ZERO; private BigDecimal sum = BigDecimal.ZERO;
public MovingAverage(int period) { public SimpleMovingAverage(int period) {
this.period = period; this.period = period;
} }