r quantStrat样板.R

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r quantStrat样板.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")

以上是关于r quantStrat样板.R的主要内容,如果未能解决你的问题,请参考以下文章

Quantitative Trading with R:两个简单的策略

如何安装R语言中的goseq包

如何在 quantstrat 中使用 sigPeak() 函数

quantstrat:尽管信号值似乎有效,但策略不采取立场?

是否有 React/Flux 样板项目或生成器? [关闭]

R Markdown与RStudio IDE深度结合