joshuaulrich/quantmod

periodReturn calculation method

Closed this issue · 3 comments

chalg commented

Not really an issue, just a confirmation if possible on how for example periodReturn = "weekly" works as I couldn't find in the documentation.

Minimal, reproducible example

In the example below from Yahoo Finance historical data, the weekly return is 33.3%. So it must be taking the Adj Close or Close from previous week close to calculate return - is that correct? As the current week starting on 10th of July shows now change.

Date Open High Low Close Adj Close Volume
12/07/2023 0.004 0.004 0.004 0.004 0.004 125000
11/07/2023 0.004 0.004 0.0035 0.004 0.004 2475371
10/07/2023 0.004 0.004 0.0035 0.004 0.004 3139964
7/07/2023 0.004 0.004 0.003 0.003 0.003 1135640
library(quantmod)

getSymbols(Symbols = "VAL.AX", src = "yahoo")
weeklyReturn(VAL.AX, subset = '2023-06::', type = 'arithmetic', leading = TRUE)

Session Info

R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.utf8  LC_CTYPE=English_Australia.utf8    LC_MONETARY=English_Australia.utf8 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] geomtextpath_0.1.1         patchwork_1.1.2            showtext_0.9-5             showtextdb_3.0             sysfonts_0.8.8            
 [6] ggtext_0.1.2               readxl_1.4.2               scales_1.2.1               forcats_1.0.0              stringr_1.5.0             
[11] dplyr_1.1.2                purrr_1.0.1                readr_2.1.4                tidyr_1.3.0                tibble_3.2.1              
[16] ggplot2_3.4.2              tidyverse_2.0.0            tidyquant_1.0.5            quantmod_0.4.20            TTR_0.24.3                
[21] PerformanceAnalytics_2.0.4 xts_0.12.1                 zoo_1.8-11                 lubridate_1.9.2           

loaded via a namespace (and not attached):
 [1] colorspace_2.0-3    class_7.3-20        timetk_2.8.1        snakecase_0.11.0    markdown_1.7        gridtext_0.1.5      rstudioapi_0.14    
 [8] listenv_0.8.0       furrr_0.3.1         farver_2.1.1        alphavantager_0.1.2 dials_1.1.0         scico_1.3.1         ggrepel_0.9.1      
[15] bit64_4.0.5         prodlim_2019.11.13  fansi_1.0.3         xml2_1.3.4          codetools_0.2-18    splines_4.2.1       knitr_1.41         
[22] jsonlite_1.8.4      workflows_1.1.3     yardstick_1.1.0     tune_1.0.1          compiler_4.2.1      httr_1.4.6          Matrix_1.5-1       
[29] fastmap_1.1.0       cli_3.5.0           htmltools_0.5.4     tools_4.2.1         gtable_0.3.1        glue_1.6.2          Rcpp_1.0.9         
[36] cellranger_1.1.0    DiceDesign_1.9      vctrs_0.6.2         iterators_1.0.14    parsnip_1.0.4       timeDate_4021.106   gower_1.0.0        
[43] xfun_0.39           globals_0.16.1      timechange_0.2.0    lifecycle_1.0.3     future_1.28.0       MASS_7.3-58.1       ipred_0.9-13       
[50] vroom_1.6.0         ragg_1.2.5          hms_1.1.3           parallel_4.2.1      yaml_2.3.6          curl_4.3.3          rpart_4.1.16       
[57] stringi_1.7.8       foreach_1.5.2       lhs_1.1.6           hardhat_1.2.0       lava_1.6.10         rlang_1.1.1         pkgconfig_2.0.3    
[64] commonmark_1.9.0    systemfonts_1.0.4   rsample_1.1.0       evaluate_0.19       lattice_0.20-45     labeling_0.4.2      recipes_1.0.1      
[71] cowplot_1.1.1       bit_4.0.4           tidyselect_1.2.0    parallelly_1.32.1   magrittr_2.0.3      R6_2.5.1            generics_0.1.3     
[78] pillar_1.9.0        withr_2.5.0         survival_3.4-0      nnet_7.3-18         future.apply_1.9.1  janitor_2.1.0       crayon_1.5.2       
[85] MetBrewer_0.2.0     Quandl_2.11.0       utf8_1.2.2          tzdb_0.3.0          rmarkdown_2.19      grid_4.2.1          digest_0.6.31      
[92] textshaping_0.3.6   GPfit_1.0-8         munsell_0.5.0       quadprog_1.5-8 
chalg commented

Could you give me an example that doesn't require tidyquant? That would make it easier for me to look into your question.

Done :-)

Thanks. It's easier to see what's going on if you use something that trades more often, like SPY. You can see below that the output of weeklyReturn() is the discrete return based on the close price. You could call weeklyReturn(Ad(x)) if you wanted the weekly adjusted returns.

x <- getSymbols("SPY", auto.assign = FALSE)
r <- weeklyReturn(x, subset = '2023-06::')
y <- merge(x, r, join = "right")
tail(merge(y, returns = drop(Cl(y/lag(y)-1))), 20)
##            SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted weekly.returns      returns
## 2023-06-02   424.50   428.74  423.95    427.92   91366700     426.3363    0.018808687           NA
## 2023-06-09   429.96   431.99  428.87    429.90   85742800     428.3090    0.004626987  0.004626987
## 2023-06-16   443.02   443.61  438.97    439.46  114121300     439.4600    0.022237724  0.022237724
## 2023-06-23   432.93   435.06  432.47    433.21   92074500     433.2100   -0.014222000 -0.014222000
## 2023-06-30   441.44   444.30  441.11    443.28  104921500     443.2800    0.023245095  0.023245095
## 2023-07-07   438.63   442.64  438.30    438.55   86076100     438.5500   -0.010670481 -0.010670481
## 2023-07-14   450.48   451.36  448.49    449.28   69761800     449.2800    0.024467019  0.024467019

I'm going to close this since it isn't something that needs to change in quantmod. But I'm happy to continue the conversation.