将 zoo 转换为 dataframe 后添加带有 Ticker 的列
Add a column with Ticker after converting zoo to dataframe
我试图在将 xts 转换为数据框后添加一列,因为 xts 只能包含数值。另外我想按行绑定它们作为最终结果,但我失败了并且没有找到解决方案。这是我的代码,是从 Whosebug 上的不同作者那里收集的。多亏了!
library(quantmod)
# Fetch all Symbols from AMEX
symbols <- stockSymbols(exchange = c("AMEX"))
symbols <- symbols[,1]
# define environment
data <- new.env()
# Load Stock data in environment
getSymbols(symbols,from="2016-01-01", env=data)
# build function to convert zoo to dataframe
zoo.to.data.frame <- function(x, index.name="Date") {
stopifnot(is.zoo(x))
xn <- if(is.null(dim(x))) deparse(substitute(x)) else colnames(x)
setNames(data.frame(index(x), x, row.names=NULL), c(index.name,xn))
}
# build function to calculate different signals (...only price and ADX in this example)
"SignalCalculator" <- function(x) {
# Adjusted Price
price <- x[,c(6)]
# ADX
adx <- ADX(x[,c("High","Low","Close")], n =14)
DIp <- adx$DIp
DIn <- adx$DIn
Adx <- adx$ADX
# bind single values together and convert to dataframe
result <- zoo.to.data.frame(cbind(price, DIp, DIn, Adx))
# the following is not working respectively I don´t know how to
# result2 <- cbind(result, Ticker = x)
}
# Loop function on data #
lapply(data,FUN = SignalCalculator)
将函数应用到所有代码后,我想做的是生成一个带有行绑定的巨大数据框,将所有代码都放在里面以将它们导出到 csv 文件。这应该如下所示。但对我来说不清楚如何将它们绑定在一起?感谢有关此主题的任何帮助。谢谢
下面的 out2
是您提供的 link 中的 table 的格式。您可以使用 xts
中的辅助函数,例如 HLC
和 Ad
,它们比对您的列选择进行硬编码更灵活,而且您不需要这样做 zoo.to.data.frame
东西。
# define environment
data <- new.env()
# Load Stock data in environment
getSymbols(symbols,from="2016-01-01", env=data)
SignalCalculator2 <- function(x) {
# Assumes first colname format is still [tickername].Open
tickername <- strsplit(colnames(x)[1], "\.")[[1]][1]
# Adjusted Price:
price <- Ad(x)
colnames(price) <- "Price"
adx <- ADX(HLC(x), n = 14)
# You dont want this column in your table:
adx$DX <- NULL
res <- merge(price, adx)
# res is an xts object. Now convert to data.frame
df_res <- data.frame("Date" = index(res), "Ticker" = tickername, coredata(res))
df_res
}
# data is an environment, not a list, so can use eapply (similar to lapply):
out <- eapply(env = data, FUN = SignalCalculator2)
out2 <- do.call(rbind, out)
# If you want to order rows by date:
out2 <- out2[order(out2$Date),]
# Optional tidy up:
rownames(out2) <- NULL
> tail(out2)
# Date Ticker Price DIp DIn ADX
# 164 2016-08-25 AAMC 12.72 17.67766 21.28021 11.31483
# 330 2016-08-25 AAU 1.42 22.36896 23.64023 30.50243
# 165 2016-08-26 AAMC 12.80 16.48608 21.07136 11.37868
# 331 2016-08-26 AAU 1.36 23.02102 21.80518 28.51742
# 166 2016-08-29 AAMC 13.15 15.75814 20.14096 11.43797
# 332 2016-08-29 AAU 1.43 21.63012 22.30030 26.58943
我试图在将 xts 转换为数据框后添加一列,因为 xts 只能包含数值。另外我想按行绑定它们作为最终结果,但我失败了并且没有找到解决方案。这是我的代码,是从 Whosebug 上的不同作者那里收集的。多亏了!
library(quantmod)
# Fetch all Symbols from AMEX
symbols <- stockSymbols(exchange = c("AMEX"))
symbols <- symbols[,1]
# define environment
data <- new.env()
# Load Stock data in environment
getSymbols(symbols,from="2016-01-01", env=data)
# build function to convert zoo to dataframe
zoo.to.data.frame <- function(x, index.name="Date") {
stopifnot(is.zoo(x))
xn <- if(is.null(dim(x))) deparse(substitute(x)) else colnames(x)
setNames(data.frame(index(x), x, row.names=NULL), c(index.name,xn))
}
# build function to calculate different signals (...only price and ADX in this example)
"SignalCalculator" <- function(x) {
# Adjusted Price
price <- x[,c(6)]
# ADX
adx <- ADX(x[,c("High","Low","Close")], n =14)
DIp <- adx$DIp
DIn <- adx$DIn
Adx <- adx$ADX
# bind single values together and convert to dataframe
result <- zoo.to.data.frame(cbind(price, DIp, DIn, Adx))
# the following is not working respectively I don´t know how to
# result2 <- cbind(result, Ticker = x)
}
# Loop function on data #
lapply(data,FUN = SignalCalculator)
将函数应用到所有代码后,我想做的是生成一个带有行绑定的巨大数据框,将所有代码都放在里面以将它们导出到 csv 文件。这应该如下所示。但对我来说不清楚如何将它们绑定在一起?感谢有关此主题的任何帮助。谢谢
out2
是您提供的 link 中的 table 的格式。您可以使用 xts
中的辅助函数,例如 HLC
和 Ad
,它们比对您的列选择进行硬编码更灵活,而且您不需要这样做 zoo.to.data.frame
东西。
# define environment
data <- new.env()
# Load Stock data in environment
getSymbols(symbols,from="2016-01-01", env=data)
SignalCalculator2 <- function(x) {
# Assumes first colname format is still [tickername].Open
tickername <- strsplit(colnames(x)[1], "\.")[[1]][1]
# Adjusted Price:
price <- Ad(x)
colnames(price) <- "Price"
adx <- ADX(HLC(x), n = 14)
# You dont want this column in your table:
adx$DX <- NULL
res <- merge(price, adx)
# res is an xts object. Now convert to data.frame
df_res <- data.frame("Date" = index(res), "Ticker" = tickername, coredata(res))
df_res
}
# data is an environment, not a list, so can use eapply (similar to lapply):
out <- eapply(env = data, FUN = SignalCalculator2)
out2 <- do.call(rbind, out)
# If you want to order rows by date:
out2 <- out2[order(out2$Date),]
# Optional tidy up:
rownames(out2) <- NULL
> tail(out2)
# Date Ticker Price DIp DIn ADX
# 164 2016-08-25 AAMC 12.72 17.67766 21.28021 11.31483
# 330 2016-08-25 AAU 1.42 22.36896 23.64023 30.50243
# 165 2016-08-26 AAMC 12.80 16.48608 21.07136 11.37868
# 331 2016-08-26 AAU 1.36 23.02102 21.80518 28.51742
# 166 2016-08-29 AAMC 13.15 15.75814 20.14096 11.43797
# 332 2016-08-29 AAU 1.43 21.63012 22.30030 26.58943