[Blotter-commits] r1282 - pkg/quantstrat/tests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Dec 12 18:44:58 CET 2012


Author: milktrader
Date: 2012-12-12 18:44:57 +0100 (Wed, 12 Dec 2012)
New Revision: 1282

Added:
   pkg/quantstrat/tests/bbands_version_for_tests.r
   pkg/quantstrat/tests/svunit_bbands.r
   pkg/quantstrat/tests/testthat_bbands.r
Log:
bbands trade stat tests

Added: pkg/quantstrat/tests/bbands_version_for_tests.r
===================================================================
--- pkg/quantstrat/tests/bbands_version_for_tests.r	                        (rev 0)
+++ pkg/quantstrat/tests/bbands_version_for_tests.r	2012-12-12 17:44:57 UTC (rev 1282)
@@ -0,0 +1,97 @@
+require(quantstrat)
+suppressWarnings(rm("order_book.bbands",pos=.strategy))
+suppressWarnings(rm("account.bbands","portfolio.bbands",pos=.blotter))
+suppressWarnings(rm("account.st","portfolio.st","stock.str","stratBBands","initDate","initEq",'start_t','end_t'))
+
+###################### LOAD TTRC ######################
+
+data('ttrc')
+TTRC = xts(ttrc[,-1],ttrc[,1])
+
+###################### DEFINE VARIABLES #################
+SD = 2 
+N = 20
+currency('USD')
+stock('TTRC', currency='USD', multiplier=1)
+initDate='1984-12-31'
+initEq=1000000
+portfolio.st='bbands'
+account.st='bbands'
+
+############################ INITIALIZE AND POSITION LOGIC ################
+
+initPortf(portfolio.st,symbols='TTRC', initDate=initDate)
+initAcct(account.st,portfolios='bbands', initDate=initDate)
+initOrders(portfolio=portfolio.st,initDate=initDate)
+addPosLimit(portfolio.st, 'TTRC', initDate, 200, 2 ) #set max pos
+stratBBands <- strategy("bbands")
+
+############################ INDICATOR ############################
+
+stratBBands <- add.indicator(strategy = stratBBands,
+                             name = "BBands", 
+                             arguments = list(HLC = quote(HLC(mktdata)), 
+                                              maType='SMA'))
+
+
+############################ SIGNALS ##############################
+
+stratBBands <- add.signal(stratBBands,
+                          name="sigCrossover",
+                          arguments = list(columns=c("Close","up"),
+                                           relationship="gt"),
+                          label="Cl.gt.UpperBand")
+
+stratBBands <- add.signal(stratBBands,
+                          name="sigCrossover",
+                          arguments = list(columns=c("Close","dn"),
+                                           relationship="lt"),
+                          label="Cl.lt.LowerBand")
+
+stratBBands <- add.signal(stratBBands,
+                          name="sigCrossover",
+                          arguments = list(columns=c("High","Low","mavg"),
+                                           relationship="op"),
+                          label="Cross.Mid")
+
+############################# RULES ###############################
+
+
+stratBBands <- add.rule(stratBBands,
+                        name='ruleSignal', 
+                        arguments = list(sigcol="Cl.gt.UpperBand",
+                                         sigval=TRUE, 
+                                         orderqty=-100, 
+                                         ordertype='market', 
+                                         orderside=NULL, 
+                                         threshold=NULL,
+                                         osFUN=osMaxPos),
+                        type='enter')
+
+stratBBands <- add.rule(stratBBands,
+                        name='ruleSignal', 
+                        arguments = list(sigcol="Cl.lt.LowerBand",
+                                         sigval=TRUE, 
+                                         orderqty= 100, 
+                                         ordertype='market', 
+                                         orderside=NULL, 
+                                         threshold=NULL,
+                                         osFUN=osMaxPos),
+                        type='enter')
+
+stratBBands <- add.rule(stratBBands,
+                        name='ruleSignal', 
+                        arguments = list(sigcol="Cross.Mid",
+                                         sigval=TRUE, 
+                                         orderqty= 'all', 
+                                         ordertype='market', 
+                                         orderside=NULL, 
+                                         threshold=NULL,
+                                         osFUN=osMaxPos),
+                        type='exit')
+
+######################## APPLY STRAT  ###################################################
+
+out<-try(applyStrategy(strategy=stratBBands , portfolios='bbands',parameters=list(sd=SD,n=N)) )
+
+updatePortf(Portfolio='bbands',Dates=paste('::',as.Date(Sys.time()),sep=''))

Added: pkg/quantstrat/tests/svunit_bbands.r
===================================================================
--- pkg/quantstrat/tests/svunit_bbands.r	                        (rev 0)
+++ pkg/quantstrat/tests/svunit_bbands.r	2012-12-12 17:44:57 UTC (rev 1282)
@@ -0,0 +1,24 @@
+
+require(svUnit)
+
+source('bbands_version_for_tests.r')
+
+bbands = 'Testing consistent trade stats'
+
+test(bbands) = function() {
+
+  stats = tradeStats(portfolio.st)
+
+  checkEquals( stats$Num.Txns, 622)
+  checkEquals( stats$Num.Trades, 258)
+  checkEquals( stats$Net.Trading.PL, -204)
+  checkEquals( stats$Largest.Winner, 417)
+  checkEquals( stats$Largest.Loser, -824)
+  checkEquals( stats$maxDrawdown, -2618)
+}
+
+
+clearLog()
+runTest(bbands)
+Log()
+#summary(Log())

Added: pkg/quantstrat/tests/testthat_bbands.r
===================================================================
--- pkg/quantstrat/tests/testthat_bbands.r	                        (rev 0)
+++ pkg/quantstrat/tests/testthat_bbands.r	2012-12-12 17:44:57 UTC (rev 1282)
@@ -0,0 +1,55 @@
+require(testthat)
+
+################## bee.r  #########################
+
+source('bbands_version_for_tests.r')
+
+stratstat   = tradeStats(portfolio.st)
+
+Txns      = stratstat$Num.Txns
+Trades    = stratstat$Num.Trades
+NetPL     = stratstat$Net.Trading.PL
+LWinner   = stratstat$Largest.Winner
+LLoser    = stratstat$Largest.Loser
+MaxDD     = stratstat$maxDrawdown
+
+suppressWarnings(rm("order_book.bbands",pos=.strategy))
+suppressWarnings(rm("account.bbands","portfolio.bbands",pos=.blotter))
+suppressWarnings(rm("account.st","portfolio.st","stock.str","stratBBands","initDate","initEq",'start_t','end_t'))
+
+######################## RUN TEST SUITE #######################
+
+context('Consistency across trade statistics')
+
+test_that('Number of transactions is consistent', {
+
+  expect_that(Txns, equals(622))
+})
+
+test_that('Number of the number of trades is consistent', {
+
+  expect_that(Trades, equals(258))
+})
+
+test_that('Net Trading PL is consistent', {
+
+  expect_that(NetPL, equals(-204))
+})
+
+test_that('Largest Winner is consistent', {
+
+  expect_that(LWinner, equals(417))
+})
+
+test_that('Largest Loser is consistent', {
+
+  expect_that(LLoser, equals(-824))
+})
+
+test_that('Max Drawdown is consistent', {
+
+  expect_that(MaxDD, equals(-2618 ))
+       
+})
+
+



More information about the Blotter-commits mailing list