Somewhat related to the question Calculate the average based on other columns and R xts and data.table
Since current version of data.table, there are functions of as.data.table.xts. I wonder if there would be an improved solution taking the advantage of data.table.
Thanks Vincent for the function.
Reproducible data:
library(data.table)
library(quantmod)
getSymbols("SPY")
getSymbols("AAPL")
as.data.table.xts <- function(x, ...) {
cn <- colnames(x)
sscn <- strsplit(cn, "\\.")
indexClass(x) <- c('POSIXct', 'POSIXt') #coerce index to POSIXct
DT <- data.table(time=index(x), coredata(x))
#DT <- data.table(IDateTime(index(x)), coredata(x))
## If there is a Symbol embedded in the colnames, strip it out and make it a
## column
if (all(sapply(sscn, "[", 1) == sscn[[1]][1])) {
Symbol <- sscn[[1]][1]
setnames(DT, names(DT)[-1], sub(paste0(Symbol, "."), "", cn))
DT <- DT[, Symbol:=Symbol]
setkey(DT, Symbol, time)[]
} else {
setkey(DT, time)[]
}
}
spy.data <- as.data.table(SPY)
SPY_Day <- as.Date("2015-11-06")
AAPL_Day <- as.Date("2015-11-03")
spy.data[, time := as.Date(time)] # Not sure whether it is necessary, but it does not work with this conversion.
What I want to achieve:
"average of the closing/Opening/High/Low prices for the 5/10/30 consecutive trading days immediately preceding and including the Announcement Day, but excluding trading halt days (days on which trading volume is 0 or NA)
Problem 1
To find the 5 consecutive trading days immediately preceding and including the Announcement Day, but excluding trading halt days (days on which trading volume is 0 or NA)
I came to mind that tail
might be useful.
spy_5 <- tail(spy.data[Volume!=0 & time < Day], 5)
When I tried the the calculation of the average part
spy <- spy_5[ , lapply(.SD, function(x) mean(x, na.rm=TRUE)), by =.(Symbol), .SDcols = c(2:5)][, Lookback:=5L]
For the current stage
Is there any way to get rid of the middle product the data.table spy_5?
Any suggestion for a more succinct code?
Problem 2
As there are other symbols for the same calculation.
If there any way to use rbindlist
and getSymbols
to get the raw data in the format of data.table and then proceed.
** the desired output **
Symbol Open High Low Close Lookback 1: SPY 209.826 210.84 208.884 209.93 5 2: SPY ** ** ** ** 10 3: AAPL ** ** ** ** 5 4: AAPL ** ** ** ** 10
Any suggestions is welcome.