[Blotter-commits] r1237 - in pkg/quantstrat: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Nov 3 17:47:18 CET 2012


Author: braverock
Date: 2012-11-03 17:47:18 +0100 (Sat, 03 Nov 2012)
New Revision: 1237

Modified:
   pkg/quantstrat/DESCRIPTION
   pkg/quantstrat/R/applyStrategy.rebalancing.R
   pkg/quantstrat/R/ruleSignal.R
   pkg/quantstrat/R/rules.R
   pkg/quantstrat/man/applyStrategy.rebalancing.Rd
   pkg/quantstrat/man/ruleSignal.Rd
Log:
- fix support for 'rebalance' rules
- minor documentation updates

Modified: pkg/quantstrat/DESCRIPTION
===================================================================
--- pkg/quantstrat/DESCRIPTION	2012-11-03 13:03:24 UTC (rev 1236)
+++ pkg/quantstrat/DESCRIPTION	2012-11-03 16:47:18 UTC (rev 1237)
@@ -1,7 +1,7 @@
 Package: quantstrat
 Type: Package
 Title: Quantitative Strategy Model Framework
-Version: 0.7.2
+Version: 0.7.3
 Date: $Date$
 Author: Peter Carl, Dirk Eddelbuettel, Brian G. Peterson,
     Jeffrey A. Ryan, Joshua Ulrich, Garrett See

Modified: pkg/quantstrat/R/applyStrategy.rebalancing.R
===================================================================
--- pkg/quantstrat/R/applyStrategy.rebalancing.R	2012-11-03 13:03:24 UTC (rev 1236)
+++ pkg/quantstrat/R/applyStrategy.rebalancing.R	2012-11-03 16:47:18 UTC (rev 1237)
@@ -2,7 +2,7 @@
 #' 
 #' This function is the wrapper that holds together the execution of a strategy with rebalancing rules.
 #' 
-#' @param strategy an object of type 'strategy' to add the indicator to
+#' @param strategy an object of type 'strategy' or the name of a stored strategy to apply
 #' @param portfolios a list of portfolios to apply the strategy to
 #' @param mktdata an xts object containing market data.  depending on indicators, may need to be in OHLCV or BBO formats, default NULL
 #' @param parameters named list of parameters to be applied during evaluation of the strategy, default NULL
@@ -147,7 +147,7 @@
             #to nest different rebalancing periods, we need to check if the pindex 'i' is in specific rebalance_on periods
             # specifically, we need to check if *this* index is in st$plist$period
             for(period in names(st$plist)){
-                if(i %in% st$plist[[period]]){
+                if(pindex[i] %in% st$plist[[period]]){
                     #this index is a rebalancing index for period
                     #call the rebalance rules for this period
                     #still need to separate the rules by rebalancing period, this will call them all

Modified: pkg/quantstrat/R/ruleSignal.R
===================================================================
--- pkg/quantstrat/R/ruleSignal.R	2012-11-03 13:03:24 UTC (rev 1236)
+++ pkg/quantstrat/R/ruleSignal.R	2012-11-03 16:47:18 UTC (rev 1237)
@@ -24,7 +24,7 @@
 #' If \code{orderside} is NULL, the function will attempt to calculate the side from the current position 
 #' (if any), the order quantity, and the order type.    
 #'   
-#' @param data an xts object containing market data.  depending on rules, may need to be in OHLCV or BBO formats, and may include indicator and signal information
+#' @param mktdata an xts object containing market data.  depending on rules, may need to be in OHLCV or BBO formats, and may include indicator and signal information
 #' @param timestamp timestamp coercible to POSIXct that will be the time the order will be inserted on 
 #' @param sigcol column name to check for signal
 #' @param sigval signal value to match against
@@ -49,15 +49,14 @@
 #' @seealso \code{\link{osNoOp}} , \code{\link{add.rule}}
 #' @export
 
-ruleSignal <- function(data=mktdata, timestamp, sigcol, sigval, orderqty=0, ordertype, orderside=NULL, orderset=NULL, threshold=NULL, tmult=FALSE, replace=TRUE, delay=0.0001, osFUN='osNoOp', pricemethod=c('market','opside','active'), portfolio, symbol, ..., ruletype, TxnFees=0, prefer=NULL, sethold=FALSE, label='')
+ruleSignal <- function(mktdata=mktdata, timestamp, sigcol, sigval, orderqty=0, ordertype, orderside=NULL, orderset=NULL, threshold=NULL, tmult=FALSE, replace=TRUE, delay=0.0001, osFUN='osNoOp', pricemethod=c('market','opside','active'), portfolio, symbol, ..., ruletype, TxnFees=0, prefer=NULL, sethold=FALSE, label='')
 {
     if(!is.function(osFUN))
         osFUN<-match.fun(osFUN)
 
-#   if (!is.na(timestamp) && !is.na(data[timestamp][,sigcol]) && data[timestamp][,sigcol] == sigval) {
     if (!is.na(timestamp) && 
-            nrow(data[timestamp])>0 && 
-            (ruletype=='chain' || (!is.na(data[timestamp][,sigcol]) && data[timestamp][,sigcol] == sigval))
+            nrow(mktdata[timestamp])>0 && 
+            (ruletype=='chain' || (!is.na(mktdata[timestamp][,sigcol]) && mktdata[timestamp][,sigcol] == sigval))
     )
     {
         #calculate order price using pricemethod
@@ -76,14 +75,14 @@
             {
                 # threshold should be the name of an indicator column in mktdata
 
-                col.idx <- grep(threshold, colnames(data))
+                col.idx <- grep(threshold, colnames(mktdata))
 
                 if(length(col.idx) < 1)
                     stop(paste('no indicator column in mktdata matches threshold name "', threshold, '"', sep=''))
                 if(length(col.idx) > 1)
                     stop(paste('more than one indicator column in mktdata matches threshold name "', threshold, '"', sep=''))
 
-                threshold <- as.numeric(data[,col.idx][timestamp])
+                threshold <- as.numeric(mktdata[,col.idx][timestamp])
             }
         }
 
@@ -91,24 +90,24 @@
                 market = ,
                 opside = ,
                 active = {
-                    if(is.BBO(data)){
+                    if(is.BBO(mktdata)){
                         if (orderqty>0) 
                             prefer='ask'  # we're buying, so pay what they're asking
                         else
                             prefer='bid'  # we're selling, so give it to them for what they're bidding  
                     } 
-                    orderprice <- try(getPrice(x=data, prefer=prefer))[timestamp] 
+                    orderprice <- try(getPrice(x=mktdata, prefer=prefer))[timestamp] 
                 },
                 passive =,
                 work =,
                 join = {
-                    if(is.BBO(data)){
+                    if(is.BBO(mktdata)){
                         if (orderqty>0) 
                             prefer='bid'  # we're buying, so work the bid price
                         else
                             prefer='ask'  # we're selling, so work the ask price
                     }
-                    orderprice <- try(getPrice(x=data, prefer=prefer))[timestamp]
+                    orderprice <- try(getPrice(x=mktdata, prefer=prefer))[timestamp]
                 },
                 maker = {
                     if(hasArg(price) & length(match.call(expand.dots=TRUE)$price)>1) {
@@ -116,9 +115,9 @@
                         orderprice <- try(match.call(expand.dots=TRUE)$price)
                     } else {
                         if(!is.null(threshold)) {
-                            baseprice<- last(getPrice(x=data)[timestamp]) # this should get either the last trade price or the Close
+                            baseprice<- last(getPrice(x=mktdata)[timestamp]) # this should get either the last trade price or the Close
                             if(hasArg(tmult) & isTRUE(match.call(expand.dots=TRUE)$tmult)) {
-                                baseprice<- last(getPrice(x=data)[timestamp]) # this should get either the last trade price or the Close
+                                baseprice<- last(getPrice(x=mktdata)[timestamp]) # this should get either the last trade price or the Close
                                 # threshold is a multiplier of current price
                                 if (length(threshold)>1){
                                     orderprice <- baseprice * threshold # assume the user has set proper threshold multipliers for each side
@@ -136,7 +135,7 @@
                         } else{
                             # no threshold, put it on the averages?
                             stop('maker orders without specified prices and without threholds not (yet?) supported')
-                            if(is.BBO(data)){
+                            if(is.BBO(mktdata)){
 
                             } else {
 
@@ -176,7 +175,7 @@
         #TODO add fancy formals matching for osFUN
         if(orderqty!='all')
         {
-            orderqty <- osFUN(strategy=strategy, data=data, timestamp=timestamp, orderqty=orderqty, ordertype=ordertype, orderside=orderside, portfolio=portfolio, symbol=symbol,...=...,ruletype=ruletype, orderprice=as.numeric(orderprice))
+            orderqty <- osFUN(strategy=strategy, data=mktdata, timestamp=timestamp, orderqty=orderqty, ordertype=ordertype, orderside=orderside, portfolio=portfolio, symbol=symbol,...=...,ruletype=ruletype, orderprice=as.numeric(orderprice))
         }
 
         if(!is.null(orderqty) && orderqty!=0 && !is.null(orderprice)) #orderprice could have length > 1

Modified: pkg/quantstrat/R/rules.R
===================================================================
--- pkg/quantstrat/R/rules.R	2012-11-03 13:03:24 UTC (rev 1236)
+++ pkg/quantstrat/R/rules.R	2012-11-03 16:47:18 UTC (rev 1237)
@@ -572,6 +572,8 @@
     while(curIndex){
         timestamp=Dates[curIndex]    
 
+        #print(paste('timestamp',timestamp,'first',first(index(mktdata)),'last',last(index(mktdata))))
+        
         # check to see if we need to release a hold
         if(isTRUE(hold) & holdtill<timestamp){
             hold=FALSE

Modified: pkg/quantstrat/man/applyStrategy.rebalancing.Rd
===================================================================
--- pkg/quantstrat/man/applyStrategy.rebalancing.Rd	2012-11-03 13:03:24 UTC (rev 1236)
+++ pkg/quantstrat/man/applyStrategy.rebalancing.Rd	2012-11-03 16:47:18 UTC (rev 1237)
@@ -7,8 +7,8 @@
     symbols = NULL, initStrat = FALSE, updateStrat = FALSE)
 }
 \arguments{
-  \item{strategy}{an object of type 'strategy' to add the
-  indicator to}
+  \item{strategy}{an object of type 'strategy' or the name
+  of a stored strategy to apply}
 
   \item{portfolios}{a list of portfolios to apply the
   strategy to}

Modified: pkg/quantstrat/man/ruleSignal.Rd
===================================================================
--- pkg/quantstrat/man/ruleSignal.Rd	2012-11-03 13:03:24 UTC (rev 1236)
+++ pkg/quantstrat/man/ruleSignal.Rd	2012-11-03 16:47:18 UTC (rev 1237)
@@ -2,7 +2,7 @@
 \alias{ruleSignal}
 \title{default rule to generate a trade order on a signal}
 \usage{
-  ruleSignal(data = mktdata, timestamp, sigcol, sigval,
+  ruleSignal(mktdata = mktdata, timestamp, sigcol, sigval,
     orderqty = 0, ordertype, orderside = NULL,
     orderset = NULL, threshold = NULL, tmult = FALSE,
     replace = TRUE, delay = 1e-04, osFUN = "osNoOp",
@@ -11,7 +11,7 @@
     prefer = NULL, sethold = FALSE, label = "")
 }
 \arguments{
-  \item{data}{an xts object containing market data.
+  \item{mktdata}{an xts object containing market data.
   depending on rules, may need to be in OHLCV or BBO
   formats, and may include indicator and signal
   information}



More information about the Blotter-commits mailing list