NexusFi: Find Your Edge


Home Menu

 





Google Finance - historical daily data retrieved programmatically


Discussion in Brokers

Updated
    1. trending_up 23,077 views
    2. thumb_up 2 thanks given
    3. group 1 followers
    1. forum 5 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
 
Nicolas11's Avatar
 Nicolas11 
near Paris, France
 
Experience: Beginner
Platform: -
Trading: -
Posts: 1,071 since Aug 2011
Thanks Given: 2,232
Thanks Received: 1,774

Hi,

In this thread, I will propose several ways to retrieve historical data from Google Finance :
- Web Query 1 (from Java code),
- Web Query 2 (from Java code),
- YQL,
- Quantmod (with R).

If you have other or better ideas, do not hesitate to contribute.

Nicolas


Visit my NexusFi Trade Journal Started this thread Reply With Quote

 
  #3 (permalink)
 
Nicolas11's Avatar
 Nicolas11 
near Paris, France
 
Experience: Beginner
Platform: -
Trading: -
Posts: 1,071 since Aug 2011
Thanks Given: 2,232
Thanks Received: 1,774


1) Web Query 1 (and related Java code)

Let's consider the following URL :
https://www.google.com/finance/historical?q=GOOG&histperiod=daily&startdate=Apr+1+2014&enddate=Apr+15+2014&output=csv

It generates a CSV file for Google stock, with daily data from April 1st to April 15th, 2014

Example of Java code:
 
Code
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

// inspired by http://stackoverflow.com/questions/9093000/read-csv-file-from-internet
public class Google_ReadCSVFromWebQuery1 {

    public static void main(String[] args) {
        try {
            URL url = new URL("http://www.google.com/finance/historical?q=GOOG&histperiod=daily&startdate=Apr+1+2014&enddate=Apr+15+2014&output=csv");
            URLConnection urlConn = url.openConnection();
            InputStreamReader inputStreamReader = new InputStreamReader(urlConn.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
            bufferedReader.close();
            inputStreamReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
It generates the following output:

Quoting 
Date,Open,High,Low,Close,Volume
15-Apr-14,536.82,538.45,518.46,536.44,3847453
14-Apr-14,538.25,544.10,529.56,532.52,2568020
11-Apr-14,532.55,540.00,526.53,530.60,3916171
10-Apr-14,565.00,565.00,539.90,540.95,4027743
9-Apr-14,559.62,565.37,552.95,564.14,3324742
8-Apr-14,542.60,555.00,541.61,554.90,3152406
7-Apr-14,540.74,548.48,527.15,538.15,4389569
4-Apr-14,574.65,577.77,543.00,543.14,6377658
3-Apr-14,569.85,587.28,564.13,569.74,5087530
2-Apr-14,570.38,571.83,561.44,566.98,2088804
1-Apr-14,559.57,568.18,558.44,566.88,7932


Visit my NexusFi Trade Journal Started this thread Reply With Quote
Thanked by:
  #4 (permalink)
 
Nicolas11's Avatar
 Nicolas11 
near Paris, France
 
Experience: Beginner
Platform: -
Trading: -
Posts: 1,071 since Aug 2011
Thanks Given: 2,232
Thanks Received: 1,774

2) Web Query 2 (and related Java code)

It appears that the above URL does not allow generating a csv file for all types of securities.

In particular, it does not seem to work for S&P 500 (INDEXSP:.INX)

Fortunately, another URL is available:
https://www.google.com/finance/getprices?q=.INX&x=INDEXSP&i=86400&p=12d&f=d,c,v,k,o,h,l&df=cpct&auto=0&ei=Ef6XUYDfCqSTiAKEMg

References on this URL format:
Simple C# DLL to download data from Google Finance using an alternative, undocumented method - CodeProject
Google's Undocumented Finance API
Google Realtime Intraday Backfill Data
https://trading.cheno.net/downloading-google-intraday-historical-data-with-python/

It generates the following Web page:

Quoting 
EXCHANGE%3DINDEXSP
MARKET_OPEN_MINUTE=570
MARKET_CLOSE_MINUTE=960
INTERVAL=86400
COLUMNS=DATE,CLOSE,HIGH,LOW,OPEN,VOLUME,CDAYS
DATA=
TIMEZONE_OFFSET=-240
a1396382400,1885.52,1885.84,1873.96,1873.96,517337094,1
1,1890.9,1893.17,1883.79,1886.61,465008732,1
2,1888.77,1893.8,1882.65,1891.43,486951242,1
3,1865.09,1897.28,1863.26,1890.25,638969604,1
6,1845.04,1864.04,1841.48,1863.92,658210321,1
7,1851.96,1854.95,1837.49,1845.48,552300967,1
8,1872.18,1872.43,1852.38,1852.64,528286495,1
9,1833.08,1872.53,1830.87,1872.28,636878418,1
10,1815.69,1835.07,1814.36,1830.65,662182271,1
13,1830.61,1834.19,1815.8,1818.18,524945814,1
14,1842.98,1844.02,1816.29,1831.45,591857217,1

Example of Java code:
 
Code
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

// inspired by http://stackoverflow.com/questions/9093000/read-csv-file-from-internet
public class Google_ReadCSVFromWebQuery2 {

    public static void main(String[] args) {
        try {
            URL url = new URL("https://www.google.com/finance/getprices?q=.INX&x=INDEXSP&i=86400&p=12d&f=d,c,v,k,o,h,l&df=cpct&auto=0&ei=Ef6XUYDfCqSTiAKEMg");
            URLConnection urlConn = url.openConnection();
            InputStreamReader inputStreamReader = new InputStreamReader(urlConn.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
            bufferedReader.close();
            inputStreamReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
... and related output:

Quoting 
EXCHANGE%3DINDEXSP
MARKET_OPEN_MINUTE=570
MARKET_CLOSE_MINUTE=960
INTERVAL=86400
COLUMNS=DATE,CLOSE,HIGH,LOW,OPEN,VOLUME,CDAYS
DATA=
TIMEZONE_OFFSET=-240
a1396382400,1885.52,1885.84,1873.96,1873.96,517337094,1
1,1890.9,1893.17,1883.79,1886.61,465008732,1
2,1888.77,1893.8,1882.65,1891.43,486951242,1
3,1865.09,1897.28,1863.26,1890.25,638969604,1
6,1845.04,1864.04,1841.48,1863.92,658210321,1
7,1851.96,1854.95,1837.49,1845.48,552300967,1
8,1872.18,1872.43,1852.38,1852.64,528286495,1
9,1833.08,1872.53,1830.87,1872.28,636878418,1
10,1815.69,1835.07,1814.36,1830.65,662182271,1
13,1830.61,1834.19,1815.8,1818.18,524945814,1
14,1842.98,1844.02,1816.29,1831.45,591857217,1


Visit my NexusFi Trade Journal Started this thread Reply With Quote
Thanked by:
  #5 (permalink)
 
Nicolas11's Avatar
 Nicolas11 
near Paris, France
 
Experience: Beginner
Platform: -
Trading: -
Posts: 1,071 since Aug 2011
Thanks Given: 2,232
Thanks Received: 1,774

3) YQL

Google Finance is supposed or was supposed to work with YQL, similarly to Yahoo Finance ( )

References:
Get Stock Data from Google Finance and YQL | Jarloo

However, it does not seem to work anymore. Results within generated XML are void.

This problem is also evoked here: https://developer.yahoo.com/forum/YQL/Google-Blocking-YQL-requests-/1337701968896-b5344530-0a0f-4abe-aa56-9aa86b5c577f


Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #6 (permalink)
 
Nicolas11's Avatar
 Nicolas11 
near Paris, France
 
Experience: Beginner
Platform: -
Trading: -
Posts: 1,071 since Aug 2011
Thanks Given: 2,232
Thanks Received: 1,774

4) R's quantmod

The following R code
 
Code
library(quantmod)

goog <- getSymbols("GOOG", src="google", from=as.Date("2014-4-1") , to=as.Date("2014-4-15"), auto.assign=FALSE)
print(goog)
returns:

Quoting 
GOOG.Open GOOG.High GOOG.Low GOOG.Close GOOG.Volume
<NA> 559.57 568.18 558.44 566.88 7932
<NA> 570.38 571.83 561.44 566.98 2088804
<NA> 569.85 587.28 564.13 569.74 5087530
<NA> 574.65 577.77 543.00 543.14 6377658
<NA> 540.74 548.48 527.15 538.15 4389569
<NA> 542.60 555.00 541.61 554.90 3152406
<NA> 559.62 565.37 552.95 564.14 3324742
<NA> 565.00 565.00 539.90 540.95 4027743
<NA> 532.55 540.00 526.53 530.60 3916171
<NA> 538.25 544.10 529.56 532.52 2568020
<NA> 536.82 538.45 518.46 536.44 3847453

Unfortunately, it does not seem to work for S&P 500 (INDEXSP:.INX)

This is the end of this series.

Nicolas


Visit my NexusFi Trade Journal Started this thread Reply With Quote




Last Updated on April 16, 2014


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts