[Blotter-commits] r1390 - pkg/quantstrat/inst/tests
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Feb 7 18:26:39 CET 2013
Author: milktrader
Date: 2013-02-07 18:26:39 +0100 (Thu, 07 Feb 2013)
New Revision: 1390
Added:
pkg/quantstrat/inst/tests/test_blotter_break.R
Log:
blotter test added to quantstrat framework for now
Added: pkg/quantstrat/inst/tests/test_blotter_break.R
===================================================================
--- pkg/quantstrat/inst/tests/test_blotter_break.R (rev 0)
+++ pkg/quantstrat/inst/tests/test_blotter_break.R 2013-02-07 17:26:39 UTC (rev 1390)
@@ -0,0 +1,62 @@
+# test_blotter_break.R
+# test for breaking trade with another trade that day in blotter
+# Author: Brian M Bielinski
+
+Sys.setenv(TZ = "GMT")
+require(blotter)
+require(testthat)
+
+port = "testBreak"
+acct = "testBreak"
+symbol = c("IBM")
+
+data(IBM)
+
+startDate = first(index(IBM))
+endDate = last(index(IBM))
+
+lines = "date,shrs,price,symbol
+2007-01-10,100,98.0,IBM
+2007-01-16,-200,99.5,IBM
+2007-01-16,150,99.45,IBM
+2007-01-18,-50,99.0,IBM"
+con = textConnection(lines)
+tt.trades = read.csv(con, as.is = TRUE)
+
+tt.trades[,"date"] = make.time.unique(as.POSIXct(tt.trades[,"date"]))
+
+currency("USD")
+stock(symbol,"USD")
+
+initPortf(port,
+ symbol,
+ initDate = startDate)
+initAcct(port,
+ portfolios = c(port),
+ initDate = startDate,
+ initEq=10^6)
+
+for(i in 1:nrow(tt.trades)){
+ addTxn(port,Symbol = tt.trades[i,"symbol"],
+ TxnDate = tt.trades[i,"date"],
+ TxnPrice = tt.trades[i,"price"],
+ TxnQty = tt.trades[i,"shrs"])
+}
+
+updatePortf(port)
+updateAcct(acct)
+updateEndEq(acct)
+
+context("check that breaking trade with another trade that day is accounted for correctly.")
+
+# should be flat
+test_that("input trades position is flat",
+ expect_that(sum(tt.trades[,"shrs"]) == 0, is_true()))
+test_that("recorded trades position in Txns is flat",
+ expect_that(sum(getTxns(port,"IBM")[,"Txn.Qty"]) == 0, is_true()))
+test_that("times of the trades are unique",
+ expect_that(length(tt.trades[,"date"]) == length(unique(tt.trades[,"date"])),is_true()))
+test_that("calculated position is flat",
+ expect_that(getPosQty(port,"IBM",Date = endDate) == 0, is_true()))
+
+#chart.Posn(port,"IBM")
More information about the Blotter-commits
mailing list