I have 9x2 dataframe DATS with prices and POSIXct datetimestamps sampled every 15 minutes. and a list of dates FOMCDATES with the dates of recent FOMC events. I then split the POSIXct datetimestamps into separate Date and Time columns. I then add column FOMCBinary to DATS containing a 1 whenever the date in DATS is contained in FOMCDATES AND time is 14:30 (EDIT: FOMC is 14:00, used 14:30 by mistake - example still valid).
I would like to record the Close before the event takes place in a separate variable. The name of the variable should be based on the date of the event. In the case at hand, the result should be: PreEvent-2016-01-27 = 1122.7. Please take into account this would actually be run in a large sample with dozens of dates and the time can be other than 14:30 (e.g. if looking at NFP rather than FOMC).
DATS <- structure(list(DateTime = structure(list(sec = c(0, 0, 0, 0,0, 0, 0, 0, 0), min = c(30L, 15L, 0L, 45L, 30L, 15L, 0L, 45L,30L), hour = c(15L, 15L, 15L, 14L, 14L, 14L, 14L, 13L, 13L),mday = c(27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L), mon = c(0L,0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), year = c(116L, 116L, 116L,116L, 116L, 116L, 116L, 116L, 116L), wday = c(3L, 3L, 3L,3L, 3L, 3L, 3L, 3L, 3L), yday = c(26L, 26L, 26L, 26L, 26L,26L, 26L, 26L, 26L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,0L, 0L), zone = c("EST", "EST", "EST", "EST", "EST", "EST","EST", "EST", "EST"), gmtoff = c(NA_integer_, NA_integer_,NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,NA_integer_, NA_integer_)), .Names = c("sec", "min", "hour","mday", "mon", "year", "wday", "yday", "isdst", "zone", "gmtoff"), class = c("POSIXlt", "POSIXt")), Close = c(1127.2, 1127.5,1126.9, 1128.3, 1125.4, 1122.7, 1122.8, 1117.3, 1116)), .Names = c("DateTime","Close"), row.names = 2131:2139, class = "data.frame")
FOMCDATES <- structure(c(16785, 16827, 16876), class = "Date")
DATS$Time <- strftime(DATS$DateTime, format="%H:%M:%S")
DATS$Date <- as.Date(DATS$DateTime)
DATS$FOMCBinary <- ifelse( DATS$Time == "14:30:00" & DATS$Date %in% FOMCDATES, 1, 0)
#Output for FOMCDATES:
[1] 2015-12-16 2016-01-27 2016-03-16
#Output for DATS after calculations performed:
DateTime Close Time Date FOMCBinary
2131 2016-01-27 15:30:00 1127.2 15:30:00 2016-01-27 0
2132 2016-01-27 15:15:00 1127.5 15:15:00 2016-01-27 0
2133 2016-01-27 15:00:00 1126.9 15:00:00 2016-01-27 0
2134 2016-01-27 14:45:00 1128.3 14:45:00 2016-01-27 0
2135 2016-01-27 14:30:00 1125.4 14:30:00 2016-01-27 1
2136 2016-01-27 14:15:00 1122.7 14:15:00 2016-01-27 0
2137 2016-01-27 14:00:00 1122.8 14:00:00 2016-01-27 0
2138 2016-01-27 13:45:00 1117.3 13:45:00 2016-01-27 0
2139 2016-01-27 13:30:00 1116.0 13:30:00 2016-01-27 0
My attempt results in a vector rather than a single value, and the variable name is not dynamic.
#My failed attempt
#Define rowShift function
rowShift <- function(x, shiftLen = 1L) {
r <- (1L + shiftLen):(length(x) + shiftLen)
r[r<1] <- NA
return(x[r]) }
PreEventLevel <- ifelse(DATS$FOMCBinary > 0, rowShift(DATS$Close, +1), 0)
How could this be achieved? Thank you very much!