1
votes

I am new to R and am trying to create a very simple trading strategy (Buy the VIX when it's up by 2%, sell it when it's down by 2%). I am getting an error with the add.signal command: "Error in match.names(column, colnames(data)) : argument "column" is missing, with no default." I've searched everywhere and I can't seem to find the fix or understand what R is trying to tell me. Any input would be appreciated.

require(quantstrat)

options("getSymbols.warning4.0"=FALSE) #suppress warnings
rm(list=ls(.blotter), envir=.blotter) #house cleaning, clears blotter
rm(list=ls(.strategy), envir=.strategy)#clears strategy
rm.strat(qs.portfolio)#clear the portfolio environment
rm.strat(qs.strategy)#clear the strategy environment
rm.strat(qs.account)#clear the account environment

currency("USD")
stock("VIX",currency="USD",multiplier=1)

# system settings
initDate <- '2013-12-31' 
startDate <- '2014-01-01'
endDate <- '2016-04-07'
initEq <- 100000
Sys.setenv(TZ="UTC") #Timezone

getSymbols('^VIX', from=startDate, to=endDate, index.class="POSIXct", adjust=TRUE)
VIX$lagROC <- lag(round(ROC(Cl(VIX)), 4), n=1, type="discrete")
VIX$lagROC[is.na(VIX$lagROC)] <- 0

# initialize portfolio, account, orders
qs.strategy <- "qsJones"
initPortf(qs.strategy,'VIX')
initAcct(qs.strategy,portfolios=qs.strategy, initEq=initEq)
initOrders(portfolio=qs.strategy)
#create a new strategy object
strategy(qs.strategy,store=TRUE)

strat <- getStrategy(qs.strategy) 

thresh1 <- (.02*-1)
thresh2 <- .02

add.indicator(strategy = qs.strategy, name = "ROC",
  arguments = list(x = quote(Cl(mktdata)), n=1), label="lagROC")
test <- applyIndicators(qs.strategy, VIX)

add.signal(qs.strategy, name="sigThreshold",
  arguments=list(column="lagROC", threshold=thresh1, relationship="lte", cross=FALSE),
  label="lt.ROCThresh1")
add.signal(qs.strategy, name="sigThreshold",
  arguments=list(column="lagROC", threshold=thresh2, relationship="gte", cross=FALSE),
  label="gt.ROCThresh2")
test <- applySignals(qs.strategy, test)

# exit when lagROC < .02
add.rule(qs.strategy, name='ruleSignal',
  arguments=list(sigcol="Cl.lt.lagROC", sigval=TRUE, replace=FALSE, orderqty='all',
                   ordertype='market', orderside='long'),
  type='exit', path.dep=TRUE)
# go long when lagROC > .02
add.rule(qs.strategy, name='ruleSignal',
  arguments=list(sigcol="Cl.gt.lagROC", sigval=TRUE, replace=FALSE, orderqty=1500,
                   ordertype='market', orderside='long'),
  type='enter', path.dep=TRUE)

applyStrategy(strategy=qs.strategy , portfolios=qs.strategy)
tail(mktdata)
mktdata["2014"]
getTxns(Portfolio=qs.strategy, Symbol="VIX")
1

1 Answers

1
votes

Your add.signal calls create two columns: "lt.ROCThresh1" and "gt.ROCThresh2", while your add.rule calls reference columns "Cl.lt.lagROC" and "Cl.gt.lagROC" (which don't exist).

You need to either:

  1. Change the label= argument in your add.signal calls from "lt.ROCThresh1" and "gt.ROCThresh2" to "Cl.lt.lagROC" and "Cl.gt.lagROC", respectively, or
  2. Change the sigcol= argument in your add.rule calls from "Cl.lt.lagROC" and "Cl.gt.lagROC" to "lt.ROCThresh1" and "gt.ROCThresh2", respectively.