尝试使用applyIndicators函数学习quantstrat时遇到了一个有趣的错误。我有3个指标,lagATR,RSI和SMA应用于同一时期的相同数据。所有三个指标一起使用applyIndicators函数生成错误。任何一对指标都没有。代码如下。 此代码主要来自两个博客条目(part 1和part 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)
答案 0 :(得分:0)
伊利亚帮助了我。我从r-forge安装了最新版本的TTR,现在代码正常工作。 FWIW,我的TTR的上一版本是0.22。现在显示0.22-0.1