2

sigAND当两个选定的列都具有值(或不是 NA)时,在创建具有日期的新列时很有用。感谢 Ilya Kipnis 和他的IKTrading包裹。

但是,我注意到在这个函数的帮助页面中,cross是使用的。我很困惑,如果我对sigAND上面的用法的理解是正确的,那么应该没有cross.

我查看了sigAND源代码,并对其进行了测试。该函数可以正常工作,并且当cross = TRUE输出列只是NAor的向量时FALSEcross因此,在函数中似乎没有真正的意义。

这是我的假数据和输入:

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。或者,我在这里遗漏了一些重要的东西吗?有人可以看看吗?

4

1 回答 1

2

感谢伊利亚的回复。sigANDcross = T非常必要的。我在下面引用 Ilya 的回答:

当收盘价 > SMA200 和 RSI < 20 时买入

如果没有十字架,你会在条件成立的每一天购买。只有当 RSI 低于 20 时收盘 > SMA200 时才有交叉。

我改变了我的简单示例,它确实有效。

数据和变量:

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我得到:

            both
2016-03-20    NA
2016-03-21 FALSE
2016-03-22 FALSE
2016-03-23  TRUE
2016-03-24 FALSE
2016-03-25 FALSE
2016-03-26  TRUE
于 2016-03-19T06:12:07.150 回答