Search code examples
rquantmodquantstrat

quantstrat signal referencing other signals


What is the correct method to have signals reference other signals? Is it not intended functionality? I cant seem to find a way to do it in my code.

library(PerformanceAnalytics)
library(quantmod)
library(lattice)
startDate <- '2010-01-01' # start of data
endDate <- '2015-05-01' # end of data
.blotter<-new.env()
.strategy<-new.env()


Sys.setenv(TZ="EST") # set time zone
symbols<-c("GOOG")
data<-getSymbols(symbols, from=startDate, to=endDate, index.class="POSIXct",env=NULL)

library(quantstrat)
initDate <- '2009-12-31'
initEq <- 1e6
currency("USD")
stock(symbols, currency="USD", multiplier=1)

rm.strat("multiAsset.bb1") # remove portfolio, account, orderbook if re-run
initPortf(name="multiAsset.bb1", symbols, initDate=initDate)
initAcct(name="multiAsset.bb1", portfolios="multiAsset.bb1",initDate=initDate, initEq=initEq)
initOrders(portfolio="multiAsset.bb1", initDate=initDate)

strategy("bbands", store=TRUE)
#Indicators are applied before signals and rules, and the output of indicators may be used as inputs to construct signals or fire rules

#mktdata is the time series object that holds the current symbols data during evaluation (pg 55)
add.indicator("bbands", name = "BBands",arguments = list(HLC = quote(HLC(mktdata)), maType='SMA'), label='bbInd')

test <- applyIndicators("bbands", mktdata=data)
head(test, 10)

add.signal("bbands", name="sigThreshold", arguments=list(columns=c("pctB.bbInd",".77"),relationship="gt"),label="H.gt.UpperBand")

add.signal("bbands", name="sigThreshold", arguments=list(columns=c("H.gt.UpperBand","0"),relationship="gt"),label="true.upper.band")

test <- applySignals("bbands", mktdata=test)
head(test, 10)

Error

Error in match.names(column, colnames(data)) : 
  argument "column" is missing, with no default

Note that this is a generalized example. It would be trivial to make the first signal an indicator and avoid this problem in this specific case.


Solution

  • You've passed the wrong arguments to sigThreshold. This corrected code works as expected, with the second signal using the H.gt.UpperBand column (singular) from the first signal. The missing arguments in your code for the sigThreshold function are column (singular) and threshold.

    library(quantstrat)
    
    startDate <- '2010-01-01' # start of data
    endDate <- '2015-05-01' # end of data
    
    Sys.setenv(TZ="EST") # set time zone
    symbols<-c("GOOG")
    data<-getSymbols.yahoo(symbols, from=startDate, to=endDate, index.class="POSIXct",auto.assign=FALSE)
    
    initDate <- '2009-12-31'
    initEq <- 1e6
    currency("USD")
    stock(symbols, currency="USD", multiplier=1)
    
    rm.strat("multiAsset.bb1") # remove portfolio, account, orderbook if re-run
    initPortf(name="multiAsset.bb1", symbols, initDate=initDate)
    initAcct(name="multiAsset.bb1", portfolios="multiAsset.bb1",initDate=initDate, initEq=initEq)
    initOrders(portfolio="multiAsset.bb1", initDate=initDate)
    
    strategy("bbands", store=TRUE)
    #Indicators are applied before signals and rules, and the output of indicators may be used as inputs to construct signals or fire rules
    
    #mktdata is the time series object that holds the current symbols data during evaluation (pg 55)
    add.indicator("bbands"
                  , name = "BBands"
                  , arguments = list(HLC = quote(HLC(mktdata))
                                     , maType='SMA')
                  , label='bbInd')
    
    test <- applyIndicators("bbands", mktdata=data)
    head(test, 10)
    
    add.signal("bbands"
               , name="sigThreshold"
               , arguments=list(column="pctB.bbInd"
                                , threshold=.77
                                , relationship="gt")
               , label="H.gt.UpperBand")
    
    add.signal("bbands"
               , name="sigThreshold"
               , arguments=list(column="H.gt.UpperBand"
                                , threshold=0
                                , relationship="gt")
               ,label="true.upper.band")
    
    test <- applySignals("bbands", mktdata=test)
    head(test, 10)