braverock / blotter

blotter provides transaction infrastructure for defining transactions, portfolios and accounts for trading systems and simulation. Provides portfolio support for multi-asset class and multi-currency portfolios. Actively maintained and developed.
112 stars 49 forks source link

perTradeStats with tradeDef="increased.to.reduced" breaks when open trade shares a timestamp with an unwind trade #57

Closed jaymon0703 closed 6 years ago

jaymon0703 commented 6 years ago

Found a potential edge case where if a transaction results in a position going through zero and the resulting open trade is part of the final open position at the end of the strategy, then we do not build the trades$Start vector appropriately...adding both time stamps as opposed to only the one which resulted in the open trade.

Solution (ln 205) is to reference only the last matching timestamp from which(index(incrPos) == testdf$start_ts[first(which(is.na(testdf$end_ts)))]) which is the index of the opening portion of the relevant transaction taking the position through zero.

           # add extra 'trade start' if there's an open trade, so 'includeOpenTrade' logic will work
           if(any(is.na(testdf$end_ts))){
             trades$Start <- c(trades$Start,last(which(index(incrPos) == testdf$start_ts[first(which(is.na(testdf$end_ts)))])))
           }
jaymon0703 commented 6 years ago

committed fix in 0758b3b2183a8f3aa0a800e7399c4b0ba2c90a3c