2

sigAND is a useful function in creating a new column with dates when two selected columns both have values (or not NA). Thanks to Ilya Kipnis and his IKTrading package.

However, I noticed in this function's help page, cross is used. I am confused that if my understanding of sigAND's usage above is correct, there should be no place for cross.

I looked into sigAND source code, and tested on it. The function works fine and when cross = TRUE the output column is only a vector of NA or FALSE. So, it seems to be no real meaning for cross to be in the function.

Here is my fake data and input:

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

Here is the source code of sigAND with some comments to help myself understand the codes (I am still new to R and programming).

   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)
    }

I loaded the data and inputs, and ran through the source code above without the first and last few lines which are commented out.

I got the following output when 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

I got the following output when 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

The second situation suggests that there is no meaning for cross to be there. Or, am I missing something important here? Could anyone have a look?

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Daniel
  • 1,428
  • 3
  • 16
  • 35

1 Answers1

2

Thanks to Ilya's reply. sigAND's cross = T is very necessary. I quote Ilya's answer in the following:

Buy when the close > SMA200 and RSI < 20

Without the cross, you would buy on every single day that condition held true. With the cross, only when the RSI crosses under 20 when close > SMA200.

I changed my simple example and it did work.

data and variables:

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

part of sigAND source code for testing:

# 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

with cross = F, I got:

           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

with cross = T, I got:

            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
Daniel
  • 1,428
  • 3
  • 16
  • 35