r - 在 quantstrat 中生成不同周期的指标

标签 r indicator quantstrat periodicity

我想使用与我正在使用的数据不同的时间框架指标。我已经看到这个问题问了几次,但目前还没有解决方案(至少对我来说)。

下面的示例使用每日股票数据,但实际项目使用日内货币数据。我现在可以轻松地导入日内 csv 数据,因此示例和现实世界应该足够互换。

library(quantstrat)
initDate="2000-01-01"
from="2003-01-01"
to="2016-12-31"

#set account currency and system timezone
currency('USD')
Sys.setenv(TZ="UTC")

#get data
symbols <- "SPY"
getSymbols(symbols, from=from, to=to, src="yahoo", adjust=TRUE)
stock(symbols, "USD")

#trade sizing and initial equity settings
tradeSize <- 100000
initEq <- tradeSize*length(symbols)

#set up the portfolio, account and strategy
strategy.st <- portfolio.st <- account.st <- "mtf.strat"
rm.strat(strategy.st)
initPortf(portfolio.st, symbols=symbols, 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)

#SMA length
nSMA <- 14

添加 SMA 作为,在这种情况下,每日指标是一种享受
add.indicator(strategy.st, name="SMA",
              arguments=list(x=quote(Cl(mktdata)), n=nSMA, maType = "SMA"),
              label="SMA")
test <- applyIndicators(strategy.st, mktdata=OHLC(SPY))

然而试图添加,在这种情况下,每周 SMA
add.indicator(strategy.st, name="SMA",
              arguments=list(x=quote(to.period(Cl(mktdata), period = "weeks", k = 1, indexAt = "startof")), n=nSMA, maType = "SMA"),
              label="SMAw1")
## Or this    
add.indicator(strategy.st, name="SMA",
              arguments=list(x=quote(to.weekly(Cl(mktdata))), n=nSMA, maType = "SMA"),
              label="SMAw1")
test <- applyIndicators(strategy.st, mktdata=OHLC(SPY))
# Error in runSum(x, n) : ncol(x) > 1. runSum only supports univariate 'x'

不使用 Cl(x) 直接调用关闭列导致相同的错误。我这样做是TTR:::runSum如果给出多于一列的数据,将抛出上述错误。

我不完全确定问题出在哪里,所以一些帮助会很棒。

最佳答案

问题是 to.period (因此 to.weekly )返回 OHLC 对象,而不是像 TTR::SMA 这样的单变量系列预计。所以你需要包装to.period的输出在 Cl .

add.indicator(strategy.st, name="SMA",
              arguments=list(x=quote(Cl(to.weekly(Cl(mktdata)))), n=nSMA, maType = "SMA"),
              label="SMAw1")
test <- applyIndicators(strategy.st, mktdata=OHLC(SPY))

现在代码运行了,但它可能仍然是您的策略的问题。会有很多NA当该指标与每日 mktdata 合并时.
R> tail(merge(SPY, test$SMA))
           SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted SMA.SMAw1
2016-11-25   221.10   221.56  221.01    221.52   37861800       221.52  215.0720
2016-11-28   221.16   221.48  220.36    220.48   70284100       220.48        NA
2016-11-29   220.52   221.44  220.17    220.91   67079400       220.91        NA
2016-11-30   221.63   221.82  220.31    220.38   99783700       220.38        NA
2016-12-01   220.73   220.73  219.15    219.57   77230500       219.57        NA
2016-12-02   219.67   220.25  219.26    219.68   70863400       219.68  215.3207

因此,最好创建自己的 SMA 包装函数来处理所有这些步骤。然后调用add.indicator使用你的包装函数。
mySMA <- function(x, on = "days", k = 1, n = 10) {
  agg <- x[endpoints(x, on, k)]
  sma <- SMA(agg, n)
  # merge with zero-width xts object w/original index, filling NA
  result <- merge(sma, xts(,index(x)), fill = na.locf)
  return(result)
}
add.indicator(strategy.st, name = "mySMA",
              arguments = list(x = quote(Cl(mktdata)),
                               on = "weeks",
                               n = nSMA),
              label = "SMAw1")
test <- applyIndicators(strategy.st, mktdata = OHLC(SPY))

现在,该指标将为 mktdata 中的每个观察值都有一个值。当它合并时。
> tail(merge(SPY, test$SMA))
           SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted SMA.SMAw1
2016-11-25   221.10   221.56  221.01    221.52   37861800       221.52  215.0720
2016-11-28   221.16   221.48  220.36    220.48   70284100       220.48  215.0720
2016-11-29   220.52   221.44  220.17    220.91   67079400       220.91  215.0720
2016-11-30   221.63   221.82  220.31    220.38   99783700       220.38  215.0720
2016-12-01   220.73   220.73  219.15    219.57   77230500       219.57  215.0720
2016-12-02   219.67   220.25  219.26    219.68   70863400       219.68  215.3207

关于r - 在 quantstrat 中生成不同周期的指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40857610/

相关文章:

r - 在 dplyr 中按行计算和正常计算之间切换

r - RQuantstrat代码中的while循环-如何使其更快?

R - FinancialInstrument Package 使用股票时更改符号名称

r - 在 R 中按簇绘制 OHLC 簇的最佳方法

用于从其他值列相同的一列中提取值的sql查询

xsd - 包含 <xs :all> and <xs:any>? 的复杂类型的 XML 模式

android webview客户端 Activity 指示器

r - quantstrat:如何在同一柱上执行?

r - 如何从 git_time 对象获取日期和时间?

tabs - 如何自定义 Material UI Tabs 指示器的宽度和位置