Created
February 16, 2018 03:03
-
-
Save wesslen/a51618d2c55a7af04d60b78df25aaa7d to your computer and use it in GitHub Desktop.
R tidyquant script for workshop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # install tidyverse if you don't have it | |
| # install.packages("tidyverse") | |
| library(tidyverse) | |
| ## Read the csv from a URL | |
| url <- "http://assets.datacamp.com/course/compfin/sbuxPrices.csv" | |
| df <- read_csv(url) | |
| ## lubridate package to format the date | |
| # if you get an error below, are you sure you have lubridate? | |
| # If not, remember how to install (hint: like line 2). | |
| library(lubridate) | |
| df$Date <- mdy(df$Date) | |
| ## ggplot2 | |
| ggplot(df, aes(x = Date, y = `Adj Close`)) + | |
| geom_line() | |
| ## ggplot is based on layers, for example, to add labels | |
| ggplot(df, aes(x = Date, y = `Adj Close`)) + | |
| geom_line() + | |
| labs(title = "Starbucks Stock Prices per Month", | |
| x = "Month", | |
| y = "ME Adjusted Stock Price") | |
| ## https://www.rstudio.com/wp-content/uploads/2016/11/ggplot2-cheatsheet-2.1.pdf | |
| ## tidyquant | |
| # see https://business-science.github.io/tidyquant/ | |
| library(tidyquant) | |
| ## getting data | |
| # see https://business-science.github.io/tidyquant/articles/TQ01-core-functions-in-tidyquant.html | |
| tq_index_options() | |
| # let's get the SP 500 | |
| tq_index("SP500") | |
| ## get quantitative data | |
| tq_get_options() | |
| # stocks | |
| aapl_prices <- tq_get("AAPL", get = "stock.prices", from = " 1990-01-01") | |
| ggplot(aapl_prices, aes(x = date, y = close)) + | |
| geom_line() + | |
| labs(title = "AAPL Stock Price", x = "Date", y = "Close Price") | |
| # dividends | |
| aapl_divs <- tq_get("AAPL", get = "dividends", from = "1990-01-01") | |
| aapl_divs | |
| # splits | |
| aapl_splits <- tq_get("AAPL", get = "splits", from = "1990-01-01") | |
| aapl_splits | |
| # financials | |
| aapl_financials <- tq_get("AAPL", get = "financials") | |
| aapl_financials | |
| # piping %>% combines functions | |
| aapl_financials %>% | |
| filter(type == "IS") %>% | |
| select(annual) %>% | |
| unnest() | |
| # economic data | |
| wti_price_usd <- tq_get("DCOILWTICO", get = "economic.data") | |
| wti_price_usd | |
| # fx | |
| eur_usd <- tq_get("EUR/USD", | |
| get = "exchange.rates", | |
| from = Sys.Date() - lubridate::days(10)) | |
| eur_usd | |
| ## manipulation | |
| # https://business-science.github.io/tidyquant/articles/TQ02-quant-integrations-in-tidyquant.html | |
| # tq_transmute: Returns a new tidy data frame typically in a different periodicity than the input. | |
| # FANG = FB, AMZN, NFLX, and GOOG from the beginning of 2013 to the end of 2016. | |
| FANG_annual_returns <- FANG %>% | |
| group_by(symbol) %>% | |
| tq_transmute(select = adjusted, | |
| mutate_fun = periodReturn, | |
| period = "yearly", | |
| type = "arithmetic") | |
| # with piping and ggplot, we can build out a very sophisticated plot | |
| FANG_annual_returns %>% | |
| ggplot(aes(x = date, y = yearly.returns, fill = symbol)) + | |
| geom_bar(stat = "identity") + | |
| facet_wrap(~ symbol, ncol = 2) | |
| # or we can be much more advanced | |
| FANG_annual_returns %>% | |
| ggplot(aes(x = date, y = yearly.returns, fill = symbol)) + | |
| geom_bar(stat = "identity") + | |
| geom_hline(yintercept = 0, color = palette_light()[[1]]) + | |
| scale_y_continuous(labels = scales::percent) + | |
| labs(title = "FANG: Annual Returns", | |
| subtitle = "Get annual returns quickly with tq_transmute!", | |
| y = "Annual Returns", x = "") + | |
| facet_wrap(~ symbol, ncol = 2) + | |
| theme_tq() + | |
| scale_fill_tq() | |
| # see https://business-science.github.io/tidyquant/articles/TQ02-quant-integrations-in-tidyquant.html | |
| # See the other parts of tidyquant for deeper analysis | |
| # https://business-science.github.io/tidyquant/articles/TQ05-performance-analysis-with-tidyquant.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment