-
Notifications
You must be signed in to change notification settings - Fork 0
/
YahooAPI.java
84 lines (64 loc) · 1.82 KB
/
YahooAPI.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
public class YahooAPI {
int startMonth;
int startDay;
int startYear;
int endMonth;
int endDay;
int endYear;
String freq;
public ArrayList<Double> getClose(String symbol){
String baseUrl = "http://ichart.finance.yahoo.com/table.csv?ignore=.csv";
baseUrl += "&s=" + symbol;
baseUrl += "&a=" + startMonth;
baseUrl += "&b=" + startDay;
baseUrl += "&c=" + startYear;
baseUrl += "&d=" + endMonth;
baseUrl += "&e=" + endDay;
baseUrl += "&f=" + endYear;
baseUrl += "&g=" + freq;
URL url;
ArrayList<Double> close = new ArrayList<Double>();
System.out.print("Opening URL: ");
System.out.print(baseUrl);
System.out.println(" ");
try {
url = new URL(baseUrl);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
in.readLine(); //Forward Header
while (true){
String thisLine = in.readLine();
if (thisLine == null){
break;
}
String[] separatedLine = thisLine.split("[,X]"); // split by commas
close.add(Double.parseDouble(separatedLine[4]));
}
return close;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void main(String args[]){
YahooAPI y = new YahooAPI();
y.startDay = 3;
y.startMonth = 2; //0 is jan, so 2 is march
y.startYear = 2011;
y.endDay = 20;
y.endMonth = 2; //0 is jan, so 2 is march
y.endYear = 2011;
y.freq = "d"; // daily frequency, w for weekly, m for monthly
ArrayList<Double> close = y.getClose("GE");
Iterator<Double> iter = close.iterator();
System.out.println("Returned Close Values:");
while (iter.hasNext()){
System.out.println(iter.next());
}
}
}