forked from Brent-Morrison/Stock_master
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_functions.R
More file actions
267 lines (214 loc) · 8.12 KB
/
plot_functions.R
File metadata and controls
267 lines (214 loc) · 8.12 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
library(dplyr)
library(tidyr)
library(purrr)
library(forcats)
library(ggplot2)
library(ggridges)
library(lubridate)
library(DBI)
library(RPostgres)
# Parameters
end_date <- '2020-12-31'
# Start date as string for query
start_date <- paste(
year(as.Date(end_date) - years(2)),
sprintf('%02d', month(as.Date(end_date) %m-% months(3))),
'01',
sep = '-')
#==============================================================================
#
# Database connection and price data retrieval
#
#==============================================================================
# Connect to postgres database
con <- stock_master_connect()
# Read data
sql1 <- "select * from access_layer.return_attributes"
qry1 <- dbSendQuery(conn = con, statement = sql1)
df_atts <- dbFetch(qry1)
sql2 <- "select * from alpha_vantage.monthly_fwd_rtn where date_stamp >= ?start_date and date_stamp <= ?end_date"
sql2 <- sqlInterpolate(conn = con, sql = sql2, start_date = start_date, end_date = end_date)
qry2 <- dbSendQuery(conn = con, statement = sql2)
df2_raw <- dbFetch(qry2)
df_filtered <- df_atts %>%
filter(date_stamp > as.Date('2011-12-31'), date_stamp < as.Date('2020-12-31')) %>%
group_by(symbol) %>%
mutate(fwd_rtn_1m = lead((adjusted_close-lag(adjusted_close))/lag(adjusted_close)),1) %>%
ungroup()
#==============================================================================
#
# Bar chart function
#
#==============================================================================
#' @param df A data frame - must contain a column 'fwd_rtn_1m' and a date at monthly intervals labelled 'date_stamp'
#' @param attribute The column representing the bars of bar chart
#' @param bins The discretisation level, either decile or quintile
#' @param date_facet A logical specifying if faceting is to be performed. If a year_filter is not selected, faceting is by year, else month
#' @param year_filter Integer, the year to filter for. If populated and date_facet = TRUE, faceting is by month
quantile_bar <- function(df, attribute, bins, date_facet, year_filter = NULL) {
lookup <- setNames(
as.list(
c('12 month arithmetic return', '12 month arithmetic return by sector','3 month kurtosis of daily returns')),
c('rtn_ari_12m_dcl', 'rtn_ari_12m_sctr_dcl','kurt_ari_120d_dcl')
)
attribute_enquo <- enquo(attribute)
attribute_name <- quo_name(attribute_enquo)
bins <- enquo(bins)
bins_name <- quo_name(bins)
end_date <- max(df$date_stamp)
# Logic for facet
if (is.null(year_filter)){
facet_var <- quo(year_stamp)
} else {
facet_var <- quo(date_stamp)
}
# Convert deciles to quintiles
df <- df %>% mutate(
quintile = case_when(
!!attribute_enquo <= 2 ~ as.integer(1),
!!attribute_enquo <= 4 ~ as.integer(2),
!!attribute_enquo <= 6 ~ as.integer(3),
!!attribute_enquo <= 8 ~ as.integer(4),
TRUE ~ as.integer(5)
),
attr_group_var = case_when(bins_name == 'decile' ~ !!attribute_enquo, TRUE ~ quintile),
year_stamp = year(date_stamp)
)
# Plot if facet specified
if (date_facet) {
# if (!is.null(year_filter)) {
# df <-
# }
df <- df %>%
filter(
if (is.null(year_filter)) {
date_stamp < as.Date('9998-12-31')
} else {
year(date_stamp) == year_filter
}
)
g <- df %>%
group_by(attr_group_var, !!facet_var) %>%
summarise(fwd_rtn_1m = mean(fwd_rtn_1m, na.rm = TRUE)) %>%
group_by(!!facet_var) %>%
mutate(fwd_rtn_1m = scale(fwd_rtn_1m)) %>%
ggplot(aes(x = attr_group_var, y = fwd_rtn_1m)) +
geom_col() +
facet_wrap(vars(!!facet_var), ncol = 3) + # FACET
theme_grey() +
theme(
plot.title = element_text(face = "bold", size = 14),
plot.subtitle = element_text(face = "italic", size = 10),
plot.caption = element_text(face = "italic", size = 8),
axis.title.y = element_text(face = "italic", size = 9),
axis.title.x = element_text(face = "italic", size = 7),
legend.position = "none"
) +
labs(title = paste('Monthly forward return by ',lookup[attribute_name], bins_name),
subtitle = paste('Covering the period', min(df$date_stamp), 'to', max(df$date_stamp)),
x = '', y = '')
# Plot if facet NOT specified
} else {
df <- df %>%
filter(
if (is.null(year_filter)) {
date_stamp < as.Date('9998-12-31')
} else {
year(date_stamp) == year_filter
}
)
g <- df %>%
group_by(attr_group_var) %>%
summarise(fwd_rtn_1m = mean(fwd_rtn_1m, na.rm = TRUE)) %>%
mutate(fwd_rtn_1m = scale(fwd_rtn_1m)) %>%
ggplot(aes(x = attr_group_var, y = fwd_rtn_1m)) +
geom_col() +
theme_grey() +
theme(
plot.title = element_text(face = "bold", size = 12),
plot.subtitle = element_text(face = "italic", size = 10),
plot.caption = element_text(face = "italic", size = 8),
axis.title.y = element_text(face = "italic", size = 9),
axis.title.x = element_text(face = "italic", size = 7),
legend.position = "none"
) +
labs(title = paste('Monthly forward return by ', lookup[attribute_name], bins_name),
subtitle = paste('Covering the period', min(df$date_stamp), 'to', max(df$date_stamp)),
x = '', y = '')
}
return(g)
}
quantile_bar(bn_data1, attribute = rtn_ari_6m_sctr_dcl, bins = quintile, date_facet = FALSE) # OK
quantile_bar(bn_data1, attribute = rtn_ari_6m_sctr_dcl, bins = quintile, date_facet = TRUE) # OK
quantile_bar(bn_data1, attribute = rtn_ari_6m_sctr_dcl, bins = quintile, date_facet = TRUE, year_filter = 2013) # OK
quantile_bar(bn_data1, attribute = rtn_ari_6m_sctr_dcl, bins = quintile, date_facet = TRUE, year_filter = 2014) # OK
quantile_bar(df_filtered, kurt_ari_120d_dcl, decile, TRUE)
lookup <- setNames(
as.list(c('12 month arithmetic return', '12 month arithmetic return by sector')),
c('rtn_ari_12m_dcl', 'rtn_ari_12m_sctr_dcl')
)
lookup$rtn_ari_12m_dcl
lookup['rtn_ari_12m_dcl']
#==============================================================================
#
# ggridges function
# https://cmdlinetips.com/2018/03/how-to-plot-ridgeline-plots-in-r/
#
#==============================================================================
quantile_ridges <- function(df, qntle) {
qntle <- enquo(qntle)
qntle_name <- quo_name(qntle)
df <- df %>% drop_na() %>%
select(date_stamp, !!qntle, fwd_rtn_1m) %>%
filter(!!qntle %in% c(1, 10)) %>%
mutate(decile_x = as.factor(!!qntle),
date_stamp = fct_rev(as.factor(date_stamp)))
ggp <- ggplot(df, aes(
x = fwd_rtn_1m,
y = date_stamp,
fill = decile_x
)) +
geom_density_ridges(
alpha = .6,
color = 'white',
from = -.6,
to = .6,
panel_scaling = FALSE
) +
theme_grey() +
theme(
plot.title = element_text(face = "bold", size = 12),
plot.subtitle = element_text(face = "italic", size = 10),
plot.caption = element_text(face = "italic", size = 8),
axis.title.y = element_text(face = "italic", size = 9),
axis.title.x = element_text(face = "italic", size = 7),
legend.position = "none"
) +
labs(title = paste(qntle_name, ' top and bottom quantile'),
subtitle = 'this is the subtitle',
x = '', y = '')
return(ggp)
}
quantile_ridges(df_filtered, skew_ari_120d_sctr_dcl)
# ==================================================================================
ggplot(df_filtered, aes(x = as.factor(rtn_ari_12m_dcl), y = fwd_rtn_1m)) +
geom_boxplot() +
coord_flip()
# ==================================================================================
df_filtered %>% drop_na() %>%
select(date_stamp, rtn_ari_12m_dcl, fwd_rtn_1m) %>%
filter(rtn_ari_12m_dcl %in% c(1, 10)) %>%
mutate(rtn_ari_12m_dcl = as.factor(rtn_ari_12m_dcl),
date_stamp = fct_rev(as.factor(date_stamp))) %>%
ggplot(aes(
x = fwd_rtn_1m,
y = date_stamp,
fill = rtn_ari_12m_dcl
)) +
geom_density_ridges(
alpha = .6,
color = 'white',
from = -.6,
to = .6,
panel_scaling = FALSE
)