这个 quantstrat 代码是否异常缓慢?

Is this quantstrat code unusually slow?

我尝试 运行 并使用 quantstrat 优化一个非常简单的系统。我的策略是:Close > SMA时进入,Close < SMA时退出。我正在 运行 收集从 2010-01-01 到 2014-01-01 的每日数据。优化是.nSMA = (10:20)。我的系统是 i5 m480 2.67Ghz, 8gb, Win7-64, Revolution R Open 3.2.0, RStudio.

执行我的代码大约需要 50 秒。 quantstrat 正常吗?还是我弄错了?

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")

你的 apply.paramset 通话在我的笔记本电脑(i7-4600U 2.1Ghz,12GB RAM)上只需要大约 15 秒。这是我的 sessionInfo 输出:

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

看来在您的机器上花费的时间确实太长了。您的计算机上是否有其他应用程序 运行 在消耗大量 CPU and/or RAM 的同时此代码为 运行?还要确保您 运行 来自 GitHub.

的最新版本的 quantstrat、blotter 和 xts