0

I have backtested a strategy using getSymbols() before, but the docs are not clear how to use CSVs. I am trying to test SMA crossover on some intraday data. Having searched online I did find that you have to give quantstrat an xts object to work with, so that it what I have done:

XBT <- as.xts(read.zoo("XBT.csv", header = TRUE, sep = ","))
head(XBT)

                       OPEN     HIGH      LOW    CLOSE
2016-09-25 22:00:00 600.1650 600.2850 599.3190 599.4575
2016-09-25 22:01:00 599.4550 600.1605 599.2980 599.5125
2016-09-25 22:02:00 599.5101 601.0850 599.2945 600.1450
2016-09-25 22:03:00 600.2950 600.6150 599.3290 599.3350
2016-09-25 22:04:00 599.3350 600.1400 599.3350 599.6972
2016-09-25 22:05:00 599.6972 601.0751 599.2275 599.2565

Initial variables:

Sys.setenv(TZ = "UTC")
currency("USD")
stock("XBT", currency = "USD")
tradesize = 100000
initeq = 100000
strategy.st <- "firststrat"
portfolio.st <- "firststrat"
account.st <- "firststrat"
rm.strat(strategy.st)
initPortf(portfolio.st, symbols = "XBT")
initAcct(account.st, portfolios = portfolio.st, initEq = initeq)
initOrders(portfolio.st)
addPosLimit(portfolio.st, "XBT", start(XBT), 100)
strategy(strategy.st, store = TRUE)

Adding SMA indicators:

add.indicator(strategy.st, name = "SMA",
           arguments = list(x = quote(Cl(XBT)),
                            n = 360),
           label = "SMA360")


add.indicator(strategy.st, name = "SMA",
           arguments = list(x = quote(Cl(XBT)),
                            n = 60),
           label = "SMA60")

Add signals based on indicators above:

add.signal(strategy.st, name = "sigComparison", 
           arguments = list(columns = c("SMA60", "SMA360"),
                            relationship = 'gt'),
           label = 'longentry')
add.signal(strategy.st, name = "sigCrossover",
           arguments = list(columns = c("SMA60", "SMA360"),
                            relationship = 'lt'),
           label = 'longexit')

Apply them as below:

test_init <- applyIndicators(strategy.st, OHLC(XBT))
test <- applySignals(strategy = strategy.st, test_init)

Add rules based on signals:

add.rule(strategy.st, name = "ruleSignal", 
             arguments = list(sigcol = "longexit", sigval = TRUE, orderqty = "all", 
                            ordertype = "market", orderside = "long", 
                            replace = FALSE, prefer = "OPEN", type = "exit"))

add.rule(strategy = strategy.st, name = "ruleSignal",
             arguments = list(sigcol = "longentry", sigval = TRUE, ordertype = "market",
                             rderside = "long", replace = FALSE, prefer = "OPEN",
                             orderqty = "all", maxSize = tradesize),type = "enter")

The next part is when the code goes into some sort of a loop and never gets executed, providing no debugging information whatsoever, in jupyter notebook, it is denoted as In[*]:. The data set is not huge, it is around 80 thousand rows.

out <- applyStrategy(strategy = strategy.st, portfolios = portfolio.st)
updatePortf(portfolio.st)
daterange <- time(getPortfolio(portfolio.st)$summary)[-1]
updateAcct(account.st, daterange)
updateEndEq(account.st)
tstats <- tradeStats(Portfolios = portfolio.st)
tstats$Profit.Factor

EDIT: Apologies as I forgot to include the last part of code into the original question. Help Appreciated!

eliquinox
  • 33
  • 5
  • 1
    You're missing a closing parenthesis. Compare the last character in both your add.rule calls. – Joshua Ulrich Apr 26 '17 at 01:06
  • Thank for input, Joshua. I have corrected the second call of `add.rule()` to reflect the first, so that `type="enter"` is within the list of arguments. The problem stays. I cannot get the output for this strategy. – eliquinox Apr 26 '17 at 21:55

1 Answers1

1

There are several problems with both your add.rule calls. Let's look at your entry rule first. I have formatted it so it is easier to read, and commented the lines with issues.

add.rule(strategy = strategy.st, name = "ruleSignal",
         arguments = list(sigcol = "longentry",
                          sigval = TRUE,
                          ordertype = "market",
                          # typo: should be orderside
                          rderside = "long",
                          replace = FALSE,
                          prefer = "OPEN",
                          # "all" is not a valid entry quantity
                          orderqty = "all",
                          # maxSize is not an argument to ruleSignal
                          # not sure what you're trying to do here...
                          maxSize = tradesize),
         type = "enter")

A correct add.rule call should look something like this:

add.rule(strategy.st, name = "ruleSignal",
         arguments = list(sigcol = "longentry",
                          sigval = TRUE,
                          ordertype = "market",
                          orderside = "long",
                          replace = FALSE,
                          prefer = "OPEN",
                          # numeric order quantity for entry
                          orderqty = 100,
                          # set order sizing function to use position limits
                          osFUN = osMaxPos),
         type = "enter")

Now let's look at your exit rule:

add.rule(strategy.st, name = "ruleSignal", 
         arguments = list(sigcol = "longexit",
                          sigval = TRUE,
                          orderqty = "all", 
                          ordertype = "market",
                          orderside = "long",
                          replace = FALSE,
                          prefer = "OPEN",
                          # type is not an argument to ruleSignal;
                          # it's an argument to add.rule
                          type = "exit"))

So all we have to do is move the type argument to the correct function:

add.rule(strategy.st, name = "ruleSignal",
         arguments = list(sigcol = "longexit",
                          sigval = TRUE,
                          ordertype = "market",
                          orderside = "long",
                          replace = FALSE,
                          prefer = "OPEN",
                          orderqty = "all"),
         type = "exit")
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Thank you, Josh! It turns out that quantsrat suppresses output in Jupyter notebook. Ran in Rstudio and worked fine. Thanks for corrections! – eliquinox May 03 '17 at 14:57
  • @eliquinox: You're welcome; glad to help. Please see [What should I do when someone answers my question?](http://stackoverflow.com/help/someone-answers). – Joshua Ulrich May 03 '17 at 15:01