[Blotter-commits] r1732 - pkg/blotter/tests/unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jan 16 15:48:03 CET 2016


Author: bodanker
Date: 2016-01-16 15:48:02 +0100 (Sat, 16 Jan 2016)
New Revision: 1732

Modified:
   pkg/blotter/tests/unitTests/runitAddTxn.R
   pkg/blotter/tests/unitTests/runitCalcValues.R
   pkg/blotter/tests/unitTests/runitUpdatePortf.R
Log:
Fix unit tests

Some function names have changed to include a leading dot, and they are
no longer exported, so we need to access them with the ::: operator.
Thanks to cloudcello for the report and patch (#6284).

Make transaction fees negative when calling addTxn. Change portfolio
and account names to avoid clashing with anything a user might have
created.

Clean up objects and environments when tests exit (instead of deleting
everything). I couldn't figure out how to do it with .setUp/.tearDown,
so I used on.exit instead. The issue with .setUp/.tearDown was likely
due to scoping issues, and I didn't want to assign to the .GlobalEnv.

Remove test.calcRealizedPL because calcRealizedPL was removed in r411.
Add test.calcPosAvgCost_C to test C version of .calcPosAvgCost.


Modified: pkg/blotter/tests/unitTests/runitAddTxn.R
===================================================================
--- pkg/blotter/tests/unitTests/runitAddTxn.R	2016-01-16 13:31:12 UTC (rev 1731)
+++ pkg/blotter/tests/unitTests/runitAddTxn.R	2016-01-16 14:48:02 UTC (rev 1732)
@@ -1,45 +1,44 @@
 # Author: Peter Carl, RUnit port by Ben McCann
 
-#Sys.setenv(TZ="America/Chicago")        # as the data set got save with this TZ
-#options("width"=78)                     # to tie down the print() statement width
-#verbose <- FALSE
-
 test.addTxn <- function() {
+  on.exit({
+    # remove objects created by unit tests
+    try(rm_currencies("USD"))
+    try(rm_stocks(symbols))
+    try(rm(list=p, pos=.blotter))
+    try(rm(IBM))
+  })
+
   currency("USD")
   symbols <- c("IBM")
   for (symbol in symbols){
     stock(symbol, currency="USD", multiplier=1)
   }
-  data(IBM)                               # data included in package
+  data(IBM, package="blotter")
 
   # Initialize a portfolio object 'p'
   # Creating portfolio:
-  p = initPortf(symbols=symbols)
+  p <- initPortf("runitAddTxn", symbols=symbols)
 
   # 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)
+  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)
 
   portfolio <- getPortfolio(p)
-  transactions <- portfolio[["IBM"]][["txn"]]
-  checkEquals(13, sum(transactions$Txn.Fees))
+  transactions <- portfolio$symbols[["IBM"]]$txn
+  checkEquals(-13, sum(transactions$Txn.Fees))
   checkEquals(0, sum(transactions$Txn.Qty))
 
   # TODO: fix bug in calcPortfSummary
   # summary <- calcPortfSummary(portfolio)
 }
 
-.tearDown <- function() {
-  rm(list=ls(all=TRUE))
-  .blotter <- new.env()
-  .instrument <- new.env()
-}

Modified: pkg/blotter/tests/unitTests/runitCalcValues.R
===================================================================
--- pkg/blotter/tests/unitTests/runitCalcValues.R	2016-01-16 13:31:12 UTC (rev 1731)
+++ pkg/blotter/tests/unitTests/runitCalcValues.R	2016-01-16 14:48:02 UTC (rev 1732)
@@ -1,17 +1,18 @@
 # Author: Peter Carl, RUnit port by Ben McCann
 
 test.calcTxnValue <- function() {
-  checkEquals(99, calcTxnValue(TxnQty=10, TxnPrice=10, TxnFees=1))
+  checkEquals(99, blotter:::.calcTxnValue(TxnQty=10, TxnPrice=10, TxnFees=1))
 }
 
 test.calcTxnAvgCost <- function() {
-  checkEquals(9.9, calcTxnAvgCost(TxnValue=99, TxnQty=10))
+  checkEquals(9.9, blotter:::.calcTxnAvgCost(TxnValue=99, TxnQty=10))
 }
 
 test.calcPosAvgCost <- function() {
-  checkEquals(101, calcPosAvgCost(PrevPosQty=10, PrevPosAvgCost=100, TxnValue=1020, PosQty=20))
+  checkEquals(101, blotter:::.calcPosAvgCost(PrevPosQty=10, PrevPosAvgCost=100, TxnValue=1020, PosQty=20))
 }
 
-test.calcRealizedPL <- function() {
-  checkEquals(11.2, calcRealizedPL(TxnQty=-10, TxnAvgCost=101.1, PrevPosAvgCost=99.98, PosQty=40, PrevPosQty=50))
+test.calcPosAvgCost_C <- function() {
+  checkEquals(101, blotter:::.calcPosAvgCost_C(PrevPosQty=10, PrevPosAvgCost=100, TxnValue=1020, PosQty=20))
 }
+

Modified: pkg/blotter/tests/unitTests/runitUpdatePortf.R
===================================================================
--- pkg/blotter/tests/unitTests/runitUpdatePortf.R	2016-01-16 13:31:12 UTC (rev 1731)
+++ pkg/blotter/tests/unitTests/runitUpdatePortf.R	2016-01-16 14:48:02 UTC (rev 1732)
@@ -1,34 +1,37 @@
 # Author: Peter Carl, RUnit port by Ben McCann
 
-.tearDown <- function() {
-  rm(list=ls(all=TRUE, pos=.blotter), pos=.blotter)
-  rm(list=ls(all=TRUE, pos=.instrument), pos=.instrument)
-}
+test.txnFees <- function() {
+  on.exit({
+    # 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))
+  })
 
-test.txnFees <- function() {
-  data(IBM)                               # data included in package
   currency("USD")
   symbols <- c("IBM")
   for (symbol in symbols){
     stock(symbol, currency="USD", multiplier=1)
   }
+  data(IBM, package="blotter")
 
   ## simple portfolio with one transaction
-  p1 <- initPortf(name="p1", symbols=symbols)
-  p1 <- addTxn(Portfolio="p1", Symbol="IBM", TxnDate='2007-01-04', TxnQty=100, TxnPrice=96.5, TxnFees=0.05*100)
-  p1 <- updatePortf(Portfolio="p1", Dates='2007-01-03::2007-01-10')
-  a1 <- initAcct(name="a1", portfolios="p1")
+  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 <- updatePortf(Portfolio="p1runitUpdatePortf", Dates='2007-01-03::2007-01-10')
+  a1 <- initAcct(name="a1runitUpdatePortf", portfolios="p1runitUpdatePortf")
   a1 <- updateAcct(a1,'2007-01')
   a1 <- updateEndEq(a1,'2007-01')
 
   ## (really) simple transaction cost function
-  fiveCents <- function(qty, prc) return(0.05*qty)
-  p2 <- initPortf(name="p2", symbols=symbols)
-  p2 <- addTxn(Portfolio="p2", Symbol="IBM", TxnDate='2007-01-04', TxnQty=100, TxnPrice=96.5, TxnFees=fiveCents)
-  p2 <- updatePortf(Portfolio="p2", Dates='2007-01-03::2007-01-10')
-  a2 <- initAcct(name="a2", portfolios="p2")
+  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 <- updatePortf(Portfolio="p2runitUpdatePortf", Dates='2007-01-03::2007-01-10')
+  a2 <- initAcct(name="a2runitUpdatePortf", portfolios="p2runitUpdatePortf")
   a2 <- updateAcct(a2,'2007-01')
   a2 <- updateEndEq(a2,'2007-01')
 
-  checkEquals(getAccount('a1')$summary$End.Eq, getAccount('a2')$summary$End.Eq)
+  checkEquals(getAccount(a1)$summary$End.Eq, getAccount(a2)$summary$End.Eq)
 }



More information about the Blotter-commits mailing list