[Blotter-commits] r1740 - in pkg/blotter: R tests/unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Mar 30 21:40:42 CEST 2016


Author: bodanker
Date: 2016-03-30 21:40:41 +0200 (Wed, 30 Mar 2016)
New Revision: 1740

Modified:
   pkg/blotter/R/addTxn.R
   pkg/blotter/tests/unitTests/runitAddTxn.R
   pkg/blotter/tests/unitTests/runitUpdatePortf.R
Log:
Fix transaction fees in addTxns

addTxns did not calculate transaction value (Txn.Value) gross of fees
as addTxn does. It also calculated Net.Txn.Realized.PL incorrectly, by
subtracting transaction fees instead of adding them.

Add unit test to ensure addTxns and addTxn produce the same transaction
table using the amzn_test data.

Set verbose=TRUE in addTxn calls in other tests, and make sure they
clean up correctly (account/portfolio names were incorrect). No need
to clean up the IBM object, since it will be available for garbage
collection after the function exits.


Modified: pkg/blotter/R/addTxn.R
===================================================================
--- pkg/blotter/R/addTxn.R	2016-03-26 15:10:30 UTC (rev 1739)
+++ pkg/blotter/R/addTxn.R	2016-03-30 19:40:41 UTC (rev 1740)
@@ -236,7 +236,7 @@
     }
     rm(Pos, PosCrossZero)  # clean up
     # calculate transaction values
-    NewTxns$Txn.Value <- .calcTxnValue(NewTxns$Txn.Qty, NewTxns$Txn.Price, NewTxns$Txn.Fees, ConMult)
+    NewTxns$Txn.Value <- .calcTxnValue(NewTxns$Txn.Qty, NewTxns$Txn.Price, 0, ConMult)  # Gross of fees
     NewTxns$Txn.Avg.Cost <- .calcTxnAvgCost(NewTxns$Txn.Value, NewTxns$Txn.Qty, ConMult)
     # intermediate objects to aid in vectorization; only first element is non-zero
     initPosQty <- initPosAvgCost <- numeric(nrow(NewTxns))
@@ -251,7 +251,7 @@
     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$Net.Txn.Realized.PL <- NewTxns$Gross.Txn.Realized.PL + NewTxns$Txn.Fees
     NewTxns$Con.Mult <- ConMult
 
     # update portfolio with new transactions

Modified: pkg/blotter/tests/unitTests/runitAddTxn.R
===================================================================
--- pkg/blotter/tests/unitTests/runitAddTxn.R	2016-03-26 15:10:30 UTC (rev 1739)
+++ pkg/blotter/tests/unitTests/runitAddTxn.R	2016-03-30 19:40:41 UTC (rev 1740)
@@ -5,8 +5,7 @@
     # remove objects created by unit tests
     try(rm_currencies("USD"))
     try(rm_stocks(symbols))
-    try(rm(list=p, pos=.blotter))
-    try(rm(IBM))
+    try(rm(list=paste0("portfolio.",p), pos=.blotter))
   })
 
   currency("USD")
@@ -22,16 +21,16 @@
 
   # Trades must be made in date order.
   # Make a couple of trades in IBM
-  addTxn(p, "IBM", '2007-01-03',  50,  96.5,  TxnFees=-0.05 * 50)
-  addTxn(p, "IBM", '2007-01-04', -50,  97.1,  TxnFees=-0.05 * 50)
-  addTxn(p, "IBM", '2007-01-08', -10,  99.2,  TxnFees=-0.05 * 10)
-  addTxn(p, "IBM", '2007-01-09', -10, 100.1,  TxnFees=-0.05 * 10)
-  addTxn(p, "IBM", '2007-01-17', -10, 100.25, TxnFees=-0.05 * 10)
-  addTxn(p, "IBM", '2007-01-19',  30,  95,    TxnFees=-0.05 * 30)
-  addTxn(p, "IBM", '2007-01-22',  25,  96.3,  TxnFees=-0.05 * 25)
-  addTxn(p, "IBM", '2007-01-23',  25,  96.42, TxnFees=-0.05 * 25)
-  addTxn(p, "IBM", '2007-01-26', -25,  97.52, TxnFees=-0.05 * 25)
-  addTxn(p, "IBM", '2007-01-31', -25,  98.80, TxnFees=-0.05 * 25)
+  addTxn(p, "IBM", '2007-01-03',  50,  96.5,  TxnFees=-0.05 * 50, verbose=FALSE)
+  addTxn(p, "IBM", '2007-01-04', -50,  97.1,  TxnFees=-0.05 * 50, verbose=FALSE)
+  addTxn(p, "IBM", '2007-01-08', -10,  99.2,  TxnFees=-0.05 * 10, verbose=FALSE)
+  addTxn(p, "IBM", '2007-01-09', -10, 100.1,  TxnFees=-0.05 * 10, verbose=FALSE)
+  addTxn(p, "IBM", '2007-01-17', -10, 100.25, TxnFees=-0.05 * 10, verbose=FALSE)
+  addTxn(p, "IBM", '2007-01-19',  30,  95,    TxnFees=-0.05 * 30, verbose=FALSE)
+  addTxn(p, "IBM", '2007-01-22',  25,  96.3,  TxnFees=-0.05 * 25, verbose=FALSE)
+  addTxn(p, "IBM", '2007-01-23',  25,  96.42, TxnFees=-0.05 * 25, verbose=FALSE)
+  addTxn(p, "IBM", '2007-01-26', -25,  97.52, TxnFees=-0.05 * 25, verbose=FALSE)
+  addTxn(p, "IBM", '2007-01-31', -25,  98.80, TxnFees=-0.05 * 25, verbose=FALSE)
 
   portfolio <- getPortfolio(p)
   transactions <- portfolio$symbols[["IBM"]]$txn
@@ -42,3 +41,38 @@
   # summary <- calcPortfSummary(portfolio)
 }
 
+test.addTxns <- function() {
+  on.exit({
+    # remove objects created by unit tests
+    try(rm_currencies("USD"), silent=TRUE)
+    try(rm_stocks("AMZN"), silent=TRUE)
+    try(rm(list=c("account.amzn_acct","portfolio.amzn_txn","portfolio.amzn_txns"), pos=.blotter), silent=TRUE)
+  })
+
+  # load the example data
+  data("amzn", package="blotter")
+  # add transaction fees
+  amzn.trades <- merge(amzn.trades, TxnFees=-0.1)
+  currency("USD")
+  stock("AMZN", currency="USD", multiplier=1)
+
+  # Initialize the account/portfolios
+  initAcct("amzn_acct", portfolios=c("amzn_txn","amzn_txns"), initEq=10000)
+  initPortf("amzn_txn", symbols="AMZN")
+  initPortf("amzn_txns", symbols="AMZN")
+
+  # Add the transactions to the portfolios
+  # addTxns:
+  addTxns("amzn_txns", "AMZN", TxnData=amzn.trades)
+  # addTxn:
+  for(i in 1:nrow(amzn.trades)) {
+    TxnTime <- index(amzn.trades)[i]
+    amzn.trade <- data.frame(amzn.trades[i,])
+    with(amzn.trade, addTxn("amzn_txn","AMZN", TxnTime, TxnQty, TxnPrice, TxnFees=TxnFees, verbose=FALSE))
+  }
+
+  t1 <- getPortfolio("amzn_txn")$symbols$AMZN$txn
+  t2 <- getPortfolio("amzn_txns")$symbols$AMZN$txn
+  checkIdentical(t1, t2)
+}
+

Modified: pkg/blotter/tests/unitTests/runitUpdatePortf.R
===================================================================
--- pkg/blotter/tests/unitTests/runitUpdatePortf.R	2016-03-26 15:10:30 UTC (rev 1739)
+++ pkg/blotter/tests/unitTests/runitUpdatePortf.R	2016-03-30 19:40:41 UTC (rev 1740)
@@ -5,8 +5,7 @@
     # remove objects created by unit tests
     try(rm_currencies("USD"))
     try(rm_stocks(symbols))
-    try(rm(list=c(p1,a1), pos=.blotter))
-    try(rm(IBM))
+    try(rm(list=c(paste0("portfolio.",p1),paste0("account.",a1)), pos=.blotter))
   })
 
   currency("USD")
@@ -18,7 +17,7 @@
 
   ## simple portfolio with one transaction
   p1 <- initPortf(name="p1runitUpdatePortf", symbols=symbols)
-  p1 <- addTxn(Portfolio="p1runitUpdatePortf", Symbol="IBM", TxnDate='2007-01-04', TxnQty=100, TxnPrice=96.5, TxnFees=-0.05*100)
+  p1 <- addTxn(Portfolio="p1runitUpdatePortf", Symbol="IBM", TxnDate='2007-01-04', TxnQty=100, TxnPrice=96.5, TxnFees=-0.05*100, verbose=FALSE)
   p1 <- updatePortf(Portfolio="p1runitUpdatePortf", Dates='2007-01-03::2007-01-10')
   a1 <- initAcct(name="a1runitUpdatePortf", portfolios="p1runitUpdatePortf")
   a1 <- updateAcct(a1,'2007-01')
@@ -27,7 +26,7 @@
   ## (really) simple transaction cost function
   fiveCents <- function(qty, prc, ...) return(-0.05*qty)
   p2 <- initPortf(name="p2runitUpdatePortf", symbols=symbols)
-  p2 <- addTxn(Portfolio="p2runitUpdatePortf", Symbol="IBM", TxnDate='2007-01-04', TxnQty=100, TxnPrice=96.5, TxnFees=fiveCents)
+  p2 <- addTxn(Portfolio="p2runitUpdatePortf", Symbol="IBM", TxnDate='2007-01-04', TxnQty=100, TxnPrice=96.5, TxnFees=fiveCents, verbose=FALSE)
   p2 <- updatePortf(Portfolio="p2runitUpdatePortf", Dates='2007-01-03::2007-01-10')
   a2 <- initAcct(name="a2runitUpdatePortf", portfolios="p2runitUpdatePortf")
   a2 <- updateAcct(a2,'2007-01')



More information about the Blotter-commits mailing list