As if there hasn’t been enough going on this week, silver prices have fallen nearly $10 per ounce. That’s a reduction of over 20%. Jeffrey Ryan’s quantmod package makes it easy to download the latest prices from OANDA’s web site and plot the excitement.
getSymbols() function is at the heart of quantmod’s data retrieval prowess, currently handling Yahoo! Finance, Google Finance, the St. Louis Fed’s FRED, and OANDA sites, in addition to MySQL databases and RData and CSV files.
First a word of warning: if you have a computer science background, you may cringe at the way
getSymbols() returns data. Rather than returning the fetched data as the result of a function call, it populates your R session’s
.GlobalEnv environment (or another one of your choosing via the
env parameter) with
zoo objects containing your data. For example, if you ask for IBM’s stock prices via
getSymbols("IBM"), you will find the data in a new “IBM” object in your
.GlobalEnv. This behavior can be changed by setting
auto.assign=F, but then you can only request one symbol at a time. But this is a minor nit about an incredibly useful package.
There’s even a wrapper function to help retrieve precious metal prices, and we will use this
getMetals() function to retrieve the last year’s worth of prices for gold (XAU) and silver (XAG):
library(quantmod) getMetals(c('XAU', 'XAG'), from=Sys.Date()-365)
Yup — that’s it.
getMetals() lets us know it has created two new objects:
 "XAUUSD" "XAGUSD"
There were also few warning messages complaining about the last line in the downloaded file. I haven’t bothered to dig into it as the data seem fine, including today’s price:
> ls()  "XAGUSD" "XAUUSD" > head(XAGUSD) XAG.USD 2010-05-07 17.6600 2010-05-08 18.4600 2010-05-09 18.4320 2010-05-10 18.4336 2010-05-11 18.5400 2010-05-12 19.3300 > tail(XAGUSD) XAG.USD 2011-05-02 47.9850 2011-05-03 45.2373 2011-05-04 44.0238 2011-05-05 40.9171 2011-05-06 37.9939 2011-05-07 35.0598
And here’s how easy it is to use the package’s built-in graphing facilities:
Yup — that’s quite a shellacking for silver.
Now I tend to be a ggplot2 guy myself, and I have never actually worked with
zoo objects before, but it’s pretty easy to get them into a suitable
silver = data.frame(XAGUSD) silver$date = as.Date(rownames(silver)) colnames(silver) = 'price' library(ggplot2) ggplot(data=silver, aes(x=date, y=price)) + geom_line() + theme_bw()
May 8, 2011 at 1:31 AM
Thanks! The topper is that after your plot of silver crashing, there’s a Google ad that reads “Silver to Hit $250 in ’11. That, of course, would be 2111. 😉
May 9, 2011 at 12:41 PM
Hey, Ed — sorry for the delay. WordPress thought your comment was spam!
May 8, 2011 at 7:27 AM
Thanks for the example. I agree about the cringing – though I wrote the function for use within the buildModel paradigm of the package – where loading different data in one call was needed. A better approach which I use is to set envir= to an environment of your choosing.
This lets you lets you maintain on object that contains all of your data – without cluttering the .GlobalEnv, yet still retaining the benefits of the flexible and automatic load.
syms <- new.env(hash=TRUE)
getMetals(c('XAU', 'XAG'), from=Sys.Date()-365, env=syms)
 "XAGUSD" "XAUUSD"
As far as the whacking sliver took – Meb Faber had a funny slide or two regarding a silver bubble last weekend (pre-crash) at the R/Finance conference we host in Chicago. Your post makes an enjoyable and appropriate connection back to that talk (slides will be forthcoming on RinFinance.com)
May 8, 2011 at 10:34 AM
Thanks for the comment — and more importantly, thanks for quantmod! It’s one of those packages which can make you look like a subject matter expert in 5 minutes — my favorite kind!
As an old programmer, there are a lot of things I have struggled to get used to in R, but your example makes me feel better about Environments in general. 🙂
I’ll watch for those prophetic silver slides!
May 8, 2011 at 11:43 AM
Yes – R is certainly a bit tough to get your head around. The fact that it is both a programming language and (more importantly?) an interactive analytics language is probably the toughest thing to adjust to. Throw in a nod to functional languages – with the ability to abuse using assign, <<-, and environments for side-effect… and you've got the ultimate in power (and related danger!)
Thanks again for the blog – and the compliments on quantmod.
May 8, 2011 at 7:40 PM
The fact that you can assign into a specific environment doesn’t change the fact that the fundamental approach of getSymbols is horribly horribly wrong. Just return a list of symbols!
May 8, 2011 at 7:43 PM
How do you really feel?
May 8, 2011 at 10:34 PM
Funny stuff. I’m obviously begging for a lecture in ‘design’ – but what would make a list more ‘correct’ than an environment? (note that WordPress filters facetiousness)
You are aware that both are valid R objects, correct? In fact, only one would be useful in the context I described.
August 17, 2011 at 10:30 AM
i get these errors when i run getmetals. How do i fix them?
In readLines(tmp) :
incomplete final line found on ‘/tmp/RtmpdijSiW/file8edbdab’
2: In readLines(tmp) :
incomplete final line found on ‘/tmp/RtmpdijSiW/file79838cb2’
August 17, 2011 at 12:41 PM
“incomplete final line” sounds like whatever data file which was downloaded was malformed or empty.
What did you try to get prices for? and were you able to download OK from Oanda?
You can use the
verbose=Tflag to see exactly what it’s downloading, etc.
August 17, 2011 at 5:25 PM
These aren’t ‘errors’, they are warnings. Should be harmless, as it typically indicated there is no EOF. This can be for a variety of reasons, and has seemingly cropped up more in recent R versions. Unless the data is corrupted, you are fine.
August 17, 2011 at 5:31 PM
Thanks for stopping by and commenting — and, of course, for quantmod, xts, iBrokers, etc.!
August 17, 2011 at 8:56 PM
I run this for silver, when i run ls() i don’t get any result
August 17, 2011 at 8:59 PM
got it working …
August 17, 2011 at 9:10 PM
thanks a shipload
August 19, 2011 at 7:39 PM
Nice quickie. Thank you Jeff.
Side commentary – it somehow doesn’t work in my head that ggplot doesn’t handle xts, since both are awesome.
September 21, 2011 at 7:50 AM
[…] Powered by Yahoo! Answers Mouse here for Related Linksquantmod makes it easy to watch silver prices crash in R #rstats […]