tjq
10/27/2017 - 5:38 PM

quantStrat boilerplate.R

library(quantmod)
library(quantstrat)

Sys.setenv(TZ="UTC")
currency('USD')

init.date <- "2010-01-01"
start.date <- "2010-01-01"
end.date <- "2017-09-30"
init.equity <- 1e4 # $10,000
adjustment <- TRUE

basic_symbols <- function() {
  symbols <- c(
    "SPY",
    "QQQ",
    "XLRN" # SPDR S&P 500 ETF Trust
  )
}

symbols <- basic_symbols()

getSymbols(Symbols = symbols,
           index.class = "POSIXct",
           from = start.date, 
           to = end.date, 
           adjust = adjustment)

stock(symbols, 
      currency = "USD", 
      multiplier = 1)

rm.strat(portfolio.st)
rm.strat(account.st)

portfolio.st <- "Port.Luxor"
account.st <- "Acct.Luxor"
strategy.st <- "Strat.Luxor"

initPortf(name = portfolio.st,
          symbols = symbols,
          initDate = init.date)

initAcct(name = account.st,
         portfolios = portfolio.st,
         initDate = init.date,
         initEq = init.equity)

initOrders(portfolio = portfolio.st,
           symbols = symbols,
           initDate = init.date)

strategy(strategy.st, store = TRUE)

add.indicator(strategy = strategy.st,
              name = "SMA",
              arguments = list(x = quote(Cl(mktdata)), 
                               n = 10),
              label = "nFast")

add.indicator(strategy = strategy.st, 
              name = "SMA", 
              arguments = list(x = quote(Cl(mktdata)), 
                               n = 30), 
              label = "nSlow")

add.signal(strategy = strategy.st,
           name="sigCrossover",
           arguments = list(columns = c("nFast", "nSlow"),
                            relationship = "gte"),
           label = "long")

add.signal(strategy = strategy.st,
           name="sigCrossover",
           arguments = list(columns = c("nFast", "nSlow"),
                            relationship = "lt"),
           label = "short")

add.rule(strategy = strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "long",
                          sigval = TRUE,
                          orderqty = 50,
                          ordertype = "stoplimit",
                          orderside = "long", 
                          threshold = 0.0005,
                          prefer = "High", 
                          TxnFees = 0, 
                          replace = FALSE),
         type = "enter",
         label = "EnterLONG")

add.rule(strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "short",
                          sigval = TRUE,
                          orderqty = -100,
                          ordertype = "stoplimit",
                          threshold = -0.005, 
                          orderside = "short", 
                          replace = FALSE, 
                          TxnFees = 0, 
                          prefer = "Low"),
         type = "enter",
         label = "EnterSHORT")

add.rule(strategy.st, 
         name = "ruleSignal", 
         arguments = list(sigcol = "short", 
                          sigval = TRUE, 
                          orderside = "long", 
                          ordertype = "market", 
                          orderqty = "all", 
                          TxnFees = 0, 
                          replace = TRUE), 
         type = "exit", 
         label = "Exit2SHORT")

add.rule(strategy.st, 
         name = "ruleSignal", 
         arguments = list(sigcol = "long", 
                          sigval = TRUE, 
                          orderside = "short", 
                          ordertype = "market", 
                          orderqty = "all", 
                          TxnFees = 0, 
                          replace = TRUE), 
         type = "exit", 
         label = "Exit2LONG")

results <- applyStrategy(strategy.st, portfolios = portfolio.st)

updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)

a <- getAccount(account.st)
equity <- a$summary$End.Eq
plot(equity, main = "Equity Curve")