Pulling Google Finance portfolios into R
Asked Answered
H

1

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("[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&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/[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&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("[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]
Helm answered 18/7, 2011 at 20:54 Comment(3)
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.Stoffel
@Joshua Ulrich: I updated the question, how does it look now?Helm
Is this still workin? [here][1] I saw that Google stopped providing data. [1]: https://mcmap.net/q/1471394/-oauth-with-yahoo-financeLegge
D
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("[email protected]", "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'
Doublecheck answered 19/7, 2011 at 4:30 Comment(5)
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.Helm
Do you get anything with kids$feed ? The entries are contained in the <feed> nodeDoublecheck
Hmmm, I must have messed up the example XML I posted. Your code works fine when I grab data straight from google financeHelm
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.)Doublecheck
Wonderful, it works great. Now I just need to figure out a good way to convert the entries feed to a data frame.Helm

© 2022 - 2024 — McMap. All rights reserved.