sigAND
是一个非常有用的功能,可以在两个选定列都具有值(或非NA)时创建包含日期的新列。感谢Ilya Kipnis和他的IKTrading
包。
但是,我注意到在此功能的帮助页面中使用了cross
。我很困惑,如果我对上面sigAND
的用法的理解是正确的,那么cross
应该没有地方。
我查看了sigAND
源代码,并对其进行了测试。该函数工作正常,当cross = TRUE
输出列只是NA
或FALSE
的向量时。因此,cross
似乎没有真正的意义。
这是我的假数据和输入:
dataAnd <- cbind(c(1, NA, 3, NA, NA, 10, 12), 7:13)
dataAnd <- xts(dataAnd, order.by = Sys.Date()+1:7)
colnames(dataAnd) <- c("col1", "col2")
label = "both"
data = dataAnd
columns = c("col1", "col2")
cross = T # F
以下是sigAND
的源代码,其中包含一些注释以帮助自己理解代码(我还是R和编程新手)。
function (label, data = mktdata, columns, cross = FALSE)
# cross = False is important, as cross=T makes no sense here.
# columns: named colums to apply comparison to
#{
# create an empty return_signal_column
ret_sig = NULL
colNums <- rep(0, length(columns)) # colNums <- c(0, 0)
for (i in 1:length(columns)) { # for each column
# example of how to use match.names()
# match.names("Close", colnames(IF_DAY)) return 4, index of '.Close'
# colNums[i] <- 4 assigned with an index
colNums[i] <- match.names(columns[i], colnames(data))
}
# extract the first/left column to be a one-column xts and assigned to ret_sig
ret_sig <- data[, colNums[1]]
# for second or third comparing column
for (i in 2:length(colNums)) {
# check whether a date has values in both columns, yes(true), no(NA)
# example: 1:4 & c(1, NA, NA, 10)
# return: [1] TRUE NA NA TRUE
ret_sig <- ret_sig & data[, colNums[i]]
}
# turn above [1] TRUE NA NA TRUE to [1] 1 NA NA 1
ret_sig <- ret_sig * 1
#### using cross option here makes no sense, right?
# if cross was assigned to be True
if (isTRUE(cross))
# create ret_sig logic value by diff(ret_sig) == 1
ret_sig <- diff(ret_sig) == 1 # !!!! NA-1 or 1-NA are NA
# name ret_sig to be label
colnames(ret_sig) <- label # label is given to the output
return(ret_sig)
}
我加载了数据和输入,并运行了上面的源代码,没有注释掉的第一行和最后几行。
cross = FALSE
时我得到了以下输出:
> ret_sig
both
2016-03-19 1
2016-03-20 NA
2016-03-21 1
2016-03-22 NA
2016-03-23 NA
2016-03-24 1
2016-03-25 1
cross = TRUE
时我得到了以下输出:
> ret_sig
both
2016-03-19 NA
2016-03-20 NA
2016-03-21 NA
2016-03-22 NA
2016-03-23 NA
2016-03-24 NA
2016-03-25 FALSE
第二种情况表明cross
没有任何意义。或者,我错过了一些重要的东西吗?有人可以看看吗?
答案 0 :(得分:2)
感谢伊利亚的回复。 sigAND
的{{1}}是非常必要的。我引用伊利亚的答案如下:
关闭时购买&gt; SMA200和RSI < 20
如果没有十字架,你会每天都买这种情况 坚持。有了十字架,只有当RSI超过20时才会 关闭&gt; SMA200。
我改变了我的简单示例,它确实有效。
数据和变量:
cross = T
用于测试的dataAnd <- cbind(c(1, 0, 0, 1, 1, 1, 1), c(0, 0, 1, 1, 1, 0,1 ))
dataAnd <- xts(dataAnd, order.by = Sys.Date()+1:7)
colnames(dataAnd) <- c("col1", "col2")
label = "both"
data = dataAnd
columns = c("col1", "col2")
cross = T # F
源代码的一部分:
sigAND
# create an empty return_signal_column
ret_sig = NULL
colNums <- rep(0, length(columns)) # colNums <- c(0, 0)
for (i in 1:length(columns)) { # for each column
# example of how to use match.names()
# match.names("Close", colnames(IF_DAY)) return 4, index of '.Close'
# colNums[i] <- 4 assigned with an index
colNums[i] <- match.names(columns[i], colnames(data))
}
# extract the first/left column to be a one-column xts and assigned to ret_sig
ret_sig <- data[, colNums[1]]
# for second or third comparing column
for (i in 2:length(colNums)) {
# check whether a date has values in both columns, yes(1), no(0)
ret_sig <- ret_sig & data[, colNums[i]]
}
ret_sig <- ret_sig * 1
#### using cross option here makes no sense, right?
# if cross was assigned to be True
if (isTRUE(cross))
# create ret_sig logic value by diff(ret_sig) == 1
ret_sig <- diff(ret_sig) == 1
# name ret_sig to be label
colnames(ret_sig) <- label # label is given to the output
ret_sig
,我得到了:
cross = F
both
2016-03-20 0
2016-03-21 0
2016-03-22 0
2016-03-23 1
2016-03-24 1
2016-03-25 0
2016-03-26 1
,我得到了:
cross = T