-
Notifications
You must be signed in to change notification settings - Fork 4
/
prep.R
56 lines (43 loc) · 1.71 KB
/
prep.R
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
# libraries
library(data.table)
library(plotly)
library(dplyr)
library(tidyr)
# read file
dt_prices <- fread("price_data.csv")
dt_prices
# create wide
print("---> csv data in wide form")
dt_prices_wide <- dcast(dt_prices, date ~ stock, value.var = "price")
dt_prices_wide
# plot
fig1 <- plot_ly(dt_prices_wide, type = "scatter", mode = "lines") %>%
add_trace(x = ~date, y = ~AAPL, name = "AAPL") %>%
add_trace(x = ~date, y = ~AMZN, name = "AMZN") %>%
add_trace(x = ~date, y = ~MSFT, name = "MSFT") %>%
layout(title = "Stock Price Series", yaxis=list(title="price"), width = 1000)
fig1
# check the html or Rmd version of this file for details on the total return series
# Compare the Total Return series for the 3 stocks
dt_returns <- dt_prices_wide %>%
mutate(across(!date, ~ .x/lag(.x) -1)) %>%
drop_na()
fig2 <- plot_ly(dt_returns, type = "scatter", mode = "lines") %>%
add_trace(x = ~date, y = ~AAPL, name = "AAPL") %>%
add_trace(x = ~date, y = ~AMZN, name = "AMZN") %>%
add_trace(x = ~date, y = ~MSFT, name = "MSFT") %>%
layout(title = "Stock Return Series", yaxis=list(title="returns"), width = 1000)
fig2
# calc total return
dt_cumulative_returns <- dt_returns %>%
mutate(across(!date, ~ 100*cumprod(1+.x)))
# plot
fig3 <- plot_ly(dt_cumulative_returns, type = "scatter", mode = "lines") %>%
add_trace(x = ~date, y = ~AAPL, name = "AAPL") %>%
add_trace(x = ~date, y = ~AMZN, name = "AMZN") %>%
add_trace(x = ~date, y = ~MSFT, name = "MSFT") %>%
layout(title = "Stock Total Return Seriess", yaxis=list(title="returns"), width = 1000)
fig3
# ### Questions
# 1. Notice the difference once adjusted to the same reference?
# 2. Which stock or combination would you have chosen?