quantmod makes it easy to watch silver prices crash in R #rstats

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.

The 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 xts and 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):

getMetals(c('XAU', 'XAG'), from=Sys.Date()-365)

Yup — that’s it. getMetals() lets us know it has created two new objects:


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()

> head(XAGUSD)
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)
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:

chartSeries(XAUUSD, theme="white")

chartSeries(XAGUSD, theme="white")

Yup — that’s quite a shellacking for silver.

Now I tend to be a ggplot2 guy myself, and I have never actually worked with xts or zoo objects before, but it’s pretty easy to get them into a suitable data.frame:

silver = data.frame(XAGUSD)
silver$date = as.Date(rownames(silver))
colnames(silver)[1] = 'price'

ggplot(data=silver, aes(x=date, y=price)) + geom_line() + theme_bw()

17 Responses to “quantmod makes it easy to watch silver prices crash in R #rstats”

  1. M. Edward (Ed) Borasky Says:

    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. 😉

  2. Jeff Says:

    Hi Jeffrey,

    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)
    [1] "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)

    Thanks again!

    • Jeffrey Breen Says:

      Hi Jeff:

      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!

      Thanks again,

      • Jeff Says:

        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.


    • Hadley Wickham Says:

      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!

      • Jeffrey Breen Says:

        Hi Hadley:

        How do you really feel?



      • Jeff Says:


        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.



  3. Aubrey Quarcoo Says:

    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’

    • Jeffrey Breen Says:

      “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=T flag to see exactly what it’s downloading, etc.


  4. Jeff Ryan Says:

    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.

  5. Aubrey Quarcoo Says:

    I run this for silver, when i run ls() i don’t get any result

  6. Aubrey Quarcoo Says:

    got it working …

  7. Aubrey Quarcoo Says:

    thanks a shipload

  8. Lao Tzu (@isomorphisms) Says:

    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.

  9. Your Questions About Silver Spot Price - Silver Spot Price · Silver Spot Price Says:

    […] Powered by Yahoo! Answers Mouse here for Related Linksquantmod makes it easy to watch silver prices crash in R #rstats […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: