Quantstrat应用策略失败

时间:2017-05-13 15:48:50

标签: r quantstrat

我正在使用quantstart但由于某种原因,applyStrategy函数不会吐出任何信息。它只是通过。结果,当我执行tradeStats函数时,我得到null。这是我使用的代码:谢谢!

library(quantstrat)
library(quantmod)
initDate = "1999-01-01"
from = "2003-01-01"
to = "2015-12-31"

Sys.setenv (TZ = "UTC")
currency ("USD")
getSymbols ("SPY", from = from,
 to = to, src = "yahoo",
 adjust = TRUE)

tradesize <- 100000
initeq <- 100000
strategy.st <- portfolio.st <- account.st <- "firststrat"
rm.strat(strategy.st)
initPortf(portfolio.st,
 symbols = "SPY",
 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)

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

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

test4 <- applyIndicators(strategy = strategy.st, mktdata) 

add.signal(strategy.st,
 name = "sigCrossover",
 arguments = list(columns = c("SMA50", "SMA200"),
 relationship = "gt"),
 label = "longfilter")

add.signal(strategy.st,
 name = "sigComparison",
 arguments = list(columns = c("SMA50", "SMA200"),
 relationship = "lt" ),
 label = "filterexit")

test4 <- applySignals(strategy.st, mktdata)

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

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



applyStrategy(strategy.st, portfolio.st)

updatePortf(portfolio.st)
daterange <- time(getPortfolio(portfolio.st)$summary)[-1]

updateAcct(account.st, daterange)
updateEndEq(account.st)

tradeStats(Portfolios = portfolio.st)

```

1 个答案:

答案 0 :(得分:4)

以下是您的问题:

1)第3行的initDate initdate应保持一致。 2)不要将orderqty = "all"用于“输入”类型规则。使用实际数量。请参阅下面的条目添加规则。下面是您可能希望代码运行的方式。作为一般规则,请使用orderqty = "all"作为退出/停止/获利类型规则。当你想要离开现有仓位时,“全部”是指当时未指明该仓位的位置(如果您将交易金额化为期望的敞口,您可能事先不知道)

3)如果您未在调用date <中设置POSIXct参数,则可能会遇到与index.classgetSymbols对象比较相关的其他错误/ p>

4)您没有为SPY定义仪器对象。即您发布的代码缺少stock("SPY", currency = "USD")

5)您的代码不能按原样重现。这样的电话:

test4 <- applyIndicators(strategy = strategy.st, mktdata)

只有在你获得了marketdata对象(在调用applyStrategy之后存在)时才能工作。

此代码的编辑形式有效:

library(quantstrat)
library(quantmod)
initdate = "1999-01-01"
from = "2003-01-01"
to = "2015-12-31"

currency ("USD")
stock("SPY", currency = "USD")

Sys.setenv (TZ = "UTC")

getSymbols ("SPY", from = from,
            to = to, src = "yahoo",
            adjust = TRUE,
            index.class=c("POSIXt","POSIXct"))

tradesize <- 100000
initeq <- 100000
strategy.st <- portfolio.st <- account.st <- "firststrat"
rm.strat(strategy.st)
initPortf(portfolio.st,
          symbols = "SPY",
          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)

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

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

add.signal(strategy.st,
           name = "sigCrossover",
           arguments = list(columns = c("SMA50", "SMA200"),
                            relationship = "gt"),
           label = "longfilter")

add.signal(strategy.st,
           name = "sigComparison",
           arguments = list(columns = c("SMA50", "SMA200"),
                            relationship = "lt" ),
           label = "filterexit")

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

add.rule(strategy.st, name = "ruleSignal",
         arguments = list(sigcol = "longfilter", sigval = TRUE,
                          orderqty = tradesize, ordertype = "market",
                          orderside = "long", replace = FALSE,
                          prefer = "Open"),
         type = "enter")



applyStrategy(strategy.st, portfolio.st)

updatePortf(portfolio.st)
daterange <- time(getPortfolio(portfolio.st)$summary)[-1]

updateAcct(account.st, daterange)
updateEndEq(account.st)

tradeStats(Portfolios = portfolio.st)

最后一个提示。在运行要重现的代码之前重新启动一个干净的r会话,然后通过运行源检查它。我在上面列出的许多问题都可以通过在新的干净R会话中运行源代码来发现。 (如果使用Rstudio,可以使用快捷键ctrl + shift + f10轻松完成。