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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Aug 28 23:20:12 CEST 2012


Author: opentrades
Date: 2012-08-28 23:20:12 +0200 (Tue, 28 Aug 2012)
New Revision: 1143

Added:
   pkg/blotter/R/chart.ME.R
   pkg/blotter/man/calcPortfWgt.Rd
   pkg/blotter/man/chart.ME.Rd
Modified:
   pkg/blotter/DESCRIPTION
   pkg/blotter/NAMESPACE
   pkg/blotter/man/extractTxns.Rd
Log:
added chart.ME(), plus some man pages that were not yet under version control



Modified: pkg/blotter/DESCRIPTION
===================================================================
--- pkg/blotter/DESCRIPTION	2012-08-28 19:55:56 UTC (rev 1142)
+++ pkg/blotter/DESCRIPTION	2012-08-28 21:20:12 UTC (rev 1143)
@@ -23,8 +23,8 @@
     Hmisc,
     RUnit,
     quantmod
-Contributors: Dirk Eddelbuettel, Lance Levenson, Ben McCann, 
-    Jeff Ryan, Garrett See, Joshua Ulrich, Wolfgang Wu 
+Contributors: Dirk Eddelbuettel, Lance Levenson, Ben McCann,
+    Jeff Ryan, Garrett See, Joshua Ulrich, Wolfgang Wu
 URL: https://r-forge.r-project.org/projects/blotter/
 Copyright: (c) 2008-2011
 Collate:
@@ -34,6 +34,7 @@
     'calcPosAvgCost.R'
     'calcTxnAvgCost.R'
     'calcTxnValue.R'
+    'chart.ME.R'
     'chart.Posn.R'
     'chart.Reconcile.R'
     'chart.Spread.R'

Modified: pkg/blotter/NAMESPACE
===================================================================
--- pkg/blotter/NAMESPACE	2012-08-28 19:55:56 UTC (rev 1142)
+++ pkg/blotter/NAMESPACE	2012-08-28 21:20:12 UTC (rev 1143)
@@ -1,6 +1,7 @@
 export(addDiv)
 export(addPortfInstr)
 export(addTxn)
+export(chart.ME)
 export(chart.Posn)
 export(chart.Reconcile)
 export(chart.Spread)
@@ -16,6 +17,7 @@
 export(getTxns)
 export(initAcct)
 export(initPortf)
+export(is.account)
 export(is.portfolio)
 export(pennyPerShare)
 export(PortfReturns)

Added: pkg/blotter/R/chart.ME.R
===================================================================
--- pkg/blotter/R/chart.ME.R	                        (rev 0)
+++ pkg/blotter/R/chart.ME.R	2012-08-28 21:20:12 UTC (rev 1143)
@@ -0,0 +1,124 @@
+#' Chart Maximum Adverse/Forward Excursion
+#'
+#' Produces a scatterplot with one point per trade, with x-axis: absolute value of Drawdown (Adverse), or RunUp (Favourable), and y-axis: absolute value of Net Profit or Loss
+#'
+#' After Jaekle & Tomasini: Trading Systems - A new approach to system development and portfolio optimisation (ISBN 978-1-905641-79-6), paragraph 3.5
+#'
+#' @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 type string specifying MAE (Adverse) or MFE (Favourable) chart type
+#' @param scale string specifying 'cash', or 'percent' for percentage of investment
+#' @export
+chart.ME <- function(portfolio, symbol, type=c('MAE', 'MFE'), scale=c('cash', 'percent'))
+{   # @author Jan Humme
+
+    require(xts)
+
+    portf <- getPortfolio(portfolio)
+
+    if(missing(symbol)) symbol <- names(portf$symbols)[[1]]
+    
+    posPL <- portf$symbols[[symbol]]$posPL
+
+    trades <- list()
+
+    # identify start and end for each trade, where end means flat position
+    trades$Start <- index(posPL[which(posPL$Pos.Value!=0 & lag(posPL$Pos.Value)==0),])
+    trades$End <- index(posPL[which(posPL$Pos.Value==0 & lag(posPL$Pos.Value)!=0),])
+
+    # discard open last trade, if any
+    trades$Start <- trades$Start[1:length(trades$End)]
+
+    for(i in 1:length(trades$End))
+    {
+        timespan <- paste(format(trades$Start[[i]], "%Y-%m-%d %H:%M:%OS6"),
+                          format(trades$End[[i]], "%Y-%m-%d %H:%M:%OS6"), sep="::")
+
+        trade <- posPL[timespan]
+
+        # close and open may occur in at same index timestamp, must be corrected
+        if(first(trade)$Pos.Qty==0) trade <- tail(trade, -1)
+        if(last(trade)$Pos.Qty!=0) trade <- head(trade, -1)
+
+        # investment
+        trades$Txn.Value[i] <- abs(first(trade$Txn.Value))
+
+        # cash-wise
+        trades$Net.Trading.PL[i] <- sum(trade$Net.Trading.PL)
+        trades$Drawdown[i] <- min(0,cumsum(trade$Net.Trading.PL))
+        trades$RunUp[i] <- max(0,cumsum(trade$Net.Trading.PL))
+    }
+
+    # percentage-wise
+    trades$Pct.Net.Trading.PL <- 100 * trades$Net.Trading.PL / trades$Txn.Value
+    trades$Pct.Drawdown <- 100 * trades$Drawdown / trades$Txn.Value
+    trades$Pct.RunUp <- 100 * trades$RunUp / trades$Txn.Value
+
+    trades <- as.data.frame(trades)
+
+    profitable <- (trades$Net.Trading.PL > 0)
+
+    if(type == 'MAE')
+    {
+        if(scale == 'cash')
+        {
+            plot(abs(trades[, c('Drawdown','Net.Trading.PL')]), type='n',
+                    xlab='Drawdown ($)', ylab='Profit (Loss) in $',
+                    main='Maximum Adverse Excursion (MAE) in $')
+
+            points(abs(trades[ profitable, c('Drawdown','Net.Trading.PL')]), pch=2, col='green')
+            points(abs(trades[!profitable, c('Drawdown','Net.Trading.PL')]), pch=25, col='red')
+        }
+        else    # scale == 'percent'
+        {
+            plot(abs(trades[, c('Pct.Drawdown','Pct.Net.Trading.PL')]), type='n',
+                    xlab='Drawdown (%)', ylab='Profit (Loss) in %',
+                    main='Maximum Adverse Excursion (MAE) in %')
+
+            points(abs(trades[ profitable, c('Pct.Drawdown','Pct.Net.Trading.PL')]), pch=2, col='green')
+            points(abs(trades[!profitable, c('Pct.Drawdown','Pct.Net.Trading.PL')]), pch=25, col='red')
+        }
+    }
+    else    # type == 'MFE'
+    {
+        if(scale == 'cash')
+        {
+            plot(abs(trades[, c('RunUp','Net.Trading.PL')]), type='n',
+                    xlab='RunUp ($)', ylab='Profit (Loss) in $',
+                    main='Maximum Favourable Excursion (MFE) in $')
+    
+            points(abs(trades[ profitable, c('RunUp','Net.Trading.PL')]), pch=2, col='green')
+            points(abs(trades[!profitable, c('RunUp','Net.Trading.PL')]), pch=25, col='red')
+        }
+        else    # scale == 'percent'
+        {
+            plot(abs(trades[, c('Pct.RunUp','Pct.Net.Trading.PL')]), type='n',
+                    xlab='RunUp (%)', ylab='Profit (Loss) in %',
+                    main='Maximum Favourable Excursion (MFE) in %')
+    
+            points(abs(trades[ profitable, c('Pct.RunUp','Pct.Net.Trading.PL')]), pch=2, col='green')
+            points(abs(trades[!profitable, c('Pct.RunUp','Pct.Net.Trading.PL')]), pch=25, col='red')
+        }
+    }
+
+    grid()
+
+    legend(
+            x='right', inset=0.1,
+            legend=c('Profitable Trade','Losing Trade'),
+            pch=c(2,6),
+            col=c('green','red')
+    )
+}
+
+###############################################################################
+# Blotter: Tools for transaction-oriented trading systems development
+# for R (see http://r-project.org/) 
+# Copyright (c) 2008-2011 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: chart.Posn.R 855 2011-11-28 23:41:46Z bodanker $
+#
+###############################################################################

Added: pkg/blotter/man/calcPortfWgt.Rd
===================================================================
--- pkg/blotter/man/calcPortfWgt.Rd	                        (rev 0)
+++ pkg/blotter/man/calcPortfWgt.Rd	2012-08-28 21:20:12 UTC (rev 1143)
@@ -0,0 +1,33 @@
+\name{calcPortfWgt}
+\alias{calcPortfWgt}
+\title{Calculates the portfolio weights for positions within a given portfolio.}
+\usage{
+  calcPortfWgt(Portfolio, Symbols = NULL, Dates = NULL,
+    denominator = c("Gross.Value", "Net.Value", "Long.Value", "Short.Value"),
+    Account = NULL)
+}
+\arguments{
+  \item{Portfolio}{a portfolio object structured with
+  initPortf()}
+
+  \item{Symbols}{an instrument identifier for a symbol
+  included in the portfolio}
+
+  \item{Dates}{dates to return the calculation over
+  formatted as xts range}
+
+  \item{denominator}{string describing the deniminator, see
+  usage}
+
+  \item{Account}{an Account object containing Portfolio
+  summaries}
+}
+\value{
+  xts timeseries object with weights by date in rows and
+  symbolname in columns
+}
+\description{
+  Portfolio weights may be calculated differently depending
+  on their use.
+}
+

Added: pkg/blotter/man/chart.ME.Rd
===================================================================
--- pkg/blotter/man/chart.ME.Rd	                        (rev 0)
+++ pkg/blotter/man/chart.ME.Rd	2012-08-28 21:20:12 UTC (rev 1143)
@@ -0,0 +1,33 @@
+\name{chart.ME}
+\alias{chart.ME}
+\title{Chart Maximum Adverse/Forward Excursion}
+\usage{
+  chart.ME(portfolio, symbol, type = c("MAE", "MFE"),
+    scale = c("cash", "percent"))
+}
+\arguments{
+  \item{Portfolio}{string identifying the portfolio to
+  chart}
+
+  \item{Symbol}{string identifying the symbol to chart. If
+  missing, the first symbol found in the \code{Portfolio}
+  portfolio will be used}
+
+  \item{type}{string specifying MAE (Adverse) or MFE
+  (Favourable) chart type}
+
+  \item{scale}{string specifying 'cash', or 'percent' for
+  percentage of investment}
+}
+\description{
+  Produces a scatterplot with one point per trade, with
+  x-axis: absolute value of Drawdown (Adverse), or RunUp
+  (Favourable), and y-axis: absolute value of Net Profit or
+  Loss
+}
+\details{
+  After Jaekle & Tomasini: Trading Systems - A new approach
+  to system development and portfolio optimisation (ISBN
+  978-1-905641-79-6), paragraph 3.5
+}
+

Modified: pkg/blotter/man/extractTxns.Rd
===================================================================
--- pkg/blotter/man/extractTxns.Rd	2012-08-28 19:55:56 UTC (rev 1142)
+++ pkg/blotter/man/extractTxns.Rd	2012-08-28 21:20:12 UTC (rev 1143)
@@ -2,14 +2,11 @@
 \alias{extractTxns}
 \title{Extract transactions from a portfolio}
 \usage{
-  extractTxns(Portfolio, Symbol = NULL)
+  extractTxns(Portfolio)
 }
 \arguments{
   \item{Portfolio}{string identifying the portfolio to
   extract from}
-
-  \item{Symbol}{an optional instrument identifier for a
-  symbol included in the portfolio}
 }
 \value{
   String vector of \code{\link{addTxn}} calls that would



More information about the Blotter-commits mailing list