ASFIP presentation

Load up our packages

install.packages("tidyverse")
install.packages("tidyquant")
install.packages("timetk")
install.packages("tibbletime")
install.packages("broom")
install.packages("dygraphs")

devtools::install_github("jbkunst/highcharter")
library(tidyverse)
library(tidyquant)
library(timetk)
library(tibbletime)
library(scales)
library(highcharter)
library(broom)
library(PerformanceAnalytics)
library(dygraphs)

Introducing R

+ Statistical programming language -> by data scientists, for data scientists
+ Base R + 17,000 packages
+ RStudio
+ Shiny
+ sparklyr
+ tensorflow
+ Rmarkdown
+ database connectors
+ htmlwidgets

Packages for finance

library(PerformanceAnalytics)
library(PortfolioAnalytics)
library(TTR)
library(tidyquant)
library(quantmod)
library(xts)

List of packages for finance here: https://cran.r-project.org/web/views/Finance.html

Packages for data visualization

library(ggplot2)
library(dygraphs)
library(highcharter)
library(shiny)

An example project

  • Import data for 5 ETFs
  • Visualize prices and returns
  • Calculate some stats of interest
  • Create an SMA 50 v. SMA 200
  • Run a rolling linear model, chart some results

    • SPY (S&P500 fund)
    • EFA (a non-US equities fund)
    • IJS (a small-cap value fund)
    • EEM (an emerging-mkts fund)
    • AGG (a bond fund)

Import the Data

+ from excel using `read_excel`

+ from csv using `read_csv`

+ from Yahoo! Finance using `getSymbols` or `tq_get`

+ from myssql, postgres etc using `dbConnect`

+ via API with `httr` and `jsonlite` (important for Alternative Data)

Get data

# The symbols vector holds our tickers. 
symbols <- c("SPY",
             "EFA", 
             "IJS", 
             "EEM",
             "AGG")

# data imported from Yahoo! Finance
etf_prices<-
  tq_get(symbols, from  = "2012-12-31") %>% 
  select(symbol, date, adjusted) %>% 
  spread(symbol, adjusted) %>% 
  tk_xts(date_var = date)

Inspect the data

head(etf_prices)
                AGG      EEM      EFA      IJS      SPY
2012-12-31 97.30676 39.63340 48.20629 74.55437 127.1599
2013-01-02 97.19288 40.41088 48.95237 76.60001 130.4191
2013-01-03 96.94758 40.12492 48.47759 76.49866 130.1244
2013-01-04 97.05273 40.20534 48.72346 77.09759 130.6959
2013-01-07 97.00017 39.90150 48.51151 76.64607 130.3387
2013-01-08 97.08776 39.54404 48.24020 76.35121 129.9637

Visualize

dygraph(etf_prices)

Another Visualization

highchart(type = "stock") %>% 
  hc_add_series(etf_prices[,1]) %>% 
  hc_add_series(etf_prices[,2]) %>% 
  hc_add_series(etf_prices[,3]) %>% 
  hc_add_series(etf_prices[,4]) %>% 
  hc_add_series(etf_prices[,5]) %>% 
  hc_title(text = "Highcharting 5 ETFS") %>%
  hc_yAxis(opposite = FALSE,
           labels = list(format = "${value}")) %>% 
  hc_legend(enabled = TRUE) %>% 
  hc_navigator(enabled = FALSE) %>% 
  hc_exporting(enabled = TRUE)

Performance Analytics: getting started

etf_returns <- 
  Return.calculate(etf_prices, method = "log")

head(etf_returns)
                     AGG          EEM          EFA          IJS
2012-12-31            NA           NA           NA           NA
2013-01-02 -0.0011710460  0.019426883  0.015358255  0.027068430
2013-01-03 -0.0025270480 -0.007101493 -0.009746094 -0.001323970
2013-01-04  0.0010840087  0.002002185  0.005058969  0.007798811
2013-01-07 -0.0005416977 -0.007585831 -0.004359509 -0.005873638
2013-01-08  0.0009026116 -0.008998854 -0.005608370 -0.003854452
                    SPY
2012-12-31           NA
2013-01-02  0.025307299
2013-01-03 -0.002261673
2013-01-04  0.004382005
2013-01-07 -0.002736513
2013-01-08 -0.002881389
table.Stats(etf_returns)
                      AGG       EEM       EFA       IJS       SPY
Observations    1448.0000 1448.0000 1448.0000 1448.0000 1448.0000
NAs                1.0000    1.0000    1.0000    1.0000    1.0000
Minimum           -0.0110   -0.0626   -0.0898   -0.0386   -0.0427
Quartile 1        -0.0011   -0.0069   -0.0040   -0.0048   -0.0028
Median             0.0001    0.0005    0.0005    0.0008    0.0007
Arithmetic Mean    0.0001    0.0001    0.0002    0.0005    0.0006
Geometric Mean     0.0001    0.0000    0.0002    0.0005    0.0005
Quartile 3         0.0013    0.0074    0.0053    0.0065    0.0048
Maximum            0.0084    0.0479    0.0327    0.0339    0.0390
SE Mean            0.0001    0.0003    0.0002    0.0002    0.0002
LCL Mean (0.95)    0.0000   -0.0005   -0.0002    0.0001    0.0002
UCL Mean (0.95)    0.0002    0.0007    0.0007    0.0010    0.0010
Variance           0.0000    0.0001    0.0001    0.0001    0.0001
Stdev              0.0020    0.0117    0.0089    0.0094    0.0077
Skewness          -0.3176   -0.2652   -1.0628   -0.2618   -0.5979
Kurtosis           1.6349    1.3469    8.4793    0.9114    3.5034
table.CAPM(etf_returns, etf_returns$SPY)
                    AGG to SPY EEM to SPY EFA to SPY IJS to SPY SPY to SPY
Alpha                   0.0001    -0.0006    -0.0003     0.0000     0.0000
Beta                   -0.0422     1.1464     0.9690     1.0166     1.0000
Beta+                  -0.0297     1.1341     0.8953     0.9174     1.0000
Beta-                  -0.0412     1.0600     0.9930     0.9401     1.0000
R-squared               0.0266     0.5649     0.6962     0.6872     1.0000
Annualized Alpha        0.0199    -0.1408    -0.0769    -0.0091     0.0000
Correlation            -0.1630     0.7516     0.8344     0.8290     1.0000
Correlation p-value     0.0000     0.0000     0.0000     0.0000     0.0000
Tracking Error          0.1307     0.1238     0.0781     0.0836     0.0000
Active Premium         -0.1336    -0.1503    -0.0956    -0.0119     0.0000
Information Ratio      -1.0226    -1.2134    -1.2251    -0.1422        NaN
Treynor Ratio          -0.3139    -0.0029     0.0529     0.1328     0.1469

Scatter returns

etf_returns %>% 
  tk_tbl(rename_index = "date") %>% 
  select(-date) %>% 
  gather(symbol, return, -SPY) %>% 
  ggplot(aes(x = SPY, y = return, color = symbol)) +
  geom_point(alpha = .5) +
  facet_wrap(~symbol)

Add regression line

etf_returns %>% 
  tk_tbl(rename_index = "date") %>% 
  dplyr::select(-date) %>% 
  gather(symbol, return, -SPY) %>% 
  ggplot(aes(x = SPY, y = return, color = symbol)) +
  geom_point(alpha = .5) + 
  geom_smooth(formula = y ~ x, se = TRUE) +
  facet_wrap(~symbol)

Interactive Scatter

etf_returns %>% 
  tk_tbl(rename_index = "date") %>% 
  hchart(., type = "scatter", hcaes(x = SPY, y = EEM, date = date)) %>% 
  hc_xAxis(title = list(text = "Market Returns"),
           labels = list(format = "{value}%")) %>% 
  hc_yAxis(title = list(text = "EEM Returns"),
           labels = list(format = "{value}%")) %>% 
  hc_title(text = "Emerging Market v. SPY") %>% 
  hc_tooltip(pointFormat = "date: {point.date} <br> 
             EEM return: {point.y:.4f}  <br> 
             mkt return: {point.x:.4f}")

Grab beta or slope of regression line.

slope <- table.CAPM(etf_returns, etf_returns$SPY)[2, 2]

Add the regression line to the original scatter

etf_returns_tibble <- 
  etf_returns %>% 
  tk_tbl(rename_index = "date")

  hchart(etf_returns_tibble, type = "scatter", 
         hcaes(x = SPY, y = EEM, date = date)) %>%
  hc_add_series(etf_returns_tibble, "line", 
                hcaes(x = SPY, 
                      y = SPY * slope)) %>% 
  hc_xAxis(title = list(text = "Market Returns"),
           labels = list(format = "{value}%")) %>% 
  hc_yAxis(title = list(text = "EEM Returns"),
           labels = list(format = "{value}%")) %>% 
  hc_title(text = "Scatter with Beta Line")