我会尽量清楚一些用户的指责。 我删除了最后一个问题,在这个问题中,我将尽力说明。 抱歉,插入的数据不足。
因此,我正在尝试通过Quantstrat
软件包来执行一项策略。
install.packages("quantstrat")
我的问题是出现以下错误:
`Error in `colnames<-`(`*tmp*`, value = seq(ncol(tmp_val))) :
attempt to set 'colnames' on an object with less than two dimensions`
当我尝试运行以下命令时:
> out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
如果我使用一个或多个已经由软件包TTR
定义的指标作为指标,我就没有这个问题。
仅当我尝试使用自定义指标时才出现此错误。 这是我使用的自定义指标的代码:
wma <- WMA(Cl(RUT), n=4, wts=c(1:4))
wmamaxt <- rollmaxr(wma, n, fill = NA)
wmamint <- - rollmaxr(- wma, n, fill = NA)
CNOwma <- function (RUT) {(wma - wmamint) / (wmamaxt - wmamint)}
请参考以下代码:
if (!require("TTR")) {
install.packages("TTR")
library(TTR)
}
if (!require("quantstrat")) {
if(!require("devtools")) {
install.packages("devtools")
require(devtools)
}
install_github("braverock/blotter") # dependency
install_github("braverock/quantstrat")
}
if (!require("IKTrading")){
install_github("IlyaKipnis/IKTrading", force=TRUE)
}
library(devtools)
library(quantmod)
library(quantstrat)
library(TTR)
library(png)
library(IKTrading)
install_github("braverock/blotter")
install_github("braverock/quantstrat")
install_github('IlyaKipnis/IKTrading')
initdate <- "2010-01-01"
from <- "2012-01-01" #start of backtest
to <- "2017-31-12" #end of backtest
Sys.setenv(TZ= "EST") #Set up environment for timestamps
currency("USD") #Set up environment for currency to be used
symbols <- c("RUT", "IXIC") #symbols used in our backtest
getSymbols(Symbols = symbols, src = "yahoo", from=from, to=to, adjust = TRUE) #receive data from google finance, adjusted for splits/dividends
stock(symbols, currency = "USD", multiplier = 1) #tells quanstrat what instruments present and what currency to use
wma <- WMA(Cl(RUT), n=4, wts=c(1:4))
wmamaxt <- rollmaxr(wma, n, fill = NA)
wmamint <- - rollmaxr(- wma, n, fill = NA)
CNOwma <- function (RUT) {(wma - wmamint) / (wmamaxt - wmamint)}
tradesize <-10000 #default trade size
initeq <- 100000 #default initial equity in our portfolio
strategy.st <- portfolio.st <- account.st <- "firststrat" #naming strategy, portfolio and account
#removes old portfolio and strategy from environment
rm.strat(portfolio.st)
rm.strat(strategy.st)
#initialize portfolio, account, orders and strategy objects
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)
add.indicator(strategy = strategy.st,
name = 'CNOwma',
arguments = list(x = quote(Cl(mktdata)), n=4),
label = 'CNOwma4')
add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "CNOwma4", threshold = 0.6,
relationship = "gt", cross = TRUE),
label = "longthreshold")
add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "CNOwma4", threshold = 0.6,
relationship = "lt", cross = TRUE),
label = "shortthreshold")
add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "long", replace = FALSE,
prefer = "Open"),
type = "enter")
add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "shortthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "long", replace = FALSE,
prefer = "Open"),
type = "exit")
add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "shortthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "short", replace = FALSE,
prefer = "Open"),
type = "enter")
add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "short", replace = FALSE,
prefer = "Open"),
type = "exit")
out <- applyStrategy(strategy = strategy.st, portfolios = portfolio.st)
当我运行错误的traceback()时,这就是我得到的:
> traceback()
4: stop("attempt to set 'colnames' on an object with less than two dimensions")
3: `colnames<-`(`*tmp*`, value = seq(ncol(tmp_val)))
2: applyIndicators(strategy = strategy, mktdata = mktdata, parameters = parameters,
...)
1: applyStrategy(strategy = strategy.st, portfolios = portfolio.st)
答案 0 :(得分:1)
很难说出是怎么回事,因为您的示例不可复制。首先,您的三个软件包不是CRAN软件包,因此我们只能猜测您从何处获得了它们。我的猜测如下:
install_github("braverock/blotter")
install_github("braverock/quantstrat")
install_github('IlyaKipnis/IKTrading')
然后,如果我们猜对了,那么在示例的第一行(加载软件包之后),您将使用以太坊的对象:
wma <- WMA(Cl(mktdata), 4, wts=c(1:4))
那么mktdata
是哪里来的?
如果您创建可以由尝试帮助您的人运行的示例,您将获得更好的帮助。