0

I'm trying to summarize two xts object. Code for example Code 1. But I get strange results. You can see in the screenshot duplicate dates. If to execute this code (Code 2), everything works fine. What am I doing wrong? This code should work on any strategy.

applyStrategy(strategy = qs.strategy, portfolios = qs.portfolio)

updatePortf(qs.portfolio)
updateAcct(qs.account)
updateEndEq(qs.account)

# tstats <- tradeStats(qs.portfolio)
tstats <- tradeStatsEx(qs.portfolio)

Code 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)
}

Code 2

bar <- xts(1:10, order.by=as.Date(1:10)) 
bar2 <- xts(1:10, order.by=as.Date(1:10)) 
bar3 <- bar + bar2

Code 1 result

Code 2 result enter image description here

enter image description here

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
user45245
  • 845
  • 1
  • 8
  • 18
  • Please provide a [reproducible example](http://stackoverflow.com/q/5963269/271616). Pictures of data are not helpful. – Joshua Ulrich Jul 20 '16 at 13:05
  • That's not reproducible. It throws an error: "Error in is.strategy(strategy) : object 'qs.strategy' not found". Run `saveRDS(getPortfolio(qs.portfolio), "portfolio.rds")` and upload `portfolio.rds` to a file sharing site so I can create a minimal reproducible example. – Joshua Ulrich Jul 20 '16 at 13:24
  • @JoshuaUlrich: https://1drv.ms/u/s!Ap74qskXBh9OgdNEilofXaO-qligVQ – user45245 Jul 20 '16 at 13:52
  • This is not a problem with xts. Your `posPL` tables for all 3 instruments have two entries per day on most days. Something upstream is causing 2 transactions to be created on each day. – Joshua Ulrich Jul 20 '16 at 15:54

0 Answers0