首页 > 解决方案 > R quantstrat applySignals 错误 - `dimnames<-.xts`(`*tmp*`, value = dn) 中的错误:'dimnames' [2] 的长度不等于数组范围

问题描述

我正在尝试对我的股票数据和我创建的附加过滤规则使用 applySignals 函数,因为 TTR 包不提供简单的过滤规则。

当我使用 applyIndi​​cators 函数时,我可以看到过滤器(每日返回)出现了一个额外的列,所以我认为这很有效。但是,当我基于此过滤器添加长短信号并使用 applySignals 函数时,出现以下错误:

test <- applySignals(strategy = strategy.st, mktdata = test_init) dimnames<-.xts( *tmp*, value = dn) 中的错误:'dimnames' [2] 的长度不等于数组范围此外:警告消息:在 match.names(column, colnames(data)) : 所有不在 OTRK 过滤器中的列。打开 OTRK.High OTRK.Low OTRK.Close FILTER.filter longfilter

我使用了相同的示例和来自提供代码的专业人士的代码,但它似乎对我不起作用。(第9.3章简单过滤规则:https ://bookdown.org/kochiuyu/technical-analysis-with-r-second-edition/simple-filter-rule.html )

我是 R 新手,所以我不太了解错误消息。提前致谢!

就错误而言,这是我的代码:



install.packages("quantstrat")
install.packages("TTR")

## Library
library("quantstrat")
library("TTR")

## Quantsrat not directly available: solution
install.packages("devtools")
require(devtools)
install_github("braverock/blotter") # dependency
install_github("braverock/quantstrat")

###############################################################################################################################
###############################################################################################################################

initdate <- "2003-12-15"
from <- "2018-12-15"
to <- "2021-04-07"

Sys.setenv(TZ = "UTC")
currency("USD")
getSymbols("OTRK", from = from, to = to, src= "yahoo", adjust = TRUE)
stock("OTRK", currency = "USD", multiplier=1)

tradesize <- 1000
initeq <- 1000

#txnfees <- -10
#orderqty <- 100
#nsamples <- 5


#Parameters indicators#

filterthreshold <- 0.05

#Initialise#
strategy.st <- portfolio.st <- account.st <- "FilterRule"
rm.strat(strategy.st)

initPortf(portfolio.st, symbols = "OTRK", initDate = initdate, currency = "USD")
initAcct(account.st, portfolios = portfolio.st, initDate = initdate, currency = "USD", initEq = initeq)
initOrders(portfolio.st, symbols = "OTRK", initDate = initdate)
strategy(strategy.st, store = TRUE)

#Adding indicators#

FILTER <- function(price) {
  lagprice <- lag(price,1)
  temp<- price/lagprice - 1
  colnames(temp) <- "FILTER"
  return(temp)
}


add.indicator(
  strategy=strategy.st,
  name = "FILTER", 
  arguments = list(price = quote(Cl(mktdata))), 
  label= "filter")




#Check test indicators work#
test_init <- applyIndicators(strategy = strategy.st, mktdata = OHLC(OTRK))
head(test_init, n=7)
tail(test_init, n=7)

#Adding signals#

add.signal(strategy.st, 
           name="sigThreshold",
           arguments = list(threshold= filterthreshold,   
                            column="filter",
                            relationship="gt",   
                            cross=TRUE),
           label="longfilter")

add.signal(strategy.st, 
           name="sigThreshold",
           arguments = list(threshold= - filterthreshold, 
                            column="filter",
                            relationship="lt",
                            cross=TRUE),
           label="shortfilter")




#Test signals on data
test <- applySignals(strategy = strategy.st, mktdata = test_init)

标签: rquantstrat

解决方案


推荐阅读