【发布时间】:2016-10-28 19:34:29
【问题描述】:
我在编写用于quanstrat::add.indicator 的自定义指标函数时遇到问题
错误:
Error in inherits(x, "xts") : object 'price' not found
我的代码:
library(quantstrat)
symbols<-getSymbols("USD/EUR",src="oanda")
strat<-acct<-portfolio<-"tempTest"
initEq<-1000
initDate <- '2009-12-31'
currency("USD")
exchange_rate(symbols, currency="USD")
rm.strat(strat) # remove portfolio, account, orderbook if re-run
initPortf(name=portfolio, symbols, initDate=Sys.Date())
initAcct(name=acct, portfolios=portfolio,initDate=Sys.Date(), initEq=initEq)
initOrders(portfolio=portfolio, initDate=Sys.Date())
strategy(strat, store=TRUE)
colnames(USDEUR)<-"Close"
#################################################################################################
RSI.lagged<-function(lag=1,n=2,...){
RSI <- RSI(price)
RSI <- lag(RSI, lag)
out <- RSI$rsi
colnames(out) <- "rsi"
return(out)
}
########RSI indicator
####THIS IS LAGGED TO PREVENT FOREKNOWLEDGE
add.indicator(strat, name="RSI.lagged", arguments=list(price = quote(Cl(mktdata)), n=2), label="rsiIndLagged")
test <- applyIndicators(strat, mktdata=USDEUR)
将参数 price 添加到 RSI.lagged 函数后,例如 RSI.lagged<-function(lag=1,n=2,price,...) 我得到错误:
Error in `colnames<-`(`*tmp*`, value = "rsi") : attempt to set 'colnames' on an object with less than two dimensions
【问题讨论】:
-
add.indicator函数在哪里?您显示代码的唯一函数是RSI.lagged(如果您希望它在其中使用price,那么您应该传入一个名为price的参数),但您不会显示任何调用它的代码。 -
抱歉,
library(quantstrat)是必需的库。我已编辑我的代码以包含此内容。 -
我还是一头雾水——我相信您回复了我评论的前 5 个字,但忽略了其他 40 个字。还是我错过了什么?
-
我的印象是
...意味着add.indicatorarguments参数中提供的所有参数(如价格)将神奇地传递给函数 RSI.lagged。我是否误解了...的含义? -
您根本没有使用
add.indicator的...参数。它既有arguments参数(您正在使用)和...参数,但您没有使用...尽管文档并没有很清楚地说明两者之间的区别。
标签: r quantstrat