【问题标题】:Calculate Stocks biWeekly Return Standard Deviation in R计算R中的股票双周回报标准差
【发布时间】:2021-12-25 12:19:30
【问题描述】:

我有一个非常长的数据集,包含很多年的股票,类似于这个:

one_ticker = tq_get("AAPL", from = "2021-06-01")
one_ticker <- one_ticker %>%
                mutate(day = day(date),
                month = month(date),
                year = year(date))


 symbol date        open  high   low close   volume adjusted   day month  year
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl> <int> <dbl> <dbl>
 1 AAPL   2021-06-01  125.  125.  124.  124. 67637100     124.     1     6  2021
 2 AAPL   2021-06-02  124.  125.  124.  125. 59278900     125.     2     6  2021
 3 AAPL   2021-06-03  125.  125.  123.  124. 76229200     123.     3     6  2021
 4 AAPL   2021-06-04  124.  126.  124.  126. 75169300     126.     4     6  2021
 5 AAPL   2021-06-07  126.  126.  125.  126. 71057600     126.     7     6  2021
 6 AAPL   2021-06-08  127.  128.  126.  127. 74403800     126.     8     6  2021
 7 AAPL   2021-06-09  127.  128.  127.  127. 56877900     127.     9     6  2021
 8 AAPL   2021-06-10  127.  128.  126.  126. 71186400     126.    10     6  2021
 9 AAPL   2021-06-11  127.  127.  126.  127. 53522400     127.    11     6  2021
10 AAPL   2021-06-14  128.  131.  127.  130. 96906500     130.    14     6  2021
  1. 我想先计算每个月内biWeekly调整后的价格回报:
    - 第一个双周间隔:第 1-15 天 -second biWeekly 间隔:第 16-30 天

  2. 计算每个季度内的调整后回报标准差。

以下是结果(Apple 过去 6 个月):

    1. Adjusted_biWeekly_Returns
    
[1]  0.043128324
[2]  0.052324355
[3]  0.081663817
[4] -0.003620508
[5]  0.026136504
[6]  0.004698278
[7] -0.022818187
[8] -0.048995111
[9]  0.0153523
[10] 0.022176775

说明:

[1] 129.257401/123.913231-1 = 0.043128324 (15/06/2021 调整价格// 01/06/2021 调整价格)
[5] 148.882721/145.090561-1 = 0.026136504 (13/08/2021 & 02/08/2021) - 因为 15 日和 1 日没有交易。

     2. Quarterly Standard Deviation: 

1/06/2021 - 1/09/2021 0.028944365 ([1]-[6] standard deviation) 
1/09/2021 - 1/01/2022 Not available yet. 

如何在 R 中计算它? *有 tq_transmute 函数,对于每周计算非常有用,但对双周计算非常有用

【问题讨论】:

    标签: r time-series tidyverse finance zoo


    【解决方案1】:

    您可以单独执行每个步骤并使用to.weeklyapply.quarterly 函数,如下面的代码所示:

    library(tidyverse)
    library(tidyquant)
    library(xts)
    library(PerformanceAnalytics)
    library(TTR)
    
    one_ticker = tq_get("AAPL", from = "2021-06-01")
    one_ticker <- one_ticker %>%
      mutate(day = day(date),
             month = month(date),
             year = year(date))
    
    aapl_adj <- xts(x = one_ticker$adjusted,
                    order.by = one_ticker$date)
    
    aapl_adj_weekly <- to.weekly(aapl_adj, OHLC = F) # convert to weekly
    
    idx <- seq_along(aapl_adj_weekly) %% 2 > 0 # create index for bi-weekly dates
     
    aapl_adj_biweekly <- aapl_adj_weekly[idx, ] # extract bi-weekly values
    
    aapl_adj_biweekly_returns <- TTR::ROC(aapl_adj_biweekly, type = "discrete", na.pad = F)
    
    aapl_adj_biweekly_returns
    
    # compute quarterly standard deviation
    xts::apply.quarterly(aapl_adj_biweekly_returns, sd)
                       e1
    2021-06-18         NA
    2021-09-24 0.03159961
    2021-11-16 0.02900001
    

    如果您不需要将采样频率降低到每两周一次,您可以为每个代码一次运行一次。这也具有更好地估计回报标准差的优点,因为您使用所有可用的数据点,而不是只使用双周数据:

    # fast version without downsampling and annualized standard deviation
    aapl_adj |> TTR::ROC(type = "discrete", na.pad = F) |> xts::apply.quarterly(FUN = PerformanceAnalytics::sd.annualized)
    
                      e1
    2021-06-30 0.1551537
    2021-09-30 0.2063587
    2021-11-16 0.1701798
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      • 2018-09-12
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多