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("[email protected]", "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/[email protected]/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&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/[email protected]/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&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("[email protected]", "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]