Graph updated

This commit is contained in:
Dennis Thiessen
2017-07-13 16:16:49 +02:00
parent cf969f4a7b
commit e6c580e136
3 changed files with 147 additions and 89 deletions

View File

@@ -6,26 +6,44 @@ import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.swing.*;
@SuppressWarnings("serial")
public class DrawGraph extends JPanel {
private static final int MAX_SCORE = 20;
private double MIN_VALUE = 0;
private double MAX_VALUE = 0;
private double MAX_SCORE = 20;
private static final int PREF_W = 800;
private static final int PREF_H = 650;
private static final int BORDER_GAP = 30;
private static final int BORDER_GAP = 15;
private static final Color GRAPH_COLOR = Color.black;
private static final Color GRAPH_POINT_COLOR = new Color(150, 50, 50, 180);
private static final Stroke GRAPH_STROKE = new BasicStroke(1f);
private static final int GRAPH_POINT_WIDTH = 2;
private static final int Y_HATCH_CNT = 10;
private List<Double> scores;
private static final int GRAPH_POINT_WIDTH = 10;
private static final int Y_HATCH_CNT = 20;
private List<Double> scoresBase;
private List<Double> scores1;
private List<Double> scores2;
private List<Point.Double> graphPointsBuy;
private List<Point.Double> graphPointsSell;
public DrawGraph(List<Double> scores) {
this.scores = scores;
public DrawGraph(List<Double> scoresBase, List<Double> scores1, List<Double> scores2, List<Point.Double> graphPointsBuy, List<Point.Double> graphPointsSell) {
this.scores1 = scores1;
this.scores2 = scores2;
this.scoresBase = scoresBase;
this.graphPointsBuy = graphPointsBuy;
this.graphPointsSell = graphPointsSell;
this.MIN_VALUE = Collections.min(scoresBase);
this.MAX_VALUE = Collections.max(scoresBase);
this.MAX_SCORE = MAX_VALUE - MIN_VALUE;
System.out.println("MAX_SCORE "+MAX_SCORE+" MIN_VALUE "+MIN_VALUE+" MAX_VALUE "+MAX_VALUE);
}
@Override
@@ -34,20 +52,28 @@ public class DrawGraph extends JPanel {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
double xScale = ((double) getWidth() - 2 * BORDER_GAP) / (scores.size() - 1);
double xScale = ((double) getWidth() - 2 * BORDER_GAP) / (scores1.size() - 1);
double yScale = ((double) getHeight() - 2 * BORDER_GAP) / (MAX_SCORE - 1);
List<Point> graphPoints = new ArrayList<Point>();
for (int i = 0; i < scores.size(); i++) {
int x1 = (int) (i * xScale + BORDER_GAP);
int y1 = (int) ((MAX_SCORE - scores.get(i)) * yScale + BORDER_GAP);
graphPoints.add(new Point(x1, y1));
List<Point.Double> graphPointsBase = new ArrayList<>();
List<Point.Double> graphPoints1 = new ArrayList<>();
List<Point.Double> graphPoints2 = new ArrayList<>();
for (int i = 0; i < scores1.size(); i++) {
double x1 = i * xScale + BORDER_GAP;
double y1_base = (MAX_SCORE - (scoresBase.get(i)-MIN_VALUE)) * yScale + BORDER_GAP;
double y1_1 = (MAX_SCORE - (scores1.get(i)-MIN_VALUE)) * yScale + BORDER_GAP;
double y1_2 = (MAX_SCORE - (scores2.get(i)-MIN_VALUE)) * yScale + BORDER_GAP;
graphPointsBase.add(new Point.Double(x1, y1_base));
graphPoints1.add(new Point.Double(x1, y1_1));
graphPoints2.add(new Point.Double(x1, y1_2));
}
// create x and y axes
g2.drawLine(BORDER_GAP, getHeight() - BORDER_GAP, BORDER_GAP, BORDER_GAP);
g2.drawLine(BORDER_GAP, getHeight() - BORDER_GAP, getWidth() - BORDER_GAP, getHeight() - BORDER_GAP);
// create hatch marks for y axis.
for (int i = 0; i < Y_HATCH_CNT; i++) {
int x0 = BORDER_GAP;
@@ -57,34 +83,79 @@ public class DrawGraph extends JPanel {
g2.drawLine(x0, y0, x1, y1);
}
/*
// and for x axis
for (int i = 0; i < scores.size() - 1; i++) {
int x0 = (i + 1) * (getWidth() - BORDER_GAP * 2) / (scores.size() - 1) + BORDER_GAP;
for (int i = 0; i < scores1.size() - 1; i++) {
int x0 = (i + 1) * (getWidth() - BORDER_GAP * 2) / (scores1.size() - 1) + BORDER_GAP;
int x1 = x0;
int y0 = getHeight() - BORDER_GAP;
int y1 = y0 - GRAPH_POINT_WIDTH;
g2.drawLine(x0, y0, x1, y1);
}
}*/
Stroke oldStroke = g2.getStroke();
g2.setColor(GRAPH_COLOR);
g2.setStroke(GRAPH_STROKE);
for (int i = 0; i < graphPoints.size() - 1; i++) {
int x1 = graphPoints.get(i).x;
int y1 = graphPoints.get(i).y;
int x2 = graphPoints.get(i + 1).x;
int y2 = graphPoints.get(i + 1).y;
g2.drawLine(x1, y1, x2, y2);
g2.setColor(Color.black);
Line2D line = new Line2D.Double();
for (int i = 0; i < graphPoints1.size() - 1; i++) {
double x1 = graphPointsBase.get(i).x;
double y1 = graphPointsBase.get(i).y;
double x2 = graphPointsBase.get(i + 1).x;
double y2 = graphPointsBase.get(i + 1).y;
line.setLine(x1, y1, x2, y2);
g2.draw(line);
}
g2.setColor(Color.gray);
for (int i = 0; i < graphPoints1.size() - 1; i++) {
double x1 = graphPoints1.get(i).x;
double y1 = graphPoints1.get(i).y;
double x2 = graphPoints1.get(i + 1).x;
double y2 = graphPoints1.get(i + 1).y;
line.setLine(x1, y1, x2, y2);
g2.draw(line);
}
g2.setColor(Color.darkGray);
for (int i = 0; i < graphPoints2.size() - 1; i++) {
double x1 = graphPoints2.get(i).x;
double y1 = graphPoints2.get(i).y;
double x2 = graphPoints2.get(i + 1).x;
double y2 = graphPoints2.get(i + 1).y;
line.setLine(x1, y1, x2, y2);
g2.draw(line);
}
g2.setStroke(oldStroke);
g2.setColor(GRAPH_POINT_COLOR);
for (int i = 0; i < graphPoints.size(); i++) {
int x = graphPoints.get(i).x - GRAPH_POINT_WIDTH / 2;
int y = graphPoints.get(i).y - GRAPH_POINT_WIDTH / 2;;
g2.setColor(Color.green);
for (int i = 0; i < graphPointsBuy.size(); i++) {
double x = graphPointsBuy.get(i).x * xScale + BORDER_GAP - GRAPH_POINT_WIDTH / 2;
double y = (MAX_SCORE - (graphPointsBuy.get(i).y-MIN_VALUE)) * yScale + BORDER_GAP - GRAPH_POINT_WIDTH / 2;
int ovalW = GRAPH_POINT_WIDTH;
int ovalH = GRAPH_POINT_WIDTH;
g2.fillOval(x, y, ovalW, ovalH);
Ellipse2D.Double elshape = new Ellipse2D.Double(x, y, ovalW, ovalH);
g2.draw(elshape);
}
g2.setColor(Color.red);
for (int i = 0; i < graphPointsSell.size(); i++) {
double x = graphPointsSell.get(i).x * xScale + BORDER_GAP - GRAPH_POINT_WIDTH / 2;
double y = (MAX_SCORE - (graphPointsSell.get(i).y-MIN_VALUE)) * yScale + BORDER_GAP - GRAPH_POINT_WIDTH / 2;
int ovalW = GRAPH_POINT_WIDTH;
int ovalH = GRAPH_POINT_WIDTH;
Ellipse2D.Double elshape = new Ellipse2D.Double(x, y, ovalW, ovalH);
g2.draw(elshape);
}
}
@@ -93,29 +164,4 @@ public class DrawGraph extends JPanel {
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
List<Double> scores = new ArrayList<>();
Random random = new Random();
int maxDataPoints = 16;
double maxScore = 20.0;
for (int i = 0; i < maxDataPoints ; i++) {
scores.add(random.nextDouble()*maxScore);
}
DrawGraph mainPanel = new DrawGraph(scores);
JFrame frame = new JFrame("DrawGraph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

View File

@@ -7,24 +7,27 @@ import org.json.JSONArray;
import org.json.JSONObject;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Point2D;
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.List;
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;
private static int NUM_ITERATIONS_FOR_ORDER = 6;
private static int BUY_THRESHOLD = 10;
private static int SELL_THRESHOLD = -10;
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(150);
private static MovingAverage ma50 = new MovingAverage(500);
private static MovingAverage ma15 = new MovingAverage(200);
private static MovingAverage ma50 = new MovingAverage(1000);
private static OrderHistory orders = new OrderHistory();
private static Funds funds = new Funds();
@@ -48,7 +51,9 @@ public class Main {
private static boolean backtest = false;
private static Map<String, Double > results = new HashMap<>();
private static ArrayList<Double> graphValues = new ArrayList<>();
private static ArrayList<Double> graphValuesBase = new ArrayList<>();
private static ArrayList<Double> graphValues1 = new ArrayList<>();
private static ArrayList<Double> graphValues2 = new ArrayList<>();
public static void main(String[] args) throws InterruptedException {
System.out.println("Starting DekaBot...");
@@ -191,6 +196,7 @@ public class Main {
private static Order decide() {
Order newOrder = new Order();
newOrder.price = currentPrice;
newOrder.timeframe = (double)counter;
if(!regressionbacktest) System.out.println("------- START ORDER -------");
if (buyindicator > BUY_THRESHOLD) {
@@ -219,6 +225,7 @@ public class Main {
private static Order decide2() {
Order newOrder = new Order();
newOrder.price = currentPrice;
newOrder.timeframe = (double)counter;
if(!regressionbacktest) System.out.println("------- START ORDER -------");
if (buyindicator > BUY_THRESHOLD) {
@@ -351,6 +358,8 @@ public class Main {
private static void doBacktest(){
try {
graphValues1.clear();
graphValues2.clear();
funds.clearFunds();
orders.clearOrders();
if(trades != null) trades.clearTrades();
@@ -369,16 +378,9 @@ public class Main {
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;
}
while ((priceline = br.readLine()) != null && (volumeline = br2.readLine()) != null) {
//sometimes two lines are written - in this case read second line as well and append
if(volumeline.charAt(volumeline.length()-1) != '}'){
volumeline += br2.readLine();
}
@@ -390,30 +392,36 @@ public class Main {
sinceid = jsonVolumeObject.getLong("last");
JSONArray volumeArray = jsonVolumeObject.getJSONArray("XETHZEUR");
currentPrice = jsonPriceObject.getDouble("EUR");
srprice.addData((double) counter, currentPrice);
ma15.add(currentPrice);
ma50.add(currentPrice);
graphValues.add(currentPrice);
trades = new TradeHistory(volumeArray, sinceid);
srbuyvolume.addData((double) counter, trades.volumeOfBuyTrades);
srsellvolume.addData((double) counter, trades.volumeOfSellTrades);
srprice.addData((double) counter, currentPrice);
priceslope = srprice.getSlope();
buyvslope = srbuyvolume.getSlope();
sellvslope = srsellvolume.getSlope();
/* DECIDE TREND CALCULATION */
trend = calculateTrend_4();
//System.out.println("Trend: "+trend+" shortAVG "+ma15.getAverage()+" longAVG "+ma50.getAverage());
/* */
ma15.add(currentPrice);
ma50.add(currentPrice);
graphValuesBase.add(currentPrice);
graphValues1.add(ma15.getAverage().doubleValue());
graphValues2.add(ma50.getAverage().doubleValue());
outputAnalyzationResults();
if (((counter % NUM_ITERATIONS_FOR_ORDER) == 0) && (counter != 0)) {
/* DECIDE DECIDE CALCULATOR */
Order order = decide2();
/* */
if (order.ordertype != null) doOrder(order);
@@ -429,11 +437,11 @@ public class Main {
counter++;
}
drawGraph("Threshold: "+BUY_THRESHOLD+" Iterations: "+NUM_ITERATIONS_FOR_ORDER,graphValues);
graphValues.clear();
System.out.println("DrawGraph -- "+graphValues1.size());
drawGraph("Threshold: "+BUY_THRESHOLD+" Iterations: "+NUM_ITERATIONS_FOR_ORDER, graphValuesBase, graphValues1, graphValues2);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error:" + e.getMessage());
}
}
@@ -538,21 +546,24 @@ public class Main {
return result;
}
private static int calculatePercentage(Double first, Double second) {
Double d = (1 - first / second) * 100;
return Math.abs(d.intValue());
}
private static void drawGraph(String title, ArrayList<Double> values){
private static void drawGraph(String title, ArrayList<Double> valuesBase, ArrayList<Double> values1, ArrayList<Double> values2){
JFrame frame = new JFrame("Graph");
frame.setSize(700, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
DrawGraph graph = new DrawGraph(values);
List<Point.Double> graphPointsBuy = new ArrayList<>();
List<Point.Double> graphPointsSell = new ArrayList<>();
for(Order o : orders.orders){
if(o.ordertype.equals("buy")) graphPointsBuy.add(new Point2D.Double(o.timeframe,o.price));
if(o.ordertype.equals("sell")) graphPointsSell.add(new Point2D.Double(o.timeframe,o.price));
}
DrawGraph graph = new DrawGraph(valuesBase, values1, values2, graphPointsBuy, graphPointsSell);
frame.add(graph);
frame.setVisible(true);

View File

@@ -6,6 +6,7 @@ public class Order {
public String type;
public String ordertype;
public Double price;
public Double timeframe;
public Double volume;
public String leverage = "none";