[Blotter-commits] r1031 - pkg/quantstrat/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon May 28 23:32:03 CEST 2012


Author: opentrades
Date: 2012-05-28 23:32:03 +0200 (Mon, 28 May 2012)
New Revision: 1031

Modified:
   pkg/quantstrat/R/orders.R
   pkg/quantstrat/R/ruleSignal.R
Log:
risk/exit-'all' orders now cancel all open orders in corresponding orderset, even if current position = 0

Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R	2012-05-26 14:58:40 UTC (rev 1030)
+++ pkg/quantstrat/R/orders.R	2012-05-28 21:32:03 UTC (rev 1031)
@@ -84,7 +84,7 @@
     # get order book
     orderbook <- getOrderBook(portfolio)
     if(!any(names(orderbook[[portfolio]]) == symbol)) stop(paste("symbol",symbol,"does not exist in portfolio",portfolio,"having symbols",names(orderbook[[portfolio]])))
-    ordersubset<-orderbook[[portfolio]][[symbol]]
+	    ordersubset<-orderbook[[portfolio]][[symbol]]
 
     #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"'))
@@ -345,7 +345,7 @@
 #' @seealso getOrderBook 
 #' @concept order book
 #' @export
-updateOrders <- function(portfolio, symbol, timespan, ordertype=NULL, side=NULL, qtysign=NULL, oldstatus="open", newstatus, statustimestamp) 
+updateOrders <- function(portfolio, symbol, timespan, ordertype=NULL, side=NULL, qtysign=NULL, oldstatus="open", newstatus, statustimestamp, orderset=NULL)
 { 
     #data quality checks
     if(!is.null(oldstatus) && !length(grep(oldstatus,c("open", "closed", "canceled","replaced")))==1) 

Modified: pkg/quantstrat/R/ruleSignal.R
===================================================================
--- pkg/quantstrat/R/ruleSignal.R	2012-05-26 14:58:40 UTC (rev 1030)
+++ pkg/quantstrat/R/ruleSignal.R	2012-05-28 21:32:03 UTC (rev 1031)
@@ -140,19 +140,26 @@
                     orderside<-'short'
             }
         }
-        
+
+	if(is.null(orderset)) orderset=NA
+
 	## now size the order
 	#TODO add fancy formals matching for osFUN
 	if(orderqty!='all' || ordertype=='market' || (ruletype!='risk' && ruletype!='exit'))
 	{
 		orderqty <- osFUN(strategy=strategy, data=data, timestamp=timestamp, orderqty=orderqty, ordertype=ordertype, orderside=orderside, portfolio=portfolio, symbol=symbol,...=...,ruletype=ruletype, orderprice=as.numeric(orderprice))
 
-		if(ruletype=='exit' && ((orderqty>0 && orderside=='long') || (orderqty<0 && orderside=='short')))
-			orderqty = NULL		# dirty trick to suppress adding order below JH
+		if(ruletype=='risk' || ruletype=='exit')
+		{
+			if(orderqty==0)	# cancel any open orders from orderset associated with this exit/risk order
+				updateOrders(portfolio, symbol, oldstatus="open", newstatus='canceled', statustimestamp=timestamp, orderset=orderset)
+
+			if(((orderqty>0 && orderside=='long') || (orderqty<0 && orderside=='short')))
+				orderqty = NULL		# dirty trick to suppress adding order below JH; (why?)
+		}
+
         }
 
-	if(is.null(orderset)) orderset=NA
-
 	if(!is.null(orderqty) && orderqty!=0 && !is.null(orderprice)) #orderqty could have length > 1
 	{
 		addOrder(portfolio=portfolio, symbol=symbol, timestamp=timestamp, qty=orderqty, price=as.numeric(orderprice), ordertype=ordertype, side=orderside, orderset=orderset, threshold=threshold, status="open", replace=replace , delay=delay, tmult=tmult, ...=..., TxnFees=TxnFees,label=label)



More information about the Blotter-commits mailing list