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