[R-SIG-Finance] apply.paramset.signal.analysis error

Joshua Ulrich josh.m.ulrich at gmail.com
Wed Aug 3 00:27:45 CEST 2016


Hi Erol,

On Mon, Aug 1, 2016 at 7:18 PM, Erol Biceroglu
<erol.biceroglu at alumni.utoronto.ca> wrote:
> Hello,
>
> I've updated to the latest quanstrat, and still get an error using
> apply.paramset.signal.analysis:
>
> Error in post.signal.returns(signals = .sig.list[[name.ref]][,
> paste(symbols[j],  :
>   number of items to replace is not a multiple of replacement length
>
>
> applyStrategy works, apply.paramset works, however I can't get the signal
> portion working and was wondering if anyone knew what the error was.
>
> When I run debug, the problem occurs in *post.signal.returns*, on the final
> signal.ret[j,] = tryCatch, where the tryCatch returns an xts object with 5
> rows, and it wants to update signal.ret[j,] which is length 6.
>
Thanks for the reproducible example.  This looks like a bug when
include.day.of.signal = TRUE.  I'll investigate.

> Any feedback would be greatly appreciated.  Thanks for your help.
>
> Here's my sessionInfo():
>
> ####
>
>> sessionInfo()R version 3.3.1 (2016-06-21)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Ubuntu 14.04.4 LTS
>
> locale:
>  [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C
> LC_TIME=en_CA.UTF-8
>  [4] LC_COLLATE=en_CA.UTF-8     LC_MONETARY=en_CA.UTF-8
> LC_MESSAGES=en_CA.UTF-8
>  [7] LC_PAPER=en_CA.UTF-8       LC_NAME=C
> LC_ADDRESS=C
> [10] LC_TELEPHONE=C             LC_MEASUREMENT=en_CA.UTF-8
> LC_IDENTIFICATION=C
>
> attached base packages:
> [1] parallel  stats     graphics  grDevices utils     datasets
> methods   base
>
> other attached packages:
>  [1] doParallel_1.0.10             iterators_1.0.8
> quantstrat_0.9.1739
>  [4] foreach_1.4.3                 blotter_0.9.1695
> FinancialInstrument_1.2.0
>  [7] quantmod_0.4-5                TTR_0.23-1
> PerformanceAnalytics_1.4.3541
> [10] xts_0.9.874                   zoo_1.7-13
>
> loaded via a namespace (and not attached):
> [1] compiler_3.3.1   tools_3.3.1      codetools_0.2-14 grid_3.3.1
>  lattice_0.20-33
>
> ####
>
> Here's the MRE:
> #################################
>
> library(xts)
> library(PerformanceAnalytics)
> library(quantmod)
> library(TTR)
> library(quantstrat)
>
> getSymbols(Symbols = "SPY", from = "2000-01-01")
>
>
> startDate<-as.Date(index(SPY)[1], format="%Y-%m-%d")
> endDate<-as.Date(index(last(SPY)), format="%Y-%m-%d")
>
> currency("USD")
>
> stock(primary_id = c("SPY"),currency = "USD")
>
> #name
> maStrategy <-"MAStrategy"
>
> #Date, one day before prices
> strategyDate <- min(index(SPY)) - 1
>
> NumSh<-1
>
> #rm.strat(maStrategy)
> #rm(mktdata)
>
>
> maLag <- 5
>
>
> #init portfolio and account
> initPortf(name = maStrategy
>           , symbols = list("SPY") #as defined in Financial instrument
>           , initDate = strategyDate
> )
>
> initAcct(name = maStrategy
>          ,portfolios = maStrategy
>          ,initDate = strategyDate
>          ,initEq = 1
> )
>
> #order book, and strategy
> initOrders(portfolio = maStrategy
>            , initDate = strategyDate
> )
>
> #position limits
> addPosLimit(maStrategy, symbol = "SPY", strategyDate, maxpos = NumSh,
> longlevels = NumSh, minpos = 0)
>
> strategy( maStrategy, store = TRUE)
>
> #add indicator
> add.indicator(strategy = maStrategy
>               , name = "EMA"
>               , arguments = list(x = quote(mktdata$SPY.Close), n =
> quote(maLag))
>               , label = "SPYma"
> )
>
>
>
>
> add.signal(strategy = maStrategy
>            , name = "sigComparison"
>            , arguments = list(columns = c("SPY.Close","SPYma")
>                               , relationship = "gt"
>            )
>            , label = "SPY.gt.ma"
> )
>
>
>
> add.signal(strategy = maStrategy
>            , name = "sigComparison"
>            , arguments = list(columns = c("SPY.Close","SPYma")
>                               , relationship = "lte"
>            )
>            , label = "SPY.lte.ma"
> )
>
>
>
>
> #Entry and exit rules
>
>
> add.rule(strategy = maStrategy
>          , name = "ruleSignal"
>          , arguments = list(sigcol = "SPY.gt.ma"
>                             , sigval = TRUE
>                             , orderqty = NumSh
>                             , ordertype = "market"
>                             , orderside = NULL
>                             , osFUN = "osMaxPos"
>                             , symbol = "SPY"
>          )
>          , type = "enter"
> )
>
>
> add.rule(strategy = maStrategy
>          , name = "ruleSignal"
>          , arguments = list(sigcol = "SPY.lte.ma"
>                             , sigval = TRUE
>                             , orderqty = "all"
>                             , ordertype = "market"
>                             , orderside = NULL
>                             , osFUN = "osMaxPos"
>                             , symbol = "SPY"
>          )
>          , type = "exit"
> )
>
>
> applyStrategy(strategy = maStrategy
>               , portfolios = maStrategy
> )
>
> updatePortf(maStrategy)
> updateAcct(maStrategy)
> updateEndEq(maStrategy)
>
>
> maLag <- seq(5,10,1)
> ####################
> #delete.paramset(maStrategy,"maLagDist")
> add.distribution(strategy = maStrategy
>                  , paramset.label = "maLagDist"
>                  , component.type = "indicator"
>                  , component.label = "SPYma"
>                  , variable = list(n = maLag)
>                  , label = "lagDist"
>
> )
>
>
>
> library(foreach)
> library(doParallel)
> cl <- makeForkCluster(nnodes = detectCores())
> registerDoParallel(cl)
> maStrategyAP <-
>   apply.paramset(strategy.st = maStrategy
>                  , paramset.label = "maLagDist"
>                  , portfolio.st = maStrategy
>                  , account.st = maStrategy
>   )
> stopCluster(cl = cl)
> cl <- NULL
> closeAllConnections()
> gc()
>
>
> #this works
> maStrategyAP$tradeStats$Profit.To.Max.Draw
>
>
> head(mktdata)
>
> #undebug(apply.paramset.signal.analysis)
> maStrategyAPSA <-
>   apply.paramset.signal.analysis(strategy.st = maStrategy
>                                  , paramset.label = "maLagDist"
>                                  , portfolio.st = maStrategy
>                                  , sigcol = "SPY.gt.ma"
>                                  , sigval = 1
>                                  , on = NULL
>                                  , forward.days = 5
>                                  , include.day.of.signal = TRUE
>   )
>
>
> ##################################
>
>
>
> Erol Biceroglu
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.



-- 
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com
R/Finance 2016 | www.rinfinance.com



More information about the R-SIG-Finance mailing list