IB Webinar

First, install our packages:

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

devtools::install_github("jbkunst/highcharter")

Introducing R and RStudio

+ Statistical programming language -> by data scientists, for data scientists
+ Base R + 17,000 packages
+ RStudio
+ Shiny

Packages for today

library(tidyverse)
library(tidyquant)
library(timetk)
library(tibbletime)
library(scales)
library(highcharter)
library(broom)
library(PerformanceAnalytics)

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

Today’s project

+ Buy when the SP500 50-day SMA is above the 200-day SMA (a 'golden cross')

+ Sell when the 50-day SMA moves below the 200-day SMA (a 'death cross'). 

+ Code up that strategy

+ visualiize its results and descriptive statistics

+ Compare it to buy-and-hold, add secondary logic, visualize that strategy

+ Conclude by building a Shiny dashboard for further exploration

Import data

We will be working with SP500 and treasury bills data so that when we exit the SP500 we can invest in treasuries and get some return.

We can use the tidyquant package and it’s tq_get() function to grab the data from yahoo! finance.

symbols <- c("^GSPC", "^IRX")


prices <- 
  tq_get(symbols, 
         get = "stock.prices",
         from = "1990-01-01")

head(prices)
# A tibble: 6 x 8
  symbol date        open  high   low close    volume adjusted
  <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
1 ^GSPC  1990-01-02  353.  360.  352.  360. 162070000     360.
2 ^GSPC  1990-01-03  360.  361.  358.  359. 192330000     359.
3 ^GSPC  1990-01-04  359.  359.  353.  356. 177000000     356.
4 ^GSPC  1990-01-05  356.  356.  351.  352. 158530000     352.
5 ^GSPC  1990-01-08  352.  354.  351.  354. 140110000     354.
6 ^GSPC  1990-01-09  354.  354.  350.  350. 155210000     350.

Let’s see how to import this data from an Excel file or a csv file if that were desired.

prices_excel <-  
 read_excel("prices.xlsx") %>% 
  mutate(date = ymd(date))
prices_csv <- 
 read_csv("prices.csv")  %>% 
  mutate(date = ymd(date))

Explore the raw data

Start with the simple line chart.

prices %>% 
  filter(symbol == "^GSPC") %>% 
  hchart(., 
         hcaes(x = date, y = adjusted),
         type = "line") %>% 
  hc_title(text = "GSPC prices")