Anatolye Anatolye - 2 months ago 19
R Question

Is this quantstrat code unusually slow?

I try to run and optimize a very simple system, using quantstrat. My strategy is: enter when

Close > SMA
, exit when
Close < SMA
. I am running on daily data from 2010-01-01 to 2014-01-01. Optimization is
.nSMA = (10:20)
. My system is i5 m480 2.67Ghz, 8gb, Win7-64, Revolution R Open 3.2.0, RStudio.

It takes about 50 seconds to execute my code. Is it normal for quantstrat? Or have I made mistake?

require(quantstrat)
require(foreach)
registerDoSEQ()

rm(list = ls(.blotter), envir = .blotter)
currency('USD')
initDate = "2010-01-01"
from = "2010-01-01"
to = "2014-01-01"
initEq = 1e5
nSMA = 50

getSymbols("GOOG", from = from, to = to)
stock("GOOG", currency = "USD", tick_size = 1, multiplier = 1)
getInstrument("GOOG", type = "instrument")

strategy.st <- "first"
portfolio.st <- "first"
account.st <- "first"
rm.strat(portfolio.st)
rm.strat(strategy.st)

initPortf(portfolio.st, symbols = 'GOOG', initDate = initDate, currency = 'USD')
initAcct(account.st, portfolios = portfolio.st, initDate = initDate, currency = 'USD', initEq = initEq)
initOrders(portfolio.st, initDate = initDate)

strategy(strategy.st, store=TRUE)

### indicators

add.indicator(strategy.st, name = "SMA",
arguments = list(x = quote(Cl(mktdata)), n = nSMA),
label = "nSMA")

### signals

add.signal(strategy.st, name='sigCrossover',
arguments = list(columns=c("Close","nSMA"),
relationship="gt"),
label='LE'
)

add.signal(strategy.st, name='sigCrossover',
arguments = list(columns=c("Close","nSMA"),
relationship="lt"),
label='LX'
)

### rules

add.rule(strategy.st, name="ruleSignal",
arguments=list(sigcol="LE" , sigval=TRUE,
orderside="long",
ordertype="market",
prefer="Open",
orderqty=1,
replace=FALSE
),
type="enter",
label="EnterLong"
)

add.rule(strategy.st, name="ruleSignal",
arguments=list(sigcol="LX" , sigval=TRUE,
orderside="long",
ordertype="market",
prefer="Open",
orderqty="all",
replace=FALSE
),
type="exit",
label="ExitLong"
)

applyStrategy(strategy.st, portfolio.st)
save.strategy(strategy.st)


# Optimization
.nSMA = (10:20)
load.strategy(strategy.st)
add.distribution(strategy.st,
paramset.label = 'nSMA',
component.type = 'indicator',
component.label = 'nSMA',
variable = list(n = .nSMA), label = 'NSMA')

results <- apply.paramset(strategy.st,
paramset.label='nSMA',
portfolio.st=portfolio.st,
account.st=account.st,
nsamples = length(.nSMA),
audit = NULL,
verbose=TRUE)
View((results$tradeStats))

plot(results$tradeStats$NSMA, results$tradeStats$Net.Trading.PL, type = "l")

Answer

Your apply.paramset call only takes about 15 seconds on my laptop (i7-4600U 2.1Ghz, 12GB RAM). Here's my sessionInfo output:

R> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] quantstrat_0.9.1739           foreach_1.4.2                
[3] blotter_0.9.1695              PerformanceAnalytics_1.4.3541
[5] FinancialInstrument_1.2.0     quantmod_0.4-5               
[7] TTR_0.23-1                    xts_0.9-7                    
[9] zoo_1.7-13                   

loaded via a namespace (and not attached):
[1] compiler_3.3.1   codetools_0.2-14 grid_3.3.1       iterators_1.0.7 
[5] lattice_0.20-33

So it does seem like it takes too long on your machine. Do you have other applications running on your computer that are consuming a lot of CPU and/or RAM at the same time this code is running? Also make sure you're running the latest versions of quantstrat, blotter, and xts from GitHub.

Comments