0

我之前使用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

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

4

1 回答 1

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

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

add.rule(strategy.st, name = "ruleSignal",
         arguments = list(sigcol = "longexit",
                          sigval = TRUE,
                          ordertype = "market",
                          orderside = "long",
                          replace = FALSE,
                          prefer = "OPEN",
                          orderqty = "all"),
         type = "exit")
于 2017-04-27T16:19:58.173 回答