-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticProviderFinContent.java
More file actions
103 lines (92 loc) · 2.99 KB
/
StaticProviderFinContent.java
File metadata and controls
103 lines (92 loc) · 2.99 KB
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
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 contangoAPI.api.ABaseStaticProvider;
import contangoAPI.api.Bar;
public class StaticProviderFinContent extends ABaseStaticProvider {
/**
* format of request:
*
* http://markets.financialcontent.com/stocks/action/gethistoricaldata?Month=12
* &Symbol=[SYMBOL NAME]&Range=300&Year=2017
*/
@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, ldt2);
try {
URL url = new URL(strUrl);
try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) {
final DateFormat df = new SimpleDateFormat("MM/dd/yy", Locale.ENGLISH);
String inputLine;
in.readLine(); // skip header line
myLabel: while ((inputLine = in.readLine()) != null) {
String[] ar = inputLine.split(",");
// check and skip damaged values
for(int i = 1; i < 7; i++) {
if (ar[i].equals("")) {
continue myLabel;
}
}
Date date = df.parse(ar[1]);
if (date.getTime() < java.sql.Timestamp.valueOf(ldt1).getTime())
continue;
dataItems.add(new Bar(
Double.parseDouble(ar[2]),
Double.parseDouble(ar[5]),
Double.parseDouble(ar[3]),
Double.parseDouble(ar[4]),
Double.parseDouble(ar[6]),
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 ldt2) {
StringBuilder buf = new StringBuilder();
buf.append("http://markets.financialcontent.com/stocks/action/gethistoricaldata?Symbol=")
.append(symbol);
buf.append("&Range=300");
buf.append("&Month=").append(ldt2.getMonthValue());
buf.append("&Year=").append(ldt2.getYear());
return buf.toString();
}
@Override
public String getDescription() {
return "Financial Content data source (DAILY)";
}
@Override
public void load() {
}
@Override
public void unload() {
}
}