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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jan 12 17:05:41 CET 2010


Author: braverock
Date: 2010-01-12 17:05:41 +0100 (Tue, 12 Jan 2010)
New Revision: 139

Modified:
   pkg/blotter/R/addTxn.R
Log:
- fix cumulation of position/cost in adding multiple txns

Modified: pkg/blotter/R/addTxn.R
===================================================================
--- pkg/blotter/R/addTxn.R	2010-01-12 14:53:23 UTC (rev 138)
+++ pkg/blotter/R/addTxn.R	2010-01-12 16:05:41 UTC (rev 139)
@@ -62,16 +62,21 @@
 
     #NewTxns=xts()
     for (row in 1:nrow(TxnData)) {
+        if(row==1) {
+            PrevPosQty     <- getPosQty(pname, Symbol, index(TxnData[row,]))
+            PrevPosAvgCost <- getPosAvgCost(pname, Symbol, index(TxnData[row,]))
+        }
         #TODO create vectorized versions of all these functions so we don't have to loop
         TxnQty         <- as.numeric(TxnData[row,'Quantity'])
         TxnPrice       <- as.numeric(TxnData[row,'Price'])
         TxnFee         <- 0 #TODO FIXME support transaction fees in addTxns
         TxnValue       <- calcTxnValue(TxnQty, TxnPrice, TxnFee)
         TxnAvgCost     <- calcTxnAvgCost(TxnValue, TxnQty)
-        PrevPosQty     <- getPosQty(pname, Symbol, index(TxnData[row,]))
+        #PrevPosQty     <- getPosQty(pname, Symbol, index(TxnData[row,]))
         PosQty         <- PrevPosQty+TxnQty
-        PrevPosAvgCost <- getPosAvgCost(pname, Symbol, index(TxnData[row,]))
-        PosAvgCost     <- calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty)
+        PosAvgCost     <- calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty) # lag this over the data?
+        PrevPosQty     <- PosQty
+        PrevPosAvgCost <- PosAvgCost
         RealizedPL = calcRealizedPL(TxnQty, TxnAvgCost, PrevPosAvgCost, PosQty, PrevPosQty)
         
         NewTxn = xts(t(c(TxnQty, 



More information about the Blotter-commits mailing list