I am trying to calculate the log returns of a dataset in R using the usual log differencing method for several stocks.
My current list of daily closing prices looks like this:
In total, I have a dataset of 4,000 stocks that cover the period of one year.
This is the trial data:
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,07)
I would like to calculate this in R.
Typically the formula is used to calculate the returns.
Return = (New Price - Old Price) / Old Price [in percentage ] with "new price = t" and "old price = t-1"
I tried the following code:
# First upload the financial data in R
library(readxl)
Closing_Prices_2020 <- read_excel("Closing_Prices_2020.xlsx")
I then tried the two options:
First try:
Returns_2020 <- Return.calculate(Daily_Returns_2020, method="log")
Second try:
CalculateReturns(Closing_Prices_2020$ACCR.PK, method = c("discrete", "log"))
Neither of them works for me. Does somebody help in calculating the daily returns? Thanks!