[Blotter-commits] r1335 - pkg/quantstrat/inst/tests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jan 3 05:10:45 CET 2013


Author: milktrader
Date: 2013-01-03 05:10:44 +0100 (Thu, 03 Jan 2013)
New Revision: 1335

Added:
   pkg/quantstrat/inst/tests/test_yellow.R
   pkg/quantstrat/inst/tests/yellow.R
Removed:
   pkg/quantstrat/inst/tests/aqua.R
   pkg/quantstrat/inst/tests/test_book.R
Log:
added 2 suites of tests for yellow, removed aqua and test_book

Deleted: pkg/quantstrat/inst/tests/aqua.R
===================================================================
--- pkg/quantstrat/inst/tests/aqua.R	2013-01-02 19:50:51 UTC (rev 1334)
+++ pkg/quantstrat/inst/tests/aqua.R	2013-01-03 04:10:44 UTC (rev 1335)
@@ -1,106 +0,0 @@
-#!/usr/bin/Rscript --vanilla
-#
-# aqua.R
-#
-# this produces trade statistics that can be compared 
-# with results from other frameworks
-#
-# the data is SPX daily data from 1/1/1970 to 12/31/1972
-#
-# copyright (c) 2009-2012, Algorithm Alpha, LLC
-# Licensed GPL-2
-#
-################### LOAD QUANTSTRAT #################
-
-require(quantstrat)
-
-###################### LOAD DATA ######################
-
-data(spx)
-
-############################# DEFINE VARIABLES ##############################
-
-port          = 'aquaPort'
-acct          = 'aquaAcct'
-initEq        = 1e6
-initDate      = '1969-12-31'
-fast          = 10 
-slow          = 30
-
-############################# INITIALIZE ####################################
-
-currency('USD')
-stock('spx' ,currency='USD', multiplier=1)
-initPortf(port, 'spx', initDate=initDate)
-initAcct(acct, port, initEq=initEq, initDate=initDate)
-initOrders(port, initDate=initDate )
-aqua = strategy(port)
-
-############################# INDICATORS ####################################
-
-aqua <- add.indicator(
-                     strategy  = aqua, 
-                     name      = 'SMA', 
-                     arguments = list(x=quote(Cl(mktdata)), 
-                                      n=fast),
-                     label     = 'fast' )
-
-aqua <- add.indicator(
-                     strategy  = aqua, 
-                     name      = 'SMA', 
-                     arguments = list(x=quote(Cl(mktdata)), 
-                                      n=slow),
-                     label     = 'slow' )
-
-############################# SIGNALS #######################################
-
-aqua <- add.signal(
-                  strategy  = aqua,
-                  name      = 'sigCrossover',
-                  arguments = list(columns=c('fast','slow'), 
-                                   relationship='lt'),
-                  label     = 'fast.lt.slow')
-
-aqua <- add.signal(
-                  strategy  = aqua,
-                  name      = 'sigCrossover',
-                  arguments = list(columns=c('fast','slow'),
-                                   relationship='gt'),
-                  label     = 'fast.gt.slow')
-
-############################# RULES #########################################
-
-aqua <- add.rule(
-                strategy  = aqua,
-                name      = 'ruleSignal',
-                arguments = list(sigcol    = 'fast.gt.slow',
-                                 sigval    = TRUE,
-                                 orderqty  = 100,
-                                 ordertype = 'market',
-                                 orderside = 'long'),
-
-                type      = 'enter',
-                label     = 'EnterLONG')
-aqua <- add.rule(
-                strategy  = aqua,
-                name      = 'ruleSignal',
-                arguments = list(sigcol    = 'fast.lt.slow',
-                                 sigval    = TRUE,
-                                 orderqty  = 'all',
-                                 ordertype = 'market',
-                                 orderside = 'long'),
-                type      = 'exit',
-                label     = 'ExitLONG')
-
-############################# APPLY STRATEGY ################################
-
-applyStrategy(aqua, port, prefer='Open', verbose=FALSE)
-
-############################# UPDATE ########################################
-
-updatePortf(port, 'spx', Date=paste('::',as.Date(Sys.time()),sep=''))
-
-########################### CONTAINERS CALLED IN TESTING #####################
-
-book = getOrderBook(port)
-

Deleted: pkg/quantstrat/inst/tests/test_book.R
===================================================================
--- pkg/quantstrat/inst/tests/test_book.R	2013-01-02 19:50:51 UTC (rev 1334)
+++ pkg/quantstrat/inst/tests/test_book.R	2013-01-03 04:10:44 UTC (rev 1335)
@@ -1,30 +0,0 @@
-require(testthat)
-
-######################## DEFINE VARIABLES TO BE TESTED #######
-
-source('blue.R')
-
-rule   = book$bluePort$spx[,'Rule']
-status = book$bluePort$spx[,'Order.Status']
-qty    = book$bluePort$spx[,'Order.Qty']
-
-
-
-######################## RUN TEST SUITE ######################
-
-context("Order Book is consistent ")
-
-test_that("The first entry is not a trade", 
-          { expect_that(as.character(rule[1]) =="", is_true()) })
-
-test_that("The first trade is a long entry", 
-          { expect_that(as.character(rule[3]) =="EnterLONG", is_true()) })
-
-test_that("The first short exit order is rejected", 
-          { expect_that(as.character(status[2]) =="rejected", is_true()) })
-
-test_that("The first long signal is entered long", 
-          { expect_that(as.character(qty[3]) =="100", is_true()) })
-
-test_that("The first short signal is entered short", 
-          { expect_that(as.character(qty[5]) =="-100", is_true()) })

Added: pkg/quantstrat/inst/tests/test_yellow.R
===================================================================
--- pkg/quantstrat/inst/tests/test_yellow.R	                        (rev 0)
+++ pkg/quantstrat/inst/tests/test_yellow.R	2013-01-03 04:10:44 UTC (rev 1335)
@@ -0,0 +1,116 @@
+require(testthat)
+#source('nuke_environments.R')
+
+######################## DEFINE VARIABLES TO BE TESTED #######
+
+source('yellow.R')
+
+qty    = book$yellowPort$spx[,'Order.Qty']
+price  = book$yellowPort$spx[,'Order.Price']
+type   = book$yellowPort$spx[,'Order.Type']
+side   = book$yellowPort$spx[,'Order.Side']
+status = book$yellowPort$spx[,'Order.Status']
+fees   = book$yellowPort$spx[,'Txn.Fees']
+rule   = book$yellowPort$spx[,'Rule']
+
+
+
+######################## ORDER BOOK ######################
+
+context("Yellow order Book is consistent ")
+
+## quantity
+test_that("The first entry is 100", 
+          { expect_that(as.character(qty[2]) =="100", is_true()) })
+
+test_that("The first exit is all", 
+          { expect_that(as.character(qty[3]) =="all", is_true()) })
+## price
+test_that("The first entry price is 88.9", 
+          { expect_that(as.character(price[2]) =="88.9", is_true()) })
+
+test_that("The first exit price is 87.42", 
+          { expect_that(as.character(price[3]) =="87.42", is_true()) })
+## type
+test_that("The first trade is a market order", 
+          { expect_that(as.character(type[2]) =="market", is_true()) })
+## side
+test_that("The first trade is entered long", 
+          { expect_that(as.character(side[2]) =="long", is_true()) })
+## status
+test_that("The last trade is closed", 
+          { expect_that(as.character(status[20]) =="closed", is_true()) })
+## fees
+test_that("The first trade has no transaction fees", 
+          { expect_that(as.character(fees[2]) =="0", is_true()) })
+## rule
+test_that("The first transaction is an enter long", 
+          { expect_that(as.character(rule[2]) =="EnterLONG", is_true()) })
+
+test_that("The second transaction is an exit long", 
+          { expect_that(as.character(rule[3]) =="ExitLONG", is_true()) })
+
+######################## STATS  ######################
+
+context("Yellow trade statistics are consistent ")
+
+test_that("Num.Txns is 19", 
+          { expect_that(stats$Num.Txns , equals(19)) })
+test_that("Num.Trades is 9", 
+          { expect_that(stats$Num.Trades , equals(9)) })
+test_that("Net.Trading.PL is -36", 
+          { expect_that(stats$Net.Trading.PL, equals(-36)) })
+test_that("Avg.Trade.PL is -4", 
+          { expect_that(stats$Avg.Trade.PL, equals(-4)) })
+test_that("Med.Trade.PL is 6", 
+          { expect_that(stats$Med.Trade.PL, equals(6)) })
+test_that("Largest.Winnner is 581", 
+          { expect_that(stats$Largest.Winner, equals(581)) })
+test_that("Largest.Loser is -1064", 
+          { expect_that(stats$Largest.Loser, equals(-1064)) })
+test_that("Gross.Profits is 1425", 
+          { expect_that(stats$Gross.Profits, equals(1425)) })
+test_that("Gross.Losses is -1461", 
+          { expect_that(stats$Gross.Losses, equals(-1461)) })
+test_that("Std.Dev.Trade.PL is 477.7599", 
+          { expect_equal(stats$Std.Dev.Trade.PL, 477.7599, .0001) })
+test_that("Percent.Positive is 55.55556", 
+          { expect_equal(stats$Percent.Positive, 55.55556, .0001) })
+test_that("Percent.Negative is 44.44444", 
+          { expect_equal(stats$Percent.Negative, 44.44444, .0001) })
+test_that("Profit.Factor is 0.9753593", 
+          { expect_equal(stats$Profit.Factor, 0.9753593, .0001) })
+test_that("Avg.Win.Trade is 285", 
+          { expect_that(stats$Avg.Win.Trade, equals(285)) })
+test_that("Med.Win.Trade is 221", 
+          { expect_that(stats$Med.Win.Trade, equals(221)) })
+test_that("Avg.Losing.Trade is -365.25", 
+          { expect_that(stats$Avg.Losing.Trade, equals(-365.25)) })
+test_that("Med.Losing.Trade is -159.5", 
+          { expect_that(stats$Med.Losing.Trade, equals(-159.5)) })
+test_that("Avg.Daily.PL is -4", 
+          { expect_that(stats$Avg.Daily.PL, equals(-4)) })
+test_that("Med.Daily.PL is 6", 
+          { expect_that(stats$Med.Daily.PL, equals(6)) })
+test_that("Std.Dev.Daily.PL is 477.7599", 
+          { expect_equal(stats$Std.Dev.Daily.PL, 477.7599, .0001) })
+test_that("Max.Drawdown is -1789", 
+          { expect_that(stats$Max.Drawdown, equals(-1789)) })
+test_that("Profit.to.Max.Draw is -0.02012297", 
+          { expect_equal(stats$Profit.to.Max.Draw, -0.02012297, .0001) })
+test_that("Avg.WinLoss.Ratio is 0.7802875", 
+          { expect_equal(stats$Avg.WinLoss.Ratio, 0.7802875, .0001) })
+test_that("Med.WinLoss.Ratio is 1.38558", 
+          { expect_equal(stats$Med.WinLoss.Ratio , 1.38558, .0001) })
+test_that("Max.Equity is 390", 
+          { expect_that(stats$Max.Equity , equals(390)) })
+test_that("Min.Equity is -1539", 
+          { expect_that(stats$Min.Equity , equals(-1539)) })
+test_that("End.Equity is -36", 
+          { expect_that(stats$End.Equity , equals(-36)) })
+
+
+
+
+ # test_that("K.Ratio is 1", 
+ #           { expect_that(stats$K.Ratio , equals(1)) })

Added: pkg/quantstrat/inst/tests/yellow.R
===================================================================
--- pkg/quantstrat/inst/tests/yellow.R	                        (rev 0)
+++ pkg/quantstrat/inst/tests/yellow.R	2013-01-03 04:10:44 UTC (rev 1335)
@@ -0,0 +1,105 @@
+#!/usr/bin/Rscript --vanilla
+#
+# yellow.R
+#
+# long only simple moving average crossover
+#
+# SPX daily data is from 1/1/1970 to 12/31/1972
+#
+# copyright (c) 2009-2013, Algorithm Alpha, LLC
+# Licensed GPL-2
+#
+################### LOAD QUANTSTRAT #################
+
+require(quantstrat)
+
+###################### LOAD DATA ######################
+
+data(spx)
+
+############################# DEFINE VARIABLES ##############################
+
+port          = 'yellowPort'
+acct          = 'yellowAcct'
+initEq        = 1e6
+initDate      = '1969-12-31'
+fast          = 10 
+slow          = 30
+
+############################# INITIALIZE ####################################
+
+currency('USD')
+stock('spx' ,currency='USD', multiplier=1)
+initPortf(port, 'spx', initDate=initDate)
+initAcct(acct, port, initEq=initEq, initDate=initDate)
+initOrders(port, initDate=initDate )
+yellow = strategy(port)
+
+############################# INDICATORS ####################################
+
+yellow <- add.indicator(
+                     strategy  = yellow, 
+                     name      = 'SMA', 
+                     arguments = list(x=quote(Cl(mktdata)), 
+                                      n=fast),
+                     label     = 'fast' )
+
+yellow <- add.indicator(
+                     strategy  = yellow, 
+                     name      = 'SMA', 
+                     arguments = list(x=quote(Cl(mktdata)), 
+                                      n=slow),
+                     label     = 'slow' )
+
+############################# SIGNALS #######################################
+
+yellow <- add.signal(
+                  strategy  = yellow,
+                  name      = 'sigCrossover',
+                  arguments = list(columns=c('fast','slow'), 
+                                   relationship='lt'),
+                  label     = 'fast.lt.slow')
+
+yellow <- add.signal(
+                  strategy  = yellow,
+                  name      = 'sigCrossover',
+                  arguments = list(columns=c('fast','slow'),
+                                   relationship='gt'),
+                  label     = 'fast.gt.slow')
+
+############################# RULES #########################################
+
+yellow <- add.rule(
+                strategy  = yellow,
+                name      = 'ruleSignal',
+                arguments = list(sigcol    = 'fast.gt.slow',
+                                 sigval    = TRUE,
+                                 orderqty  = 100,
+                                 ordertype = 'market',
+                                 orderside = 'long'),
+
+                type      = 'enter',
+                label     = 'EnterLONG')
+yellow <- add.rule(
+                strategy  = yellow,
+                name      = 'ruleSignal',
+                arguments = list(sigcol    = 'fast.lt.slow',
+                                 sigval    = TRUE,
+                                 orderqty  = 'all',
+                                 ordertype = 'market',
+                                 orderside = 'long'),
+                type      = 'exit',
+                label     = 'ExitLONG')
+
+############################# APPLY STRATEGY ################################
+
+applyStrategy(yellow, port, prefer='Open', verbose=FALSE)
+
+############################# UPDATE ########################################
+
+updatePortf(port, 'spx', Date=paste('::',as.Date(Sys.time()),sep=''))
+
+########################### CONTAINERS CALLED IN TESTING #####################
+
+book = getOrderBook(port)
+stats = tradeStats(port)



More information about the Blotter-commits mailing list