Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle hyphens in yahoo finance tickers in Quantmod [duplicate]

Tags:

r

quantmod

When executing the following commands the hyphen in the ticker HM-B.ST is interpreted as a minus sign. I have tried to rename the xts object to something else but have not succeeded. Does anybody know a solution for this?

>library(quantmod)
>getSymbols("HM-B.ST")
>chartSeries(HM-B.ST)
Error in inherits(x, "xts") : object 'HM' not found
like image 881
johansson.lc Avatar asked Oct 12 '13 16:10

johansson.lc


1 Answers

The cleanest way to deal with this is to not rely on getSymbols()' default auto-assignment behavior, and instead assign the time series object to a more standard name of your own choosing. For example:

HM.B.ST <- getSymbols("HM-B.ST", auto.assign=FALSE) # h.t. Joshua Ulrich
chartSeries(HM.B.ST)

If for some reason you do want the time-series to retain its by-default hyphenated name, you can access it by doing:

chartSeries(`HM-B.ST`)

The reason it works is that the backticks signal to the R parser that the characters between them are to be parsed as a single name (aka symbol), not as two names separated by the subtraction operator.

To drive that point home once and for all, try something like the following:

assign("a really stupidly constructed name!*&^", 5)
`a really stupidly constructed name!*&^`
# [1] 5
like image 172
Josh O'Brien Avatar answered Oct 18 '22 04:10

Josh O'Brien