Yahoo 股票数据接口 实现 Java版
package com.trendfollower.data.process;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class GetCsvDataFromYahoo {
public static final String YAHOO_FINANCE_URL ="http://table.finance.yahoo.com/table.csv?";
public static final StringYAHOO_FINANCE_URL_TODAY = "http://download.finance.yahoo.com/d/quotes.csv?";
private Date x;//time
private double y1 = 0.0;
private double y2 = 0.0;
private double y3 = 0.0;
private double y4 = 0.0;
private double y5 = 0.0;
private double y6 = 0.0;
public GetCsvDataFromYahoo() {
super();
}
public GetCsvDataFromYahoo(Date x, double y1){
super();
this.x = x;
this.y1 = y1;
}
public GetCsvDataFromYahoo(Date x, double y1,double y2) {
super();
this.x = x;
this.y1 = y1;
this.y2 = y2;
}
public GetCsvDataFromYahoo(Date x, double y1,double y2, double y3) {
super();
this.x = x;
this.y1 = y1;
this.y2 = y2;
this.y3 = y3;
}
public GetCsvDataFromYahoo(Date x, double y1,double y2, double y3,double y4) {
super();
this.x = x;
this.y1 = y1;
this.y2 = y2;
this.y3 = y3;
this.y4 = y4;
}
public GetCsvDataFromYahoo(Date x, double y1,double y2, double y3,double y4, double y5) {
super();
this.x = x;
this.y1 = y1;
this.y2 = y2;
this.y3 = y3;
this.y4 = y4;
this.y5 = y5;
}
public GetCsvDataFromYahoo(Date x, double y1,double y2, double y3,double y4, double y5, double y6) {
super();
this.x = x;
this.y1 = y1;
this.y2 = y2;
this.y3 = y3;
this.y4 = y4;
this.y5 = y5;
this.y6 = y6;
}
public Date getX() {
return x;
}
public void setX(Date x) {
this.x = x;
}
public double getY1() {
return y1;
}
public void setY1(double y1) {
this.y1 = y1;
}
public double getY2() {
return y2;
}
public void setY2(double y2) {
this.y2 = y2;
}
public double getY3() {
return y3;
}
public void setY3(double y3) {
this.y3 = y3;
}
public double getY4() {
return y4;
}
public void setY4(double y4) {
this.y4 = y4;
}
public double getY5() {
return y5;
}
public void setY5(double y5) {
this.y5 = y5;
}
public double getY6() {
return y6;
}
public void setY6(double y6) {
this.y6 = y6;
}
public static ArrayList getStockCsvData(String stockName, int T) throws Exception {
ArrayList list = newArrayList();
//===================================================
// 创建url
//===================================================
// 参数
// s –股票名称
// a –起始时间,月
// b –起始时间,日
// c –起始时间,年
// d –结束时间,月
// e –结束时间,日
// f –结束时间,年
// g –时间周期。Example: g=w, 表示周期是’周’。d->’日’(day),
// w->’周’(week),m->’月’(mouth),v->’dividendsonly’
String period = "";
if (T > 0){
//
Calendar cal=Calendar.getInstance(Locale.CHINESE);
int d =cal.get(Calendar.MONTH);
int e =cal.get(Calendar.DAY_OF_MONTH);
int f =cal.get(Calendar.YEAR);
System.out.print("获取数据\t"+ stockName);
System.out.print("\t"+ cal.getTime().toLocaleString());
cal.add(Calendar.DAY_OF_MONTH, 0 - T);
System.out.println("\t"+ cal.getTime().toLocaleString());
int a =cal.get(Calendar.MONTH);
int b =cal.get(Calendar.DAY_OF_MONTH);
int c =cal.get(Calendar.YEAR);
period ="&a=" + a + "&b=" + b +"&c=" + c
+ "&d=" + d +"&e=" + e + "&f=" + f;
} else {
//<=0 时,取全部数据
period ="";
}
String url =YAHOO_FINANCE_URL + "s=" + stockName + period;
//=======================================
// 从URL获取数据,并且解析每一行数据
//=========================================
URL MyURL = null;
URLConnection con = null;
InputStreamReader ins =null;
BufferedReader in = null;
try {
MyURL = newURL(url);
con =MyURL.openConnection();
ins = newInputStreamReader(con.getInputStream(),"UTF-8");
in = newBufferedReader(ins);
//标题行
StringnewLine = in.readLine();
//今天的股票数据
// TODOYahoo财经股票数据接口,取得的历史数据不包含今天的股票数据。
list.add(createObject(getStockTodayData(stockName).trim()));
//历史数据,不含今天数据
while((newLine = in.readLine()) != null) {
//System.out.println(newLine);
list.add(createObject(newLine.trim()));
}
} catch (Exception ex){
ex.printStackTrace();
} finally {
if(in !=null)
in.close();
}
return list;
}
public static String getStockTodayData(StringstockName) throws Exception {
String result = "";
//===================================================
// 创建url
//===================================================
// 【例子】
// http://download.finance.yahoo.com/d/quotes.csv?s=000300.ss&f=d1ohgl1vl1
// d1 Last Trade Date 交易日期
// o Open 开盘价
// h Day’s High 最高价
// g Day’s Low 最低价
// l1 Last Trade (with Date) 最后一次交易价格,相当于收盘价
// v Volume 交易量
// l1 Last Trade (with Date) 最后一次交易价格 (今天的收盘价当做加权价格)
//
// k 52-week High 年内最高价
// j 52-week Low 年内最低价
String url =YAHOO_FINANCE_URL_TODAY + "s=" + stockName +"&f=d1ohgl1vl1";
//=======================================
// 从URL获取数据
//=========================================
URL MyURL = null;
URLConnection con = null;
InputStreamReader ins =null;
BufferedReader in = null;
try {
MyURL = newURL(url);
con =MyURL.openConnection();
ins = newInputStreamReader(con.getInputStream(),"UTF-8");
in = newBufferedReader(ins);
result =in.readLine();
// httpresponse返回的字符串中,日期包含 双引号“ 必须删掉。
if(result !=null) result = result.replace("\"","");
} catch (SocketException ex){
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} finally{
if(in !=null)
in.close();
}
return result;
}
public static GetCsvDataFromYahoocreateObject(String line){
GetCsvDataFromYahoo point = newGetCsvDataFromYahoo();
int pos = 0;
String format = null;
String [] data = null;
if(line.indexOf(',')> 0){
data =line.split(",");
} else {
data =line.split(" ");
}
try {
//寻找合适的日期格式
if(data[0].indexOf('-') > 0) {
if(data[0].length() == 10)
format= "yyyy-MM-dd";
else
format= "yyyy-M-d";
} else if(data[0].indexOf('.') > 0) {
if(data[0].length() == 10)
format= "yyyy.MM.dd";
else
format= "yyyy.M.d";
} else if(data[0].indexOf('/') > 0) {
if(data[0].length() == 10){
if(data[0].indexOf('/')> 3)
format= "yyyy/MM/dd";//年月日
else
format= "MM/dd/yyyy";//月日年
}else {
if(data[0].indexOf('/')> 3)
format= "yyyy/M/d";//年月日
else
format= "M/d/yyyy";//月日年
}
}
//Date,Open,High,Low,Close,Volume,AdjClose
SimpleDateFormatsdf = new SimpleDateFormat(format);
try {
point.setX(sdf.parse(data[pos++]));
} catch(ParseException ex) {
ex.printStackTrace();
}
point.setY1(parseDouble(data[pos++]));//Open
point.setY2(parseDouble(data[pos++]));//High
point.setY3(parseDouble(data[pos++]));//Low