How to work with Google Finance?

拟墨画扇 提交于 2019-12-03 08:20:00
rajaram_s

The Google Finance Gadget API has been officially deprecated since October 2012, but as of April 2014, it's still active:

http://www.google.com/finance/info?q=NASDAQ:ADBE

Note that if your application is for public consumption, using the Google Finance API is against Google's terms of service.

This gives a JSON response which can be parsed using a simple JSON parser in C# after chopping off the first two chars ('//').

For downloading historic data again, you could use the Google APIs.

http://www.google.com/finance/historical?q=NASDAQ:ADBE&startdate=Jan+01%2C+2009&enddate=Aug+2%2C+2012&output=csv

gives out a CSV of end of day stock prices from startdate to enddate. Use a simple CSV parser to get meaningful data out of this stored on your db. However, this format=csv option does not work for a few stock exchanges.

If you want to download historical data you can use the Google Finance API (which still works as of May 2016). You do not have to provide an end date, it will automatically fetch data from the start date (or later if the stock did not trade then) to the last full trade date:

http://www.google.com/finance/historical?q=NASDAQ:AAPL&startdate=Jan+01%2C+2000&output=csv

Remember that Google Finance API are for personal consumption ONLY. I suggest you their terms of service.

If you want to simply download the latest date (which could be useful to update your local db) you can use the googlefinance library developed by Hongtao Cai:

https://pypi.python.org/pypi/googlefinance

I have just implemented this with PHP. It might be useful.

<?php

echo readGoogle('AAPL', 'Aug+21%2C+2017', 'Aug+22%2C+2017');

function readGoogle($ticker, $startDate, $endDate) {


$fp = fopen("http://finance.google.com/finance/historical?q=".$ticker."&startdate=".$startDate."&enddate=".$endDate."&output=csv", 'r');


if (FALSE === $fp) return 'Can not open data.';

$buffer = '';
while (!feof($fp)) $buffer .= implode(',', (array)fgetcsv($fp, 5000));

fclose($fp);

return $buffer;

}

?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!