Duncan, Since you asked, here is an updated version of my function. # This method gets the Data. getReturns1 <- function(symbol, norm = FALSE) { library(quantmod) series = getSymbols(symbol, src = "yahoo", from = start, to = end, auto.assign = FALSE) length <- nrow( series ) close <- as.numeric(series[, paste0(symbol, ".Close")]) cat( "length = ", length(close ), "\n" ) diff = seq(1:(length-1)) for( i in 1:length-1 ) diff[i] = ((close[i+1] - close[i]) ) / close[i] u = mean(diff) stdDev = sd(diff) cat( "stdDev = ", stdDev, "\n" ) if ( norm == TRUE ) { diff = (diff - u) diff = diff / stdDev } cat( "length = ", length(diff ), "\n" ) return (diff) } I believe it is now working correctly. I did add the following statement: diff = seq(1:(length-1)) I thank you for your help. I also think the version of the function posted by Joshua Ulrich is better. I found his post to be very educational. Bob On 8/9/2018 6:48 PM, Duncan Murdoch wrote:> On 09/08/2018 6:21 PM, rsherry8 wrote: >> Duncan, >> >> You are right and when I run with auto.assign=FALSE it works. > > You should post your working version of the function to the mailing list. > > Duncan Murdoch > >> >> Thank you very much, >> Bob >> >> On 8/9/2018 6:11 PM, Duncan Murdoch wrote: >>> On 09/08/2018 4:56 PM, rsherry8 wrote: >>>> Duncan, >>>> >>>> Thanks for the response. I tired the following: >>>> > series <- getSymbols("AVB", src = "yahoo", from = start, to >>>> = end) >>> >>> You missed the auto.assign=FALSE argument. >>> >>>> > series[0] >>>> character(0) >>>> > nrow( series ) >>>> NULL >>>> nrow( series ) returned NULL. I do not understand why. I am thinking >>>> that there should be an R command to >>>> tell me about the structure of series. I tried: typeof( series ) and >>>> got: "character". Is there a better command for >>>> me to use other than typeof? >>> >>> Won't help here, but often str() is more informative than typeof(). >>> >>> Duncan Murdoch >>> >>>> >>>> I also tried this command: >>>> c1 <- as.numeric(series[, paste0(symbol, ".Close")]) >>>> where symbol held the value "AVB" and I got: >>>> Error in series[, paste0(symbol, ".Close")] : >>>> incorrect number of dimensions >>>> >>>> Please help. >>>> Thanks, >>>> Bob >>>> >>>> On 8/9/2018 3:51 PM, Duncan Murdoch wrote: >>>>> On 09/08/2018 1:12 PM, rsherry8 wrote: >>>>>> >>>>>> I wrote the following function: >>>>>> >>>>>> # This method gets historical stock data for the stock Avalon Bay >>>>>> whose >>>>>> symbol is AVB. >>>>>> getReturns <- function(norm = FALSE) >>>>>> { >>>>>> library(quantmod) >>>>>> >>>>>> getSymbols("AVB", src = "yahoo", from = start, to = end) >>>>>> length = length( AVB$AVB.Close ) >>>>>> close = as.numeric( AVB$AVB.Close ) >>>>>> cat( "length = ", length(close ), "\n" ) >>>>>> for( i in 1:length-1 ) >>>>>> diff[i] = ((close[i+1] - close[i]) ) / close[i] >>>>>> u = mean(diff) >>>>>> stdDev = sd(diff) >>>>>> cat( "stdDev = ", stdDev, "\n" ) >>>>>> >>>>>> if ( norm == TRUE ) { >>>>>> diff = (diff - u) >>>>>> diff = diff / stdDev >>>>>> } >>>>>> return (diff) >>>>>> } >>>>>> >>>>>> I would like to generalize it to work for any stock by passing in >>>>>> the >>>>>> stock symbol. So the header for the >>>>>> function would be: >>>>>> >>>>>> getReturns <- function(symbol, norm = FALSE) >>>>> >>>>> The quantmod function getSymbols has arguments auto.assign which >>>>> defaults to TRUE. Set it to FALSE, and then keep the result of the >>>>> call, instead of assuming that a variable named AVB has been created. >>>>> >>>>> That is, >>>>> >>>>> series <- getSymbols(symbol, src = "yahoo", from = start, >>>>> to >>>>> end, auto.assign = FALSE) >>>>> length <- nrow( series ) >>>>> >>>>> It will still name the columns according to the symbol, so you would >>>>> also need >>>>> >>>>> close <- as.numeric(series[, paste0(symbol, ".Close")]) >>>>> >>>>> Duncan Murdoch >>>>> >>>>>> >>>>>> Now how do I update this line: >>>>>> length = length( AVB$AVB.Close ) >>>>>> This statement will not work: >>>>>> length = length( symbol$AVB.Close ) >>>>>> because the name that holds the closing price is a function of the >>>>>> stock >>>>>> symbol. >>>>>> >>>>>> Thanks, >>>>>> Bob >>>>>> >>>>>> ______________________________________________ >>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>>>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>>>> PLEASE do read the posting guide >>>>>> http://www.R-project.org/posting-guide.html >>>>>> and provide commented, minimal, self-contained, reproducible code. >>>>>> >>>>> >>>>> >>>> >>> >>> >> > >