forked from jtr13/EDAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidyquant.Rmd
128 lines (109 loc) · 4.64 KB
/
tidyquant.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
# Stock data with tidyquant {#tidyquant}
![](images/banners/banner_tidyquant.png)
*This chapter originated as a community contribution created by [ naotominakawa](https://github.com/naotominakawa){target="_blank"}*
*This page is a work in progress. We appreciate any input you may have. If you would like to help improve this page, consider [contributing to our repo](contribute.html).*
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Overview
This section covers how to use the `tidyquant` package to conduct timeseries analysis.
## What is tidyquant?
`tidyquant` is an one-stop shop for financial analysis. It is suitable for analyzing timeseries data, such as financial and economic data. `tidyquant` connects to various data sources such as Yahoo! Finance, Morning Star, Bloomberg market data, etc. It also behaves well with other `Tidyverse` packages.
## Installing tidyquant
You can install **tidyquant** from CRAN:
```
install.packages("tidyquant")
```
If you want to see which functions are available, you can run the following:
```{r, eval = FALSE}
# to see which functions are available (not run)
library(tidyquant)
tq_transmute_fun_options()
```
## Single timeseries
Obtain historical data for single stock (for example, Google):
```{r, message=FALSE}
# get historical data for single stock. e.g. google
library(tidyquant)
tq_get("GOOGL", get="stock.prices")
```
Calculate monthly return of single stock:
```{r, message = FALSE}
library(dplyr)
# calculate monthly return of single stock
tq_get(c("GOOGL"), get="stock.prices") %>%
tq_transmute(select=adjusted,
mutate_fun=periodReturn,
period="monthly",
col_rename = "monthly_return")
```
Create a line chart of the *closing price* for single stock:
```{r, message=FALSE}
# showing closing price for single stock
library(ggplot2)
tq_get(c("GOOGL"), get="stock.prices") %>%
ggplot(aes(date, close)) +
geom_line()
```
Create a line chart of the *monthly return* for single stock:
```{r}
# showing monthly return for single stock
tq_get(c("GOOGL"), get="stock.prices") %>%
tq_transmute(select=adjusted,
mutate_fun=periodReturn,
period="monthly",
col_rename = "monthly_return") %>%
ggplot(aes(date, monthly_return)) +
geom_line()
```
## Multiple timeseries
Obtain historical data for multiple stocks (for example, GAFA):
```{r}
# get historical data for multiple stocks. e.g. GAFA
tq_get(c("GOOGL","AMZN","FB","AAPL"), get="stock.prices")
```
Create a multiple line chart of the closing prices of multiple stocks (again, GAFA). We can show each stock in a different color on the same graph:
```{r}
# Create a multiple line chart of the closing prices of the four stocks,
# showing each stock in a different color on the same graph.
tq_get(c("GOOGL","AMZN","FB","AAPL"), get="stock.prices") %>%
ggplot(aes(date, close, color=symbol)) +
geom_line()
```
Transform the data so each stock begins at 100 and replot (Standardize the data so that we can compare timeseries):
```{r}
# Create a multiple line chart of the closing prices of the four stocks,
# showing each stock in a different color on the same graph.
# Transform the data so each stock begins at 100 and replot.
tq_get(c("GOOGL","AMZN","FB","AAPL"), get="stock.prices") %>%
group_by(symbol) %>%
mutate(close = 100*close/first(close)) %>%
ggplot(aes(date, close, color=symbol)) +
geom_line()
```
Calculate *monthly return* of multiple stocks (again, GAFA):
```{r}
# calculate monthly return of multiple stocks
tq_get(c("GOOGL","AMZN","FB","AAPL"), get="stock.prices") %>%
group_by(symbol) %>%
tq_transmute(select=adjusted,
mutate_fun=periodReturn,
period="monthly",
col_rename = "monthly_return")
```
Create a multiple line chart of monthly return of the four stocks. Again, we can show each stock in a different color on the same graph:
```{r}
# Create a multiple line chart of monthly return of the four stocks,
# showing each stock in a different color on the same graph
tq_get(c("GOOGL","AMZN","FB","AAPL"), get="stock.prices") %>%
group_by(symbol) %>%
tq_transmute(select=adjusted,
mutate_fun=periodReturn,
period="monthly",
col_rename = "monthly_return") %>%
ggplot(aes(date, monthly_return, color=symbol)) +
geom_line()
```
## External Resources
- [tidyquant CRAN doc](https://cran.r-project.org/web/packages/tidyquant/vignettes/TQ00-introduction-to-tidyquant.html){target="_blank"}: formal documentation on the package
- [tidyquant Github repo](https://github.com/business-science/tidyquant){target="_blank"}: Github repository for the `tidyquant` package with a great README