0

I'm a newbye in R and I've seen several posts about downloading more stocks, but for a reason or another they don't work as suggested. My purpose is to download a vector of stocks and create a whole xts-matrix containing only Close prices for every stock (so a nobservations x 3 columns). Anyway, I'd like to start from a basic script that doesn't work properly:

 library(quantmod)
ticker=c("KO","AAPL","^GSPC")
for (i in 1:length(ticker)) {
  simbol=as.xts(na.omit(getSymbols(ticker[i],from="2016-01-01",auto.assign=F)))
  new=Cl(simbol)
  merge(new[i])
} 

It would be even better to write a function(symbols) that allows me to call whenever I need to just change the name of the stocks to download. Thanks to everyone

FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67
Polar
  • 147
  • 1
  • 11
  • The last statement sounds like you want others to write functions for you. Stack Overflow is not a programming request service, rather it is a question and answer forum to help you write your own code. Please update your question with more information about why your code does not work. Include error detail and a specific question about the code. If previous attempts have not worked, explain more detail. – C Perkins Oct 25 '17 at 16:53
  • No I don't want someone to write a function on behalf of me but , as a newbie, I indeed need some help. Anyway, I solved with my script but I'm still finding troubles with "function" command. I come from Matlab where you can call a pre-written function (i.e for retrieving stocks prices) and use it in another script. In this script, I'm supposed to just change the name of stocks after the name of my function. I followed a script for R found here(https://stackoverflow.com/questions/28337205/total-least-squares-regression-without-intercept-with-r) but...I'm not good at making it work. – Polar Oct 26 '17 at 11:43

1 Answers1

0

This is how I would do what you want with a function wrapper (which is a pretty common kind of manipulation with xts):

ticker=c("KO","AAPL","^GSPC")

collect_close_series <- function(ticker) {
  # Preallocate a list to store the result from each loop iteration (Note: lapply is another alternative to a direct loop)
  lst <- vector("list", length(ticker))
  for (i in 1:length(ticker)) {
    symbol <- na.omit(getSymbols(ticker[i],from="2016-01-01",auto.assign = FALSE))
    lst[[i]] <- Cl(symbol)
  } 
  # You have a list of close prices.  You can combine the objects in the list compactly using do.call; this is a common "data manipulation pattern" with xts objects.
  rr <- do.call(what = merge, lst)
  rr
}

out <- collect_close_series(ticker)

More advanced (better code design): You could write cleaner code by writing a function that handles each symbol (rather than a function that wraps and passes in all the symbols together) and then run lapply on it:

per_sym_close <- function(tick) {
  symbol <- na.omit(getSymbols(tick,from="2016-01-01",auto.assign = FALSE))
  Cl(symbol)
}

out2 <- do.call(merge, lapply(X = ticker, FUN = per_sym_close))

This gives the same result.

Hope this helps getting you started toward writing good R code!

FXQuantTrader
  • 6,821
  • 3
  • 36
  • 67