У меня есть эта пользовательская функция индикатора (CloseRange), и она дает мне следующую ошибку. Если бы я использовал индикатор RSI вместо этого, стратегия работает нормально. Пользовательский индикатор работает нормально, если я использую applyIndicators или applySignals. Что-то не так с правилами? Я не могу понять, почему. Я новичок ie.
Сообщение об ошибке:
Error in NextMethod(.Generic) :
dims [product 1] do not match the length of object [2]
Traceback:
9.
NextMethod(.Generic)
8.
Ops.xts((orderqty + pos), PosLimit[, "MaxPos"])
7.
osMaxPos(data, timestamp, UnitSize, ordertype, orderside, portfolio,
symbol, ruletype, digits = 0)
6.
osFUN(strategy = strategy, data = mktdata, timestamp = timestamp,
orderqty = orderqty, ordertype = ordertype, orderside = orderside,
portfolio = portfolio, symbol = symbol, ... = ..., ruletype = ruletype,
orderprice = as.numeric(orderprice))
5.
(function (mktdata = mktdata, timestamp, sigcol, sigval, orderqty = 0,
ordertype, orderside = NULL, orderset = NULL, threshold = NULL,
tmult = FALSE, replace = TRUE, delay = 1e-04, osFUN = "osNoOp",
pricemethod = c("market", "opside", "active"), portfolio, ...
4.
do.call(ruleFun, .formals, envir = parent.frame(1))
3.
ruleProc(strategy$rules[[type]], timestamp = timestamp, path.dep = path.dep,
mktdata = mktdata, portfolio = portfolio, symbol = symbol,
ruletype = type, mktinstr = mktinstr, parameters = parameters,
curIndex = curIndex, ...)
2.
applyRules(portfolio = portfolio, symbol = symbol, strategy = strategy,
mktdata = mktdata, Dates = NULL, indicators = sret$indicators,
signals = sret$signals, parameters = parameters, ..., path.dep = TRUE,
rule.subset = rule.subset, debug = debug)
1.
applyStrategy(strategy = strategy.st, portfolios = portfolio.st)
Ниже приведены коды:
КОД СТАРТ
library(quantstrat)
Sys.setenv(TZ="UTC")
currency('USD')
init_date <- "2017-12-31"
start_date <- "2018-01-01"
end_date <- "2020-12-31"
init_equity <- 1e4
adjustment <- TRUE
basic_symbols <- function() {
symbols <- c(
"SPY"
)
}
symbols <- basic_symbols()
getSymbols(Symbols = symbols,
src = "yahoo",
index.class = "POSIXct",
from = start_date,
to = end_date,
adjust = adjustment)
stock(symbols,
currency = "USD",
multiplier = 1)
checkBlotterUpdate <- function(port.st = portfolio.st,
account.st = account.st,
verbose = TRUE) {
ok <- TRUE
p <- getPortfolio(port.st)
a <- getAccount(account.st)
syms <- names(p$symbols)
port.tot <- sum(
sapply(
syms,
FUN = function(x) eval(
parse(
text = paste("sum(p$symbols",
x,
"posPL.USD$Net.Trading.PL)",
sep = "$")))))
port.sum.tot <- sum(p$summary$Net.Trading.PL)
if(!isTRUE(all.equal(port.tot, port.sum.tot))) {
ok <- FALSE
if(verbose) print("portfolio P&L doesn't match sum of symbols P&L")
}
initEq <- as.numeric(first(a$summary$End.Eq))
endEq <- as.numeric(last(a$summary$End.Eq))
if(!isTRUE(all.equal(port.tot, endEq - initEq)) ) {
ok <- FALSE
if(verbose) print("portfolio P&L doesn't match account P&L")
}
if(sum(duplicated(index(p$summary)))) {
ok <- FALSE
if(verbose)print("duplicate timestamps in portfolio summary")
}
if(sum(duplicated(index(a$summary)))) {
ok <- FALSE
if(verbose) print("duplicate timestamps in account summary")
}
return(ok)
}
# Function: Close Range
CR <- function(HLC) {
cr1<-(Cl(HLC) - Lo(HLC)) / (Hi(HLC) - Lo(HLC)) * 100
colnames(cr1)<-"CloseRange1"
return(cr1)
}
###
rm(list = ls(.blotter), envir = .blotter)
strategy.st <- "Strat.CR"
portfolio.st <- "Port.CR"
account.st <- "Acc.CR"
rm.strat(portfolio.st)
rm.strat(account.st)
rm.strat(strategy.st)
initPortf(name = portfolio.st,
symbols = symbols,
initDate = init_date)
initAcct(name = account.st,
portfolios = portfolio.st,
initDate = init_date,
initEq = init_equity)
initOrders(portfolio = portfolio.st,
symbols = symbols,
initDate = init_date)
strategy(strategy.st, store = TRUE)
add.indicator(strategy.st, name = "CR",
arguments = list(HLC = quote(HLC(mktdata))),
label = "CloseRange")
add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "CloseRange1.CloseRange",
threshold = 50,
relationship = "lt",
cross = TRUE),
label = "longEntry")
add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "CloseRange1.CloseRange",
threshold = 90, relationship = "gt",
cross = TRUE), label = "longExit")
osInvestAll <- function (data, timestamp, orderqty, ordertype, orderside, equity, portfolio, symbol, ruletype, ..., initEq) {
datePos <- format(timestamp,"%Y-%m-%d")
updatePortf(portfolio.st,symbol,Dates=paste0(start(data), "/", datePos))
# After updating portfolio profit, we can extract the Net.Trading.PL earned up to datePos.
trading_pl <- sum(.getPortfolio(portfolio)$summary$Net.Trading.PL)
# The total equity in the strategy for this symbol (and this symbol only in isolation always, as this is how quantstrat by default works with applyStrategy)
equity <- init_equity + trading_pl
ClosePrice <- getPrice(data, prefer = "Close")[datePos]
UnitSize <- as.numeric(trunc(1 * equity / ClosePrice))
UnitSize <- osMaxPos(data, timestamp, UnitSize, ordertype, orderside, portfolio, symbol, ruletype, digits=0)
UnitSize
}
##Entry rule
add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longEntry", sigval = TRUE,
ordertype = "market", orderside ="long", replace = FALSE,
prefer = "Open", orderqty = 100000,osFUN="osInvestAll",
pctATR = pctATR, atrMod = "X"), type = "enter", path.dep = TRUE)
# Rebalance monthly 50-50 if have 2 instruments to trade
args(rulePctEquity)
add.rule(strategy.st, 'rulePctEquity',
arguments=list(rebalance_on='months',
trade.percent=1/length(symbols),
refprice=quote(
last(getPrice(mktdata)[paste('::',as.character(curIndex),sep='')][,1])
),
digits=0
),
type='rebalance',
label='rebalance'
)
# Set max position limit
args(addPosLimit)
posval <- init_equity/length(symbols)
for(symbol in symbols){
pos<-round((posval/first(getPrice(get(symbol)))),0)
addPosLimit(portfolio.st,symbol,init_date, maxpos=pos,minpos=-pos)
}
## Exit rUle
add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longExit", sigval = TRUE,
orderqty = "all", ordertype = "market", orderside = "long",
replace = FALSE, prefer = "Open"), type = "exit", path.dep = TRUE)
# Apply Strategy
t1 <- Sys.time()
out <- applyStrategy(strategy = strategy.st,
portfolios = portfolio.st)
t2 <- Sys.time()
print(t2 - t1)
updatePortf(portfolio.st)
dateRange <- time(getPortfolio(portfolio.st)$summary)[-1]
updateAcct(account.st,dateRange)
updateEndEq(account.st)
КОД КОНЕЦ