Home > OS >  Calculate daily log return in R
Calculate daily log return in R

Time:01-12

I am trying to calculate the daily log return based on daily closing prices for different stocks. In total, I have a dataset of 4,000 stocks covering a time period of one year.

Here is some trial data to illustrate my current data basis:

df <- tibble(
  Date = c(
    '01.11.2019',
    '04.11.2019',
    '05.11.2019',
    '06.11.2019',
    '07.11.2019',
    '08.11.2019'
  ),
  ACCR.PK = c(0.0035, 0.003, 0.0035, 0.0057, 0.0032, 0.0032),
  SWGI.PK = c(0.51, 0.51, 0.51, 0.51, 0.51, 0.51),
  HURC.OQ = c(35.53, 35.62, 35.76, 35.52, 35.6, 36)
) %>% 
  mutate(Date = Date %>% 
           as.Date(format = "%d.%m.%Y"))

df <- pivot_longer(df, -Date, names_to = "ticker", values_to = "price") 
df <- group_by(ticker)

Which should look like this: enter image description here

I am trying to calculate the daily log returns for each stock. I tried this function:

lrtn=diff(log(df$price))
Log.return.df <- data.frame(lrtn)

enter image description here

This function does indeed calculate the log returns, but it seems to delete the first row (since it is not possible to delete the first log without the prior data). I still want to keep the first row though (e.g. indicated as zero or N/A).

Also, the formula seems to not differentiate between the stocks.

Can somebody help me?

CodePudding user response:

You can do,

library(dplyr)

df %>% 
 group_by(ticker) %>% 
 mutate(lrtn = c(NA, diff(log(price)))) %>% 
 select(lrtn)

Adding missing grouping variables: `ticker`
# A tibble: 18 × 2
# Groups:   ticker [3]
   ticker      lrtn
   <chr>      <dbl>
 1 ACCR.PK NA      
 2 SWGI.PK NA      
 3 HURC.OQ NA      
 4 ACCR.PK -0.154  
 5 SWGI.PK  0      
 6 HURC.OQ  0.00253
 7 ACCR.PK  0.154  
 8 SWGI.PK  0      
 9 HURC.OQ  0.00392
10 ACCR.PK  0.488  
11 SWGI.PK  0      
12 HURC.OQ -0.00673
13 ACCR.PK -0.577  
14 SWGI.PK  0      
15 HURC.OQ  0.00225
16 ACCR.PK  0      
17 SWGI.PK  0      
18 HURC.OQ  0.0112 
  • Related