这个量子代码是否异常缓慢?

时间:2016-07-31 13:32:58

标签: r quantstrat

我尝试使用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")

1 个答案:

答案 0 :(得分:1)

我的笔记本电脑上的apply.paramset通话只需大约15秒(i7-4600U 2.1Ghz,12GB RAM)。这是我的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和/或RAM?还要确保你从GitHub运行最新版本的quantstrat,blotter和xts。