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

Backtesting SMA crossover in quanstrat using CSV files

我之前使用 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 notebook 中,它表示为 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

编辑: 抱歉,我忘了将代码的最后一部分包含在原始问题中。 帮助赞赏!

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

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

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