{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

{r cars} summary(cars)

Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}

install.packages(“dplyr”) install.packages(“rvest”) install.packages(“tidyverse”) install.packages(“tidyquant”) install.packages(“janitor”) install.packages(“quantmod”) install.packages(“xml2”) install.packages(“XML”) library(rvest) library(XML) library(xml2) library(tidyverse) library(tidyquant) library(janitor) library(dplyr) today <- Sys.Date() date = today %m+%months(-3) one_ticker = tq_get(“^GSPC”, from = date) one_ticker %>% head() url <- “https://en.wikipedia.org/wiki/List_of_S%26P_500_companies” tickers <- url %>%read_html() %>% html_nodes(xpath = ’//*[@id="constituents"]‘) %>% html_table() sp500tickers <- tickers[[1]] sp500tickers = sp500tickers %>% mutate(Symbol = case_when(Symbol == “BRK.B” ~ “BRK-B”,Symbol == “BF.B” ~ “BF-B”,TRUE ~ as.character(Symbol))) numbers = seq(1:5) print(’for loop’) for (i in numbers){

print(i) } print(‘purrr :)’) list = map(numbers, print) get_symbols =function(ticker=“AAPL”){df = tq_get(ticker, from = date) %>% mutate(symbol = rep(ticker, length(date)))} tickers_df = map(symbols, get_symbols) %>% bind_rows() tickers_df = tickers_df %>% left_join(sp500tickers, by = c(‘symbol’ = ‘Symbol’)) %>% clean_names() %>% select(date:security, gics_sector, gics_sub_industry) tickers_df %>% select(symbol)%>% distinct()%>% count() %>% select(“Total Number of Tickers” = n) tickers_df %>% head() ticker = “GOOGL” tickers_df %>% filter(symbol == !!ticker) %>%ggplot(aes(date, adjusted))+geom_line() daily_sector = tickers_df %>% group_by(security, gics_sector, symbol) %>% tq_transmute(select = adjusted, mutate_fun = periodReturn,period = “daily”) %>%ungroup() avg_return =daily_sector %>% group_by(security, gics_sector) %>% summarise(avg_return = round(mean(daily.returns), 4),Volatility = sd(daily.returns)) %>%
arrange(desc(avg_return), desc(Volatility)) avg_return %>% head() avg_return %>% head(20) %>% ggplot(aes(reorder(security, -avg_return), avg_return, fill = avg_return))+ geom_col()+ coord_flip()+ labs(title = “Securities With Highest Average Returns In SP500 Over Past 3 Month”, x = “Security”, y = “Average Return”)+ theme_classic()+ theme(legend.position=“none”) plot = avg_return %>% ggplot(aes(avg_return, Volatility))+ geom_text(aes(label = symbol), size = 3)+ labs(title = “Average Return vs Volatility Over Last 3 Months In SP500”, x = “Average Return”, subtitle = “Data Source: Yahoo Finance”)+ theme_minimal()

plot avg_return = avg_return %>% mutate(Indicator = case_when(symbol %in% c(‘FOX’, ‘FOXA’) ~ “Fox Corporation”, TRUE ~ “The Rest of the SP500”)) plot = avg_return %>% ggplot(aes(avg_return, Volatility, color = Indicator))+ geom_text(aes(label = symbol), size = 3)+ labs(title = “Average Return vs Volatility Over Last 3 Months In SP500”, x = “Average Return”, subtitle = “Data Source: Yahoo Finance”)+ theme_minimal()

plot plot(pressure) ```

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.