#' collect statstics on transactions and P&L for a symbol or symbols in a portfolio
#' 
#' may be aggregated with rbind on calls to all symbols in the portfolio
#' @param Portfolio portfolio string 
#' @param Symbols character vector of symbol strings, default NULL
#' @author Lance Levenson
#' @export
tradeStats <- function(Portfolio, Symbols=NULL)
{
	## Error Handling Borrowed from getPortfolio
	pname <- Portfolio
	if (!grepl("portfolio\\.", pname)) 
		Portfolio <- try(get(paste("portfolio", pname, sep = "."), 	envir = .blotter))
	else Portfolio <- try(get(pname, envir = .blotter))
	if (inherits(Portfolio, "try-error")) 
		stop(paste("Portfolio", pname, " not found, use initPortf() to create a new portfolio"))
	if (!inherits(Portfolio, "portfolio")) 
		stop("Portfolio", pname, "passed is not the name of a portfolio object.")
	
    ret<-NULL
    
    if(is.null(Symbols)) Symbols<-names(Portfolio$symbols)
    
	## Trade Statistics
	for (Symbol in Symbols){
        txn <- Portfolio$symbols[[Symbol]]$txn
        posPL <- Portfolio$symbols[[Symbol]]$posPL
        posPL <- posPL[-1,]
        
        TotalNetProfit <- sum(txn$Net.Txn.Realized.PL)
        AvgTradePL <- mean(txn$Net.Txn.Realized.PL[txn$Net.Txn.Realized.PL!=0])
        StdTradePL <- as.numeric(sd(txn$Net.Txn.Realized.PL[txn$Net.Txn.Realized.PL!=0]))   
        
        PercentPositive <- length(txn$Net.Txn.Realized.PL[txn$Net.Txn.Realized.PL>0])/length(txn$Net.Txn.Realized.PL[txn$Net.Txn.Realized.P!=0])
        PercentNegative <- length(txn$Net.Txn.Realized.PL[txn$Net.Txn.Realized.PL<0])/length(txn$Net.Txn.Realized.PL[txn$Net.Txn.Realized.P!=0])
        
        AvgWinTrade <- mean(txn$Net.Txn.Realized.PL[txn$Net.Txn.Realized.PL>0])
        AvgLossTrade <- mean(txn$Net.Txn.Realized.PL[txn$Net.Txn.Realized.PL<0])
        
        AvgDailyPL <- mean(apply.daily(txn$Net.Txn.Realized.PL[txn$Net.Txn.Realized.PL!=0],sum))
        StdDailyPL <- as.numeric(sd(apply.daily(txn$Net.Txn.Realized.PL[txn$Net.Txn.Realized.PL!=0],sum)))  
        
        Equity <- cumsum(posPL$Net.Trading.PL)
        Equity.max <- cummax(Equity)
        
        MaxDrawdown <- -max(Equity.max - Equity)
        ProfitToMaxDraw <- -TotalNetProfit / MaxDrawdown
        
        tmpret <- as.data.frame(cbind( Portfolio=pname, 
                        Symbol=Symbol,
                        Total.Net.Profit=TotalNetProfit,
                        Avg.Trade.PL=AvgTradePL,
                        Std.Dev.Trade.PL=StdTradePL,
                        Percent.Positive=PercentPositive,
                        Percent.Negative=PercentNegative,
                        Avg.Win.Trade=AvgWinTrade,
                        Avg.Losing.Trade=AvgLossTrade,
                        Avg.Daily.PL=AvgDailyPL,
                        Std.Dev.Daily.PL=StdDailyPL,
                        maxDrawdown=MaxDrawdown,
                        Profit.to.Max.Draw=ProfitToMaxDraw))
        if(is.null(ret)) ret<-tmpret
        else ret<-rbind(ret,tmpret)
    }
	
	return(ret)
}


###############################################################################
# Blotter: Tools for transaction-oriented trading systems development
# for R (see http://r-project.org/) 
# Copyright (c) 2008-2010 Peter Carl and Brian G. Peterson
#
# This library is distributed under the terms of the GNU Public License (GPL)
# for full details see the file COPYING
#
# $Id$
#
###############################################################################
