1

I am fairly new to coding and I am trying to pick r. I am running a back test in quantstrat. I have looked at nuts and bolts online and this helped me clean up my code but I am also trying to create custom functions and this is where the problem begins. I follow the rule> signal > indicator hierarchy and I know the problem is a combination between the indicator and some custom data I am trying to add to mktdata. I am adding an extra column I imported from excel in xls format as follows:

>PEDATA <- readxl::read_xls()

>headers <- c("Date","PEValues","lowpeindicator","highpeindicator","inpe")

>names(PEDATA) <- c(headers)

>mktdata$PEValues <- PEDATA$PEValues 

>head(mktdata)

I know the data is properly added once I look at thehead function. Then I proceed to create my customized indicators where I am just adapting the add.indicator and SMA functions.

>add.indicator(strategy.name, name = "SMA", arguments =
list(x=quote(PEValues(mktdata)), n = PEs), label = SMA1)

From here on, it all goes through till the end. Once I try to apply the strategy I get the following:

> applyStrategy(strategy = strategy.name, portfolios = portfolio.name)
Error in `colnames<-`(`*tmp*`, value = seq(ncol(tmp_val))) : 
attempt to set 'colnames' on an object with less than two dimensions

I have run a traceback and get the following:

4: stop("attempt to set 'colnames' on an object with less than two dimensions") 3: colnames<-(*tmp*, value = seq(ncol(tmp_val))) 2: applyIndicators(strategy = strategy, mktdata = mktdata, parameters = parameters, ...) 1: applyStrategy(strategy = strategy.name, portfolios = portfolio.name)

To get PEValues to pull something I did this:

>has.PEValues<- function (x, which = FALSE) 
>{
>      colAttr <- attr(x, "PEValues")
>     if (!is.null(colAttr)) 
>         return(if (which) colAttr else TRUE)
>      loc <- grep("PEValues", colnames(x), ignore.case = TRUE)
>       if (!identical(loc, integer(0))) {
>        return(if (which) loc else TRUE)
>      }
>       else FALSE
>     }
>     
>     
>     
>     PEValues<- function (x) 
>    {
>      if (has.inpe(x)) 
>        return(x[, grep("PEValues", colnames(x), ignore.case = TRUE)])
>      stop("subscript out of bounds: no column name containing 
>\"PEValues\"")
>    }

At this stage I honestly have no idea what to do and a lot of the forums are still beyond me. Any help will be hugely appreciated and I will be more than happy to answer any questions.

As per request, sample data using dput(head(mktdata)

> dput(head(mktdata))
>structure(c(44.2187, 44.4062, 44.9687, 44.9687, 44.9687, 44.8125, 
>44.375, 44.8437, 45.0937, 45.0625, 45.125, 44.8125, 44.125, 44.375, 
>44.4687, 44.7187, 44.9062, 44.5625, 44.3437, 44.8125, 45, 44.9687, 
>44.9687, 44.6562, 201300, 529400, 531500, 492100, 596100, 122100, 
>27.362904, 27.652185, 27.767893, 27.748577, 27.748577, 27.555738, 
>NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
>NA, NA, NA, NA, NA, NA, NA, NA, 22.55, 22.55, 22.55, 22.55, 22.55, 
>22.55, 1, 1, 1, 1, 1, 1), class = c("xts", "zoo"), .indexCLASS = "Date",                 
>.indexTZ = "UTC", tclass = "Date", tzone = "UTC", src = "yahoo", updated =     
>structure(1544551441.04075, class = c("POSIXct", 
>"POSIXt")), index = structure(c(728611200, 728697600, 728784000, 
>728870400, 729129600, 729216000), tzone = "UTC", tclass = "Date"), .Dim =     
>c(6L,12L), .Dimnames = list(NULL, c("SPY.Open", "SPY.High", "SPY.Low", 
>"SPY.Close", "SPY.Volume", "SPY.Adjusted", "SMA.SMA150", "SMA.SMA50", 
>"opensma", "closesma", "PEValues", "inpe")))

head(mktdata)

> head(mktdata)
>           SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted 
>SMA.SMA150 SMA.SMA50 opensma closesma PEValues inpe
>1993-02-02  44.2187  44.3750 44.1250   44.3437     201300     27.36290         
>NA        NA      NA       NA    22.55    1
>1993-02-03  44.4062  44.8437 44.3750   44.8125     529400     27.65218         
>NA        NA      NA       NA    22.55    1
>1993-02-04  44.9687  45.0937 44.4687   45.0000     531500     27.76789         
>NA        NA      NA       NA    22.55    1
>1993-02-05  44.9687  45.0625 44.7187   44.9687     492100     27.74858         
>NA        NA      NA       NA    22.55    1
>1993-02-08  44.9687  45.1250 44.9062   44.9687     596100     27.74858         
>NA        NA      NA       NA    22.55    1
>1993-02-09  44.8125  44.8125 44.5625   44.6562     122100     27.55574         
>NA        NA      NA       NA    22.55    1
Sean Wells
  • 11
  • 2
  • 1
    It would be easier to help, if you paste in some sample data. You can use `dput(mkdata)` or `dput(head(mkdata))` if it's big, and then paste in the output to your question. – r_alanb Dec 11 '18 at 02:38
  • Hi! Yes, just updated the main post to reflect! – Sean Wells Dec 11 '18 at 18:34
  • Thanks for adding in the data. You may also want to check out https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example, which goes over how to make a good reproducible example. At the very least `strategy.name` needs to be defined in your code. It would also be helpful if you removed all the `>` characters on the input side of the code, so that it's easier to copy/paste into R. – r_alanb Dec 11 '18 at 20:09

0 Answers0