Graph implemented
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7">
|
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||||
<output url="file://$MODULE_DIR$/target/classes" />
|
<output url="file://$MODULE_DIR$/target/classes" />
|
||||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
|
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
<orderEntry type="library" name="Maven: com.mashape.unirest:unirest-java:1.4.9" level="project" />
|
<orderEntry type="library" name="Maven: com.mashape.unirest:unirest-java:1.4.9" level="project" />
|
||||||
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.3.6" level="project" />
|
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.3.6" level="project" />
|
||||||
@@ -24,5 +24,7 @@
|
|||||||
<orderEntry type="library" name="Maven: junit:junit:4.10" level="project" />
|
<orderEntry type="library" name="Maven: junit:junit:4.10" level="project" />
|
||||||
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
|
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
|
||||||
<orderEntry type="library" name="Maven: joda-time:joda-time:2.9.9" level="project" />
|
<orderEntry type="library" name="Maven: joda-time:joda-time:2.9.9" level="project" />
|
||||||
|
<orderEntry type="library" name="Maven: org.jgrapht:jgrapht-core:1.0.1" level="project" />
|
||||||
|
<orderEntry type="library" name="Maven: jgraph:jgraph:5.13.0.0" level="project" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
||||||
12
pom.xml
12
pom.xml
@@ -80,8 +80,16 @@
|
|||||||
<artifactId>joda-time</artifactId>
|
<artifactId>joda-time</artifactId>
|
||||||
<version>2.9.9</version>
|
<version>2.9.9</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jgrapht</groupId>
|
||||||
|
<artifactId>jgrapht-core</artifactId>
|
||||||
|
<version>1.0.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jgraph</groupId>
|
||||||
|
<artifactId>jgraph</artifactId>
|
||||||
|
<version>5.13.0.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
121
src/main/java/DrawGraph.java
Normal file
121
src/main/java/DrawGraph.java
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
import java.awt.BasicStroke;
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.RenderingHints;
|
||||||
|
import java.awt.Stroke;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
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 static final int PREF_W = 800;
|
||||||
|
private static final int PREF_H = 650;
|
||||||
|
private static final int BORDER_GAP = 30;
|
||||||
|
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;
|
||||||
|
|
||||||
|
public DrawGraph(List<Double> scores) {
|
||||||
|
this.scores = scores;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
Graphics2D g2 = (Graphics2D)g;
|
||||||
|
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
|
|
||||||
|
double xScale = ((double) getWidth() - 2 * BORDER_GAP) / (scores.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));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
int x1 = GRAPH_POINT_WIDTH + BORDER_GAP;
|
||||||
|
int y0 = getHeight() - (((i + 1) * (getHeight() - BORDER_GAP * 2)) / Y_HATCH_CNT + BORDER_GAP);
|
||||||
|
int y1 = y0;
|
||||||
|
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;
|
||||||
|
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.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;;
|
||||||
|
int ovalW = GRAPH_POINT_WIDTH;
|
||||||
|
int ovalH = GRAPH_POINT_WIDTH;
|
||||||
|
g2.fillOval(x, y, ovalW, ovalH);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Dimension getPreferredSize() {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import com.mashape.unirest.http.exceptions.UnirestException;
|
|||||||
import org.apache.commons.math3.stat.regression.SimpleRegression;
|
import org.apache.commons.math3.stat.regression.SimpleRegression;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
@@ -22,8 +23,8 @@ 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(15);
|
private static MovingAverage ma15 = new MovingAverage(150);
|
||||||
private static MovingAverage ma50 = new MovingAverage(50);
|
private static MovingAverage ma50 = new MovingAverage(500);
|
||||||
|
|
||||||
private static OrderHistory orders = new OrderHistory();
|
private static OrderHistory orders = new OrderHistory();
|
||||||
private static Funds funds = new Funds();
|
private static Funds funds = new Funds();
|
||||||
@@ -47,6 +48,7 @@ public class Main {
|
|||||||
private static boolean backtest = false;
|
private static boolean backtest = false;
|
||||||
|
|
||||||
private static Map<String, Double > results = new HashMap<>();
|
private static Map<String, Double > results = new HashMap<>();
|
||||||
|
private static ArrayList<Double> graphValues = 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...");
|
||||||
@@ -87,6 +89,7 @@ public class Main {
|
|||||||
return;
|
return;
|
||||||
}else if(input.equals("y")){
|
}else if(input.equals("y")){
|
||||||
backtest = true;
|
backtest = true;
|
||||||
|
|
||||||
doBacktest();
|
doBacktest();
|
||||||
|
|
||||||
doOrder(new Order("buy"));
|
doOrder(new Order("buy"));
|
||||||
@@ -213,6 +216,31 @@ public class Main {
|
|||||||
return newOrder;
|
return newOrder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Order decide2() {
|
||||||
|
Order newOrder = new Order();
|
||||||
|
newOrder.price = currentPrice;
|
||||||
|
|
||||||
|
if(!regressionbacktest) System.out.println("------- START ORDER -------");
|
||||||
|
if (buyindicator > BUY_THRESHOLD) {
|
||||||
|
|
||||||
|
if(!regressionbacktest) System.out.println("BUY NOW - PRICE: " + currentPrice);
|
||||||
|
newOrder.ordertype = "buy";
|
||||||
|
|
||||||
|
} else if (buyindicator > SELL_THRESHOLD) {
|
||||||
|
if(!regressionbacktest) System.out.println("DO NOTHING - NEUTRAL: " + currentPrice);
|
||||||
|
counterneutrals++;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
if(!regressionbacktest) System.out.println("SELL NOW - PRICE: " + currentPrice);
|
||||||
|
newOrder.ordertype = "sell";
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
if(!regressionbacktest) System.out.println("------- END ORDER -------");
|
||||||
|
|
||||||
|
return newOrder;
|
||||||
|
}
|
||||||
|
|
||||||
private static String calculateTrend() {
|
private static String calculateTrend() {
|
||||||
if ((Double.compare(buyvslope, 0) > 0) && (Double.compare(priceslope, 0) > 0)) {
|
if ((Double.compare(buyvslope, 0) > 0) && (Double.compare(priceslope, 0) > 0)) {
|
||||||
if (Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) > 0) {
|
if (Double.compare(trades.volumeOfBuyTrades, trades.volumeOfSellTrades) > 0) {
|
||||||
@@ -308,13 +336,13 @@ public class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static String calculateTrend_4() {
|
private static String calculateTrend_4() {
|
||||||
if(counter < 50) return "neutral";
|
if(counter < 500) return "neutral";
|
||||||
|
|
||||||
if(ma15.getAverage().compareTo(ma50.getAverage()) == 1){
|
if(ma15.getAverage().compareTo(ma50.getAverage()) == 1){
|
||||||
buyindicator += 2;
|
buyindicator += 3;
|
||||||
return "normal buy";
|
return "normal buy";
|
||||||
}else if(ma15.getAverage().compareTo(ma50.getAverage()) == -1){
|
}else if(ma15.getAverage().compareTo(ma50.getAverage()) == -1){
|
||||||
buyindicator -= 2;
|
buyindicator -= 3;
|
||||||
return "normal sell";
|
return "normal sell";
|
||||||
}else{
|
}else{
|
||||||
return "neutral";
|
return "neutral";
|
||||||
@@ -368,8 +396,7 @@ public class Main {
|
|||||||
|
|
||||||
ma15.add(currentPrice);
|
ma15.add(currentPrice);
|
||||||
ma50.add(currentPrice);
|
ma50.add(currentPrice);
|
||||||
|
graphValues.add(currentPrice);
|
||||||
|
|
||||||
|
|
||||||
trades = new TradeHistory(volumeArray, sinceid);
|
trades = new TradeHistory(volumeArray, sinceid);
|
||||||
|
|
||||||
@@ -381,11 +408,12 @@ public class Main {
|
|||||||
sellvslope = srsellvolume.getSlope();
|
sellvslope = srsellvolume.getSlope();
|
||||||
|
|
||||||
trend = calculateTrend_4();
|
trend = calculateTrend_4();
|
||||||
|
//System.out.println("Trend: "+trend+" shortAVG "+ma15.getAverage()+" longAVG "+ma50.getAverage());
|
||||||
|
|
||||||
outputAnalyzationResults();
|
outputAnalyzationResults();
|
||||||
|
|
||||||
if (((counter % NUM_ITERATIONS_FOR_ORDER) == 0) && (counter != 0)) {
|
if (((counter % NUM_ITERATIONS_FOR_ORDER) == 0) && (counter != 0)) {
|
||||||
Order order = decide();
|
Order order = decide2();
|
||||||
|
|
||||||
if (order.ordertype != null) doOrder(order);
|
if (order.ordertype != null) doOrder(order);
|
||||||
|
|
||||||
@@ -401,6 +429,9 @@ public class Main {
|
|||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
drawGraph("Threshold: "+BUY_THRESHOLD+" Iterations: "+NUM_ITERATIONS_FOR_ORDER,graphValues);
|
||||||
|
graphValues.clear();
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -513,5 +544,18 @@ public class Main {
|
|||||||
return Math.abs(d.intValue());
|
return Math.abs(d.intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void drawGraph(String title, ArrayList<Double> values){
|
||||||
|
JFrame frame = new JFrame("Graph");
|
||||||
|
|
||||||
|
frame.setSize(700, 400);
|
||||||
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
JPanel panel = new JPanel();
|
||||||
|
|
||||||
|
DrawGraph graph = new DrawGraph(values);
|
||||||
|
frame.add(graph);
|
||||||
|
|
||||||
|
frame.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user