2

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")
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Anatolye
  • 75
  • 5

1 Answers1

1

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.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Thanks for your reply! But I was not interested in comparing the performance of systems, and if I was wrong to use the functions of quantstrat. I understand that if the system is faster, then the script quickly) Are you using quantstrat for testing systems? Are you have experience in the development and testing of systems? Or just run my script? In fact, my script is the easiest possible. And it must be run on any system quickly, much faster than one second. Maybe I need to activate any quantstrat options? For example, disable console output the results of the script? – Anatolye Aug 02 '16 at 17:10
  • @Anatolye: I wasn't comparing system performance. I said it takes too long on your machine, so something on your machine makes the code take longer than it should. The script runs >3x faster on my machine, but my machine isn't 3x faster than yours. Yes, I test strategies with quantstrat, and I'm one of the package authors. It is unreasonable to expect a parameter search over 11 values to take less than 1 second. – Joshua Ulrich Aug 02 '16 at 17:47
  • Joshua, our systems are from different generations, so I do not think that such a direct comparison is correct. But thanks! I'll check my system. When I run the same sсript on my trading and testing platform for calculating 200 values, I couldn't fix the time, because the result appeared immediately. I don't expect, I see it every day. – Anatolye Aug 02 '16 at 18:16
  • @Anatolye: Don't check your system. Do what I suggested in my answer. Make sure you're running the latest development versions of all relevant packages. I've made speed improvements to some/all of them. Comparing your trading/testing platform to quantstrat isn't relevant. I could write faster code in a compiled language. I can even write faster R code for your specific strategy. But quantstrat is designed to be flexible and to avoid common errors. Speed is not a primary concern. You asked if your quantstrat code was slow. I said it seems to be taking longer than it should. – Joshua Ulrich Aug 02 '16 at 18:37
  • Joshua, sorry, I mean installing the latest versions of quantstrat, blotter, and xts from GitHub. I'm using API of some Algorithmic Trading Systems, c#. – Anatolye Aug 02 '16 at 18:48
  • @Anatolye: Yes, I would expect something similar to quantstrat, but written in C#, to be faster by an order of magnitude or more. – Joshua Ulrich Aug 02 '16 at 19:06