[Blotter-commits] r1052 - pkg/blotter/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jun 15 19:06:17 CEST 2012


Author: braverock
Date: 2012-06-15 19:06:16 +0200 (Fri, 15 Jun 2012)
New Revision: 1052

Modified:
   pkg/blotter/R/addTxn.R
Log:
- move timeBased TxnDate check to beginning of function
- convert 'Date' indices to POSIXct

Modified: pkg/blotter/R/addTxn.R
===================================================================
--- pkg/blotter/R/addTxn.R	2012-06-15 15:39:44 UTC (rev 1051)
+++ pkg/blotter/R/addTxn.R	2012-06-15 17:06:16 UTC (rev 1052)
@@ -41,11 +41,16 @@
 
     pname <- Portfolio
     PrevPosQty = getPosQty(pname, Symbol, TxnDate)
+    
+    if(!is.timeBased(TxnDate) || is.Date(TxnDate)){
+        TxnDate<-as.POSIXct(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
+        TxnDate=TxnDate+2*eps #transactions need unique timestamps, so increment a bit
         TxnQty=TxnQty+PrevPosQty
         PrevPosQty=0
     }
@@ -98,9 +103,7 @@
 		GrossTxnRealizedPL = 0
 	
 	NetTxnRealizedPL = GrossTxnRealizedPL + txnfees
-    if(!is.timeBased(TxnDate)){
-        TxnDate<-as.POSIXct(TxnDate)
-    }
+
     # Store the transaction and calculations
     NewTxn = xts(t(c(TxnQty, TxnPrice, TxnValue, TxnAvgCost, PosQty, PosAvgCost, GrossTxnRealizedPL, txnfees, NetTxnRealizedPL, ConMult)), order.by=TxnDate)
     #colnames(NewTxns) = c('Txn.Qty', 'Txn.Price', 'Txn.Value', 'Txn.Avg.Cost', 'Pos.Qty', 'Pos.Avg.Cost', 'Gross.Txn.Realized.PL', 'Txn.Fees', 'Net.Txn.Realized.PL', 'Con.Mult')



More information about the Blotter-commits mailing list