[英]Error with using add.indicator() in Quantstrat
I am really despairing of this problem and I haven't found any solution.我真的对这个问题感到绝望,我还没有找到任何解决方案。 If i remove the last call to add.indicator applyIndicators and thus the backtest will function, but this way I always run in the runSum error message.
如果我删除了对 add.indicator applyIndicators 的最后一次调用,因此回测将是 function,但这样我总是在 runSum 错误消息中运行。
Someone with an idea?有想法的人?
# load necessary packages
library(quantstrat)
library(quantmod)
# initialize basic settings
initdate = "1999-01-01"
from = "2003-01-01"
to = "2015-12-31"
currency("USD")
stock("SPY", currency = "USD")
Sys.setenv(TZ = "UTC")
tradesize <- 100000
initeq <- 100000
strategy.st <- portfolio.st <- account.st <- "firststrat"
# load market data from yahoo finance
getSymbols ("SPY", from = from,
to = to, src = "yahoo",
adjust = TRUE,
index.class=c("POSIXt","POSIXct"))
# initialize portfolio, account, orders and strategy
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 the strategy's backbone: indicators, signals and rule
add.indicator(strategy = strategy.st,
name = "SMA",
arguments = list(x = quote(Cl(mktdata)), n = 20),
label = "SMAClose")
add.indicator(strategy = strategy.st,
name = "SMA",
arguments = list(x = quote(Op(mktdata)), n = 20),
label = "SMAOpen")
add.indicator(strategy = strategy.st,
name = "SMA",
arguments = list(x = quote(Hi(mktdata)), n = 200),
label = "SMA200")
add.indicator(strategy = strategy.st,
name = "SMA",
arguments = list(x = quote(Cl(mktdata)), n = 10),
label = "SMA10")
applyIndicators(strategy.st, SPY)
Fehler in runSum(x, n) : ncol(x) > 1. runSum only supports univariate 'x'
The error indicates your x variable is multivariate.该错误表明您的 x 变量是多变量的。 If you run
head(x)
you will see the series'如果你运行
head(x)
你会看到这个系列
Browse[1]> head(x)
SPY.Close SMA.SMAClose
2003-01-02 70.37356 NA
2003-01-03 70.58992 NA
2003-01-06 71.83404 NA
2003-01-07 71.65631 NA
2003-01-08 70.62083 NA
2003-01-09 71.71812 NA
The problem stems from the label "SMAClose" in your first indicator.问题源于您的第一个指标中的 label“SMAClose”。 The
Cl()
function from quantmod uses grep and a pattern "Close" to identify Close prices.来自 quantmod 的
Cl()
function 使用 grep 和模式“Close”来识别收盘价。 By the time you try to create your 4th indicator, you have 2 columns with the name "Close" in your mktdata object...so both are returned and SMA errors out.当您尝试创建第 4 个指标时,您的 mktdata object 中有 2 个名为“Close”的列...所以两者都返回并且 SMA 错误输出。 Simply naming the first indicator
"SMACl20"
or anything without the word "Close" will work fine for your above code.只需将第一个指标命名为
"SMACl20"
或任何不带“Close”一词的名称即可适用于您的上述代码。 HTH. HTH。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.