assets_tbl <- tribble(~ticker, ~asset_type,
"ESGU", "ESG Fund",
"SUSA", "ESG Fund - Long History",
"IVV", "Benchmark",
"SHY", "Risk Free")
assets_tbl
asset_prices_tbl <- assets_tbl %>%
tq_get()
asset_prices_tbl
compare_ESG_regular <- asset_prices_tbl %>%
filter(date >= ymd("2017-12-31")) %>%
group_by(asset_type) %>%
mutate(index_100 = adjusted/first(adjusted) * 100) %>% #relative return
ggplot(aes(x = date, y = index_100, color = fct_reorder2(.f = asset_type,
.x = date, .y = index_100))) +
geom_line() +
labs(color = "")
compare_ESG_regular

compare_ESG_long_regular <- asset_prices_tbl %>%
filter(ticker != "ESGU") %>%
group_by(asset_type) %>%
mutate(index_100 = adjusted/first(adjusted) * 100) %>%
ggplot(aes(x = date, y = index_100, color = fct_reorder2(.f = asset_type, .x = date, .y = index_100))) +
geom_line() +
labs(color = "")
compare_ESG_long_regular

ggplotly(compare_ESG_long_regular)
asset_returns <- asset_prices_tbl %>%
group_by(ticker, asset_type) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "weekly") %>%
ungroup()
asset_returns
calculate_weekly_returns <- function(data, col_rename = NULL) {
data %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "weekly",
col_rename = col_rename)
}
benchmark_returns <- asset_returns %>%
filter(ticker == "IVV") %>%
select(date, benchmark_returns = weekly.returns)
benchmark_returns
esg_asset_returns <- asset_returns %>%
filter(str_detect(asset_type, "ESG")) %>%
rename(asset_returns = weekly.returns) %>%
left_join(benchmark_returns, by = "date")
esg_asset_returns
esg_asset_returns %>%
filter(date >= ymd("2018-01-01")) %>%
group_by(ticker, asset_type) %>%
tq_performance(Ra = asset_returns,
Rb = benchmark_returns,
performance_fun = InformationRatio,
scale = 52)
esg_asset_returns %>%
filter(date >= ymd("2018-01-01")) %>%
group_by(ticker, asset_type) %>%
tq_performance(Ra = asset_returns,
Rb = benchmark_returns,
performance_fun = table.CAPM,
scale = 52)
esg_asset_returns_capm_by_year <- esg_asset_returns %>%
filter(date >= ymd("2018-01-01")) %>%
mutate(year = year(date)) %>%
group_by(ticker, asset_type, year) %>%
tq_performance(Ra = asset_returns,
Rb = benchmark_returns,
performance_fun = table.CAPM,
scale = 52)
esg_asset_returns_capm_by_year
esg_asset_returns_capm_by_year %>%
ggplot(aes(x = year, y = AnnualizedAlpha, color = fct_reorder2(.f = ticker, .x = year, .y = AnnualizedAlpha))) +
geom_line() +
labs(color = "") +
geom_hline(yintercept = 0, lty = "dashed")

returns_reg <- function(data) {
lm(benchmark_returns ~ asset_returns, data = data)
}
esg_asset_returns_nested <- esg_asset_returns %>%
filter(date >= ymd("2018-01-01")) %>%
group_by(ticker, asset_type) %>%
nest() %>%
ungroup() %>%
mutate(regression_model = map(.x = data, .f = returns_reg))
esg_asset_returns_nested
esg_asset_returns_nested %>%
mutate(tidy = map(.x = regression_model, .f = broom::tidy)) %>%
unnest(tidy)
esg_asset_returns_nested %>%
mutate(glance = map(.x = regression_model, .f = broom::glance)) %>%
unnest(glance)
equities_filepath <- partial(here, "01_data")
equities_filepath() %>% list.files()
## [1] "equities_data_since_2020-2022-10-03.csv"
## [2] "equity_tickers-2022_10-03.csv"
## [3] "etf_comparison_tbl-2022-10-03.csv"
## [4] "etf_screener_processed-2022-10-03.csv"
etf_comparison <- equities_filepath("etf_comparison_tbl-2022-10-03.csv") %>% read_csv()
## Rows: 537 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): ticker, company_name, sector, esg_uw_ow
## dbl (7): esg_etf, standard_etf, esg_tilt, esg_tilt_z_score, esg_tilt_rank, e...
## lgl (3): in_esg_only, in_standard_only, in_on_index_only
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
etf_comparison
equities_tickers <- equities_filepath("equity_tickers-2022_10-03.csv") %>% read_csv()
## Rows: 537 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): ticker, company_name
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
equities_tickers
start_time <- Sys.time()
equities_data <- equities_tickers %>%
tq_get()
end_time <- Sys.time()
equities_data
end_time - start_time
## Time difference of 3.602071 mins
MMM <- tq_get("MMM", get = "stock.prices", from = "2021-10-13", to = "2022-10-13")
BLK <- tq_get("BLK", get = "stock.prices", from = "2021-10-13", to = "2022-10-13")
MMM %>%
ggplot(aes(x = date, y = close)) +
geom_line() +
labs( title = "3M Stock Price Line Chart", y = "Closing Price", x = "") +
theme_tq()

BLK %>%
ggplot(aes(x = date, y = close)) +
geom_candlestick(aes(open = open, high = high, low = low, close = close),
colour_up = "darkgreen", color_down = "red",
fill_up = "darkgreen", fill_down = "red") +
labs(title = "BlackRock Candlestick Stock Price Chart", y = "Closing Price", x = "") +
theme_tq()
## Warning: Duplicated aesthetics after name standardisation: colour_down
## Duplicated aesthetics after name standardisation: colour_down
