[Blotter-commits] r1198 - in pkg/quantstrat: R demo man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Sep 28 01:43:08 CEST 2012


Author: opentrades
Date: 2012-09-28 01:43:08 +0200 (Fri, 28 Sep 2012)
New Revision: 1198

Modified:
   pkg/quantstrat/R/orders.R
   pkg/quantstrat/R/ruleOrderProc.R
   pkg/quantstrat/demo/luxor.orderchains.R
   pkg/quantstrat/man/addOrder.Rd
   pkg/quantstrat/man/getOrders.Rd
   pkg/quantstrat/man/ruleOrderProc.Rd
   pkg/quantstrat/man/updateOrders.Rd
Log:
- implemented stopenter orders
- modified luxor.orderchains.R to use stopenter orders



Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R	2012-09-27 22:39:00 UTC (rev 1197)
+++ pkg/quantstrat/R/orders.R	2012-09-27 23:43:08 UTC (rev 1198)
@@ -70,7 +70,7 @@
 #' @param symbol identfier of the instrument to find orders for.  The name of any associated price objects (xts prices, usually OHLC) should match these
 #' @param status one of "open", "closed", "canceled", or "replaced", default "open"
 #' @param timespan xts-style character timespan to be the period to find orders of the given status and ordertype
-#' @param ordertype one of NULL, "market","limit","stoplimit", "stoptrailing", or "iceberg" default NULL
+#' @param ordertype one of NULL, "market","limit","stoplimit", "stoptrailing", "stopenter", or "iceberg" default NULL
 #' @param side one of NULL, "long" or "short", default NULL 
 #' @param qtysign one of NULL, -1,0,1 ; could be useful when all qty's are reported as positive numbers and need to be identified other ways, default NULL
 #' @param orderset a tag identifying the orderset
@@ -90,8 +90,8 @@
     #data quality checks
     if(!is.null(status) & !length(grep(status,c("open", "closed", "canceled","replaced")))==1) stop(paste("order status:",status,' must be one of "open", "closed", "canceled", or "replaced"'))
     if(!is.null(ordertype)) {
-        if(is.na(charmatch(ordertype,c("market","limit","stoplimit","stoptrailing","iceberg")))){
-            stop(paste("ordertype:",ordertype,' must be one of "market","limit","stoplimit", "stoptrailing", or "iceberg"'))
+        if(is.na(charmatch(ordertype,c("market","limit","stoplimit","stoptrailing","stopenter","iceberg")))){
+            stop(paste("ordertype:",ordertype,' must be one of "market","limit","stoplimit", "stoptrailing", "stopenter", or "iceberg"'))
         }
     }
 
@@ -152,8 +152,11 @@
 #' than growing or shrinking the threshold distance from the current market price in relation to the threshold,
 #' and will result in fewer unintended consequences and more understandable behavior.
 #' 
-#' The 'limit', 'stoplimit', 'stoptrailing' and 'iceberg' order types are the only order types that make use
-#' of the order \code{threshold}. Scalar thresholds \code{tmult=FALSE} on stoplimit or stoptrailing orders
+#' We have also modeled a 'stopenter' order, which will enter a position when current price + threshold is being
+#' triggered.
+#'
+#' The 'limit', 'stoplimit', 'stoptrailing', 'stopenter' and 'iceberg' order types are the only order types that make
+#' use of the order \code{threshold}. Scalar thresholds \code{tmult=FALSE} on stoplimit, stoptrailing or stopenter orders
 #' will be added to the current market price to set the limit price.  In other worlds, a scalar threshold
 #' is the difference either positive or negative from the current price when the order is entered. With a
 #' stoptrailing order, the order may be moved ("replaced") frequently.
@@ -195,9 +198,9 @@
 #' @param timestamp timestamp coercible to POSIXct that will be the time to search for orders before this time 
 #' @param qty numeric quantity of the order
 #' @param price numeric price at which the order is to be inserted
-#' @param ordertype one of "market","limit","stoplimit", "stoptrailing",or "iceberg"
+#' @param ordertype one of "market","limit","stoplimit", "stoptrailing", "stopenter", or "iceberg"
 #' @param side one of either "long" or "short" 
-#' @param threshold numeric threshold to apply to limit, stoplimit, stoptrailing and iceberg orders, default NULL
+#' @param threshold numeric threshold to apply to limit, stoplimit, stoptrailing, stopenter and iceberg orders, default NULL
 #' @param orderset set a tag identifying the orderset
 #' @param status one of "open", "closed", "canceled", or "replaced", default "open"
 #' @param statustimestamp timestamp of a status update, will be blank when order is initiated 
@@ -249,15 +252,16 @@
     #if(price==0) warning(paste(ordertype, "order for", qty, "has a price of zero."))
 
     if(!is.null(side) & !length(grep(side,c('long','short')))==1) stop(paste("side:",side," must be one of 'long' or 'short'"))
-    if(is.na(charmatch(ordertype,c("market","limit","stoplimit","stoptrailing","iceberg")))) stop(paste("ordertype:",ordertype,' must be one of "market","limit","stoplimit","stoptrailing", or"iceberg"'))
+    if(is.na(charmatch(ordertype,c("market","limit","stoplimit","stoptrailing","stopenter","iceberg")))) stop(paste("ordertype:",ordertype,' must be one of "market","limit","stoplimit","stoptrailing", "stopenter" or"iceberg"'))
     if(!is.null(threshold) & length(price)>=1 ) {
-        if(length(grep(paste("^",ordertype,"$",sep=""),c("limit","stoplimit","stoptrailing","iceberg")))==1) {
+        if(length(grep(paste("^",ordertype,"$",sep=""),c("limit","stoplimit","stoptrailing","stopenter","iceberg")))==1) {
             #we have a threshold set on a stop* order, process it
             switch(ordertype,
                     limit =, 
                     stoplimit =, 
                     iceberg =, 
-                    stoptrailing = {
+                    stoptrailing =,
+                    stopenter = {
                         if(isTRUE(tmult))
                         {
                             threshold = price*threshold
@@ -266,11 +270,19 @@
                         if(!is.null(side)&& ordertype!='iceberg' && ordertype!='limit'){
                             #check to make sure the order wouldn't instantly cross, reverse threshold if that's the case
                             if(side=='long') {
-                                #this is a stop exit, so it will sell *lower* than the current market
-                                if(price+threshold>price) threshold=-threshold 
+                                if(ordertype=='stopenter') {
+                                    if(price+threshold<price) threshold=-threshold 
+                                } else {
+                                    #this is a stop exit, so it will sell *lower* than the current market
+                                    if(price+threshold>price) threshold=-threshold 
+                                }
                             } else { #side=='short'
-                                #this is a stop exit, so it will buy *higher* than the current market
-                                if(price+threshold<price) threshold=-threshold 
+                                if(ordertype=='stopenter') {
+                                    if(price+threshold>price) threshold=-threshold 
+                                } else {
+                                    #this is a stop exit, so it will buy *higher* than the current market
+                                    if(price+threshold<price) threshold=-threshold 
+                                }
                             }
                         }
                         price = price+threshold                        
@@ -377,7 +389,7 @@
 #' @param portfolio text name of the portfolio to associate the order book with
 #' @param symbol identfier of the instrument to find orders for.  The name of any associated price objects (xts prices, usually OHLC) should match these
 #' @param timespan xts-style character timespan to be the period to find orders of the given status and ordertype 
-#' @param ordertype one of NULL, "market","limit","stoplimit", or "stoptrailing" default NULL
+#' @param ordertype one of NULL, "market","limit","stoplimit","stopenter" or "stoptrailing" default NULL
 #' @param side one of NULL, "long" or "short", default NULL 
 #' @param qtysign one of NULL, -1,0,1 ; could be useful when all qty's are reported as positive numbers and need to be identified other ways, default NULL
 #' @param orderset set a tag identifying the orderset
@@ -410,8 +422,8 @@
         stop(paste("side:",side," must be one of 'long' or 'short'"))
     if(!is.null(qtysign) && (qtysign != -1 && qtysign != 1 && qtysign != 0))
         stop(paste("qtysign:",qtysign," must be one of NULL, -1, 0, or 1"))
-    if(!is.null(ordertype) && is.na(charmatch(ordertype,c("market","limit","stoplimit","stoptrailing","iceberg")))) 
-        stop(paste("ordertype:",ordertype,' must be one of "market","limit","stoplimit","stoptrailing", or "iceberg"'))
+    if(!is.null(ordertype) && is.na(charmatch(ordertype,c("market","limit","stoplimit","stoptrailing","stopenter","iceberg")))) 
+        stop(paste("ordertype:",ordertype,' must be one of "market","limit","stoplimit","stoptrailing","stopenter", or "iceberg"'))
     if(!is.null(orderset) && newstatus=='replaced'){
         #replace any outstanding orders for this orderset
         ordertype=NULL

Modified: pkg/quantstrat/R/ruleOrderProc.R
===================================================================
--- pkg/quantstrat/R/ruleOrderProc.R	2012-09-27 22:39:00 UTC (rev 1197)
+++ pkg/quantstrat/R/ruleOrderProc.R	2012-09-27 23:43:08 UTC (rev 1198)
@@ -40,7 +40,7 @@
 #' @param symbol identfier of the instrument to find orders for.  The name of any associated price objects (xts prices, usually OHLC or BBO) should match these
 #' @param mktdata an xts object containing market data.  depending on indicators, may need to be in OHLCV or BBO formats, default NULL
 #' @param timespan xts-style character timespan to be the period to find orders to process in
-#' @param ordertype one of NULL, "market","limit","stoplimit", or "stoptrailing" default NULL
+#' @param ordertype one of NULL, "market","limit","stoplimit","stopenter", or "stoptrailing" default NULL
 #' @param ... any other passthru parameters
 #' @param slippageFUN default  NULL, not yet implemented
 #' @seealso add.rule
@@ -137,13 +137,14 @@
                     },
                     limit= ,
                     stoplimit =,
+                    stopenter =,
                     iceberg = {
                         if (!isBBOmktdata) { #(isOHLCmktdata){
                             if( orderType == 'iceberg'){
                                 stop("iceberg orders only supported for BBO data")
                             } 
                             # check to see if price moved through the limit                        
-                            if((orderQty > 0 && orderType != 'stoplimit') || (orderQty < 0 && orderType == 'stoplimit') ) {  
+                            if((orderQty > 0 && orderType != 'stoplimit' && orderType != 'stopenter') || (orderQty < 0 && (orderType=='stoplimit' || orderType=='stopenter'))) {
                                 # buy limit, or sell stoplimit
                                 if( (has.Lo(mktdata) && orderPrice > as.numeric(Lo(mktdataTimestamp))) || 
                                     (!has.Lo(mktdata) && orderPrice >= as.numeric(getPrice(mktdataTimestamp, prefer=prefer))))
@@ -151,7 +152,7 @@
                                     txnprice = orderPrice
                                     txntime = timestamp
                                 } else next() # price did not move through my order, should go to next order  
-                            } else if((orderQty < 0 && orderType != 'stoplimit') || (orderQty > 0 && orderType == 'stoplimit')) { 
+                            } else if((orderQty < 0 && orderType != 'stoplimit' && orderType != 'stopenter') || (orderQty > 0 && (orderType=='stoplimit' || orderType=='stopenter'))) { 
                                 # sell limit or buy stoplimit
                                 if ( (has.Hi(mktdata) && orderPrice < as.numeric(Hi(mktdataTimestamp))) ||
                                      (!has.Hi(mktdata) && orderPrice <= as.numeric(getPrice(mktdataTimestamp,prefer=prefer))) )

Modified: pkg/quantstrat/demo/luxor.orderchains.R
===================================================================
--- pkg/quantstrat/demo/luxor.orderchains.R	2012-09-27 22:39:00 UTC (rev 1197)
+++ pkg/quantstrat/demo/luxor.orderchains.R	2012-09-27 23:43:08 UTC (rev 1198)
@@ -253,13 +253,12 @@
 	arguments=list(sigcol='long' , sigval=TRUE,
 		replace=FALSE,
 		orderside='long' ,
-		ordertype='stoplimit',
+		ordertype='stopenter',
 		prefer='High',
 		threshold=.th,
 		TxnFees=0,
 		orderqty=+.qty,
 		osFUN=osMaxPos,
-#		osFUN=osNoOp,
 		orderset='ocolong'
 	),
 	type='enter',
@@ -271,13 +270,12 @@
 	arguments=list(sigcol='short', sigval=TRUE,
 		replace=FALSE,
 		orderside='short',
-		ordertype='stoplimit',
+		ordertype='stopenter',
 		prefer='Low',
 		threshold=-.th,
 		TxnFees=0,
 		orderqty=-.qty,
 		osFUN=osMaxPos,
-#		osFUN=osNoOp,
 		orderset='ocoshort'
 	),
 	type='enter',

Modified: pkg/quantstrat/man/addOrder.Rd
===================================================================
--- pkg/quantstrat/man/addOrder.Rd	2012-09-27 22:39:00 UTC (rev 1197)
+++ pkg/quantstrat/man/addOrder.Rd	2012-09-27 23:43:08 UTC (rev 1198)
@@ -25,12 +25,13 @@
   inserted}
 
   \item{ordertype}{one of "market","limit","stoplimit",
-  "stoptrailing",or "iceberg"}
+  "stoptrailing", "stopenter", or "iceberg"}
 
   \item{side}{one of either "long" or "short"}
 
   \item{threshold}{numeric threshold to apply to limit,
-  stoplimit, stoptrailing and iceberg orders, default NULL}
+  stoplimit, stoptrailing, stopenter and iceberg orders,
+  default NULL}
 
   \item{orderset}{set a tag identifying the orderset}
 
@@ -121,16 +122,21 @@
   relation to the threshold, and will result in fewer
   unintended consequences and more understandable behavior.
 
-  The 'limit', 'stoplimit', 'stoptrailing' and 'iceberg'
-  order types are the only order types that make use of the
-  order \code{threshold}. Scalar thresholds
-  \code{tmult=FALSE} on stoplimit or stoptrailing orders
-  will be added to the current market price to set the
-  limit price.  In other worlds, a scalar threshold is the
-  difference either positive or negative from the current
-  price when the order is entered. With a stoptrailing
-  order, the order may be moved ("replaced") frequently.
+  We have also modeled a 'stopenter' order, which will
+  enter a position when current price + threshold is being
+  triggered.
 
+  The 'limit', 'stoplimit', 'stoptrailing', 'stopenter' and
+  'iceberg' order types are the only order types that make
+  use of the order \code{threshold}. Scalar thresholds
+  \code{tmult=FALSE} on stoplimit, stoptrailing or
+  stopenter orders will be added to the current market
+  price to set the limit price.  In other worlds, a scalar
+  threshold is the difference either positive or negative
+  from the current price when the order is entered. With a
+  stoptrailing order, the order may be moved ("replaced")
+  frequently.
+
   Some markets and brokers recognize a stop that triggers a
   market order, when the stop is triggered, a market order
   will be executed at the then-prevailing price.  We have

Modified: pkg/quantstrat/man/getOrders.Rd
===================================================================
--- pkg/quantstrat/man/getOrders.Rd	2012-09-27 22:39:00 UTC (rev 1197)
+++ pkg/quantstrat/man/getOrders.Rd	2012-09-27 23:43:08 UTC (rev 1198)
@@ -21,8 +21,8 @@
   period to find orders of the given status and ordertype}
 
   \item{ordertype}{one of NULL,
-  "market","limit","stoplimit", "stoptrailing", or
-  "iceberg" default NULL}
+  "market","limit","stoplimit", "stoptrailing",
+  "stopenter", or "iceberg" default NULL}
 
   \item{side}{one of NULL, "long" or "short", default NULL}
 

Modified: pkg/quantstrat/man/ruleOrderProc.Rd
===================================================================
--- pkg/quantstrat/man/ruleOrderProc.Rd	2012-09-27 22:39:00 UTC (rev 1197)
+++ pkg/quantstrat/man/ruleOrderProc.Rd	2012-09-27 23:43:08 UTC (rev 1198)
@@ -22,8 +22,8 @@
   period to find orders to process in}
 
   \item{ordertype}{one of NULL,
-  "market","limit","stoplimit", or "stoptrailing" default
-  NULL}
+  "market","limit","stoplimit","stopenter", or
+  "stoptrailing" default NULL}
 
   \item{...}{any other passthru parameters}
 

Modified: pkg/quantstrat/man/updateOrders.Rd
===================================================================
--- pkg/quantstrat/man/updateOrders.Rd	2012-09-27 22:39:00 UTC (rev 1197)
+++ pkg/quantstrat/man/updateOrders.Rd	2012-09-27 23:43:08 UTC (rev 1198)
@@ -19,8 +19,8 @@
   period to find orders of the given status and ordertype}
 
   \item{ordertype}{one of NULL,
-  "market","limit","stoplimit", or "stoptrailing" default
-  NULL}
+  "market","limit","stoplimit","stopenter" or
+  "stoptrailing" default NULL}
 
   \item{side}{one of NULL, "long" or "short", default NULL}
 



More information about the Blotter-commits mailing list