Home > Mobile >  Buy and Hold return around event date in R
Buy and Hold return around event date in R

Time:09-12

I have a question in calculating returns in the following case. For each ID, if Date=EventDate, I hope to calculate "buy and hold return" from 5 days prior to the event date to 5 days after.

To be more specific with the table below, I want to calculate 11 returns for each ID, where the returns are (9/10-1), (12/10-1), (14/10-1), ~ , (14/10-1), (17/10-1), (16/10-1) for ID = 1 and (57/50-1), (60/50-1), (49/50-1), ~ , (65/50-1), (57/50-1), (55/50-1) for ID = 2. (That is the price 6 days prior to the event date is the denominator in the return calculation.)

 ---- ------------ ------- ------------ 
| ID |    Date    | Price | EventDate  |
 ---- ------------ ------- ------------ 
|  1 | 2011-03-06 |    10 | NA         |
|  1 | 2011-03-07 |     9 | NA         |
|  1 | 2011-03-08 |    12 | NA         |
|  1 | 2011-03-09 |    14 | NA         |
|  1 | 2011-03-10 |    15 | NA         |
|  1 | 2011-03-11 |    17 | NA         |
|  1 | 2011-03-12 |    12 | 2011-03-12 |
|  1 | 2011-03-13 |    14 | NA         |
|  1 | 2011-03-14 |    17 | NA         |
|  1 | 2011-03-15 |    14 | NA         |
|  1 | 2011-03-16 |    17 | NA         |
|  1 | 2011-03-17 |    16 | NA         |
|  1 | 2011-03-18 |    15 | NA         |
|  1 | 2011-03-19 |    16 | NA         |
|  1 | 2011-03-20 |    17 | NA         |
|  1 | 2011-03-21 |    18 | NA         |
|  1 | 2011-03-22 |    11 | NA         |
|  1 | 2011-03-23 |    15 | NA         |
|  1 | 2011-03-24 |    12 | 2011-03-24 |
|  1 | 2011-03-25 |    13 | NA         |
|  1 | 2011-03-26 |    15 | NA         |
|  2 | 2011-06-11 |    48 | NA         |
|  2 | 2011-06-12 |    49 | NA         |
|  2 | 2011-06-13 |    50 | NA         |
|  2 | 2011-06-14 |    57 | NA         |
|  2 | 2011-06-15 |    60 | NA         |
|  2 | 2011-06-16 |    49 | NA         |
|  2 | 2011-06-17 |    64 | NA         |
|  2 | 2011-06-18 |    63 | NA         |
|  2 | 2011-06-19 |    67 | 2011-06-19 |
|  2 | 2011-06-20 |    70 | NA         |
|  2 | 2011-06-21 |    58 | NA         |
|  2 | 2011-06-22 |    65 | NA         |
|  2 | 2011-06-23 |    57 | NA         |
|  2 | 2011-06-24 |    55 | NA         |
|  2 | 2011-06-25 |    57 | NA         |
|  2 | 2011-06-26 |    60 | NA         |
 ---- ------------ ------- ------------ 

Eventually, I hope to make the following table with a new column.

 ---- ------------ ------- ------------ --------------- 
| ID |    Date    | Price | EventDate  | BuyHoldReturn |
 ---- ------------ ------- ------------ --------------- 
|  1 | 2011-03-06 |    10 | NA         | NA            |
|  1 | 2011-03-07 |     9 | NA         | -0.1          |
|  1 | 2011-03-08 |    12 | NA         | 0.2           |
|  1 | 2011-03-09 |    14 | NA         | 0.4           |
|  1 | 2011-03-10 |    15 | NA         | 0.5           |
|  1 | 2011-03-11 |    17 | NA         | 0.7           |
|  1 | 2011-03-12 |    12 | 2011-03-12 | 0.2           |
|  1 | 2011-03-13 |    14 | NA         | 0.4           |
|  1 | 2011-03-14 |    17 | NA         | 0.7           |
|  1 | 2011-03-15 |    14 | NA         | 0.4           |
|  1 | 2011-03-16 |    17 | NA         | 0.7           |
|  1 | 2011-03-17 |    16 | NA         | 0.6           |
|  1 | 2011-03-18 |    15 | NA         | NA            |
|  1 | 2011-03-19 |    16 | NA         | 0.066666667   |
|  1 | 2011-03-20 |    17 | NA         | 0.133333333   |
|  1 | 2011-03-21 |    18 | NA         | 0.2           |
|  1 | 2011-03-22 |    11 | NA         | -0.266666667  |
|  1 | 2011-03-23 |    15 | NA         | 0             |
|  1 | 2011-03-24 |    12 | 2011-03-24 | -0.2          |
|  1 | 2011-03-25 |    13 | NA         | -0.133333333  |
|  1 | 2011-03-26 |    15 | NA         | 0             |
|  2 | 2011-06-11 |    48 | NA         | NA            |
|  2 | 2011-06-12 |    49 | NA         | NA            |
|  2 | 2011-06-13 |    50 | NA         | NA            |
|  2 | 2011-06-14 |    57 | NA         | 0.14          |
|  2 | 2011-06-15 |    60 | NA         | 0.2           |
|  2 | 2011-06-16 |    49 | NA         | -0.02         |
|  2 | 2011-06-17 |    64 | NA         | 0.28          |
|  2 | 2011-06-18 |    63 | NA         | 0.26          |
|  2 | 2011-06-19 |    67 | 2011-06-19 | 0.34          |
|  2 | 2011-06-20 |    70 | NA         | 0.4           |
|  2 | 2011-06-21 |    58 | NA         | 0.16          |
|  2 | 2011-06-22 |    65 | NA         | 0.3           |
|  2 | 2011-06-23 |    57 | NA         | 0.14          |
|  2 | 2011-06-24 |    55 | NA         | 0.1           |
|  2 | 2011-06-25 |    57 | NA         | NA            |
|  2 | 2011-06-26 |    60 | NA         | NA            |
 ---- ------------ ------- ------------ --------------- 

I have an idea of using the code below, but couldn't figure out how to calculate the 11 buy and hold returns around the event date.

data<-data%>%
group_by(ID)%>%
mutate(BuyHoldReturn=ifelse(Date==EventDate, ....

Thanks in advance!

CodePudding user response:

  • We can try
library(dplyr)

df |> group_by(ID) |> mutate( x = Price/lag(Price) - 1 ,
      y = which(Date == EventDate) - 1:n() ,
      BuyHoldReturn = case_when(between(y , -5 , 5) ~ x , TRUE ~ NA_real_)) |> 
      select(-x , -y)
  • Output
# A tibble: 28 × 5
# Groups:   ID [2]
      ID Date       Price EventDate  BuyHoldReturn
   <int> <chr>      <int> <chr>              <dbl>
 1     1 2011-03-06    10 NA               NA     
 2     1 2011-03-07     9 NA               -0.1   
 3     1 2011-03-08    12 NA                0.333 
 4     1 2011-03-09    14 NA                0.167 
 5     1 2011-03-10    15 NA                0.0714
 6     1 2011-03-11    17 NA                0.133 
 7     1 2011-03-12    12 2011-03-12       -0.294 
 8     1 2011-03-13    14 NA                0.167 
 9     1 2011-03-14    17 NA                0.214 
10     1 2011-03-15    14 NA               -0.176 
# … with 18 more rows
  • Related