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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Aug 9 18:30:00 CEST 2015


Author: bodanker
Date: 2015-08-09 18:29:59 +0200 (Sun, 09 Aug 2015)
New Revision: 1693

Modified:
   pkg/blotter/R/addTxn.R
Log:
Make Gross Txn Realized P&L zero if Posn increases

addTxns was missing this logic from addTxn, which was causing a bug in
the amzn_test demo.


Modified: pkg/blotter/R/addTxn.R
===================================================================
--- pkg/blotter/R/addTxn.R	2015-06-27 09:50:07 UTC (rev 1692)
+++ pkg/blotter/R/addTxn.R	2015-08-09 16:29:59 UTC (rev 1693)
@@ -246,9 +246,11 @@
     NewTxns$Pos.Qty <- cumsum(initPosQty + NewTxns$Txn.Qty)
     # only pass non-zero initial position qty and average cost
     NewTxns$Pos.Avg.Cost <- .calcPosAvgCost_C(initPosQty[1], initPosAvgCost[1], NewTxns$Txn.Value, NewTxns$Pos.Qty, ConMult)
-    # need lagged position average cost
+    # need lagged position average cost and quantity
     lagPosAvgCost <- c(initPosAvgCost[1], NewTxns$Pos.Avg.Cost[-nrow(NewTxns)])
+    lagPosQty <- c(initPosQty[1], NewTxns$Pos.Qty[-nrow(NewTxns)])
     NewTxns$Gross.Txn.Realized.PL <- NewTxns$Txn.Qty * ConMult * (lagPosAvgCost - NewTxns$Txn.Avg.Cost)
+    NewTxns$Gross.Txn.Realized.PL[abs(lagPosQty) < abs(NewTxns$Pos.Qty) | lagPosQty == 0] <- 0
     NewTxns$Net.Txn.Realized.PL <- NewTxns$Gross.Txn.Realized.PL - NewTxns$Txn.Fees
     NewTxns$Con.Mult <- ConMult
 



More information about the Blotter-commits mailing list