【问题标题】:How can I calculate log returns for a financial year different from calendar year in R?如何计算与 R 中日历年不同的财政年度的对数回报?
【发布时间】: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


    【解决方案1】:

    也许这不是最优雅的,但我认为它有效,我手动获取了每年的第一天和最后一天,并相应地计算了日志返回

    # Get data
    library("tibble")
    library("tidyquant")
    RIL<- tq_get("RELIANCE.NS")
    RIL1<- RIL %>% mutate(CalYear = year(date),
                        Month = month(date),
                        FinYear = if_else(Month<4,CalYear,CalYear+1))
    
    # Get minimum and max dates in each year
    start_dates = c()
    end_dates  = c()
    for(year in format(min(RIL1$date),"%Y"):format(max(RIL1$date),"%Y")){
            start_dates = 
                c(start_dates,
                min(RIL1$date[format(RIL1$date, "%Y") ==  format(as.Date(ISOdate(year, 1, 1)),"%Y")])
                )
            end_dates = 
                c(end_dates,
                max(RIL1$date[format(RIL1$date, "%Y") ==  format(as.Date(ISOdate(year, 1, 1)),"%Y")])
                )
    }
    
    # Get filtered data
    RIL2 <- RIL1[(RIL1$date %in% start_dates | RIL1$date %in% end_dates),]
    
    # Get log returns, even indexes represent end of each year rows
    end_adjusted = RIL2$adjusted[1:length(RIL2$adjusted) %% 2 == 0]
    beginning_adjusted = RIL2$adjusted[1:length(RIL2$adjusted) %% 2 != 0]
    log_returns = log(end_adjusted/beginning_adjusted)
    
    # Put log returns and years in a tibble.
    result = tibble(log_returns ,format(RIL2$date[1:length(RIL2$date) %% 2 == 0], "%Y"))
    
    # Result
    result
    

    输出

    # A tibble: 11 x 2
       log_returns `format(RIL2$date[1:length(RIL2$date)%%2 == 0],…
             <dbl> <chr>                                           
     1     -0.412  2011                                            
     2      0.185  2012                                            
     3      0.0739 2013                                            
     4      0.0117 2014                                            
     5      0.145  2015                                            
     6      0.0743 2016                                            
     7      0.537  2017                                            
     8      0.215  2018                                            
     9      0.306  2019                                            
    10      0.287  2020                                            
    11      0.0973 2021
    

    【讨论】:

    • 感谢您的支持。我创建了一个自定义函数“log_return
    猜你喜欢
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多