Question

I'm trying to create function that provides historical volatility after getting symbol from Yahoo. However, when I pass output to volatility function it doesn't like it; The Get variable gets assigned a vector with quotes, e.g. "SPY", but the volatility function only takes without quotes (SPY no "SPY"). I try to take quotes off using noquote() and now get following error:

Error in log(x) : Non-numeric argument to mathematical function

My code

require(quantmod)

vClose = function(X){
Get <- getSymbols(X, from="2000-01-01", src="yahoo")
Set <- noquote(Get)
volatility(Set, calc="close")
}

Any help would be great.

Was it helpful?

Solution

Just set auto.assign=FALSE in your call to getSymbols:

require(quantmod)
Get <- getSymbols("SPY", from="2000-01-01", auto.assign=FALSE)
volatility(Get, calc="close")

OTHER TIPS

noquote()is not the answer. Instead you want get(). The following example works, though you might want to change the variable names as getand Getcan get confused.

require(quantmod)

vClose = function(X){
Get <- getSymbols(X, from="2000-01-01", src="yahoo")
volatility(get(Get), calc="close")
}

vClose("SPY")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top