[Pnl-commits] r8 - in pkg: . R man tests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Mar 27 12:17:40 CET 2010


Author: mark
Date: 2010-03-27 12:17:40 +0100 (Sat, 27 Mar 2010)
New Revision: 8

Added:
   pkg/R/addDvd.R
   pkg/R/addTrans.R
   pkg/R/calcDvd.R
   pkg/man/addDvd.Rd
   pkg/man/addTrans.Rd
   pkg/man/pnl.Rd
Removed:
   pkg/R/addDiv.R
   pkg/R/addTxn.R
   pkg/R/calcDiv.R
Modified:
   pkg/NAMESPACE
   pkg/R/pnl.R
   pkg/tests/pnlTest.R
Log:
Renamed add functions to prevent name conflicts with blotter's add functions

Modified: pkg/NAMESPACE
===================================================================
--- pkg/NAMESPACE	2010-03-25 17:04:11 UTC (rev 7)
+++ pkg/NAMESPACE	2010-03-27 11:17:40 UTC (rev 8)
@@ -1,3 +1,3 @@
-export(addDiv)
-export(addTxn)
+export(addDvd)
+export(addTrans)
 export(pnl)
\ No newline at end of file

Deleted: pkg/R/addDiv.R
===================================================================
--- pkg/R/addDiv.R	2010-03-25 17:04:11 UTC (rev 7)
+++ pkg/R/addDiv.R	2010-03-27 11:17:40 UTC (rev 8)
@@ -1,14 +0,0 @@
-#################################################################################
-# Add a cash dividend to the corporate actions
-# Corporate action types for dividends are:
-# 0 = cumdate cash dividend
-# 1 = paydate cash dividend
-#################################################################################
-addDiv <-
-function(corp, cumdate, paydate, amount=0.0) {
-	corp = rbind(corp, xts(matrix(c(0, 1, amount, amount), nrow=2), c(as.Date(cumdate), as.Date(paydate))))
-
-	if(nrow(corp) == 2) colnames(corp) = c("type", "amount") 
-
-	return(corp)
-}

Copied: pkg/R/addDvd.R (from rev 6, pkg/R/addDiv.R)
===================================================================
--- pkg/R/addDvd.R	                        (rev 0)
+++ pkg/R/addDvd.R	2010-03-27 11:17:40 UTC (rev 8)
@@ -0,0 +1,14 @@
+#################################################################################
+# Add a cash dividend to the corporate actions
+# Corporate action types for dividends are:
+# 0 = cumdate cash dividend
+# 1 = paydate cash dividend
+#################################################################################
+addDvd <-
+function(corp, cumdate, paydate, amount=0.0) {
+	corp = rbind(corp, xts(matrix(c(0, 1, amount, amount), nrow=2), c(as.Date(cumdate), as.Date(paydate))))
+
+	if(nrow(corp) == 2) colnames(corp) = c("type", "amount") 
+
+	return(corp)
+}

Copied: pkg/R/addTrans.R (from rev 6, pkg/R/addTxn.R)
===================================================================
--- pkg/R/addTrans.R	                        (rev 0)
+++ pkg/R/addTrans.R	2010-03-27 11:17:40 UTC (rev 8)
@@ -0,0 +1,17 @@
+#################################################################################
+# Add a transaction
+# 
+# @param txn xts timeseries with transactions
+# @author Mark Breman
+# @export
+addTrans <-
+function(txn, date=as.character(Sys.Date()), size=0, price=0.0, fees=0.0) {
+	if(fees > 0) stop("fees must be <= 0")
+ 
+	txn = rbind(txn, xts(matrix(c(size, price, fees), ncol=3), as.Date(date)))
+
+	if(nrow(txn) == 1) colnames(txn) = c("size", "price", "fees") 
+
+	return(txn)
+}
+

Deleted: pkg/R/addTxn.R
===================================================================
--- pkg/R/addTxn.R	2010-03-25 17:04:11 UTC (rev 7)
+++ pkg/R/addTxn.R	2010-03-27 11:17:40 UTC (rev 8)
@@ -1,17 +0,0 @@
-#################################################################################
-# Add a transaction
-# 
-# @param txn xts timeseries with transactions
-# @author Mark Breman
-# @export
-addTxn <-
-function(txn, date=as.character(Sys.Date()), size=0, price=0.0, fees=0.0) {
-	if(fees > 0) stop("fees must be <= 0")
- 
-	txn = rbind(txn, xts(matrix(c(size, price, fees), ncol=3), as.Date(date)))
-
-	if(nrow(txn) == 1) colnames(txn) = c("size", "price", "fees") 
-
-	return(txn)
-}
-

Deleted: pkg/R/calcDiv.R
===================================================================
--- pkg/R/calcDiv.R	2010-03-25 17:04:11 UTC (rev 7)
+++ pkg/R/calcDiv.R	2010-03-27 11:17:40 UTC (rev 8)
@@ -1,47 +0,0 @@
-##################################################################################
-# Calculate unrealized and realized dividends from corporate actions and transactions,
-# and add to series
-#	
-# constraints:
-# - dividends cumdate <= dividends paydate
-# - dividendN cumdate > dividendN-1 paydate
-#
-##################################################################################
-calcDiv <-
-function(series, txn, corp) {
-	divs=corp[which(corp$type %in% c(0,1)),]
-
-	if(!is.null(txn) & !is.null(divs)) {
-		# remove columns from possible previous runs
-		if("rdiv" %in% colnames(series)) series$rdiv = NULL
-		if("diva" %in% colnames(series)) series$diva = NULL
-		if("udiv" %in% colnames(series)) series$udiv = NULL
-		
-		divs = rbind(divs, xts(matrix(c(0,0), ncol=2), as.Date(0)))
-		txn = rbind(txn, xts(matrix(c(0,0,0), ncol=3), as.Date(0)))
-
-		txn$pos = cumsum(txn$size)
-		divs = cbind(divs, na.locf(cbind(divs, txn$pos)$pos), join="left")
-
-		# add realized dividends
-		divs$rdiv = round(lag(divs$pos) * divs$amount, 2)
-		series = cbind(series, divs[-1, "rdiv"], fill=0)
-
-		# add dividend amount
-		diva = divs[which(divs$type==0), c("pos", "amount")]
-		colnames(diva) = c("pos", "diva")
-		series = cbind(series, diva[-1, "diva"], fill=0)
-
-		# add unrealized dividend
-		# TODO: udiv should not depend on rdiv, but how? 
-		series$udiv = cumsum(round(series[, "diva"] * series[, "pos"] - series$rdiv, 2))
-
-	
-	} else {
-	 	series$diva=0
-		series$udiv = 0
-		series$rdiv = 0
-	}
-
-	return(series)
-}

Copied: pkg/R/calcDvd.R (from rev 6, pkg/R/calcDiv.R)
===================================================================
--- pkg/R/calcDvd.R	                        (rev 0)
+++ pkg/R/calcDvd.R	2010-03-27 11:17:40 UTC (rev 8)
@@ -0,0 +1,47 @@
+##################################################################################
+# Calculate unrealized and realized dividends from corporate actions and transactions,
+# and add to series
+#	
+# constraints:
+# - dividend cumdate <= dividend paydate
+# - dividendN cumdate > dividendN-1 paydate
+#
+##################################################################################
+calcDvd <-
+function(series, txn, corp) {
+	divs=corp[which(corp$type %in% c(0,1)),]
+
+	if(!is.null(txn) & !is.null(divs)) {
+		# remove columns from possible previous runs
+		if("rdiv" %in% colnames(series)) series$rdiv = NULL
+		if("diva" %in% colnames(series)) series$diva = NULL
+		if("udiv" %in% colnames(series)) series$udiv = NULL
+		
+		divs = rbind(divs, xts(matrix(c(0,0), ncol=2), as.Date(0)))
+		txn = rbind(txn, xts(matrix(c(0,0,0), ncol=3), as.Date(0)))
+
+		txn$pos = cumsum(txn$size)
+		divs = cbind(divs, na.locf(cbind(divs, txn$pos)$pos), join="left")
+
+		# add realized dividends
+		divs$rdiv = round(lag(divs$pos) * divs$amount, 2)
+		series = cbind(series, divs[-1, "rdiv"], fill=0)
+
+		# add dividend amount
+		diva = divs[which(divs$type==0), c("pos", "amount")]
+		colnames(diva) = c("pos", "diva")
+		series = cbind(series, diva[-1, "diva"], fill=0)
+
+		# add unrealized dividend
+		# TODO: udiv should not depend on rdiv, but how? 
+		series$udiv = cumsum(round(series[, "diva"] * series[, "pos"] - series$rdiv, 2))
+
+	
+	} else {
+	 	series$diva=0
+		series$udiv = 0
+		series$rdiv = 0
+	}
+
+	return(series)
+}

Modified: pkg/R/pnl.R
===================================================================
--- pkg/R/pnl.R	2010-03-25 17:04:11 UTC (rev 7)
+++ pkg/R/pnl.R	2010-03-27 11:17:40 UTC (rev 8)
@@ -33,7 +33,7 @@
 			pseries$udiv=0
 			pseries$rdiv=0
 		} else {
-			pseries = calcDiv(pseries, txn, corp)
+			pseries = calcDvd(pseries, txn, corp)
 		}
 
 		pseries = calcPnl(pseries, closepricecol, txn)

Added: pkg/man/addDvd.Rd
===================================================================
--- pkg/man/addDvd.Rd	                        (rev 0)
+++ pkg/man/addDvd.Rd	2010-03-27 11:17:40 UTC (rev 8)
@@ -0,0 +1,27 @@
+\name{addDvd}
+\alias{addDvd}
+\title{ Add a cash dividend to a series of corporate actions }
+\description{
+  To add a cash dividend to the series of corporate actions for an instrument.
+}
+\usage{ corp = addDvd(corp, cumdate, paydate, amount) }
+\arguments{
+  \item{corp}{ the xts time series with corporate actions for this instrument. }
+  \item{cumdate}{ the in-dividend date ( cum dividend) as ISO 8106, e.g., '2008-09-01'. }
+  \item{paydate}{ the paydate of the dividend as ISO 8106, e.g., '2008-09-01'. }
+  \item{amount}{ the cash dividend amount per share. }
+}
+\details{
+  The \code{addDvd} function will only add the cash dividend to the series of corporate actions.
+  No PnL calculations are done.
+}
+\value{
+  Returns the series of coprporate actions with the new cash dividend.
+}
+\author{ Mark Breman }
+\seealso{ \code{\link{addTrans}}, \code{\link{pnl}} }
+\examples{
+  ## pay a cash dividend on 2009-01-06 for the amount of stock held on 2009-01-02 times 0.0936
+  corp = addDvd(NULL, cumdate='2009-01-02', paydate='2009-01-06', amount=0.0936)
+}
+

Added: pkg/man/addTrans.Rd
===================================================================
--- pkg/man/addTrans.Rd	                        (rev 0)
+++ pkg/man/addTrans.Rd	2010-03-27 11:17:40 UTC (rev 8)
@@ -0,0 +1,31 @@
+\name{addTrans}
+\alias{addTrans}
+\title{ Add a transaction to a series of transactions}
+\description{
+  To add a transaction to the series of transactions for an instrument.
+}
+\usage{ txn = addTrans(txn, date, size=100, price, fees = 0) }
+\arguments{
+  \item{txn}{ the xts time series with transactions for this instrument. }
+  \item{date}{ the transaction date as ISO 8106, e.g., '2008-09-01'. }
+  \item{size}{ the size of the transaction. Positive values indicate a 'buy'; negative values indicate a 'sell'. }
+  \item{price}{ market clearing price at which the transaction was done. }
+  \item{fees}{ fees for the transaction, e.g. commissions. Fees are indicated as negative values. }
+}
+\details{
+  The \code{addTrans} function will only add the transaction to the series of transactions.
+  No PnL calculations are done.
+}
+\value{
+  Returns the series of transactions with the new transaction.
+}
+\author{ Mark Breman }
+\seealso{ \code{\link{addDvd}}, \code{\link{pnl}} }
+\examples{
+  ## first transaction for this instrument:  on 2009-01-02 buy 36 at 23.05 and pay a commission of 1.0
+  txn = addTrans(NULL, date='2009-01-02', size=36, price=23.05, fees=-1.0)
+
+  ## sell them on 2009-01-04 for 23.09, and pay a commission of 1.0 
+  txn = addTrans(txn, date='2009-01-04', size=-36, price=23.09, fees=-1.0)
+}
+

Added: pkg/man/pnl.Rd
===================================================================
--- pkg/man/pnl.Rd	                        (rev 0)
+++ pkg/man/pnl.Rd	2010-03-27 11:17:40 UTC (rev 8)
@@ -0,0 +1,25 @@
+\name{pnl}
+\alias{pnl}
+\title{ Calculate profit and loss }
+\description{
+  Calculate profit and loss based on price series, transactions and corporate actions for an instrument.
+}
+\usage{ pl = pnl(pseries, closepricecol, txn, corp) }
+\arguments{
+  \item{pseries}{ the xts time series with price data for this instrument. }
+  \item{closepricecol}{ the column of pseries that holds the close prices for this instrument. }
+  \item{txn}{ the xts time series with transactions for this instrument. }
+  \item{corp}{ the xts time series with corporate actions for this instrument. }
+}
+\details{
+  TODO
+}
+\value{
+  Returns an xts series with period profit and loss.
+}
+\author{ Mark Breman }
+\seealso{ \code{\link{addTrans}}, \code{\link{addDvd}} }
+\examples{
+  ## TODO
+}
+

Modified: pkg/tests/pnlTest.R
===================================================================
--- pkg/tests/pnlTest.R	2010-03-25 17:04:11 UTC (rev 7)
+++ pkg/tests/pnlTest.R	2010-03-27 11:17:40 UTC (rev 8)
@@ -7,8 +7,8 @@
 pseries = xts(prices, as.POSIXct(strptime(paste("2009-01-", seq(1:length(prices)), sep=""),"%Y-%m-%d")))
 colnames(pseries) = "Close"
 
-txn = addTxn(NULL, date='2009-01-02', size=36, price=23.05, fees=-1.0)
-txn = addTxn(txn, date='2009-01-04', size=-36, price=23.09, fees=-1.0)
+txn = addTrans(NULL, date='2009-01-02', size=36, price=23.05, fees=-1.0)
+txn = addTrans(txn, date='2009-01-04', size=-36, price=23.09, fees=-1.0)
 
 res = pnl(pseries, txn=txn)
 



More information about the pnl-commits mailing list