quantstrat信号引用其他信号

时间:2016-06-01 06:15:45

标签: r quantmod quantstrat

让信号参考其他信号的正确方法是什么?它不是有意的功能吗?我似乎无法在我的代码中找到一种方法。

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 in match.names(column, colnames(data)) : 
  argument "column" is missing, with no default

请注意,这是一个通用示例。将第一个信号作为指标并在这种特定情况下避免这个问题是微不足道的。

1 个答案:

答案 0 :(得分:0)

您已将错误的参数传递给sigThreshold。此校正代码按预期工作,第二个信号使用第一个信号的H.gt.UpperBand列(单数)。代码中sigThreshold函数中缺少的参数是column(单数)和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)