简体   繁体   中英

How do you store the results of a quantmod loop if the output is a single number?

Please note: I'm an R novice(which is probably clear from the question).

I'm trying to turn some code which outputs the number of overnight returns above .05, into a loop which can take multiple tickers. Frustratingly, despite my best efforts, I'm apparently not having success binding the results together, perhaps because the rbind and cbind which I'm used to using don't work on single values, even if it's in a DF. What I want to end up with is a single number, (or a DF of values I can add up), which shows the number of overnight return values>.05 for all the input tickers(stored in column 1 of TEST.xlsx). Thank you so much for your help, I appreciate it!

Original(working) code:

library(quantmod)
data1 <- getSymbols(Symbols = "AAPL", src = "yahoo", from = Sys.Date() - 525, auto.assign = FALSE)
de = head(data1,365)
colnames(de) <- c("open","high","low","close","volume","adj.")
overnightRtn <- as.data.frame(as.numeric(de[2:nrow(de),"open"])/as.numeric(de[1:(nrow(de)-1),"close"])) - 1
r = overnightRtn[overnightRtn>.05, ]
results = NROW(r)
View(results)

Loop attempt:

library(gdata)
d = read.xls("~/Documents/TEST.xlsx", sheet = 1, stringsAsFactors=F)

library(quantmod)
sym <- as.character(d[,1])
results <- NULL

for (i in sym){
  data1 <- try(getSymbols(Symbols = i, src = "yahoo", from = Sys.Date() - 525, auto.assign = FALSE))
  if(inherits(data1, "try-error")) next
  de = head(data1,365)
  colnames(de) <- c("open","high","low","close","volume","adj.")
  overnightRtn <- as.data.frame(as.numeric(de[2:nrow(de),"open"])/as.numeric(de[1:(nrow(de)-1),"close"])) - 1
  r = as.data.frame(overnightRtn[overnightRtn>.05, ])
  x = NROW(r)
  results= rbind(x)
}

colnames(results) = c("overnightRtn")
rownames(results) = sym
View(results)

Error message:

Error in `rownames<-`(`*tmp*`, value = c("AAPL", "W")) : 
  length of 'dimnames' [1] not equal to array extent

DF of View(results) is 7, the (correct) value of the first ticker in my excel file of tickers - by which I mean the # of values >.05

You can use lag to select the previous value in a column, and sum(overnightRtn > 0.05) to check how many values are greater than 0.05. Here is the full code

library(quantmod)

sym <- c('AAPL', 'GOOG', 'MSFT')
results <- NULL

for (i in sym){
  data1 <- try(getSymbols(Symbols = i, src = 'yahoo', from = Sys.Date() - 525, auto.assign = FALSE))
  if(inherits(data1, 'try-error')) next
  de <- head(data1, 365)
  colnames(de) <- c('open', 'high', 'low', 'close', 'volume', 'adj.')
  overnightRtn <- as.vector(de$open/lag(de$close, default = 0) - 1)
  results <- rbind(results, c(i, sum(overnightRtn > 0.05)))
}

results
#     [,1]   [,2]
#[1,] "AAPL" "3" 
#[2,] "GOOG" "4" 
#[3,] "MSFT" "3" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM