2

I'm running a portfolio optimization and to do that I want to download historical stock data using the stockPortfolio package. I ran the below code, which should generate the pricing data from Yahoo Finance. Instead, I'm getting an error:

```{r}
stocks <- c("AAPL")
getReturns(ticker = stocks, freq = "year")
```

Here's the traceback:

Error in file(file, "rt") : cannot open the connection
4.file(file, "rt")
3.read.table(file = file, header = header, sep = sep, quote = quote, dec = dec, fill = fill, comment.char = comment.char, ...)
2.read.delim(URL, TRUE, sep = ",")
1.getReturns(ticker = stocks, freq = "year")

Looking around other threads, I can't find a great answer. I have the latest version of R and updating my proxy settings doesn't do anything.

1 Answers1

3

getReturns doesn't work anymore because Yahoo Finance has discontinued the service on which most functions fetching data from Yahoo rely (similar problems for get.hist.quote from tseries or getSymbols from quantmod). I guess this is the same issue.

Instead, you can just download the data from Yahoo manually and import it with read.csv. Alternatively, you could get the (daily) data from google with getSymbols:

    library(quantmod)
    S <- getSymbols("AAPL", src = 'google', auto.assign = FALSE, from = "2004-01-01", to = "2016-12-31")
apitsch
  • 1,532
  • 14
  • 31