forked from mdeverdelhan/ta4j-origins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuandlTicksLoader.java
139 lines (121 loc) · 5.37 KB
/
QuandlTicksLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* The MIT License (MIT)
*
* Copyright (c) 2014-2016 Marc de Verdelhan & respective authors (see AUTHORS)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package ta4jexamples.loaders;
import au.com.bytecode.opencsv.CSVReader;
import eu.verdelhan.ta4j.Tick;
import eu.verdelhan.ta4j.TimeSeries;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class QuandlTicksLoader {
public static class QuandlDataFeed {
private final String quandlCode;
private final DateTime startDate;
private final DateTime endDate;
private static final String ISO_8601_DATE_FMT = "yyyy-MM-dd";
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormat.forPattern(ISO_8601_DATE_FMT);
// changes these to a Builder if more params
public QuandlDataFeed(String quandlCode) {
this(quandlCode, DateTime.now().minusYears(1), DateTime.now());
}
public QuandlDataFeed(String quandlCode, DateTime startdate) {
this(quandlCode, startdate, DateTime.now());
}
public QuandlDataFeed(String quandlCode, DateTime startDate, DateTime endDate) {
this.quandlCode = quandlCode;
this.startDate = startDate;
this.endDate = endDate;
}
public TimeSeries load() {
return get(buildURL());
}
private URL buildURL() {
String urlString = String.format("https://www.quandl.com/api/v3/datasets/%s.csv?start_date=%s&end_date=%s&order=asc",
quandlCode, DATE_FORMATTER.print(startDate), DATE_FORMATTER.print(endDate));
System.out.println(String.format("Quandl Dataset URL for %s", urlString));
try {
return new URL(urlString);
} catch (MalformedURLException e) {
throw new RuntimeException("Invalid URL");
}
}
private TimeSeries toTimeSeries(InputStream is) {
List<Tick> ticks = new ArrayList<Tick>();
CSVReader csvReader = new CSVReader(new InputStreamReader(is, Charset.forName("UTF-8")), ',', '"', 1);
try {
String[] line;
while ((line = csvReader.readNext()) != null) {
DateTime date = new DateTime(DATE_FORMATTER.parseDateTime(line[0]));
double open = Double.parseDouble(line[1]);
double high = Double.parseDouble(line[2]);
double low = Double.parseDouble(line[3]);
double close = Double.parseDouble(line[4]);
double volume = Double.parseDouble(line[5]);
ticks.add(new Tick(date, open, high, low, close, volume));
}
} catch (IOException ioe) {
Logger.getLogger(CsvTicksLoader.class.getName()).log(Level.SEVERE, "Unable to load ticks from CSV", ioe);
} catch (NumberFormatException nfe) {
Logger.getLogger(CsvTicksLoader.class.getName()).log(Level.SEVERE, "Error while parsing value", nfe);
}
return new TimeSeries(quandlCode + "_ticks", ticks);
}
private TimeSeries get(URL url) {
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setUseCaches(true);
return toTimeSeries(new BufferedInputStream(connection.getInputStream()));
} catch (FileNotFoundException fnfe) {
throw new RuntimeException(String.format("%s is not a valid Quandl URL", url));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
}
public static void main(String args[]) {
QuandlDataFeed quandl = new QuandlDataFeed("YAHOO/TWTR", DateTime.now().minusMonths(6));
TimeSeries series = quandl.load();
System.out.println("Series: " + series.getName() + " (" + series.getSeriesPeriodDescription() + ")");
System.out.println("Number of ticks: " + series.getTickCount());
System.out.println("Last tick: \n"
+ "\tVolume: " + series.getLastTick().getVolume() + "\n"
+ "\tOpen price: " + series.getLastTick().getOpenPrice()+ "\n"
+ "\tClose price: " + series.getLastTick().getClosePrice());
}
}