[R] How to use getSymbols() to get annual data
Jeff Newmiller
jdnewmil at dcn.davis.ca.us
Sat Sep 2 06:54:59 CEST 2017
I suppose that might depend what you mean by "annual data".
?yearlyReturn
or
###
library(quantmod)
#> Loading required package: xts
#> Loading required package: zoo
#>
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#>
#> as.Date, as.Date.numeric
#> Loading required package: TTR
#> Version 0.4-0 included new data defaults. See ?getSymbols.
getSymbols("AAPL",from="2000-01-01",to="2016-12-31")
#> 'getSymbols' currently uses auto.assign=TRUE by default, but will
#> use auto.assign=FALSE in 0.5-0. You will still be able to use
#> 'loadSymbols' to automatically load data.
getOption("getSymbols.env")
#> and getOption("getSymbols.auto.assign") will still be checked for
#> alternate defaults.
#>
#> This message is shown once per session and may be disabled by
setting
#> options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
#>
#> WARNING: There have been significant changes to Yahoo Finance data.
#> Please see the Warning section of '?getSymbols.yahoo' for details.
#>
#> This message is shown once per session and may be disabled by
setting
#> options("getSymbols.yahoo.warning"=FALSE).
#> [1] "AAPL"
AAPL[ aggregate( index( AAPL )
, list( Yr = as.POSIXlt( index( AAPL ) )$year + 1900 )
, FUN=function(d) head(d,1)
)$x
]
#> AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
#> 2000-01-03 4.163 4.466 4.037 3.997768 133949200
#> 2001-01-02 1.181 1.211 1.156 1.062500 113078000
#> 2002-01-02 1.751 1.850 1.744 1.664286 132374200
#> 2003-01-02 1.140 1.185 1.139 1.057143 45357200
#> 2004-01-02 1.711 1.727 1.682 1.520000 36160600
#> 2005-01-03 5.143 5.169 4.970 4.520714 172998000
#> 2006-01-03 11.493 11.870 11.473 10.678572 201808600
#> 2007-01-03 13.702 13.748 13.005 11.971429 309579900
#> 2008-01-02 31.642 31.799 30.575 27.834286 269794700
#> 2009-01-02 13.637 14.456 13.523 12.964286 186503800
#> 2010-01-04 33.891 34.061 33.724 30.572857 123432400
#> 2011-01-03 51.709 52.442 51.582 47.081429 111284600
#> 2012-01-03 65.009 65.501 64.945 58.747143 75555200
#> 2013-01-02 87.167 87.353 85.249 78.432854 140129500
#> 2014-01-02 85.317 85.524 84.755 79.018570 58671200
#> 2015-01-02 117.249 117.302 112.997 109.330002 53204600
#> 2016-01-04 106.198 109.055 105.567 105.349998 67649400
#> AAPL.Adjusted
#> 2000-01-03 3.596616
#> 2001-01-02 0.955884
#> 2002-01-02 1.497285
#> 2003-01-02 0.951065
#> 2004-01-02 1.367477
#> 2005-01-03 4.067089
#> 2006-01-03 9.607041
#> 2007-01-03 10.770167
#> 2008-01-02 25.041281
#> 2009-01-02 11.663397
#> 2010-01-04 27.505054
#> 2011-01-03 42.357094
#> 2012-01-03 52.852215
#> 2013-01-02 71.189217
#> 2014-01-02 73.522530
#> 2015-01-02 103.866470
#> 2016-01-04 101.790649
###
Please study the Posting Guide (plain text format, etc), and the vignette
for the reprex package which can help you confirm your example will run
for us. (quantmod is not a package we all use...)
On Fri, 1 Sep 2017, Yingrui Liu wrote:
> Dear Sir/Madam,
>
>
> How to use getSymbols() to get annual data? For example, I need the annual stock price of APPLE from the year 2000 to 2016. How to write the command? I only know how to get the daily data. It is:
>
>
> getSymbols("AAPL",from="2000-01-01",to="2016-12-31")
>
>
> Thank you very much.
>
>
> Have a good week!
>
>
> Best regards,
>
>
> Yingrui Liu
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
>
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
More information about the R-help
mailing list