我通过R中的Quantstrat获得的结果存在问题。 我为一种交易策略运行了一个代码,该代码提供了正的每日平均回报,如下所示:
> mean(rets)
[1] 0.004187857
结果的总和也是正数:
> sum(rets)
[1] 6.315288
但是,当我绘制性能图表时,您会看到一个负的累积回报,如图所示:
那怎么可能? 也许我的代码会收取佣金吗?
请参考以下代码: (这是一个很长的代码,由于需要回答所有问题,因此我将其全部发布)注意:Quantstrat和blotter软件包是从GitHub下载的。 谢谢。
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(quantmod)
library(quantstrat)
library(TTR)
library(IKTrading)
rm(list = ls(.blotter), envir = .blotter)
initdate <- "2010-01-01"
from <- "2012-01-01" #start of backtest
to <- "2017-12-31" #end of backtest
Sys.setenv(TZ= "EST") #Set up environment for timestamps
currency("USD") #Set up environment for currency to be used
symbol <- c("RUT") #symbols used in our backtest
getSymbols("^RUT", src="yahoo", from = from, to = to, periodicity = "daily")
stock(symbol, currency = "USD", multiplier = 1) #tells quanstrat what instruments present and what currency to use
n <- 30
m <- 10
l <- 10
RUT$RUT.Close.Prev <- c(NA, coredata(RUT)[, "RUT.Close"][-nrow(RUT)])
RUT$Close.up <- ifelse(coredata(RUT$RUT.Close > RUT$RUT.Close.Prev), 1, 0)
RUT$Close.up <- ifelse(coredata(RUT$RUT.Close > RUT$RUT.Close.Prev), 1, 0)
UDVR <- rollapplyr(RUT, width = m, by.column = FALSE, FUN = function(window) {
upvol <- sum(as.numeric(coredata(window)[, "RUT.Volume"][coredata(window)[, "Close.up"] == 1]), na.rm = TRUE)
downvol <- sum(as.numeric(coredata(window)[, "RUT.Volume"][coredata(window)[, "Close.up"] == 0]), na.rm = TRUE)
upvol/downvol
})
UDVR[!is.finite(UDVR)] <- NA
UDVR <- na.locf(UDVR)
UDVR_MA <- SMA(UDVR, l)
UDVR_MA[is.na(UDVR_MA)] <- 1
RUT$RUT.Volume <- coredata(UDVR_MA)[, 1]
RUT <- RUT[, 1:6]
wma <- WMA(Vo(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 <- 1000 #default trade size
initeq <- 100000 #default initial equity in our portfolio
#naming strategy, portfolio and account
strategy.st <- "Str.firststrat"
portfolio.st <- "Poft.firststrat"
account.st <- "Acc.firststrat"
#removes old portfolio and strategy from environment
rm.strat(portfolio.st)
rm.strat(strategy.st)
rm.strat(account.st)
#initialize portfolio, account, orders and strategy objects
initPortf(name = portfolio.st, symbols = symbol, initDate = initdate, currency = "USD")
initAcct(name = account.st, portfolios = portfolio.st, initDate = initdate, currency = "USD", initEq = initeq)
initOrders(portfolio.st, initDate = initdate)
strategy(strategy.st, store=TRUE)
addPosLimit(portfolio.st, symbol, start(get(symbol)), maxpos = tradesize, longlevels = 1)
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 = "X1.CNOwma4", threshold = 0.6,
relationship = "gt", cross = TRUE),
label = "longthreshold")
add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "X1.CNOwma4", threshold = 0.6,
relationship = "lt", cross = TRUE),
label = "shortthreshold")
add.rule(strategy.st, name = "ruleSignal", label = "rule1",
arguments = list(sigcol = "shortthreshold", sigval = TRUE,
orderqty = 1000, ordertype = "market",
orderside = "long", replace = FALSE,
prefer = "Open", osFUN = osMaxPos),
type = "enter")
add.rule(strategy.st, name = "ruleSignal", label = "rule2",
arguments = list(sigcol = "longthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "long", replace = FALSE,
prefer = "Open"),
type = "exit")
add.rule(strategy.st, name = "ruleSignal", label = "rule3",
arguments = list(sigcol = "longthreshold", sigval = TRUE,
orderqty = 1000, ordertype = "market",
orderside = "short", replace = FALSE,
prefer = "Open", osFUN = osMaxPos),
type = "enter")
add.rule(strategy.st, name = "ruleSignal", label = "rule4",
arguments = list(sigcol = "shortthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "short", replace = FALSE,
prefer = "Open"),
type = "exit")
out <- applyStrategy(strategy = strategy.st, portfolios = portfolio.st)
updatePortf(portfolio.st)
daterange <- time(getPortfolio(portfolio.st)$summary)[-1]
updateAcct(account.st, daterange)
updateEndEq(account.st)
chart.Posn(Portfolio = portfolio.st, Symbol = symbol,
TA = c("add_SMA(n=15, col='blue')", "add_SMA(n=30, col='red')", "add_SMA(n=100, col='green')"))
add_TA(mktdata$X1.CNOwma4, col = "black", lwd = 1, name = "UDVR CNOwma4", type = "l")
rets <- PortfReturns(Account = account.st)
rownames(rets) <- NULL
charts.PerformanceSummary(rets, colorset = bluefocus)
rets_vector <- coredata(rets)[, 1]
答案 0 :(得分:0)
charts.PerformanceSummary
复合默认返回。在查看大多数交易策略时,这是不合理的,因为您不是在进行利润再投资或根据亏损减少头寸。
charts.PerformanceSummary(rets, colorset = bluefocus, geometric=FALSE)
应该消除复合。