8

I want to access data from my google finance portfolio in R. I am trying to do this by reading the Google Finance API documentation and following the lead of RGoogleDocs. I've made some progress, but I'm having a lot of trouble parsing the XML version of a google finance portfolio.

require(RGoogleDocs)

#Autheticate using RGoogleDocs
auth = getGoogleAuth("me@gmail.com", "password",service="finance")
con = getGoogleDocsConnection(auth)

#Get positions
positions <- getURL("http://finance.google.com/finance/feeds/default/portfolios/6/positions",curl=con)
positions <- xmlInternalTreeParse(positions)
positions['entry'] #Returns nothing, should return a list of stocks
xpathApply(positions, "//a") #Also returns nothing

Here is an example. I am trying to parse all the 'entry' elements out of this document.

require(XML)
positions <-  "<?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:gf='http://schemas.google.com/finance/2007' xmlns:gd='http://schemas.google.com/g/2005'><id>http://finance.google.com/finance/feeds/me@gmail.com/portfolios/7/positions/NYSE:SPY</id><updated>2011-07-18T21:42:07.000Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/finance/2007#position'/><title type='text'>SPDR S&amp;P 500 ETF</title><link rel='self' type='application/atom+xml' href='http://finance.google.com/finance/feeds/default/portfolios/7/positions/NYSE%3ASPY'/><gd:feedLink href='http://finance.google.com/finance/feeds/me@gmail.com/portfolios/7/positions/NYSE:SPY/transactions'/><gf:positionData gainPercentage='0.0' return1w='0.0' return1y='0.0' return3m='0.0' return3y='0.0' return4w='0.0' return5y='0.0' returnOverall='0.0' returnYTD='0.0' shares='0.0'/><gf:symbol exchange='NYSE' fullName='SPDR S&amp;P 500 ETF' symbol='SPY'/></entry>"
positions <- xmlInternalTreeParse(positions)
positions['entry']

EDIT:

For some reason the example XML string I posted doesn't quite work the same as a live connection to google docs. The only way you'll be able to reproduce this code is by setting up a portfolio on google finance. Make sure to note the ID of your portfolio. In my example I use portfolio ID 6 (finance/feeds/default/portfolios/6/positions), but yours may be different.

#Autheticate using RGoogleDocs
require(RGoogleDocs)
auth = getGoogleAuth("me@gmail.com", "password",service="finance")
con = getGoogleDocsConnection(auth)

#Get positions
positions <- getURL("http://finance.google.com/finance/feeds/default/portfolios/6/positions",curl=con)
doc = xmlTreeParse(positions, useInternalNodes = TRUE)
kids = xmlChildren(doc, addFinalizer = NA)
entries <- sapply(kids, "[", "entry")
entries[1]
Zach
  • 29,791
  • 35
  • 142
  • 201
  • 1
    Please provide a reproducible example. Only you have access to your portfolio and most people probably don't have a Google Finance portfolio they can use to test your code. – Joshua Ulrich Jul 18 '11 at 21:18
  • @Joshua Ulrich: I updated the question, how does it look now? – Zach Jul 18 '11 at 21:45
  • Is this still workin? [here][1] I saw that Google stopped providing data. [1]: http://stackoverflow.com/questions/11544287/oauth-with-yahoo-finance – Suresh Gorakala Jun 26 '14 at 14:22

1 Answers1

5

I made a test portfolio. After mostly erroneous efforts and finally looking at the class of the doc object below and the methods available, I hit upon this strategy that extracts entries from a GoogleFinance portfolio:

auth = getGoogleAuth("your_goog_id@gmail.com", "yourpwd123",service="finance")
con = getGoogleDocsConnection(auth)

#Get positions
positions <- getURL("http://www.google.com/finance/portfolio?action=view&pid=1",curl=con)
   # Parse
doc = xmlTreeParse(positions, useInternalNodes = TRUE)
   # Convert to an R accessible form
kids = xmlChildren(doc, addFinalizer = NA)
   # access with `$` or "[" functions
entries <- sapply(kids, "[", "entry")
entries[1]   #  You may need to adjust this index if you get more than one 'entry'
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • if I start with the position string I posted above, `doc = xmlTreeParse(positions, useInternalNodes = TRUE)` works and `kids = xmlChildren(doc, addFinalizer = NA)` works, but entries is an empty list. – Zach Jul 19 '11 at 13:24
  • Do you get anything with `kids$feed` ? The entries are contained in the node – IRTFM Jul 19 '11 at 14:15
  • Hmmm, I must have messed up the example XML I posted. Your code works fine when I grab data straight from google finance – Zach Jul 19 '11 at 14:33
  • Yes. I did notice that there are pointers involved so I think the code needs conducted in a manner to passes them properly. I will add the getURL line (that I copied from you setup.) – IRTFM Jul 19 '11 at 16:14
  • Wonderful, it works great. Now I just need to figure out a good way to convert the entries feed to a data frame. – Zach Jul 19 '11 at 17:21