-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro.Rmd
486 lines (285 loc) · 13.6 KB
/
macro.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
---
title: "macro"
output:
html_document: default
pdf_document: default
date: '2023-09-19'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(flipTime)
library(lubridate)
library(purrr)
library(sweep)
library(dplyr)
library(tidyr)
library(tibbletime)
library(timetk)
library(tidyverse)
library(tidyquant)
library(readxl)
library(xts)
library(PerformanceAnalytics)
library(ggplot2)
library(ggfortify)
library(ggforce)
library(rvest)
library(OECD)
library(Quandl)
Quandl.api_key("YOU_KEY")
library(eurostat)
```
```{r}
CLI_america <- c( "USALOLITOAASTSAM","USALORSGPNOSTSAM")%>% tq_get(get = "economic.data",
from = "1900-01-01",
to = "2023-12-31") %>% rename ( Date = date, Value = price)
CLI_america %>%
mutate(symbol = ifelse(symbol == "USALOLITOAASTSAM", "CLI_USA", "GDP_USA")) -> CLI_america
CLI_america %>% spread( symbol , Value ) %>% filter(Date >= "2015-01-01") %>% tail()
CLI_america %>% filter(Date >= "2015-01-01") %>% ggplot(aes(x=Date,y=Value,color=symbol))+
geom_line(size=1)+
geom_hline(yintercept = 100, color = palette_light()[[1]]) +
facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
ggtitle("USA OECD Economic Cycle Road Map")+
theme_tq() +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.title.x = element_blank())+
scale_color_tq()+
scale_x_date(date_breaks = "3 months",date_labels = "%d (%b %y)")
CLI_america %>% filter(Date >= "2000-01-01") %>% ggplot(aes(x=Date,y=Value, color = symbol))+
geom_line(size=1)+
geom_hline(yintercept = 100, color = palette_light()[[1]]) +
ggtitle("USA OECD Economic Cycle Road Map")+
theme_tq() +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.title.x = element_blank())+
scale_color_tq()+
scale_x_date(date_breaks = "6 months",date_labels = "%y (%b)")
```
```{r}
## Monthly Economic indicator for G7 Economies
dataset_list <- get_datasets()
search_dataset("MEI", data = dataset_list)
datasetG7 <- "MEI"
dstrucG7 <- get_data_structure(datasetG7)
G7_filter_list <- list("G-7", "LOLITOAA" )
G7_cli <- get_dataset(dataset = datasetG7 , filter = G7_filter_list )
G7_cli %>% tail(10)
G7_cli$obsTime %>% AsDate() %>% ceiling_date( "month") -1 -> G7_cli$obsTime
G7_cli %>% select(obsTime,obsValue) %>% rename(Date = obsTime, G7cli = obsValue) -> G7_cli
G7_cli %>% filter(Date >= "2012-01-01") %>% ggplot(aes(x=Date,y=G7cli))+
geom_line(size=1,color='red')+
geom_hline(yintercept = 100, color = palette_light()[[1]]) +
ggtitle("G7 OECD Economic Cycle Road Map")+
theme_tq() +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.title.x = element_blank())+
scale_color_tq()+
scale_x_date(date_breaks = "3 months",date_labels = "%d (%b %y)")
```
```{r}
## Inflation data
search_dataset("CPI", data = dataset_list)
dataset_cpi <- "PRICES_CPI"
filter_list <- list("G-7", "CPALTT01", "GY", "M" )
cpi_g7 <- get_dataset(dataset = dataset_cpi, filter = filter_list)
cpi_g7$obsTime %>% AsDate() %>% ceiling_date( "month") -1 -> cpi_g7$obsTime
cpi_g7 %>% select(obsTime,obsValue) %>% rename(Date = obsTime, G7cpi = obsValue) -> cpi_g7
cpi_g7 %>% tail(10)
cpi_g7%>% filter(Date >= "2000-01-01") %>% ggplot(aes(x=Date,y=G7cpi))+
geom_line(size=1,color='red')+
geom_hline(yintercept = 0, color = palette_light()[[1]]) +
ggtitle("G7 CPI YoY %")+
theme_tq() +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.title.x = element_blank())+
scale_color_tq()+
scale_x_date(date_breaks = "6 months",date_labels = "%d (%b %y)")
```
```{r}
## VIX
vix <- tq_get(c("^VIX"),
get = "stock.prices",
from = "1900-01-01",
to = Sys.Date()) %>%
mutate(symbol = "VIX")
vix %>%
#filter(date > "2019-12-31") %>%
ggplot(aes(x = date, y = adjusted, color = symbol)) +
geom_line(color = "#E69F00") +
geom_hline(yintercept = vix$adjusted %>% quantile(0.25,na.rm = T) , color = "blue" , linetype = "dotted") +
geom_hline(yintercept = vix$adjusted %>% quantile(0.50,na.rm = T), color = "blue" , linetype = "dotted") +
geom_hline(yintercept = vix$adjusted %>% mean() , color = "black" , linetype = "dashed") +
geom_hline(yintercept = vix$adjusted %>% quantile(0.75,na.rm = T), color = "blue" , linetype = "dotted") +
geom_hline(yintercept = vix$adjusted %>% quantile(1,na.rm = T), color = "blue" , linetype = "dotted")+
#geom_smooth(color = "black") +
scale_x_date(date_breaks = "6 months", date_labels = "%d %b %Y", expand = c(0, 0)) +
scale_y_continuous(breaks = seq(0,100, by = 5)) +
scale_fill_brewer(type = "qual", palette = "Set3", guide = F) +
labs(title = "CBOE Volatility Index (VIX)", subtitle = "",
x = "", y = "value", caption = "Data: Yahoo Finance") +
theme_tq() + theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.title.x = element_blank()) +
theme(legend.position = "none")+
geom_ma(ma_fun = SMA, n = 30, color="red") +
geom_ma(ma_fun = SMA, n = 90, color="black") +
coord_x_date(xlim = c("2015-07-01", "2023-12-31"))
vix %>% select(date,adjusted)%>%rename(Date = date, VIX = adjusted)-> VIX
VIX %>% tq_transmute(select = VIX , mutate_fun = to.monthly, indexAt = "lastof") -> VIX
```
```{r}
# Yields Spreads
TED_spread <- tq_get(c("TEDRATE"),
get = "economic.data",
from = "1900-01-01",
to = Sys.Date())
TED_spread %>% select(-symbol) %>% rename(Date = date, TED = price) -> TED_spread
TED_spread %>% tq_transmute(select = TED , mutate_fun = to.monthly, indexAt = "lastof") -> TED_spread
Tenyearminustwo_spread <- tq_get(c("T10Y2Y"),
get = "economic.data",
from = "1900-01-01",
to = Sys.Date())
Tenyearminustwo_spread %>% select(-symbol) %>% rename(Date = date, TENTWO = price) -> Tenyearminustwo_spread
Tenyearminustwo_spread %>% tq_transmute(select = TENTWO , mutate_fun = to.monthly, indexAt = "lastof") -> Tenyearminustwo_spread
Tenyear <- tq_get(c("DGS10"),
get = "economic.data",
from = "1900-01-01",
to = Sys.Date())
Tenyear %>% select(-symbol) %>% rename(Date = date, TEN = price) -> Tenyear
Tenyear %>% tq_transmute(select = TEN , mutate_fun = to.monthly, indexAt = "lastof") -> Tenyear
Twoyear <- tq_get(c("DGS2"),
get = "economic.data",
from = "1900-01-01",
to = Sys.Date())
Twoyear %>% select(-symbol) %>% rename(Date = date, TWO = price) -> Twoyear
Twoyear %>% tq_transmute(select = TWO , mutate_fun = to.monthly, indexAt = "lastof") -> Twoyear
```
```{r}
Tenyearminustwo_spread %>%
ggplot(aes(x = Date, y = TENTWO )) +
geom_line(color = "#E69F00") +
scale_x_date(date_breaks = "2 years", date_labels = "%d %b %Y", expand = c(0, 0)) +
scale_y_continuous(breaks = seq(0,100, by = 5)) +
scale_fill_brewer(type = "qual", palette = "Set3", guide = F) +
labs(title = "10 Years minus 2 Years Yield spread", subtitle = "",
x = "", y = "value", caption = "Data: Fred") +
theme_tq() + theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.title.x = element_blank()) +
theme(legend.position = "none")+
geom_ma(ma_fun = SMA, n = 30, color="red") +
geom_ma(ma_fun = SMA, n = 90, color="black") +
coord_x_date(xlim = c("1980-01-01", "2023-12-31"))
```
```{r}
## PMI data
Quandl("ISM/MAN_PMI") -> PMI
PMI %>% as.tibble() %>%
tq_transmute(select = PMI, mutate_fun = to.monthly, indexAt = "lastof") -> PMI
PMI %>%
ggplot(aes(x = Date, y = PMI)) +
geom_line(color = "#E69F00") +
geom_hline(yintercept = 50 , color = "black" , linetype = "dashed") +
scale_x_date(date_breaks = "6 months", date_labels = "%d %b %Y", expand = c(0, 0)) +
scale_y_continuous(breaks = seq( 30,70, by = 5)) +
scale_fill_brewer(type = "qual", palette = "Set3", guide = F) +
labs(title = "PMI", subtitle = "",
x = "", y = "value", caption = "") +
theme_tq() + theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.title.x = element_blank()) +
theme(legend.position = "none")+
geom_ma(ma_fun = SMA, n = 3, color="red") +
geom_ma(ma_fun = SMA, n = 12, color="black") +
coord_x_date(xlim = c("2007-01-01", "2023-12-31"))
```
```{r}
## FED Funds data
FEDFUNDS <- tq_get(c("FEDFUNDS"),
get = "economic.data",
from = "1900-01-01",
to = Sys.Date())
FEDFUNDS %>% select(-symbol) %>% rename(Date = date, FF = price) -> FEDFUNDS
FEDFUNDS %>% tq_transmute(select = FF , mutate_fun = to.monthly, indexAt = "lastof") -> FEDFUNDS
```
```{r}
## Join macro data
left_join( G7_cli, cpi_g7 , by = 'Date') %>% left_join( VIX , by = 'Date')%>%
left_join( Tenyearminustwo_spread, by = 'Date')%>%
left_join( Tenyear , by = 'Date')%>% left_join( Twoyear , by = 'Date') %>%
left_join( FEDFUNDS , by = 'Date') -> big_matrix
big_matrix %>% na.omit() %>% mutate( G7cpi_m3 = SMA(G7cpi,3) ) %>%
mutate( G7cpi_m36 = SMA(G7cpi,36) ) %>% mutate( G7cli_m3 = SMA(G7cli,3) ) -> big_matrix
big_matrix %>% tail(10)
```
```{r}
### Download etf prices and filter out etf quoted after 2015/04/30
etfs <- read_excel("C:/Users/Utente/Downloads/asd.xlsx")
prices_factors <- etfs %>%
tq_get(get = "stock.prices", from = "1960-01-01") %>%
group_by(Ticker, Name)
prices_factors %>% filter( min(date) <= "2015-04-30" ) -> prices_factors
prices_factors
prices_factors %>% tq_transmute(select = close , mutate_fun = periodReturn , type = 'arithmetic', period = 'monthly' ,
indexAt ="lastof", values_fill = 0,
col_rename = "Returns") -> Returns_factors_etfs
Returns_factors_etfs %>% rename(Date = date) -> Returns_factors_etfs
```
```{r}
## Join macro and etfs returns data
left_join( Returns_factors_etfs , big_matrix , by = 'Date' ) %>% group_by(Ticker, Name) -> Returns_MSCI_cli
Returns_MSCI_cli %>% tail(10)
```
```{r, fig.width = 30 ,fig.height = 30}
### performance when economy is in recovery after recession
Returns_MSCI_cli %>% filter ( G7cli < 100 & G7cli > lag(G7cli) ) %>% tq_performance( Ra = Returns , performance_fun = table.Stats , Rb = NULL ) %>% arrange(desc( GeometricMean))
```
```{r, fig.width = 30 ,fig.height = 30, dpi = 500, fig.retina = 2}
Returns_MSCI_cli %>% filter( Date >= "2020-01-01" ) %>%
filter( G7cli < 100 & G7cli > lag(G7cli) ) %>%
ggplot(aes(y = Returns , x = reorder(Name, desc(Name)), color = Name)) +
geom_hline(yintercept = 0, color = "red") +
geom_boxplot(color = "black", alpha = 0.5) +
geom_point() +
scale_y_continuous(labels = scales::percent, limits = c(-0.5, 0.5)) +
coord_flip() +
labs(title = "Price returns of selected stocks/ETFs", subtitle = "2020-2023, monthly returns, ( G7cli < 100 & G7cli > lag(G7cli) )",
x = "Stock", y = "return") +
theme_tq() +
theme(legend.position = "none")
```
```{r}
### performance when economy is in expansion and high inflation
Returns_MSCI_cli %>% filter ( G7cli > 100 & G7cli > lag(G7cli) & G7cpi > FF & G7cpi > G7cpi_m3 ) %>% tq_performance( Ra = Returns , performance_fun = table.Stats , Rb = NULL ) %>% arrange(desc( GeometricMean))
```
```{r, fig.width = 30 ,fig.height = 30, dpi = 500, fig.retina = 2}
Returns_MSCI_cli %>% filter( Date >= "2020-01-01" ) %>%
filter(G7cli > 100 & G7cli > lag(G7cli) & G7cpi > FF & G7cpi > G7cpi_m3 ) %>%
ggplot(aes(y = Returns , x = reorder(Name, desc(Name)), color = Name)) +
geom_hline(yintercept = 0, color = "red") +
geom_boxplot(color = "black", alpha = 0.5) +
geom_point() +
scale_y_continuous(labels = scales::percent, limits = c(-0.5, 0.5)) +
coord_flip() +
labs(title = "Price returns of selected stocks/ETFs", subtitle = "2020-2023, monthly returns, ( G7cli < 100 & G7cli > lag(G7cli) )",
x = "Stock", y = "return") +
theme_tq() +
theme(legend.position = "none")
```
```{r}
### performance when economy is in Slowdown ,inflation and rising interest rates
Returns_MSCI_cli %>% filter ( G7cli > 100 & G7cli < lag(G7cli) & FF > lag(FF) & G7cpi > G7cpi_m3 ) %>% tq_performance( Ra = Returns , performance_fun = table.Stats , Rb = NULL ) %>% arrange(desc( GeometricMean))
```
```{r, fig.width = 30 ,fig.height = 30, dpi = 500, fig.retina = 2}
Returns_MSCI_cli %>% filter( Date >= "2020-01-01" ) %>%
filter( G7cli > 100 & G7cli < lag(G7cli) & FF > lag(FF) & G7cpi > G7cpi_m3 ) %>%
ggplot(aes(y = Returns , x = reorder(Name, desc(Name)), color = Name)) +
geom_hline(yintercept = 0, color = "red") +
geom_boxplot(color = "black", alpha = 0.5) +
geom_point() +
scale_y_continuous(labels = scales::percent, limits = c(-0.5, 0.5)) +
coord_flip() +
labs(title = "Price returns of selected stocks/ETFs", subtitle = "2020-2023, monthly returns, ( G7cli < 100 & G7cli > lag(G7cli) )",
x = "Stock", y = "return") +
theme_tq() +
theme(legend.position = "none")
```