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

前提是你 提交于 2019-12-08 19:35:57

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/19336499/how-to-handle-hyphens-in-yahoo-finance-tickers-in-quantmod

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!