首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Quantstrat中使用add.indicator()时出错

在Quantstrat中使用add.indicator()时出错
EN

Stack Overflow用户
提问于 2020-07-27 02:37:58
回答 1查看 35关注 0票数 0

我真的对这个问题感到绝望,而且我还没有找到任何解决方案。如果我删除了对add.indicator的最后一次调用,applyIndicators就会正常工作,但这样我总是会在runSum错误消息中运行。

有想法的人吗?

代码语言:javascript
复制
# 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'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-27 03:40:03

该错误指示您的x变量是多变量。如果运行head(x),您将看到该系列的

代码语言:javascript
复制
    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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63104180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档