[Blotter-commits] r1287 - in pkg/quantstrat: inst inst/tests sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Dec 12 23:47:25 CET 2012


Author: milktrader
Date: 2012-12-12 23:47:25 +0100 (Wed, 12 Dec 2012)
New Revision: 1287

Added:
   pkg/quantstrat/inst/tests/
   pkg/quantstrat/inst/tests/bbands_version_for_tests.R
   pkg/quantstrat/inst/tests/bee_version_for_tests.R
   pkg/quantstrat/inst/tests/testthat_bee.R
Removed:
   pkg/quantstrat/sandbox/tests/
Modified:
   pkg/quantstrat/inst/tests/bbands_version_for_tests.r
   pkg/quantstrat/inst/tests/testthat_bbands.r
Log:
added tests for bee and moved to inst/tests

Added: pkg/quantstrat/inst/tests/bbands_version_for_tests.R
===================================================================
--- pkg/quantstrat/inst/tests/bbands_version_for_tests.R	                        (rev 0)
+++ pkg/quantstrat/inst/tests/bbands_version_for_tests.R	2012-12-12 22:47:25 UTC (rev 1287)
@@ -0,0 +1,104 @@
+#################### CLEANUP PREVIOUS TEST ######################
+
+suppressWarnings(rm(list=ls(.strategy), pos=.strategy))
+suppressWarnings(rm(list=ls(.blotter), pos=.blotter))
+suppressWarnings(rm(list=ls()))
+
+################### LOAD QUANTSTRAT #################
+
+suppressMessages(require(quantstrat))
+
+###################### LOAD TTRC ######################
+
+data('ttrc')
+TTRC = xts(ttrc[,-1],ttrc[,1])
+TTRC = head(TTRC, n=500)
+
+###################### 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=''))

Modified: pkg/quantstrat/inst/tests/bbands_version_for_tests.r
===================================================================
--- pkg/quantstrat/sandbox/tests/bbands_version_for_tests.r	2012-12-12 20:47:38 UTC (rev 1286)
+++ pkg/quantstrat/inst/tests/bbands_version_for_tests.r	2012-12-12 22:47:25 UTC (rev 1287)
@@ -1,9 +1,13 @@
-require(quantstrat)
+#################### CLEANUP PREVIOUS TEST ######################
 
-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'))
+suppressWarnings(rm(list=ls(.strategy), pos=.strategy))
+suppressWarnings(rm(list=ls(.blotter), pos=.blotter))
+suppressWarnings(rm(list=ls()))
 
+################### LOAD QUANTSTRAT #################
+
+suppressMessages(require(quantstrat))
+
 ###################### LOAD TTRC ######################
 
 data('ttrc')
@@ -11,6 +15,7 @@
 TTRC = head(TTRC, n=500)
 
 ###################### DEFINE VARIABLES #################
+
 SD = 2 
 N = 20
 currency('USD')

Added: pkg/quantstrat/inst/tests/bee_version_for_tests.R
===================================================================
--- pkg/quantstrat/inst/tests/bee_version_for_tests.R	                        (rev 0)
+++ pkg/quantstrat/inst/tests/bee_version_for_tests.R	2012-12-12 22:47:25 UTC (rev 1287)
@@ -0,0 +1,136 @@
+
+#################### CLEANUP PREVIOUS TEST ######################
+
+suppressWarnings(rm(list=ls(.strategy), pos=.strategy))
+suppressWarnings(rm(list=ls(.blotter), pos=.blotter))
+suppressWarnings(rm(list=ls()))
+
+################### LOAD QUANTSTRAT #################
+
+suppressMessages(require(quantstrat))
+
+###################### LOAD TTRC ######################
+
+data('ttrc')
+TTRC = xts(ttrc[,-1],ttrc[,1])
+
+############################# DEFINE VARIABLES ##############################
+
+sym           = head(TTRC, n=500)
+port          = 'bug'
+acct          = 'colony'
+initEq        = 100000
+initDate      = '1984-12-31'
+fast          = 10
+slow          = 30
+sd            = 0.5
+
+
+############################# INITIALIZE ####################################
+
+currency('USD')
+stock('sym' ,currency='USD', multiplier=1)
+initPortf(port, 'sym', initDate=initDate)
+initAcct(acct, port, initEq=initEq, initDate=initDate)
+initOrders(port, initDate=initDate )
+bee = strategy(port)
+
+############################# MAX POSITION LOGIC ############################
+
+addPosLimit(
+            portfolio=port,
+            symbol='sym', 
+            timestamp=initDate,  
+            maxpos=100)
+
+
+############################# INDICATORS ####################################
+
+bee <- add.indicator( 
+                     strategy  = bee, 
+                     name      = 'BBands', 
+                     arguments = list(HLC=quote(HLC(mktdata)), 
+                                      n=slow, 
+                                      sd=sd))
+
+bee <- add.indicator(
+                     strategy  = bee, 
+                     name      = 'SMA', 
+                     arguments = list(x=quote(Cl(mktdata)), 
+                                      n=fast),
+                     label     = 'fast' )
+
+############################# SIGNALS #######################################
+
+bee <- add.signal(
+                  strategy  = bee,
+                  name      = 'sigCrossover',
+                  arguments = list(columns=c('fast','dn'), 
+                                   relationship='lt'),
+                  label     = 'fast.lt.dn')
+
+bee <- add.signal(
+                  strategy  = bee,
+                  name      = 'sigCrossover',
+                  arguments = list(columns=c('fast','up'),
+                                   relationship='gt'),
+                  label     = 'fast.gt.up')
+
+############################# RULES #########################################
+
+bee <- add.rule(
+                strategy  = bee,
+                name      = 'ruleSignal',
+                arguments = list(sigcol    = 'fast.gt.up',
+                                 sigval    = TRUE,
+                                 orderqty  = 100,
+                                 ordertype = 'market',
+                                 orderside = 'long',
+                                 osFUN     = 'osMaxPos'),
+
+                type      = 'enter',
+                label     = 'EnterLONG')
+
+bee <- add.rule(
+                strategy  = bee,
+                name      = 'ruleSignal',
+                arguments = list(sigcol    = 'fast.lt.dn',
+                                 sigval    = TRUE,
+                                 orderqty  = 'all',
+                                 ordertype = 'market',
+                                 orderside = 'long'),
+                type      = 'exit',
+                label     = 'ExitLONG')
+
+bee <- add.rule(
+                strategy  = bee,
+                name      = 'ruleSignal',
+                arguments = list(sigcol     = 'fast.lt.dn',
+                                  sigval    = TRUE,
+                                  orderqty  =  -100,
+                                  ordertype = 'market',
+                                  orderside = 'short',
+                                  osFUN     = 'osMaxPos'),
+                type      = 'enter',
+                label     = 'EnterSHORT')
+
+bee <- add.rule(
+                strategy  = bee,
+                name      = 'ruleSignal',
+                arguments = list(sigcol     = 'fast.gt.up',
+                                 sigval     = TRUE,
+                                 orderqty   = 'all',
+                                 ordertype  = 'market',
+                                 orderside  = 'short'),
+                type      = 'exit',
+                label     = 'ExitSHORT')
+
+############################# APPLY STRATEGY ################################
+
+applyStrategy(bee, port, prefer='Open', verbose=FALSE)
+
+############################# UPDATE ########################################
+
+updatePortf(port, 'sym', Date=paste('::',as.Date(Sys.time()),sep=''))
+
+


Property changes on: pkg/quantstrat/inst/tests/bee_version_for_tests.R
___________________________________________________________________
Added: svn:executable
   + *

Modified: pkg/quantstrat/inst/tests/testthat_bbands.r
===================================================================
--- pkg/quantstrat/sandbox/tests/testthat_bbands.r	2012-12-12 20:47:38 UTC (rev 1286)
+++ pkg/quantstrat/inst/tests/testthat_bbands.r	2012-12-12 22:47:25 UTC (rev 1287)
@@ -19,7 +19,7 @@
 
 ######################## RUN TEST SUITE #######################
 
-context('Consistency across trade statistics')
+context('Consistent trade statistics for bbands.R')
 
 test_that('Number of transactions is consistent', {
 

Added: pkg/quantstrat/inst/tests/testthat_bee.R
===================================================================
--- pkg/quantstrat/inst/tests/testthat_bee.R	                        (rev 0)
+++ pkg/quantstrat/inst/tests/testthat_bee.R	2012-12-12 22:47:25 UTC (rev 1287)
@@ -0,0 +1,60 @@
+require(testthat)
+
+################## bee.r  #########################
+
+source('bee_version_for_tests.R')
+
+bing = ls()
+bang = ls(.strategy)
+boom = ls(.blotter)
+
+stratstat   = tradeStats(port)
+
+Txns      = stratstat$Num.Txns
+Trades    = stratstat$Num.Trades
+NetPL     = stratstat$Net.Trading.PL
+LWinner   = stratstat$Largest.Winner
+LLoser    = stratstat$Largest.Loser
+MaxDD     = stratstat$Max.Drawdown
+
+
+suppressWarnings(rm(list=bing))
+suppressWarnings(rm(list=bang, pos=.strategy))
+suppressWarnings(rm(list=boom, pos=.blotter))
+
+######################## RUN TEST SUITE #######################
+
+context('Consistent trade statistics for bee.R')
+
+test_that('Number of transactions is consistent', {
+
+  expect_that(Txns, equals(24))
+})
+
+test_that('Number of the number of trades is consistent', {
+
+  expect_that(Trades, equals(16))
+})
+
+test_that('Net Trading PL is consistent', {
+
+  expect_that(NetPL, equals(63))
+})
+
+test_that('Largest Winner is consistent', {
+
+  expect_that(LWinner, equals(12))
+})
+
+test_that('Largest Loser is consistent', {
+
+  expect_that(LLoser, equals(-32))
+})
+
+test_that('Max Drawdown is consistent', {
+
+  expect_that(MaxDD, equals(-867))
+       
+})
+
+



More information about the Blotter-commits mailing list