Help with sp500 dataset

Hello!

I need an help with a formula to calculate daily returns of sp500 data.

The formula is the following: (closing price - previous closing price)/ previous closing price

This are the data I have

Which formula should I use to calculate daily returns?

Thanks!

I would lag the Close (or AdjClose) column and calculate as in the following code.

library(dplyr)
DF <- data.frame(Close = c(45.6, 46.7,44.3, 41.7, 45.2))
DF
#>   Close
#> 1  45.6
#> 2  46.7
#> 3  44.3
#> 4  41.7
#> 5  45.2
DF <- DF |> mutate(PrevClose = lag(Close))
DF
#>   Close PrevClose
#> 1  45.6        NA
#> 2  46.7      45.6
#> 3  44.3      46.7
#> 4  41.7      44.3
#> 5  45.2      41.7
DF <- DF |> mutate(Return = (Close - PrevClose)/PrevClose)
DF
#>   Close PrevClose      Return
#> 1  45.6        NA          NA
#> 2  46.7      45.6  0.02412281
#> 3  44.3      46.7 -0.05139186
#> 4  41.7      44.3 -0.05869074
#> 5  45.2      41.7  0.08393285

Created on 2022-03-25 by the reprex package (v2.0.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.