-
Notifications
You must be signed in to change notification settings - Fork 0
/
StaticProviderGoogle.java
96 lines (85 loc) · 3.24 KB
/
StaticProviderGoogle.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
package contangoStaticProvider;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.StringTokenizer;
import contangoAPI.api.ABaseStaticProvider;
import contangoAPI.api.Bar;
public class StaticProviderGoogle extends ABaseStaticProvider {
/**
* Convert number of month to name
* @param month: number of month (1..12)
* @return month as name
*/
private static String theMonth(int month) {
String[] monthNames = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
return monthNames[month];
}
@Override
public ArrayList<Bar> getData(String symbol, LocalDateTime ldt1, LocalDateTime ldt2, LocalTime lt1, LocalTime lt2,
int timeframe) {
ArrayList<Bar> dataItems = new ArrayList<Bar>();
String strUrl = getURL(symbol, ldt1, ldt2);
try {
URL url = new URL(strUrl);
try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) {
final DateFormat df = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
String inputLine;
in.readLine(); // skip 1-st line
while ((inputLine = in.readLine()) != null) {
StringTokenizer st = new StringTokenizer(inputLine, ",");
Date date = df.parse(st.nextToken());
double open = Double.parseDouble(st.nextToken());
double high = Double.parseDouble(st.nextToken());
double low = Double.parseDouble(st.nextToken());
double close = Double.parseDouble(st.nextToken());
double volume = Double.parseDouble(st.nextToken());
dataItems.add(new Bar(open, close, high, low, volume, date));
}
}
} catch (ParseException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(String.format("URL problem: %s", strUrl), e);
}
return dataItems;
}
/**
* Prepare URL string for the data provider
* @param symbol: security code
* @param ldt1: start date
* @param ldt2: end date
* @return URL string
*/
private static String getURL(String symbol, LocalDateTime ldt1, LocalDateTime ldt2) {
StringBuilder buf = new StringBuilder();
buf.append("http://www.google.com/finance/historical?q=").append(symbol);
buf.append("&histperiod=daily&startdate=").append(theMonth(ldt1.getMonthValue()));
buf.append("+").append(String.valueOf(ldt1.getDayOfMonth()));
buf.append("+").append(String.valueOf(ldt1.getYear()));
buf.append("&enddate=").append(theMonth(ldt2.getMonthValue()));
buf.append("+").append(String.valueOf(ldt2.getDayOfMonth()));
buf.append("+").append(String.valueOf(ldt2.getYear()));
buf.append("&output=csv");
return buf.toString();
}
@Override
public String getDescription() {
return "Google data source (DAILY)";
}
@Override
public void load() {
}
@Override
public void unload() {
}
}