1

The following quantstrat system produces the following error:

Error in `colnames<-`(`*tmp*`, value = "hardStop") : 
  attempt to set 'colnames' on an object with less than two dimensions

This is an issue with my "hardStop" function/indicator. It is an if/else function which takes a timestamp of the current ATR-based stop (produced by function/indicator "stopLimit" and stored as a column in mktdata).

When I run the following code for column "buyTrigger", it produces satisfactory TRUE/FALSE signals.

applyIndicatorSignals(strategy=strategy.st, portfolio=portfolio.st, mktdata, sigcol="buyTrigger")

2015-10-21          0
2015-10-22          1
2015-10-23          0
2015-10-26          0
2015-10-27          0
2015-10-28          1
2015-10-29          0
2015-10-30          0

So here's the function at fault.

hardStop <- function(strategy, portfolio, mktdata, sigcol) {
  if (sigcol==TRUE) {
    stopHere <- mktdata$loss.stopLimit
  }
  else {
    stopHere <- NA
  }
  stopHere <- na.locf(stopHere)
  out <- stopHere
  colnames(out) <- "hardStop"
  return(out)

Any idea why I have this error?

The complete code's below, which makes this a lengthy post. Sorry.

require(quantstrat)

initDate="1990-01-01"
from="2011-07-07"
to="2015-12-12"
options(width=70)

options("getSymbols.warning4.0"=FALSE)
rm(list=ls(.blotter), envir=.blotter)

currency('USD')
Sys.setenv(TZ="UTC")

symbols <- "SPY"

suppressMessages(getSymbols(symbols, from=from, to=to, src="yahoo", adjust=TRUE))
stock(symbols, currency="USD", multiplier=1)

#trade sizing and initial equity settings
tradeSize <- 100000
initEq <- tradeSize*length(symbols)

strategy.st <- portfolio.st <- account.st <- "NewerTry"
rm.strat(portfolio.st)
rm.strat(strategy.st)
initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate, currency='USD',initEq=initEq)
initOrders(portfolio.st, initDate=initDate)
strategy(strategy.st, store=TRUE)

#parameters
daysHigh <- 20

pctATR <- .01
period <- 14

#create functions
periodHigh <- function(HLC, n) {
  high <- (runMax(Hi(HLC), n=n))
  out <- lag(high, 1)
  colnames(out) <- "periodHighest"
  return(out)
}

stopLimit <- function(HLC, n, maType, pctATR) {
  ATR <- ATR(HLC, n=n, maType=maType)
  ATR <- ATR$atr
  close <- Cl(HLC)
  atrStopProfit <- close+(ATR*300*pctATR)
  atrStopLoss <- close-(ATR*100*pctATR)
  atrStop <- cbind(atrStopProfit, atrStopLoss)
  colnames(atrStop) <- c("profit", "loss")
  return(atrStop)
}

hardStop <- function(strategy, portfolio, mktdata, sigcol) {
  if (sigcol==TRUE) {
    stopHere <- mktdata$loss.stopLimit
  }
  else {
    stopHere <- NA
  }
  stopHere <- na.locf(stopHere)
  out <- stopHere
  colnames(out) <- "hardStop"
  return(out)
}

#indicators and signals
add.indicator(strategy.st, name="periodHigh",
              arguments=list(HLC=quote(HLC(mktdata)), n=daysHigh),
              label="periodHighest")

add.indicator(strategy.st, name="stopLimit",
              arguments=list(HLC=quote(HLC(mktdata)), n=period, wilder=TRUE, pctATR=pctATR),
              label="stopLimit")

add.signal(strategy.st, name="sigCrossover",
           arguments=list(columns=c("High", "periodHighest"), relationship="gt", cross=TRUE),
           label="buyTrigger")

applyIndicatorSignals(strategy=strategy.st, portfolio=portfolio.st, mktdata, sigcol="buyTrigger")

add.indicator(strategy.st, name="hardStop",
              arguments=list(strategy=strategy.st, portfolio=portfolio.st, mktdata=quote(HLC(mktdata)),
                             sigcol="buyTrigger"),
              label="hardStop")

#rules
add.rule(strategy.st, name="ruleSignal", 
         arguments=list(sigcol="buyTrigger", sigval=TRUE, ordertype="market", 
                        orderside="long", replace=FALSE, prefer="Open", 
                        orderqty=10000, orderset="orders"), 
         type="enter", path.dep=TRUE,
         label="newEntry")

#run
t1 <- Sys.time()
out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
t2 <- Sys.time()
print(t2-t1)

#set up analytics
updatePortf(portfolio.st)

#performance analytics
chart.Posn(portfolio.st)
Saile
  • 115
  • 1
  • 11
  • 2
    Please provide both a [minimally reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and **note the libraries that you are using**. As written, it's impossible to answer this. – alexwhitworth Jan 19 '16 at 21:20
  • Thanks, Alex. This is using quantstrat. Link to reproducible code is: eliassimpson.com/investing/wp-content/uploads/system_7.R – Saile Jan 20 '16 at 04:50
  • Very few people, including me, are going to go to another website to find a reproducible example. Please provide said example here. – alexwhitworth Jan 20 '16 at 05:05
  • Sure--included it in the original question now. – Saile Jan 20 '16 at 05:23
  • I'm not really sure how this this a **programming question.** It seems like you want someone to do your research for you, not like you have a **specific** programming question. Can you please clarify / simplify your question? – alexwhitworth Jan 20 '16 at 19:06
  • The signal "buyTrigger" creates a boolean that is referenced in the rule "newEntry" through sigcol and sigval. Can I also use this boolean to write a function which results in a timestamp of the column mktdata$loss.stopLimit whenever the signal is true? – Saile Jan 21 '16 at 15:55

1 Answers1

1

For this, I ended up running a function inside the function. That way I didn't have to deal with bringing the column data back into another function. Though I still feel like paste() or grep() should work somehow.

stopLimit <- function(HLC, n, maType, pctATR) {
  ATR <- ATR(HLC, n=n, maType=maType)
  ATR <- ATR$atr
  close <- Cl(HLC)
  atrStopProfit <- close+(ATR*300*pctATR)
  atrStopLoss <- close-(ATR*100*pctATR)
  atrStop <- cbind(atrStopProfit, atrStopLoss)
  colnames(atrStop) <- c("profit", "loss")
  return(atrStop)
}

hardStop <- function(HLC, m, n, maType, pctATR) {
  dayHigh <- Hi(HLC)[,1]
  runHigh <- periodHigh(HLC, m)
  runHigh <- replace(runHigh, is.na(runHigh), 0)
  potsLoss <- stopLimit(HLC, n, maType, pctATR)$loss
  potsLoss <- ifelse(dayHigh > runHigh, potsLoss, NA)
  potsLoss <- na.locf(potsLoss)
  out <- potsLoss
  potsWin <- stopLimit(HLC, n, maType, pctATR)$profit
  potsWin <- ifelse(dayHigh > runHigh, potsWin, NA)
  print(potsWin <- na.locf(potsWin))
  pots <- cbind(potsLoss, potsWin)
  colnames(pots) <- c("potsloss", "potswin")
  return(pots)
}

add.indicator(strategy.st, name="hardStop",
              arguments=list(HLC=quote(HLC(mktdata)), m=daysHigh,
                             n=period, wilder=TRUE, pctATR=pctATR),
              label="potsSpot")

add.signal(strategy.st, name="sigCrossover",
           arguments=list(columns=c("Close", "potswin.potsSpot"), relationship="lt", cross=FALSE),
           label="sellTriggerProfit")
Saile
  • 115
  • 1
  • 11