我正在尝试总结两个xts对象。代码示例代码1.但我得到奇怪的结果。您可以在屏幕截图中看到重复日期。如果要执行此代码(代码2),一切正常。我究竟做错了什么?此代码应适用于任何策略。
applyStrategy(strategy = qs.strategy, portfolios = qs.portfolio)
updatePortf(qs.portfolio)
updateAcct(qs.account)
updateEndEq(qs.account)
# tstats <- tradeStats(qs.portfolio)
tstats <- tradeStatsEx(qs.portfolio)
代码1
tradeStatsEx <-
function(Portfolios, Symbols, use = c("txns", "trades"), tradeDef = "flat.to.flat",
inclZeroDays = FALSE)
{
ret <- NULL
tradeDef <- tradeDef[1]
for (Portfolio in Portfolios) {
pname <- Portfolio
Portfolio <- .getPortfolio(pname)
if (missing(Symbols))
symbols <- ls(Portfolio$symbols)
else symbols <- Symbols
Equity <- equity.function(Portfolio, symbols)
}
return(ret)
}
equity.function <- function(Portfolio, Symbols)
{
equity <- NULL
for (symbol in Symbols) {
posPL <- Portfolio$symbols[[symbol]]$posPL
posPL <- posPL[-1, ]
if(is.null(equity)){
equity <- posPL$Net.Trading.PL
}
else{
equity <- equity + posPL$Net.Trading.PL
}
}
return(equity)
}
代码2
bar <- xts(1:10, order.by=as.Date(1:10))
bar2 <- xts(1:10, order.by=as.Date(1:10))
bar3 <- bar + bar2
代码1结果