[R-SIG-Finance] Quantstrat Parameter Optimization

Colton Smith coltonsmith321 at gmail.com
Wed Aug 24 19:19:44 CEST 2016


Hello all,

I'm fairly new to backtesting in R. I made a Bollinger Bands and R strategy
to learn how to use quantstrat which works fine. After playing around with
different Bollinger Bands and RSI variables, I am now trying to run a
Bollinger Bands parameter analysis. I followed this:
https://timtrice.github.io/backtesting-strategies/
parameter-optimization.html to add the distribution and run the param set.
I got it to work but the results are the same for each parameter which is
wrong even though it indicates it is using a different parameter value each
time. There really isn't any error or debugging information I can use to
figure this out but I believe the issue is with where/how I'm applying the
indicators/signals which makes it stuck on a single parameter value. This
also could be from the location of the added distribution in the code but
I'm not sure. Does anyone have any guidance? Below is the portion of
interest and attached is the full code.

#DISTRIBUTIONS
sdb = c(1.5,1.75,2,2.25,2.5)
RSIhigh = 70
RSIlow = 30

add.distribution(strategy.st,
                 paramset.label = "BBands",
                 component.type = "indicator",
                 component.label = "BBands",
                 variable = list(n = sdb),
                 label = "RBBands")

#INDICATORS
add.indicator(strategy = strategy.st,
              name = "BBands",
              arguments = list(HLC = quote(HLC(mktdata)),
                               n = 20, maType = "SMA", "RBBands"),
              label = "BBands")

add.indicator(strategy = strategy.st,
              name = "RSI",
              arguments = list(price = quote(Cl(mktdata)), n = 14,
                               maType = "WMA"),
              label = "rsi")

applyIndicators(strategy = strategy.st, mktdata=OHLC(SPY))
colnames(mktdata) <- c("SPY.Open","SPY.High","SPY.
Low","SPY.Close","dn.BBands","mavg.BBands","up.BBands","pctB.BBands","rsi")


#SIGNALS
add.signal(strategy = strategy.st,
           name="sigFormula",
           arguments = list(label = "Cl.gt.UpperBB.RSI",
           data = mktdata,
           formula = "(SPY.Close >= up.BBands & rsi >= RSIhigh)",
           cross = FALSE),
           label = "Cl.gt.UpperBB.RSI")

add.signal(strategy = strategy.st,
           name="sigFormula",
           arguments = list(label = "Cl.lt.LowerBB.RSI",
           data = mktdata,
           formula = "(SPY.Close <= dn.BBands & rsi <= RSIlow)",
           cross = FALSE),
           label = "Cl.lt.LowerBB.RSI")

applySignals(strategy = strategy.st,  mktdata)


#RULES
add.rule(strategy = strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "Cl.gt.UpperBB.RSI",
                          sigval = TRUE,
                          orderqty = "all",
                          ordertype = "market",
                          orderside = NULL,
                          threshold = NULL),
         type = "exit")

add.rule(strategy = strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "Cl.lt.LowerBB.RSI",
                          sigval = TRUE,
                          orderqty = 500,
                          ordertype = "market",
                          orderside = NULL,
                          threshold = NULL),
         type = "enter")


#APPLY STRAT
cwd <- getwd()
results_file <- paste("results", strategy.st, "RData", sep = ".")
if( file.exists(results_file) ) {
  load(results_file)
} else {
  results <- apply.paramset(strategy.st,
                            paramset.label = "BBands",
                            portfolio.st = portfolio.st,
                            account.st = account.st,
                            nsamples = 5)
  if(checkBlotterUpdate(portfolio.st, account.st, verbose = TRUE)) {
    save(list = "results", file = results_file)
    save.strategy(strategy.st)
    }
}
setwd(cwd)




Thanks for your help,
Colton Smith
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20160824/2a9b4957/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: BBRSI2.R
Type: application/octet-stream
Size: 7060 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20160824/2a9b4957/attachment.obj>


More information about the R-SIG-Finance mailing list