【发布时间】:2021-06-08 10:44:29
【问题描述】:
我想计算 R 中股票的对数回报。问题是我的财政年度是从 4 月 1 日到 3 月 31 日。我尝试使用包 tidyquant 和 tidyverse。我试过的代码如下:
library(tidyquant)
RIL<- tq_get("RELIANCE.NS") # download the stock price data of Reliance Industries Limited listed on NSE of India. The data is from January 2011 to May 2021.
library(tidyverse)
RIL1<- RIL %>% mutate(CalYear = year(date),
Month = month(date),
FinYear = if_else(Month<4,CalYear,CalYear+1)) # This creates a new variable called FinYear, which correctly shows the financial year. If the month is >3 (ie March), the financial year is calendar year +1.
RIL_Returns<- RIL1 %>%
group_by(FinYear) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "yearly",
type = "log") #This part of the code has the problem.
从这段代码中,我每年得到两个日志回报值。这不可能是真的。我想要一个包含 FinYear 和 Log_Returns 列的表,其中 Log_Returns 定义为 ln(给定 FinYear 最后一个交易日的调整收盘价/给定 FinYear 第一个交易日的调整收盘价)。我该怎么做?
【问题讨论】:
标签: r