Emanuele Citera
Emanuele Citera

Reputation: 23

Computing multiperiod stock returns on a daily basis

I would like to compute multiperiod (daily, monthly, weekly, quarterly, etc.) stock returns on a daily basis. Basically, I have the following dataset and I would like to add other columns in which, for every single day, I have weekly, monthly, quarterly, etc., returns.

library(tidyquant)
library(dplyr)
MSFT <- na.omit(SP500) %>% select(c('symbol', 'date', 'adjusted')) %>%
  filter(symbol=='MSFT') %>% 
  tq_mutate(select = adjusted, mutate_fun = dailyReturn, col_rename = 'simple_daily')

With the tidyquant package, if I use, for instance, the monthlyReturn function, I only get the return at the end of each month. Instead, what I would like to have is a monthly return for every single day of my dataset.

Is there any way to apply this function to every observation (day) of my dataset? Any suggestion on alternatives to carry out this process?

Thanks a lot,

Emanuele

Upvotes: 0

Views: 77

Answers (1)

Pierre Lapointe
Pierre Lapointe

Reputation: 16277

You need to calculate the returns using bylagging the data 20 business days like this:

library(dplyr
df <- data.frame(symbol="MSFT",Price=runif(40))
df%>%
  mutate(monthly_return=Price/lag(Price,20)-1)
   symbol      Price monthly_return
1    MSFT 0.06606346             NA
2    MSFT 0.93950484             NA
3    MSFT 0.35694822             NA
4    MSFT 0.50215478             NA
5    MSFT 0.55052927             NA
6    MSFT 0.10507392             NA
7    MSFT 0.70965491             NA
8    MSFT 0.73443583             NA
9    MSFT 0.18867477             NA
10   MSFT 0.47592981             NA
11   MSFT 0.47760796             NA
12   MSFT 0.10975491             NA
13   MSFT 0.67172197             NA
14   MSFT 0.65957624             NA
15   MSFT 0.53295690             NA
16   MSFT 0.60927845             NA
17   MSFT 0.34113058             NA
18   MSFT 0.55938078             NA
19   MSFT 0.19547903             NA
20   MSFT 0.25671083             NA
21   MSFT 0.05525222    -0.16364940
22   MSFT 0.85370547    -0.09132403
23   MSFT 0.14294135    -0.59954598
24   MSFT 0.39539573    -0.21260188
25   MSFT 0.93238011     0.69360678
26   MSFT 0.20216682     0.92404382
27   MSFT 0.75496007     0.06384112
28   MSFT 0.62566157    -0.14810588
29   MSFT 0.57485095     2.04678225
30   MSFT 0.36178250    -0.23984063
31   MSFT 0.78743389     0.64870344
32   MSFT 0.92720872     7.44799343
33   MSFT 0.25836030    -0.61537614
34   MSFT 0.36156615    -0.45182053
35   MSFT 0.65566870     0.23024713
36   MSFT 0.17325979    -0.71563119
37   MSFT 0.55117234     0.61572246
38   MSFT 0.31505656    -0.43677621
39   MSFT 0.21256675     0.08741456
40   MSFT 0.44369580     0.72838754

Upvotes: 1

Related Questions