0

尝试使用 applyIndi​​cators 函数学习 quantstrat 时遇到了一个有趣的错误。我有 3 个指标,lagATR、RSI 和 SMA 在同一时期应用于相同的数据。所有三个指标一起使用 applyIndi​​cators 函数生成错误。任何一对指标都没有。代码如下。此代码主要取自两个博客条目(第 1部分和第 2 部分)。该代码的结构是初始化一些变量,获取 XLB 的价格数据。然后它运行 4 个场景:所有 3 个指标,然后是每一对。我希望能帮助我理解为什么所有 3 个指标都会产生错误,而这对不会产生错误。谢谢。


#Part 1 from http://www.r-bloggers.com/nuts-and-bolts-of-quantstrat-part-i/
require(IKTrading)
require(quantstrat)
require(PerformanceAnalytics)

initDate="1990-01-01"
from="2003-01-01"
to="2012-12-31"
options(width=70)

currency('USD')
Sys.setenv(TZ="UTC")

options("getSymbols.warning4.0"=FALSE)
rm(list=ls(.blotter), envir=.blotter)
symbols <- "XLB" #SPDR Materials sector
getSymbols(symbols, from=from, to=to, src="yahoo", adjust=TRUE)
stock(symbols, currency="USD", multiplier=1)


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



#Part 2 from http://www.r-bloggers.com/nuts-and-bolts-of-quantstrat-part-ii/
#parameters
pctATR <- .02
period <- 10
atrOrder <- TRUE

nRSI <- 2
buyThresh <- 20
sellThresh <- 80
nSMA <- 200

#FIRST RUN - TRY ALL 3 INDICATORS: 
strategy.st <- portfolio.st <- account.st <- "DollarVsATRos"
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)
#indicators
add.indicator(strategy.st, name="lagATR", 
              arguments=list(HLC=quote(HLC(mktdata)), maType="SMA", n=period), 
              label="atrX")
add.indicator(strategy.st, name="RSI",
              arguments=list(price=quote(Cl(mktdata)), maType="SMA", n=nRSI),
              label="rsi")
add.indicator(strategy.st, name="SMA",
              arguments=list(x=quote(Cl(mktdata)), n=nSMA),
              label="sma")
test <- applyIndicators(strategy.st, mktdata=OHLC(XLB))
head(test,12)

#SECOND RUN - TRY lagATR and RSI INDICATORS: 
strategy.st <- portfolio.st <- account.st <- "DollarVsATRos"
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)
#indicators
add.indicator(strategy.st, name="lagATR", 
              arguments=list(HLC=quote(HLC(mktdata)), maType="SMA", n=period), 
              label="atrX")
add.indicator(strategy.st, name="RSI",
              arguments=list(price=quote(Cl(mktdata)), maType="SMA", n=nRSI),
              label="rsi")
test <- applyIndicators(strategy.st, mktdata=OHLC(XLB))
head(test,12)

#THIRD RUN - TRY RSI and SMA INDICATORS: 
strategy.st <- portfolio.st <- account.st <- "DollarVsATRos"
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)
#indicators
add.indicator(strategy.st, name="RSI",
              arguments=list(price=quote(Cl(mktdata)), maType="SMA", n=nRSI),
              label="rsi")
add.indicator(strategy.st, name="SMA",
              arguments=list(x=quote(Cl(mktdata)), n=nSMA),
              label="sma")
test <- applyIndicators(strategy.st, mktdata=OHLC(XLB))
head(test,12)

#FOURTH RUN - TRY lagATR and SMA INDICATORS: 
strategy.st <- portfolio.st <- account.st <- "DollarVsATRos"
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)
#indicators
add.indicator(strategy.st, name="lagATR", 
              arguments=list(HLC=quote(HLC(mktdata)), maType="SMA", n=period), 
              label="atrX")
add.indicator(strategy.st, name="SMA",
              arguments=list(x=quote(Cl(mktdata)), n=nSMA),
              label="sma")
test <- applyIndicators(strategy.st, mktdata=OHLC(XLB))
head(test,12)

4

1 回答 1

0

伊利亚帮助了我。我从 r-forge 安装了最新版本的 TTR,代码现在可以工作了。FWIW,我以前的 TTR 版本是 0.22。现在它显示 0.22-0.1

于 2015-01-18T19:43:18.123 回答