使用CSV文件在quanstrat中回测SMA交叉

时间:2017-04-25 18:37:50

标签: r quantstrat back-testing

我之前已经使用getSymbols()回溯了一项策略,但文档并不清楚如何使用CSV。我试图在一些日内数据上测试SMA交叉。在线搜索后,我确实发现你必须给quantstrat一个xts对象来使用,这就是我所做的:

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

初始变量:

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)

添加SMA指标:

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.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')

按如下方式应用它们:

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

根据信号添加规则:

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")

下一部分是当代码进入某种循环而永远不会被执行时,不提供任何调试信息,在jupyter笔记本中,它表示为In[*]:。数据集不是很大,大约有8万行。

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

编辑:道歉,因为我忘了将最后一部分代码包含在原始问题中。 帮助感谢!

1 个答案:

答案 0 :(得分:0)

add.rule次来电都有几个问题。让我们先看看你的入门规则。我已将其格式化,因此更容易阅读,并对问题进行了评论。

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")

正确的add.rule来电应该是这样的:

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")

现在让我们来看看退出规则:

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"))

所以我们要做的就是将type参数移动到正确的函数:

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