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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue May 1 21:35:45 CEST 2012


Author: braverock
Date: 2012-05-01 21:35:45 +0200 (Tue, 01 May 2012)
New Revision: 1007

Modified:
   pkg/blotter/DESCRIPTION
   pkg/blotter/R/addTxn.R
   pkg/blotter/R/getPos.R
Log:
- split transactions that would cross through 0
- don't use paste() in getPos, use the timestamp directly
- bump version

Modified: pkg/blotter/DESCRIPTION
===================================================================
--- pkg/blotter/DESCRIPTION	2012-04-24 00:26:23 UTC (rev 1006)
+++ pkg/blotter/DESCRIPTION	2012-05-01 19:35:45 UTC (rev 1007)
@@ -2,7 +2,7 @@
 Type: Package
 Title: Tools for transaction-oriented trading systems
     development.
-Version: 0.8.5
+Version: 0.8.6
 Date: $Date$
 Author: Peter Carl, Brian G. Peterson
 Maintainer: Brian G. Peterson <brian at braverock.com>

Modified: pkg/blotter/R/addTxn.R
===================================================================
--- pkg/blotter/R/addTxn.R	2012-04-24 00:26:23 UTC (rev 1006)
+++ pkg/blotter/R/addTxn.R	2012-05-01 19:35:45 UTC (rev 1007)
@@ -13,6 +13,12 @@
 #' The \code{pennyPerShare} function provides a simple example of a transaction cost
 #' function.
 #' 
+#' Transactions which would cross your position through zero will be split 
+#' into two transactions, one to flatten the position, and another to initiate 
+#' a new position on the opposite side of the market.  The new (split) 
+#' transaction will have it's timestamp inclremented by eps to preserve ordering. 
+#' This transaction splitting vastly simplifies realized P&L calculations elsewhere in the code.
+#' 
 #' @param Portfolio  A portfolio name that points to a portfolio object structured with \code{initPortf()}
 #' @param Symbol An instrument identifier for a symbol included in the portfolio, e.g., "IBM"
 #' @param TxnDate  Transaction date as ISO 8601, e.g., '2008-09-01' or '2010-01-05 09:54:23.12345'
@@ -22,6 +28,7 @@
 #' @param TxnFees Fees associated with the transaction, e.g. commissions., See Details
 #' @param ConMult Contract/instrument multiplier for the Symbol if it is not dened in an instrument specication
 #' @param verbose If TRUE (default) the function prints the elements of the transaction in a line to the screen, e.g., "2007-01-08 IBM 50 @@ 77.6". Suppress using FALSE.
+#' @param eps value to add to force unique indices
 #' @note 
 #' The addTxn function will eventually also handle other transaction types, 
 #' such as adjustments for corporate actions or expire/assign for options. 
@@ -29,10 +36,20 @@
 #' @seealso \code{\link{addTxns}}, \code{\link{pennyPerShare}}, \code{\link{initPortf}}
 #' @author Peter Carl
 #' @export
-addTxn <- function(Portfolio, Symbol, TxnDate, TxnQty, TxnPrice, ..., TxnFees=0, ConMult=NULL, verbose=TRUE)
+addTxn <- function(Portfolio, Symbol, TxnDate, TxnQty, TxnPrice, ..., TxnFees=0, ConMult=NULL, verbose=TRUE, eps=1e-06)
 { # @author Peter Carl
 
-    pname<-Portfolio
+    pname <- Portfolio
+    PrevPosQty = getPosQty(pname, Symbol, TxnDate)
+    # split transactions that would cross through zero
+    if(PrevPosQty!=0 && sign(PrevPosQty+TxnQty)!=sign(PrevPosQty) && PrevPosQty!=-TxnQty){
+        addTxn(Portfolio=pname, Symbol=Symbol, TxnDate=TxnDate, TxnQty=-PrevPosQty, TxnPrice=TxnPrice, ..., 
+                TxnFees = TxnFees, ConMult = ConMult, verbose = verbose, eps=eps)
+        TxnDate=TxnDate+2*eps
+        TxnQty=TxnQty+PrevPosQty
+        PrevPosQty=0
+    }
+    
     Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
 
     if(is.null(ConMult) | !hasArg(ConMult)){
@@ -63,9 +80,9 @@
     TxnAvgCost = .calcTxnAvgCost(TxnValue, TxnQty, ConMult)
 
     # Calculate the change in position
-    PrevPosQty = getPosQty(pname, Symbol, TxnDate)
     PosQty = PrevPosQty + TxnQty
 
+
     # Calculate the resulting position's average cost
     PrevPosAvgCost = .getPosAvgCost(pname, Symbol, TxnDate)
     PosAvgCost = .calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty, ConMult)

Modified: pkg/blotter/R/getPos.R
===================================================================
--- pkg/blotter/R/getPos.R	2012-04-24 00:26:23 UTC (rev 1006)
+++ pkg/blotter/R/getPos.R	2012-05-01 19:35:45 UTC (rev 1007)
@@ -18,8 +18,7 @@
     # position average cost are returned.
     #if(nrow(PosData)>1) Pos = last(PosData[toDate][,Columns],n=n)
     if(nrow(PosData)>1) {
-        toDate = paste('::', Date, sep="")
-        Pos = last(PosData[toDate][,Columns],n=n)
+        Pos = last(PosData[which(index(PosData)<=Date),][, Columns], n = n)
     } else Pos <- PosData[,Columns]
     return(Pos)
 }



More information about the Blotter-commits mailing list