Skip to content

Instantly share code, notes, and snippets.

@chalg
Created July 12, 2023 08:42
Show Gist options
  • Save chalg/5031412284f311851a96abb5a742441a to your computer and use it in GitHub Desktop.
Save chalg/5031412284f311851a96abb5a742441a to your computer and use it in GitHub Desktop.
Gather a group of ticker codes, wrangle and visualise weekly, monthly and yearly returns.
# Load libraries ----
library(tidyverse)
library(tidyquant)
library(tidytext)
library(showtext)
showtext_opts(dpi = 300)
showtext_auto(enable = TRUE)
font_add_google("Fira Sans Condensed", "fira sans")
font_add_google("Copse", "copse")
font_add_google("Oswald", "oswald")
font_add_google("Acme", "acme")
theme_set(theme_light(base_size = 12, base_family = "fira sans"))
end <- Sys.Date()
# Setup ticker codes ----
ura_stocks <- c(
"DYL.AX", "BMN.AX", "BOE.AX", "LOT.AX", "PEN.AX",
"PDN.AX", "AGE.AX", "EL8.AX", "92E.AX", "AEE.AX",
"ACB.AX", "BKY.AX", "TOE.AX", "VAL.AX", "DEV.AX",
"SLX.AX", "URNM.AX", "ATOM.AX", "THR.AX", "T92.AX",
"OKR.AX", "EME.AX", "ERA.AX", "TOE.AX", "GTR.AX",
"CXU.AX", "GLA.AX", "OKR.AX", "BSN.AX", "1AE.AX",
"NXG.AX"
)
# Gather stock data ----
stock_prices <- tq_get(ura_stocks,
get = "stock.prices",
from = "2022-01-01",
to = end)
# Set correct time zone
stock_prices$date <- as_date(stock_prices$date, tz="Australia/Brisbane")
# Remove country code
stock_prices$symbol <- gsub("\\..*", "", stock_prices$symbol)
# Data Wrangling ----
stock_returns_weekly <- stock_prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "weekly",
col_rename = "weekly_return") %>%
ungroup()
stock_returns_monthly <- stock_prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "monthly_return") %>%
ungroup()
stock_returns_yearly <- stock_prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "yearly",
col_rename = "yearly_return") %>%
ungroup()
returns <- stock_returns_weekly %>%
filter(date == max(date)) %>%
left_join(stock_returns_monthly, by = c("symbol", "date")) %>%
left_join(stock_returns_yearly, by = c("symbol", "date")) %>%
pivot_longer(
cols = ends_with("_return"),
names_to = "return_period",
values_to = "returns"
)
# Recode
returns <- returns %>%
mutate(return_period = recode(return_period,
"weekly_return" = "Weekly",
"monthly_return" = "Monthly",
"yearly_return" = "Yearly"))
# Visualisation ----
returns %>%
mutate(
return_period = factor(return_period,
levels=c("Weekly", "Monthly", "Yearly")),
symbol = reorder_within(symbol, returns, return_period),
text_lbl = returns %>% scales::percent(accuracy = 0.1),
colourtext = case_when(
returns > 0 ~ "#008B00",
returns < 0 ~ "#8B3A3A",
.default = "grey45"
)) %>%
ggplot(aes(x = symbol, y = returns)) + #, fill = return_period
geom_col(alpha = 0.50, show.legend = FALSE, fill = "grey50") + #, position = "dodge",
geom_text(aes(symbol, returns, label = text_lbl, colour = colourtext),
position = position_dodge(0.9),
size = 2,
# fontface = "bold",
hjust="inward",
) +
scale_y_continuous(labels = scales::label_percent(), expand = c(0,0)) +
scale_colour_identity() +
facet_wrap(~ return_period, scales = "free") +
coord_flip() +
scale_x_reordered() +
theme_tq() +
labs(title = "Selected ASX Uranium Stock Returns",
y = "Return",
caption = "DataViz: @GrantChalmers | Source: Yahoo Finance") +
theme(legend.position = "none",
axis.title.y = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.y = element_text(size = 7.3, family = "copse"),
axis.text = element_text(size = 7.5, family = "copse"),
strip.text = element_text(size = 8.2, family = "acme"), #, colour = "orange"
plot.title = element_text(size = 10, face = "bold"),
plot.background = element_rect(fill = 'antiquewhite', colour = 'antiquewhite'),
panel.background = element_rect(fill = 'snow'),
panel.grid.major = element_line(linetype = "dashed"),
panel.grid.major.x = element_blank(),
panel.spacing = unit(0.75, "lines"),
plot.caption = element_text(size = 6, colour = "gray50", family = "oswald"))
# Save PNG ----
ggsave("asx_ura_wmy_returns.png", path = "images", width = 5, height = 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment