R:操纵日期栏,将非纽约证券交易所的交易日期更正为最近的交易日期



我有一组股票和事件公告日期(showdate)数据,我在数据中查找每只股票在该日期前后的价格。

我想检查+/-1天(测试日期)的价格,看看是否有任何价格变化。所以

df%>%
mutate(testdate=ymd(showdate)+days(1))

但当我加入表格按测试日期搜索价格信息时,问题是+/-1天操纵后的测试日期可能不是纽约证券交易所的交易日,可能是周末、假期或股票在该日期退市。

问题是,我如何操作showdate列+/-1以保留最正确的日期,而如果计算的日期是在周末或非交易日,则会更改为以前最近的日期。例如,如果+1天是一个特定行/股票的周日,我希望将日期更改为周五。

我不介意先做+1天,然后在下一批中做-1天。这个想法将扩展到调查+20,-20天等…

展示日期示例集:

> dput(t)
structure(c(18857, 18368, 17487, 17248, 16934, 17081, 17000, 
16994, 16993, 16917, 16910, 18822, 18456, 18194, 16959, 17805, 
17757, 17511, 17178, 18883, 18858, 18842, 18837, 18836, 18835, 
18831, 18821, 18815, 18814, 18808, 18800, 18795, 18792, 18773, 
18752, 18745, 18744, 18740, 18738, 18731, 18722, 18717, 18662, 
18661, 18659, 18649, 18648, 18647, 18646, 18642, 18618, 18611, 
18597, 18596, 18589, 18577, 18576, 18575, 18570, 18565, 18562, 
18561, 18558, 18556, 18555, 18548, 18547, 18542, 18528, 18519, 
18514, 18498, 18494, 18492, 18486, 18480, 18473, 18472, 18470, 
18466), class = c("IDate", "Date"))

您可以为此使用RQuantLib库。运行install.packages("RQuantLib")安装它,然后你可以尝试这个:

library(RQuantLib)
library(dplyr)
library(lubridate)
showdate <- structure(c(18857, 18368, 17487, 17248, 16934, 17081, 17000, 
16994, 16993, 16917, 16910, 18822, 18456, 18194, 16959, 17805, 
17757, 17511, 17178, 18883, 18858, 18842, 18837, 18836, 18835, 
18831, 18821, 18815, 18814, 18808, 18800, 18795, 18792, 18773, 
18752, 18745, 18744, 18740, 18738, 18731, 18722, 18717, 18662, 
18661, 18659, 18649, 18648, 18647, 18646, 18642, 18618, 18611, 
18597, 18596, 18589, 18577, 18576, 18575, 18570, 18565, 18562, 
18561, 18558, 18556, 18555, 18548, 18547, 18542, 18528, 18519, 
18514, 18498, 18494, 18492, 18486, 18480, 18473, 18472, 18470, 
18466), class = c("IDate", "Date"))
df <- tibble(my_date = showdate) %>% 
mutate(testdate = adjust(
calendar = "UnitedStates/NYSE",
dates = ymd(showdate)+days(1)
))

这提供了一个列为testdate的数据帧,该列要么是第二天,要么如果是在非交易日,则是下一个交易日。例如,2017-11-18是一个星期六,所以这被转移到2017-11-20:

# A tibble: 80 x 2
my_date    testdate  
<date>     <date>    
1 2021-08-18 2021-08-19
2 2020-04-16 2020-04-17
3 2017-11-17 2017-11-20
4 2017-03-23 2017-03-24
5 2016-05-13 2016-05-16
6 2016-10-07 2016-10-10
7 2016-07-18 2016-07-19
8 2016-07-12 2016-07-13
9 2016-07-11 2016-07-12
10 2016-04-26 2016-04-27
# ... with 70 more rows

相关内容

  • 没有找到相关文章

最新更新