[Blotter-commits] r373 - in pkg/blotter: R tests
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Aug 16 20:04:41 CEST 2010
Author: braverock
Date: 2010-08-16 20:04:41 +0200 (Mon, 16 Aug 2010)
New Revision: 373
Added:
pkg/blotter/tests/runitUpdatePortf.R
Modified:
pkg/blotter/R/updatePosPL.R
pkg/blotter/tests/runitAddTxn.R
pkg/blotter/tests/runitCalcValues.R
pkg/blotter/tests/testSuite.R
Log:
- fix edge case bug in updatePosPL (first bug spotted by a regression test!)
- update tests with new code from Ben McCann (thanks!)
Modified: pkg/blotter/R/updatePosPL.R
===================================================================
--- pkg/blotter/R/updatePosPL.R 2010-08-16 15:30:39 UTC (rev 372)
+++ pkg/blotter/R/updatePosPL.R 2010-08-16 18:04:41 UTC (rev 373)
@@ -114,7 +114,7 @@
PrevPosValue = 0
} else {
PrevPosQty = getPosQty(pname, Symbol, as.character(PrevDate))
- PrevPosValue <- as.numeric(Portfolio[[Symbol]]$posPL[PrevDate, 'Pos.Value'])
+ ifelse(PrevPosQty != 0 , PrevPosValue <- as.numeric(Portfolio[[Symbol]]$posPL[PrevDate, 'Pos.Value']), PrevPosValue<-0)
}
ifelse(PrevPosQty==0, PrevClosePrice <- 0 , PrevClosePrice <- as.numeric(getPrice(Prices)[as.character(PrevDate)]))
Modified: pkg/blotter/tests/runitAddTxn.R
===================================================================
--- pkg/blotter/tests/runitAddTxn.R 2010-08-16 15:30:39 UTC (rev 372)
+++ pkg/blotter/tests/runitAddTxn.R 2010-08-16 18:04:41 UTC (rev 373)
@@ -1,3 +1,4 @@
+# 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
Modified: pkg/blotter/tests/runitCalcValues.R
===================================================================
--- pkg/blotter/tests/runitCalcValues.R 2010-08-16 15:30:39 UTC (rev 372)
+++ pkg/blotter/tests/runitCalcValues.R 2010-08-16 18:04:41 UTC (rev 373)
@@ -1,4 +1,4 @@
-# Copyright 2010. All rights reserved.
+# Author: Peter Carl, RUnit port by Ben McCann
test.calcTxnValue <- function() {
checkEquals(99, calcTxnValue(TxnQty=10, TxnPrice=10, TxnFees=1))
Added: pkg/blotter/tests/runitUpdatePortf.R
===================================================================
--- pkg/blotter/tests/runitUpdatePortf.R (rev 0)
+++ pkg/blotter/tests/runitUpdatePortf.R 2010-08-16 18:04:41 UTC (rev 373)
@@ -0,0 +1,34 @@
+# 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() {
+ data(IBM) # data included in package
+ currency("USD")
+ symbols <- c("IBM")
+ for (symbol in symbols){
+ stock(symbol, currency="USD", multiplier=1)
+ }
+
+ ## 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")
+ 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")
+ a2 <- updateAcct(a2,'2007-01')
+ a2 <- updateEndEq(a2,'2007-01')
+
+ checkEquals(getAccount('a1')$TOTAL$End.Eq, getAccount('a2')$TOTAL$End.Eq)
+}
Modified: pkg/blotter/tests/testSuite.R
===================================================================
--- pkg/blotter/tests/testSuite.R 2010-08-16 15:30:39 UTC (rev 372)
+++ pkg/blotter/tests/testSuite.R 2010-08-16 18:04:41 UTC (rev 373)
@@ -1,3 +1,5 @@
+# RUnit blotter port/framework by Ben McCann
+
# Set working directory to this directory before running
# Load deps
@@ -3,7 +5,8 @@
library(RUnit)
library(quantmod)
+library(blotter)
-# Load blotter
-for (file in dir("../R", pattern="*.R", full.names=TRUE)) {
+#Load blotter files. When is this necessary?
+for (file in list.files("../R", pattern="*.R$", full.names=TRUE)) {
source(file)
}
More information about the Blotter-commits
mailing list