简体   繁体   English

继续在 quantstrat 中获取“dims 与对象的长度不匹配”

[英]Keep getting the "dims do not match the length of object" in quantstrat

I have been modifying a backtest I made a few months ago using quantstrat.我一直在修改几个月前使用 quantstrat 进行的回测。 It was all working fine until I added in signal and rule 6 (donchian channel low) My full code is below.一切正常,直到我添加了信号和规则 6(Donchian 通道低)我的完整代码如下。 Any help would be greatly appreciated.任何帮助将不胜感激。

Thanks in advance.提前致谢。

PS: Sorry if the code looks messy, this is my first time asking a question here. PS:对不起,如果代码看起来很乱,这是我第一次在这里提问。

library(blotter)
library(quantstrat)
library(xts)
library(quantmod)
Sys.setenv(TZ="UTC") 

getSymbols('^DJI', from="2016-01-01", to="2016-12-31", index.class="POSIXct", adjust=T)


colnames(DJI)<- c("Open","High","Low","Close","Volume","Adjusted")

currency("AUD") 
Sys.setenv(TZ="UTC") 
initdate <- '2016-01-03' 
startdate <- '2016-01-04' 
enddate <- '2016-12-30'
portfolioname <- "Dow Jones" 
accountname <- "Dow Jones" 
contracts <- 1 
txncost <- -0.79 
margin <- 15000
strategyname <- accountname
startingcapital <- 1e+6
symbollist <- 'DJI'
instrument<-"DJI"


rm.strat(strategyname) 
initPortf(portfolioname,
      symbollist,initDate=initdate,currency="AUD")
initAcct(accountname,portfolios=portfolioname,initDate=initdate,initEq=startingcapital,currency="AUD")
initOrders(portfolio=portfolioname,initDate=initdate)
strategy(strategyname,store=TRUE)
strat <- getStrategy(strategyname)
summary(strat)


osInvestAll <- function (data, timestamp, orderqty, ordertype, orderside, equity, portfolio, symbol, ruletype, ..., orderprice) 
{   
datePos <- format(timestamp,"%Y-%m-%d")

  updatePortf(Portfolio=portfolio,Symbol=symbol,Dates=datePos)
updateAcct(portfolio,Dates=datePos)   
updateEndEq(portfolio,Dates=datePos)     
Posn <- getPosQty(portfolio,Symbol=symbol,Date=datePos)
equity <- getEndEq(portfolio,datePos) 
ClosePrice <- getPrice(get(symbol))[datePos]
#UnitSize <- as.numeric(trunc(equity*0.25/ClosePrice)) 

if (Posn == 0) { 
    osInvestAll <- UnitSize } else
        {osInvestAll <- 0
         }

}

Rule1<-function(price,SMA,...)
{ifelse(price>SMA,1,-1)}
add.indicator(strategy=strategyname,name="SMA",
          arguments=list(x=quote(mktdata$Close),n=40),label="SMA40")

add.indicator(strategyname, name="Rule1", arguments=list(price =     quote(mktdata$Close), SMA=quote(mktdata$SMA.SMA40)), label="Rule1Signal")

Rule2<-function(SMA2,SMA5,...)
{ifelse(SMA5>SMA2,1,-1)}
add.indicator(strategy=strategyname,name="SMA",
          arguments=list(x=quote(mktdata$Close),n=2),label="SMA2")
add.indicator(strategy=strategyname,name="SMA",
          arguments=list(x=quote(mktdata$Close),n=5),label="SMA5")

add.indicator(strategyname, name="Rule2",     arguments=list(SMA5=quote(mktdata$SMA.SMA5), SMA2=quote(mktdata$SMA.SMA2)), label="Rule2Signal")




#Daily range less than the 10 day average range, buy next day if todays close was higher (+1) or short if it was lower 
#HLC <- HLC(DJI)
Rule3<-function(HLC, n)
{ 
   DJI$TR <- ATR(HLC(DJI),10)[,1]    
    DJI$todays <-DJI$High-DJI$Low

Rule3 <- ifelse(DJI$todays>DJI$TR, ifelse((HLC$Close > lag.xts(HLC$Close)), -1,1), ifelse((HLC$Close >       lag.xts(HLC$Close)), 1, -1))

}

    add.indicator(strategyname, name="Rule3", arguments=list(HLC=quote(HLC(mktdata)), n=10), label="Rule3Signal")


##Donchian Channel
#High
Rule4 <- function(H,L,Close)
{ifelse(Close>H,1,0)}

add.indicator(strategy=strategyname,name="DonchianChannel",
          arguments=list(HL=quote(mktdata$Close),n=50),label="DCH")

add.indicator(strategyname, name="Rule4", arguments=list(H=quote(mktdata$high.DCH),L=quote(mktdata$low.DCH),Close=quote(mktdata$Close)), label="Rule4Signal")
#Low
Rule6 <- function(H,L,Close)
{ifelse(Close<L,-1,0)}

add.indicator(strategy=strategyname,name="DonchianChannel",
          arguments=list(HL=quote(mktdata$Close),n=50),label="DCH")

add.indicator(strategyname, name="Rule6", arguments=list(H=quote(mktdata$high.DCH),L=quote(mktdata$low.DCH),Close=quote(mktdata$Close)), label="Rule6Signal")



#Rule 5
Rule5<-function(H15,L15)
{
X<-(H15+L15)/2
ifelse(DJI$Close>X,1,-1)}

    add.indicator(strategy=strategyname,name="SMA",
          arguments=list(x=quote(mktdata$High),n=15),label="H15")
    add.indicator(strategy=strategyname,name="SMA",
          arguments=list(x=quote(mktdata$Low),n=15),label="L15")

    add.indicator(strategyname, name="Rule5", arguments=list(Close=quote(mktdata$Close), H15=quote(mktdata$High),L15=quote(mktdata$Low)), label="Rule5Signal")




RuleSum <- function(R1,R2,R3,R4,R5,R6,...)
{
RuleSum <- ( R1 + R2 + R3 + R4 + R5 + R6 )
}
add.indicator(strategyname, name="RuleSum",     arguments=list(R1=quote(mktdata$Close.Rule1Signal), R2=quote(mktdata$SMA.SMA5.Rule2Signal),R3=quote(mktdata$todays.Rule3Signal),R4=quote(mktdata$Rule4Signal),R5=quote(mktdata$Close.Rule5Signal),R6=quote(mktdata$Close.Rule6Signal)), label="RuleSum")


#Signals
add.signal(strategyname,name="sigThreshold",
       arguments=list(column="Close.Rule1Signal.RuleSum",relationship="gt",threshold=0,cross=FALSE),
  label="signal.gt.zero")


 add.signal(strategyname,name="sigThreshold",
  arguments=list(column="Close.Rule1Signal.RuleSum",relationship="lt",threshold=0,cross=FALSE),
  label="signal.lt.zero")



#Rules
 add.rule(strategyname,name='ruleSignal',
arguments = list(sigcol="signal.gt.zero", sigval=TRUE,
replace=FALSE,
prefer='open',
orderside='long',
ordertype='market',
orderqty=1,
orderset='ocolong'
  ),
  type='enter',
  label='LE'
 )


add.rule(strategyname,name='ruleSignal',
arguments = list(sigcol="signal.gt.zero", sigval=TRUE,
replace=FALSE, 
prefer='close',
orderside='long',
ordertype='market',
orderqty='all',
orderset='ocolong'
  ),
  type='exit',
  label='LX'
)

trailingStopPercent <- .20

 add.rule(strategyname, name = 'ruleSignal',
 arguments=list(sigcol="signal.gt.zero" , sigval=TRUE,
 replace=FALSE,
 prefer='close',
 orderside='long',
 ordertype='stoptrailing',
 tmult=TRUE,
 threshold=quote(trailingStopPercent),
 orderqty='all',
orderset='ocolong'
  ),
type='chain', parent="LE",
label='StopTrailingLong',
enabled=TRUE
 )

###short rules
add.rule(strategyname,name='ruleSignal',
arguments = list(sigcol="signal.lt.zero", sigval=TRUE,
replace=FALSE,
prefer='open',
orderside='short',
ordertype='market',
orderqty=-1,
orderset='ocoshort'
),
type='enter',
label='SE'
)


add.rule(strategyname,name='ruleSignal',
arguments = list(sigcol="signal.lt.zero", sigval=TRUE,
replace=FALSE,  
prefer='close',
orderside='short',
ordertype='market',
orderqty='all',
orderset='ocoshort'
  ),
  type='exit',
  label='SX'
)

trailingStopPercent <- .20

add.rule(strategyname, name = 'ruleSignal',
arguments=list(sigcol="signal.lt.zero" , sigval=TRUE,
replace=FALSE,
prefer='close',
orderside='long',
ordertype='stoptrailing',
tmult=TRUE,
threshold=quote(trailingStopPercent),
orderqty='all',
orderset='ocoshort'
  ),
type='chain', parent="SE",
label='StopTrailingShort',
enabled=TRUE
 )

summary(getStrategy(strategyname))

     applyStrategy(strategy=strategyname,portfolios=portfolioname,verbose=TRUE,update.equity = TRUE, showEq = TRUE) #verbose=FALSE not to print


updatePortf(strategyname)
updateAcct(strategyname)
updateEndEq(strategyname)

If you run traceback() after getting the error, you can quickly identify where the trouble in your code likely lies.如果在收到错误后运行traceback() ,则可以快速确定代码中可能存在的问题所在。

You haven't correctly specified the column name used in R4 for the RuleSum indicator, which should be R4=quote(mktdata$Close.Rule4Signal) .您没有正确指定R4用于RuleSum指标的列名,应该是R4=quote(mktdata$Close.Rule4Signal)

Your backtest works once you correct the add.indicator for RuleSum :一旦您更正了add.indicator ,您的add.indicator RuleSum

add.indicator(strategyname, name="RuleSum",     arguments=list(R1=quote(mktdata$Close.Rule1Signal), R2=quote(mktdata$SMA.SMA5.Rule2Signal),R3=quote(mktdata$todays.Rule3Signal),R4=quote(mktdata$Close.Rule4Signal),R5=quote(mktdata$Close.Rule5Signal),R6=quote(mktdata$Close.Rule6Signal)), label="RuleSum")

Tip: For debugging purposes, you can test all the indicators work in isolation from the signals and rules by calling提示:出于调试目的,您可以通过调用与信号和规则隔离来测试所有指标的工作

res <- applyIndicators(strategyname, DJI)

after you have completed all add.indicator 's.在您完成所有add.indicator之后。 (And before you add add.signal s, and way before you run applyStrategy . If this code works as expected, then you know the problem may either be with the signals or the rules. Then you can test the signals work using applySignals , etc... (在添加add.signal之前,以及运行applyStrategy之前的方式。如果此代码按预期工作,那么您知道问题可能出在信号或规则上。然后您可以使用applySignals等测试信号的工作情况...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R错误:暗淡与物体长度不符 - R error: dims do not match the length of an object 你知道为什么尝试pairw.anova时dims [product 0]与object [1]的长度不匹配吗? - Do you know why dims [product 0] do not match the length of object [1] when trying pairw.anova 估算值:VIM软件包(aggr函数)-暗淡[产品284088]与对象的长度不匹配[284121] - Imputing values : VIM package (aggr function) - dims [product 284088] do not match the length of object [284121] 使用daply进行频率计数时,R中的“尺寸[积0]与对象长度不匹配”错误 - “dims [product 0] do not match the length of object” error in R when using daply for frequency counts R 重塑 package:“Dim(x) 中的错误”…“dims [产品 100] 与 object [109] 的长度不匹配” - R reshape package: “Error in Dim(x)”… “dims [product 100] do not match the length of object [109]” R 星星错误:在评估代理时,暗淡与 object 的长度不匹配 - R stars error: dims do not match the length of object when evaluating proxy dplyr总结str.default(obj,...)中的错误dims [product 11]与对象的长度不匹配[3] - dplyr summarise Error in str.default(obj, …) dims [product 11] do not match the length of object [3] dplyr,dunn测试,dim(robj)&lt;-c(dX,dY)中的错误:dims [产品0]与对象的长度不匹配 - dplyr, dunn test, Error in dim(robj) <- c(dX, dY) : dims [product 0] do not match the length of object R-交叉验证错误处理--“产品变暗与对象长度不匹配” - R - cross validation error handling-- "dims product do not match the length of object" 使用R函数“外部”时出现“尺寸[产品xx]与对象[xx]的长度不匹配”错误 - “dims [product xx] do not match the length of object [xx]” error in using R function `outer`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM