在 Quantstrat 中使用 add.indicator() 时出错
Error with using add.indicator() in Quantstrat
我对这个问题真的很绝望,我还没有找到任何解决办法。如果我删除了对 add.indicator applyIndicators 的最后一次调用,因此回测将起作用,但这样我总是 运行 在 运行Sum 错误消息中。
有人有想法吗?
# 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'
该错误表明您的 x 变量是多变量的。如果你 运行 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
问题源于您的第一个指标中的标签“SMAClose”。 quantmod 的 Cl()
函数使用 grep 和模式“收盘价”来识别收盘价。当您尝试创建您的第 4 个指标时,您的 mktdata 对象中有 2 个名称为“Close”的列...所以两者都被返回并且 SMA 错误出现。简单地将第一个指标命名为 "SMACl20"
或任何没有“关闭”一词的名称都可以很好地用于您的上述代码。 HTH.
我对这个问题真的很绝望,我还没有找到任何解决办法。如果我删除了对 add.indicator applyIndicators 的最后一次调用,因此回测将起作用,但这样我总是 运行 在 运行Sum 错误消息中。
有人有想法吗?
# 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'
该错误表明您的 x 变量是多变量的。如果你 运行 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
问题源于您的第一个指标中的标签“SMAClose”。 quantmod 的 Cl()
函数使用 grep 和模式“收盘价”来识别收盘价。当您尝试创建您的第 4 个指标时,您的 mktdata 对象中有 2 个名称为“Close”的列...所以两者都被返回并且 SMA 错误出现。简单地将第一个指标命名为 "SMACl20"
或任何没有“关闭”一词的名称都可以很好地用于您的上述代码。 HTH.