加载中…
个人资料
文和_股市脉冲波复利
文和_股市脉冲波复利
  • 博客等级:
  • 博客积分:0
  • 博客访问:486,425
  • 关注人气:353
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

[转载]Google finance api

(2013-02-03 10:58:17)
标签:

转载

分类: 数据接口/API
原文地址:Google finance api作者:月下老人
//从google api 可以得到及时得到证券交易信息,包括中国A股的实时信息,而且基本没有时间上的延迟。
但是,我还没有发现如何能的到比如,现在的股价,成交量,最高价,最低价,开盘价,涨跌数,和涨跌百分比。
但是所有这些可以通过计算得出。
api在拿现在持仓市值上有些问题,在国内交易时间,我们拿到的总是一昨天收盘价计算的市值,
而当天的收益是最准确的。
因此,我们只能通过这两个价格来计算,现在的股价以及今天的涨跌。而其它信息则不能得到。
I don't why google does not  open this api directly。
//notes
在进行查询之前必须先add a share‘s transaction

package com.ysh.utils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.EnumMap;

import com.google.gdata.client.finance.FinanceService;
import com.google.gdata.client.finance.FinanceUtilities;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.extensions.Money;
import com.google.gdata.data.finance.PositionData;
import com.google.gdata.data.finance.PositionEntry;
import com.google.gdata.data.finance.TransactionEntry;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class GooFiTest {

private static String server = "http://finance.google.com";
private static String basePath = "/finance/feeds/";
private static String baseUrl = server + basePath + "default";

// Feed and Entry URI suffixes:
private static final String PORTFOLIO_FEED_URL_SUFFIX = "/portfolios";
private static final String POSITION_FEED_URL_SUFFIX = "/positions";
private static final String TRANSACTION_FEED_URL_SUFFIX = "/transactions";
private static final String POSITION_TICKER_SHA = "SHA:";
private static final String POSITION_TICKER_SHE = "SHE:";
private FinanceService service = new FinanceService(
"Google-PortfoliosDemo-1.0");

private enum Columns {
CODE, PRICE, PERCENTGAIN, GAIN,LASTUPDATE
}


// private final String

public String getWholeTicker(String code) {
if (code.startsWith("6")) {
return POSITION_TICKER_SHA + code;
} else
return POSITION_TICKER_SHE + code;
}

public void login() {
String userEmail = "yushihui@gmail.com";
String userPassword = "************";
if (!loginUser(userEmail, userPassword))
System.exit(0);
}

public void addPosition(String shareCode) throws IOException, IOException,
ServiceException {

String requestUrl = baseUrl + PORTFOLIO_FEED_URL_SUFFIX;
System.out.println("Enter portfolio ID");

System.out.println("Enter ticker (<exchange>:<ticker>) ");
String tickerProperty = getWholeTicker(shareCode);

requestUrl += "1" + POSITION_FEED_URL_SUFFIX + "/" + tickerProperty
+ TRANSACTION_FEED_URL_SUFFIX;

TransactionEntry entry = FinanceUtilities.makeTransactionEntry("Buy",
"2010-03-08", "100", "1.00", "0.0", "CNY", "ysh");
insertTransactionEntry(requestUrl, entry);

}

public EnumMap<Columns, String>  getCurrentShareData(String code) throws MalformedURLException,
IOException, ServiceException {

String requestUrl = baseUrl + PORTFOLIO_FEED_URL_SUFFIX + "/1";
String tickerProperty = getWholeTicker(code);
requestUrl += POSITION_FEED_URL_SUFFIX + "/" + tickerProperty;
return queryPositionEntry(code, requestUrl);

}

private EnumMap<Columns, String>  queryPositionEntry(String code, String entryUrl)
throws IOException, MalformedURLException, ServiceException {
//System.out.println("Requesting Entry at location " + entryUrl);
PositionEntry positionEntry = service.getEntry(new URL(entryUrl),
PositionEntry.class);
return getPositionData(code, positionEntry);
}

private EnumMap<Columns, String> getPositionData(String code,
PositionEntry positionEntry) {
// System.out.println("nPosition Entryn--------------");
// printBasicEntryDetails(positionEntry);
positionEntry.getUpdated();

EnumMap<Columns, String> sd = new EnumMap<Columns, String>(Columns.class);
DateTime lastUpdate=positionEntry.getUpdated();
lastUpdate.setTzShift(480);
sd.put(Columns.LASTUPDATE, lastUpdate.toString());
sd.put(Columns.CODE, code);
// System.out.println("tFeed Link: "
// + positionEntry.getFeedLink().getHref());
if (positionEntry.getFeedLink().getFeed() == null) {
System.out.println("tNo inlined feed.");
} else {
System.out
.println("********** Beginning of inline feed ***************");

System.out
.println("************* End of inlined feed *****************");
}

PositionData positionData = positionEntry.getPositionData();

Double yesterDay = 0.0;
Double daysGain;
DecimalFormat df=new DecimalFormat("#####0.00");

if (positionData.getMarketValue() == null) {
System.out.println("ttMarket Value not specified");
} else {
for (int i = 0; i < positionData.getMarketValue().getMoney().size(); i++) {
Money m = positionData.getMarketValue().getMoney().get(i);
yesterDay = m.getAmount();
// System.out.printf(
// "ttThis position per share is worth yesterday %.2f %s.n", m
// .getAmount() / positionData.getShares(), m.getCurrencyCode());


}
}

if (positionData.getDaysGain() == null) {
System.out.println("ttDay's Gain not specified");
} else {
for (int i = 0; i < positionData.getDaysGain().getMoney().size(); i++) {
Money m = positionData.getDaysGain().getMoney().get(i);
daysGain = m.getAmount();
Double price=(m.getAmount()+yesterDay) / positionData.getShares();
Double c_price=Double.valueOf(price);
sd.put(Columns.PRICE, df.format(price));
// System.out.printf("ttThis position made %.2f %s today.n", m
// .getAmount(), m.getCurrencyCode());
String gain=df.format(m.getAmount() / positionData.getShares());
sd.put(Columns.GAIN, gain);
String pergain=
sd.put(Columns.PERCENTGAIN, df.format(100*(daysGain
/ yesterDay))
+ "%");

}
}

return sd;
}

private void insertTransactionEntry(String feedUrl, TransactionEntry entry)
throws IOException, MalformedURLException, ServiceException {
//System.out.println("Inserting Entry at location: " + feedUrl);
service.insert(new URL(feedUrl), entry);
}

private Boolean loginUser(String userID, String userPassword) {
try {
service.setUserCredentials(userID, userPassword);
} catch (AuthenticationException e) {
System.err.println("Invalid Credentials!");
e.printStackTrace();
return false;
}
return true;
}
public static void main(String args[]){
GooFiTest gft=new GooFiTest();
long t1=System.currentTimeMillis();
gft.login();
try {
EnumMap<Columns, String> shareResult= gft.getCurrentShareData("601318");
System.out.println(shareResult.get(Columns.CODE));
System.out.println(" current value is ");
System.out.println(shareResult.get(Columns.PRICE));
System.out.println("  ");
System.out.println(shareResult.get(Columns.GAIN));
System.out.println("  ");
System.out.println(shareResult.get(Columns.PERCENTGAIN));
System.out.println(" last update ");
System.out.println(shareResult.get(Columns.LASTUPDATE));

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(" time:"+(System.currentTimeMillis()-t1));
}
}

  评论这张
转发至微博

0

  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有