library(tidyquant)
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Registered S3 method overwritten by 'xts':
##   method     from
##   as.zoo.xts zoo
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Version 0.4-0 included new data defaults. See ?getSymbols.
## Loading required package: tidyverse
## ── Attaching packages ───────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1     ✔ purrr   0.3.2
## ✔ tibble  2.1.3     ✔ dplyr   0.8.3
## ✔ tidyr   0.8.3     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ──────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ lubridate::as.difftime() masks base::as.difftime()
## ✖ lubridate::date()        masks base::date()
## ✖ dplyr::filter()          masks stats::filter()
## ✖ dplyr::first()           masks xts::first()
## ✖ lubridate::intersect()   masks base::intersect()
## ✖ dplyr::lag()             masks stats::lag()
## ✖ dplyr::last()            masks xts::last()
## ✖ lubridate::setdiff()     masks base::setdiff()
## ✖ lubridate::union()       masks base::union()
library(tidyverse)

Calculate monthly returns over the past 20 years for S&P 500, Apple, and GE.

from = today() - years(20)
## Warning in system("timedatectl", intern = TRUE): running command
## 'timedatectl' had status 1
Stocks <- 
  tq_get(c("^GSPC", "AAPL", "GE"), get = "stock.prices", from = from) %>%
  group_by(symbol)
Stocks
## # A tibble: 15,090 x 8
## # Groups:   symbol [3]
##    symbol date        open  high   low close     volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>
##  1 ^GSPC  1999-11-05 1363. 1387. 1363. 1370. 1007300000    1370.
##  2 ^GSPC  1999-11-08 1370. 1381. 1366. 1377.  806800000    1377.
##  3 ^GSPC  1999-11-09 1377. 1384. 1361. 1365.  854300000    1365.
##  4 ^GSPC  1999-11-10 1365. 1379. 1360. 1373.  984700000    1373.
##  5 ^GSPC  1999-11-11 1373. 1382. 1372. 1381.  891300000    1381.
##  6 ^GSPC  1999-11-12 1381. 1396. 1369. 1396.  900200000    1396.
##  7 ^GSPC  1999-11-15 1396. 1399. 1392. 1394.  795700000    1394.
##  8 ^GSPC  1999-11-16 1394. 1420. 1394. 1420.  942200000    1420.
##  9 ^GSPC  1999-11-17 1420. 1423. 1411. 1411.  960000000    1411.
## 10 ^GSPC  1999-11-18 1411. 1425. 1411. 1425. 1022800000    1425.
## # … with 15,080 more rows
returns_monthly <- 
  Stocks %>%
    tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "monthly")
returns_monthly
## # A tibble: 723 x 3
## # Groups:   symbol [3]
##    symbol date       monthly.returns
##    <chr>  <date>               <dbl>
##  1 ^GSPC  1999-11-30          0.0136
##  2 ^GSPC  1999-12-31          0.0578
##  3 ^GSPC  2000-01-31         -0.0509
##  4 ^GSPC  2000-02-29         -0.0201
##  5 ^GSPC  2000-03-31          0.0967
##  6 ^GSPC  2000-04-28         -0.0308
##  7 ^GSPC  2000-05-31         -0.0219
##  8 ^GSPC  2000-06-30          0.0239
##  9 ^GSPC  2000-07-31         -0.0163
## 10 ^GSPC  2000-08-31          0.0607
## # … with 713 more rows
returns_monthly %>% 
  summarise(returns_avg = mean(monthly.returns))
## # A tibble: 3 x 2
##   symbol returns_avg
##   <chr>        <dbl>
## 1 ^GSPC    0.00423  
## 2 AAPL     0.0262   
## 3 GE      -0.0000445

Compare Sharpe ratio with an annualized risk-free rate of 3% and a confidence interval of 0.99.

returns_monthly %>% 
    tq_performance(Ra              = monthly.returns, 
                   performance_fun = SharpeRatio,
                   Rf              = 0.03 / 12, 
                   p               = 0.99)
## # A tibble: 3 x 4
## # Groups:   symbol [3]
##   symbol `ESSharpe(Rf=0.2%,p=… `StdDevSharpe(Rf=0.2%… `VaRSharpe(Rf=0.2%,p…
##   <chr>                  <dbl>                  <dbl>                 <dbl>
## 1 ^GSPC                0.0117                  0.0414                0.0149
## 2 AAPL                 0.0632                  0.204                 0.0632
## 3 GE                  -0.00803                -0.0313               -0.0112

Apple by far has the best sharpe ratios because they are all higher than GE and S&P500. GE having negative ratios for each, is the worst of the 3 stocks. And S&P500 is in the middle with positive ratios but not as high as Apple.