[Blotter-commits] r1186 - in pkg/blotter: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Sep 15 17:15:57 CEST 2012


Author: braverock
Date: 2012-09-15 17:15:57 +0200 (Sat, 15 Sep 2012)
New Revision: 1186

Modified:
   pkg/blotter/R/chart.ME.R
   pkg/blotter/man/chart.ME.Rd
   pkg/blotter/man/perTradeStats.Rd
Log:
- add details of the returned data.frame for perTradeStats
- includeOpenTrade default TRUE (mark to market)
- add cross references


Modified: pkg/blotter/R/chart.ME.R
===================================================================
--- pkg/blotter/R/chart.ME.R	2012-09-15 14:26:22 UTC (rev 1185)
+++ pkg/blotter/R/chart.ME.R	2012-09-15 15:15:57 UTC (rev 1186)
@@ -11,6 +11,8 @@
 #' @param \dots any other passthrough parameters, in particular includeOpenTrades (see perTradeStats())
 #' @author Jan Humme
 #' @references Tomasini, E. and Jaekle, U. \emph{Trading Systems - A new approach to system development and portfolio optimisation} (ISBN 978-1-905641-79-6), section 3.5
+#' @seealso \code{\link{perTradeStats}} for the calculations used by this chart, 
+#' and \code{\link{tradeStats}} for a summary view of the performance
 #' @export
 chart.ME <- function(Portfolio, Symbol, type=c('MAE','MFE'), scale=c('cash','percent','tick'), ...)
 {   # @author Jan Humme
@@ -25,7 +27,6 @@
     profitable <- (trades$Net.Trading.PL > 0)
 
     switch(scale,
-
         cash = {
             .ylab <- 'Profit/Loss (cash)'
             if(type == 'MAE')
@@ -103,15 +104,40 @@
 #' Note that a trade that is open at the end of the measured period will
 #' be marked to the timestamp of the end of the series.  
 #' If that trade is later closed, the stats for it will likely change. 
+#' This is 'mark to market' for the open position, and corresponds to
+#' most trade accounting systems and risk systems in including the open
+#' position in reporting.
 #'  
-#' @param Portfolio string identifying the portfolio to chart
-#' @param Symbol string identifying the symbol to chart. If missing, the first symbol found in the \code{Portfolio} portfolio will be used
-#' @param includeOpenTrades: whether to process only finished trades, or the last trade if it is still open
+#' @param Portfolio string identifying the portfolio
+#' @param Symbol string identifying the symbol to examin trades for. If missing, the first symbol found in the \code{Portfolio} portfolio will be used
+#' @param includeOpenTrade whether to process only finished trades, or the last trade if it is still open, default TRUE
 #' @param \dots any other passthrough parameters
 #' @author Brian G. Peterson, Jan Humme
 #' @references Tomasini, E. and Jaekle, U. \emph{Trading Systems - A new approach to system development and portfolio optimisation} (ISBN 978-1-905641-79-6)
+#' @return 
+#' A \code{data.frame} containing:
+#' 
+#' \describe{
+#'      \item{Start}{the \code{POSIXct} timestamp of the start of the trade}
+#'      \item{End}{the \code{POSIXct} timestamp of the end of the trade, when flat}
+#'      \item{Init.Pos}{the initial position on opening the trade}
+#'      \item{Max.Pos}{the maximum (largest) position held during the open trade}
+#'      \item{Num.Txns}{ the number of transactions included in this trade}
+#'      \item{Max.Notional.Cost}{ the largest notional investment cost of this trade}
+#'      \item{Net.Trading.PL}{ net trading P&L in the currency of \code{Symbol}}
+#'      \item{MAE}{ Maximum Adverse Excursion (MAE), in the currency of \code{Symbol}}
+#'      \item{MFE}{ Maximum Favorable Excursion (MFE), in the currency of \code{Symbol}}
+#'      \item{Pct.Net.Trading.PL}{ net trading P&L in percent of invested \code{Symbol} price gained or lost}
+#'      \item{Pct.MAE}{ Maximum Adverse Excursion (MAE), in percent}
+#'      \item{Pct.MFE}{ Maximum Favorable Excursion (MFE), in percent}
+#'      \item{tick.Net.Trading.PL}{  net trading P&L in ticks}
+#'      \item{tick.MAE}{ Maximum Adverse Excursion (MAE) in ticks}
+#'      \item{tick.MFE}{ Maximum Favorable Excursion (MFE) in ticks} 
+#' }
+#' @seealso \code{\link{chart.ME}} for a chart of MAE and MFE derived from this function, 
+#' and \code{\link{tradeStats}} for a summary view of the performance
 #' @export
-perTradeStats <- function(Portfolio, Symbol, includeOpenTrades=FALSE, ...) {
+perTradeStats <- function(Portfolio, Symbol, includeOpenTrade=TRUE, ...) {
     portf <- getPortfolio(Portfolio)
     
     if(missing(Symbol)) Symbol <- names(portf$symbols)[[1]]
@@ -153,15 +179,21 @@
         #add running posPL column
         trade$PosPL <- trade$Pos.Value-trade$Pos.Cost.Basis
         
+        #position sizes
+        trades$Init.Pos[i] <- first(trade$Pos.Qty)
+        trades$Max.Pos[i] <- first(trade[which(abs(trade$Pos.Qty)==max(abs(trade$Pos.Qty))),]$Pos.Qty)
+
         #count number of transactions
         trades$Num.Txns[i]<-length(which(trade$Txn.Value!=0))
         
-        #position sizes
-        trades$Init.Pos[i] <- first(trade$Pos.Qty)
-        trades$Max.Pos[i] <- first(trade[which(abs(trade$Pos.Qty)==max(abs(trade$Pos.Qty))),]$Pos.Qty)
         # investment
         trades$Max.Notional.Cost[i] <- first(trade[which(abs(trade$Pos.Qty)==max(abs(trade$Pos.Qty))),]$Pos.Cost.Basis)
         
+        # cash P&L
+        trades$Net.Trading.PL[i] <- last(trade)$PosPL
+        trades$MAE[i] <- min(0,trade$PosPL)
+        trades$MFE[i] <- max(0,trade$PosPL)
+        
         # percentage P&L
         trade$Pct.PL <- trade$PosPL/abs(trade$Pos.Cost.Basis) #broken for last timestamp
         trade$Pct.PL[length(trade$Pct.PL)]<-last(trade)$PosPL/abs(trades$Max.Notional.Cost[i])
@@ -170,11 +202,6 @@
         trades$Pct.MAE[i] <- min(0,trade$Pct.PL)
         trades$Pct.MFE[i] <- max(0,trade$Pct.PL)
         
-        # cash P&L
-        trades$Net.Trading.PL[i] <- last(trade)$PosPL
-        trades$MAE[i] <- min(0,trade$PosPL)
-        trades$MFE[i] <- max(0,trade$PosPL)
-        
         # tick P&L
         #Net.Trading.PL/position/tick value=ticks
         trade$tick.PL <- trade$PosPL/abs(trade$Pos.Qty)/tick_value #broken for last observation
@@ -184,7 +211,6 @@
         trades$tick.MAE[i] <- min(0,trade$tick.PL)
         trades$tick.MFE[i] <- max(0,trade$tick.PL)
     }
-#print(trades)
 
     return(as.data.frame(trades))
 }

Modified: pkg/blotter/man/chart.ME.Rd
===================================================================
--- pkg/blotter/man/chart.ME.Rd	2012-09-15 14:26:22 UTC (rev 1185)
+++ pkg/blotter/man/chart.ME.Rd	2012-09-15 15:15:57 UTC (rev 1186)
@@ -36,4 +36,9 @@
   approach to system development and portfolio
   optimisation} (ISBN 978-1-905641-79-6), section 3.5
 }
+\seealso{
+  \code{\link{perTradeStats}} for the calculations used by
+  this chart, and \code{\link{tradeStats}} for a summary
+  view of the performance
+}
 

Modified: pkg/blotter/man/perTradeStats.Rd
===================================================================
--- pkg/blotter/man/perTradeStats.Rd	2012-09-15 14:26:22 UTC (rev 1185)
+++ pkg/blotter/man/perTradeStats.Rd	2012-09-15 15:15:57 UTC (rev 1186)
@@ -2,22 +2,47 @@
 \alias{perTradeStats}
 \title{calculate flat to flat per-trade statistics}
 \usage{
-  perTradeStats(Portfolio, Symbol,
-    includeOpenTrades = FALSE, ...)
+  perTradeStats(Portfolio, Symbol, includeOpenTrade = TRUE,
+    ...)
 }
 \arguments{
-  \item{Portfolio}{string identifying the portfolio to
-  chart}
+  \item{Portfolio}{string identifying the portfolio}
 
-  \item{Symbol}{string identifying the symbol to chart. If
-  missing, the first symbol found in the \code{Portfolio}
-  portfolio will be used}
+  \item{Symbol}{string identifying the symbol to examin
+  trades for. If missing, the first symbol found in the
+  \code{Portfolio} portfolio will be used}
 
-  \item{includeOpenTrades:}{whether to process only
-  finished trades, or the last trade if it is still open}
+  \item{includeOpenTrade}{whether to process only finished
+  trades, or the last trade if it is still open, default
+  TRUE}
 
   \item{\dots}{any other passthrough parameters}
 }
+\value{
+  A \code{data.frame} containing:
+
+  \describe{ \item{Start}{the \code{POSIXct} timestamp of
+  the start of the trade} \item{End}{the \code{POSIXct}
+  timestamp of the end of the trade, when flat}
+  \item{Init.Pos}{the initial position on opening the
+  trade} \item{Max.Pos}{the maximum (largest) position held
+  during the open trade} \item{Num.Txns}{ the number of
+  transactions included in this trade}
+  \item{Max.Notional.Cost}{ the largest notional investment
+  cost of this trade} \item{Net.Trading.PL}{ net trading
+  P&L in the currency of \code{Symbol}} \item{MAE}{ Maximum
+  Adverse Excursion (MAE), in the currency of
+  \code{Symbol}} \item{MFE}{ Maximum Favorable Excursion
+  (MFE), in the currency of \code{Symbol}}
+  \item{Pct.Net.Trading.PL}{ net trading P&L in percent of
+  invested \code{Symbol} price gained or lost}
+  \item{Pct.MAE}{ Maximum Adverse Excursion (MAE), in
+  percent} \item{Pct.MFE}{ Maximum Favorable Excursion
+  (MFE), in percent} \item{tick.Net.Trading.PL}{ net
+  trading P&L in ticks} \item{tick.MAE}{ Maximum Adverse
+  Excursion (MAE) in ticks} \item{tick.MFE}{ Maximum
+  Favorable Excursion (MFE) in ticks} }
+}
 \description{
   One 'trade' is defined as the entire time the symbol is
   not flat. It may contain many transactions.  From the
@@ -31,7 +56,10 @@
   Note that a trade that is open at the end of the measured
   period will be marked to the timestamp of the end of the
   series. If that trade is later closed, the stats for it
-  will likely change.
+  will likely change. This is 'mark to market' for the open
+  position, and corresponds to most trade accounting
+  systems and risk systems in including the open position
+  in reporting.
 }
 \author{
   Brian G. Peterson, Jan Humme
@@ -41,4 +69,9 @@
   approach to system development and portfolio
   optimisation} (ISBN 978-1-905641-79-6)
 }
+\seealso{
+  \code{\link{chart.ME}} for a chart of MAE and MFE derived
+  from this function, and \code{\link{tradeStats}} for a
+  summary view of the performance
+}
 



More information about the Blotter-commits mailing list