-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMultiple Tickers.RMd
140 lines (115 loc) · 5.03 KB
/
Multiple Tickers.RMd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
---
title: "R Notebook"
output: html_notebook
---
Single Presentation with Slides on Multiple Tickers
```{r}
library(tidyverse)
library(officer)
library(quantmod)
library(yfinance)
library(ggdark)
library(rvg)
get_placeholder_dims <- function(presentation, label, dim = "width", unit = "in") {
# Gets the width and height of the powerpoint placeholders and coverts them to needed unit
layout <- officer::layout_properties(presentation)
ifelse(
dim=="width",
dimension <- layout$cx[match(label, layout$ph_label)],
dimension <- layout$cy[match(label, layout$ph_label)]
)
if (unit == "in") {
dimension
} else if (unit == "px") {
dimension*96
} else if (unit == "cm") {
dimension * 2.54
} else stop(glue::glue("Only 'in', 'px' and 'cm' are supported as units. You entered '{unit}'."))
}
company_slide <- function(presentation, ticker, chart_path) {
company_name <- yfinance::get_company_names(ticker)
summary <- yfinance::get_summaries(ticker)
ttm_performance_path <- glue::glue("{chart_path}/{ticker} TTM Chart.png")
prices <- getSymbols(ticker, from=Sys.Date()-365, to = Sys.Date(), auto.assign = FALSE)
png(filename = ttm_performance_path,
width = get_placeholder_dims(presentation, "TTM Perfromance", dim ="width", unit = "px"),
height = get_placeholder_dims(presentation, "TTM Perfromance", dim = "hight", unit = "px")
)
chartSeries(prices, name = glue::glue("{ticker} - 12 Months"), type="line")
dev.off()
income_stat <- get_income(ticker)
ebit_margin <-
income_stat %>%
mutate(
ebit_margin = ebit / totalRevenue
) %>%
ggplot()+
geom_bar(mapping = aes(x = date, y = ebit_margin), stat = "identity", fill = "#5efc82", width = 0.4)+
geom_text(
mapping = aes(x = date, y = ebit_margin, label = paste0(round(ebit_margin*100, 2), "%")),
vjust = -0.2
)+
scale_y_continuous(labels = scales::percent_format(accuracy = 1))+
labs(title = "EBIT Margin", x = "Date", y = "EBIT Margin")+
dark_theme_gray()+
theme(
text = element_text(size = 11),
plot.background =element_blank(),
panel.background = element_blank(),
panel.grid.major = element_line(color = "grey30", size = 0.2),
panel.grid.minor = element_blank()
)
sales_growth <-
income_stat %>%
arrange(date) %>%
mutate(
sales_growth = totalRevenue / lag(totalRevenue) - 1
) %>%
dplyr::filter(!is.na(sales_growth)) %>%
ggplot()+
geom_bar(mapping = aes(x = date, y = sales_growth), stat = "identity", fill = "#6200ea", width = 0.4)+
geom_text(
mapping = aes(x = date, y = sales_growth, label = paste0(round(sales_growth*100, 2), "%")),
vjust = -0.2
)+
scale_y_continuous(labels = scales::percent_format(accuracy = 1))+
labs(title = "Sales Growth", x = "Date", y = "Sales Growth")+
dark_theme_gray()+
theme(
text = element_text(size = 11),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_line(color = "grey30", size = 0.2),
panel.grid.minor = element_blank()
)
presentation %>%
add_slide(layout = "Company Summary", master = "Office Theme") %>%
ph_with(value = company_name, location = ph_location_label(ph_label = "Company Name") ) %>%
ph_with(value = ticker, location = ph_location_label(ph_label = "Ticker") ) %>%
ph_with(value = glue::glue("Sector: {summary$sector}") , location = ph_location_label(ph_label = "Sector") ) %>%
ph_with(value = glue::glue("Industry: {summary$industry}"), location = ph_location_label(ph_label = "Industry") ) %>%
ph_with(value = summary$longBusinessSummary, location = ph_location_label(ph_label = "Company Summary") ) %>%
ph_with(value = external_img(ttm_performance_path), location = ph_location_label(ph_label = "TTM Perfromance") ) %>%
ph_with(value = rvg::dml(ggobj = sales_growth, bg = "transparent"), location = ph_location_label(ph_label = "Sales Growth") ) %>%
ph_with(value = rvg::dml(ggobj = ebit_margin, bg = "transparent"), location = ph_location_label(ph_label = "EBIT Margin") )
}
```
```{r}
tickers <- c("FB", "AAPL", "AMZN", "NFLX", "GOOG")
pres <- read_pptx("stock_summary_template.pptx") %>%
remove_slide(index = 1) %>%
company_slide(ticker = "FB", chart_path = "presentations/images") %>%
company_slide(ticker = "AAPL", chart_path = "presentations/images") %>%
company_slide(ticker = "AMZN", chart_path = "presentations/images") %>%
company_slide(ticker = "NFLX", chart_path = "presentations/images") %>%
company_slide(ticker = "GOOG", chart_path = "presentations/images")
print(pres, glue::glue("presentations/FAANG Summaries {Sys.Date()}.pptx"))
```
```
initial_object <- read_pptx("stock_summary_template.pptx") %>%
remove_slide(index = 1)
pres_reduce <- reduce(tickers,
company_slide,
chart_path = "presentations/images",
.init = initial_object)
```