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) 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 ______________________________________________ [hidden email] 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. |
If I understand it correctly, the function getSymbols creates a
variable with the name being the stock symbol. Then use the function get(symbol) to retrieve the value of the variable whose name is contained in the character string `symbol'. Assign that to a variable (e.g. AVB). You may also have to modify the names of the components you retrieve from the list AVB. For that, you can use AVB[["AVB.Close"]] instead of AVB$AVB.Close. You can them use something like AVB[[paste0(symbol, ".Close"]] to generalize the retrieval of list components. HTH, Peter On Thu, Aug 9, 2018 at 12:40 PM rsherry8 <[hidden email]> 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) > > 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 > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
Peter,
Thanks for the response. I tired the following command: AVB[["AVB.Close"]] and I got: Error in AVB[["AVB.Close"]] : subscript out of bounds Are you assuming that AVB is a data frame? I do not think AVB is a data frame. Is there a way for me to check? Thanks, Bob On 8/9/2018 3:46 PM, Peter Langfelder wrote: > If I understand it correctly, the function getSymbols creates a > variable with the name being the stock symbol. Then use the function > get(symbol) to retrieve the value of the variable whose name is > contained in the character string `symbol'. Assign that to a variable > (e.g. AVB). You may also have to modify the names of the components > you retrieve from the list AVB. For that, you can use > AVB[["AVB.Close"]] instead of AVB$AVB.Close. You can them use > something like AVB[[paste0(symbol, ".Close"]] to generalize the > retrieval of list components. > > HTH, > > Peter > On Thu, Aug 9, 2018 at 12:40 PM rsherry8 <[hidden email]> 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) >> >> 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 >> >> ______________________________________________ >> [hidden email] 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. ______________________________________________ [hidden email] 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. |
In reply to this post by plangfelder
Duncan,
Thanks for the response. I tired the following: > series <- getSymbols("AVB", src = "yahoo", from = start, to = end) > 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? 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:46 PM, Peter Langfelder wrote: > If I understand it correctly, the function getSymbols creates a > variable with the name being the stock symbol. Then use the function > get(symbol) to retrieve the value of the variable whose name is > contained in the character string `symbol'. Assign that to a variable > (e.g. AVB). You may also have to modify the names of the components > you retrieve from the list AVB. For that, you can use > AVB[["AVB.Close"]] instead of AVB$AVB.Close. You can them use > something like AVB[[paste0(symbol, ".Close"]] to generalize the > retrieval of list components. > > HTH, > > Peter > On Thu, Aug 9, 2018 at 12:40 PM rsherry8 <[hidden email]> 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) >> >> 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 >> >> ______________________________________________ >> [hidden email] 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. ______________________________________________ [hidden email] 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. |
In reply to this post by rsherry8
Well, your function uses AVB$AVB.Close, so I assumed AVB is a list (a
data frame can be thought of as a special list). What do you get when you type class(AVB)? Peter On Thu, Aug 9, 2018 at 2:24 PM rsherry8 <[hidden email]> wrote: > > Peter, > > Thanks for the response. I tired the following command: > AVB[["AVB.Close"]] > and I got: > Error in AVB[["AVB.Close"]] : subscript out of bounds > Are you assuming that AVB is a data frame? I do not think AVB is a data > frame. Is there a way > for me to check? > Thanks, > Bob > > On 8/9/2018 3:46 PM, Peter Langfelder wrote: > > If I understand it correctly, the function getSymbols creates a > > variable with the name being the stock symbol. Then use the function > > get(symbol) to retrieve the value of the variable whose name is > > contained in the character string `symbol'. Assign that to a variable > > (e.g. AVB). You may also have to modify the names of the components > > you retrieve from the list AVB. For that, you can use > > AVB[["AVB.Close"]] instead of AVB$AVB.Close. You can them use > > something like AVB[[paste0(symbol, ".Close"]] to generalize the > > retrieval of list components. > > > > HTH, > > > > Peter > > On Thu, Aug 9, 2018 at 12:40 PM rsherry8 <[hidden email]> 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) > >> > >> 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 > >> > >> ______________________________________________ > >> [hidden email] 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. > ______________________________________________ [hidden email] 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. |
Peter,
Here is the R command and its output that you requested: > class(AVB) [1] "xts" "zoo" Bob On 8/9/2018 5:29 PM, Peter Langfelder wrote: > Well, your function uses AVB$AVB.Close, so I assumed AVB is a list (a > data frame can be thought of as a special list). What do you get when > you type class(AVB)? > > Peter > On Thu, Aug 9, 2018 at 2:24 PM rsherry8 <[hidden email]> wrote: >> Peter, >> >> Thanks for the response. I tired the following command: >> AVB[["AVB.Close"]] >> and I got: >> Error in AVB[["AVB.Close"]] : subscript out of bounds >> Are you assuming that AVB is a data frame? I do not think AVB is a data >> frame. Is there a way >> for me to check? >> Thanks, >> Bob >> >> On 8/9/2018 3:46 PM, Peter Langfelder wrote: >>> If I understand it correctly, the function getSymbols creates a >>> variable with the name being the stock symbol. Then use the function >>> get(symbol) to retrieve the value of the variable whose name is >>> contained in the character string `symbol'. Assign that to a variable >>> (e.g. AVB). You may also have to modify the names of the components >>> you retrieve from the list AVB. For that, you can use >>> AVB[["AVB.Close"]] instead of AVB$AVB.Close. You can them use >>> something like AVB[[paste0(symbol, ".Close"]] to generalize the >>> retrieval of list components. >>> >>> HTH, >>> >>> Peter >>> On Thu, Aug 9, 2018 at 12:40 PM rsherry8 <[hidden email]> 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) >>>> >>>> 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 >>>> >>>> ______________________________________________ >>>> [hidden email] 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. ______________________________________________ [hidden email] 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. |
In reply to this post by plangfelder
" I am thinking that there should be an R command to
tell me about the structure of series" ?str ## perhaps also/instead ?summary Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, Aug 9, 2018 at 2:29 PM, Peter Langfelder <[hidden email] > wrote: > Well, your function uses AVB$AVB.Close, so I assumed AVB is a list (a > data frame can be thought of as a special list). What do you get when > you type class(AVB)? > > Peter > On Thu, Aug 9, 2018 at 2:24 PM rsherry8 <[hidden email]> wrote: > > > > Peter, > > > > Thanks for the response. I tired the following command: > > AVB[["AVB.Close"]] > > and I got: > > Error in AVB[["AVB.Close"]] : subscript out of bounds > > Are you assuming that AVB is a data frame? I do not think AVB is a data > > frame. Is there a way > > for me to check? > > Thanks, > > Bob > > > > On 8/9/2018 3:46 PM, Peter Langfelder wrote: > > > If I understand it correctly, the function getSymbols creates a > > > variable with the name being the stock symbol. Then use the function > > > get(symbol) to retrieve the value of the variable whose name is > > > contained in the character string `symbol'. Assign that to a variable > > > (e.g. AVB). You may also have to modify the names of the components > > > you retrieve from the list AVB. For that, you can use > > > AVB[["AVB.Close"]] instead of AVB$AVB.Close. You can them use > > > something like AVB[[paste0(symbol, ".Close"]] to generalize the > > > retrieval of list components. > > > > > > HTH, > > > > > > Peter > > > On Thu, Aug 9, 2018 at 12:40 PM rsherry8 <[hidden email]> 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) > > >> > > >> 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 > > >> > > >> ______________________________________________ > > >> [hidden email] 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. > > > > ______________________________________________ > [hidden email] 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. > [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
In reply to this post by rsherry8
Peter was on the right track. getSymbols() allows you to specify that
you want the value returned as an object instead of load()ed by setting auto.assign = FALSE. I've also made other changes to your function: - Use requireNamespace() so you don't alter the search() path - Use TTR::ROC() to calculate returns, instead of a loop - Use more meaningful names for the mean and standard deviation objects - Use isTRUE() to ensure 'norm' is 'TRUE' and not '1' or '"true"' or anything else that could be coerced to TRUE getReturns <- function(symbol, start = "2015-01-01", end = Sys.Date(), norm = FALSE) { stopifnot(requireNamespace("quantmod")) Data <- quantmod::getSymbols(symbol, src = "yahoo", from = start, to = end, auto.assign = FALSE) cat("length = ", NROW(Data), "\n") ret <- TTR::ROC(quantmod::Cl(Data), type = "discrete") mu <- mean(ret, na.rm = TRUE) sigma <- sd(ret, na.rm = TRUE) cat("stdDev = ", sigma, "\n") if (isTRUE(norm)) { ret <- (ret - mu) ret <- ret / sigma } return(ret) } x <- getReturns("IBM") length = 907 stdDev = 0.01245428 head(x) IBM.Close 2015-01-02 NA 2015-01-05 -0.015734932 2015-01-06 -0.021565971 2015-01-07 -0.006535554 2015-01-08 0.021734892 2015-01-09 0.004355530 On Thu, Aug 9, 2018 at 4:24 PM, rsherry8 <[hidden email]> wrote: > Peter, > > Thanks for the response. I tired the following command: > AVB[["AVB.Close"]] > and I got: > Error in AVB[["AVB.Close"]] : subscript out of bounds > Are you assuming that AVB is a data frame? I do not think AVB is a data > frame. Is there a way > for me to check? > Thanks, > Bob > > > On 8/9/2018 3:46 PM, Peter Langfelder wrote: >> >> If I understand it correctly, the function getSymbols creates a >> variable with the name being the stock symbol. Then use the function >> get(symbol) to retrieve the value of the variable whose name is >> contained in the character string `symbol'. Assign that to a variable >> (e.g. AVB). You may also have to modify the names of the components >> you retrieve from the list AVB. For that, you can use >> AVB[["AVB.Close"]] instead of AVB$AVB.Close. You can them use >> something like AVB[[paste0(symbol, ".Close"]] to generalize the >> retrieval of list components. >> >> HTH, >> >> Peter >> On Thu, Aug 9, 2018 at 12:40 PM rsherry8 <[hidden email]> 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) >>> >>> 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 >>> >>> ______________________________________________ >>> [hidden email] 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. > > > ______________________________________________ > [hidden email] 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. -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com R/Finance 2018 | www.rinfinance.com ______________________________________________ [hidden email] 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. |
In reply to this post by rsherry8
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 >>>>>> >>>>>> ______________________________________________ >>>>>> [hidden email] 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. >>>>>> >>>>> >>>>> >>>> >>> >>> >> > > ______________________________________________ [hidden email] 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. |
Free forum by Nabble | Edit this page |