5

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.

jd8585
  • 187
  • 1
  • 8

2 Answers2

2

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")
Henry
  • 6,704
  • 2
  • 23
  • 39
1

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")
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418