我真的对这个问题感到绝望,而且我还没有找到任何解决方案。如果我删除了对add.indicator的最后一次调用,applyIndicators就会正常工作,但这样我总是会在runSum错误消息中运行。
有想法的人吗?
# 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'发布于 2020-07-27 03:40:03
该错误指示您的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和模式" Close“来标识收盘价。当您尝试创建第4个指示器时,在mktdata object...so中有两列名称为"Close“的列都被返回,并且SMA错误输出。只需将第一个指示器命名为"SMACl20"或不带"Close“一词的任何内容,就可以很好地用于上面的代码。HTH。
https://stackoverflow.com/questions/63104180
复制相似问题