Problem downloading data from getSymbols in a given period

Viewed 39

I want to download the prices of Apple starting from 2000.01.01 but it always downloads the data from 1980

library(quantmod)
apple<-getSymbols("AAPL", src="yahoo", from=2000/01/01, auto.assign = F)
Rets<-dailyReturn(apple,subset=NULL,"continuous")

This is the error in the console:

Error in try.xts(x) :
Error in UseMethod("as.xts") : no applicable method for 'as.xts' applied to an object of class "character"

Then I want to calculate the daily returns but it doesn't work. Can someone help me?
Thanks!

1 Answers

Not too familiar with the quantmod package, but here is a method with tidyquant. It might be the format of the date that causes the problem, try "2000-01-01" instead of 2000/01/01

library(tidyverse)
library(tidyquant)

apple <- tq_get("AAPL",
                from = "2000-01-01") %>%
  tq_mutate(select = adjusted,
            mutate_fun = periodReturn,
            period = "daily")

# A tibble: 5,666 × 9
   symbol date        open  high   low close     volume adjusted daily.returns
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>         <dbl>
 1 AAPL   2000-01-03 0.936 1.00  0.908 0.999  535796800    0.855        0     
 2 AAPL   2000-01-04 0.967 0.988 0.903 0.915  512377600    0.782       -0.0843
 3 AAPL   2000-01-05 0.926 0.987 0.920 0.929  778321600    0.794        0.0146
 4 AAPL   2000-01-06 0.948 0.955 0.848 0.848  767972800    0.725       -0.0865
 5 AAPL   2000-01-07 0.862 0.902 0.853 0.888  460734400    0.760        0.0474
 6 AAPL   2000-01-10 0.911 0.913 0.846 0.873  505064000    0.746       -0.0176
 7 AAPL   2000-01-11 0.857 0.887 0.808 0.828  441548800    0.708       -0.0512
 8 AAPL   2000-01-12 0.848 0.853 0.772 0.778  976068800    0.666       -0.0600
 9 AAPL   2000-01-13 0.844 0.882 0.826 0.864 1032684800    0.739        0.110 
10 AAPL   2000-01-14 0.893 0.913 0.887 0.897  390376000    0.767        0.0381
# … with 5,656 more rows
Related