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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Dec 9 19:06:17 CET 2012


Author: braverock
Date: 2012-12-09 19:06:17 +0100 (Sun, 09 Dec 2012)
New Revision: 1275

Modified:
   pkg/quantstrat/R/ruleSignal.R
Log:
- rearrange the 'side' detection, and allow for proper side detection when orderqty=='all'

Modified: pkg/quantstrat/R/ruleSignal.R
===================================================================
--- pkg/quantstrat/R/ruleSignal.R	2012-12-05 18:08:45 UTC (rev 1274)
+++ pkg/quantstrat/R/ruleSignal.R	2012-12-09 18:06:17 UTC (rev 1275)
@@ -88,12 +88,41 @@
             }
         }
 
+        if(is.null(orderside) & !isTRUE(orderqty == 0))
+        {
+            curqty<-getPosQty(Portfolio=portfolio, Symbol=symbol, Date=timestamp)
+            if (curqty>0 ){
+                #we have a long position
+                orderside<-'long'
+            } else if (curqty<0){
+                #we have a short position
+                orderside<-'short'
+            } else {
+                # no current position, which way are we going?
+                if (orderqty>0) 
+                    orderside<-'long'
+                else
+                    orderside<-'short'
+            }
+        }
+        
+        if(orderqty=='all'){
+            if (orderside=='long'){
+                #we're flattenting a long position
+                tmpqty <-  1
+            } else {
+                tmpqty <- -1
+            }
+        } else {
+            tmpqty <- orderqty
+        }
+        
         switch(pricemethod,
                 market = ,
                 opside = ,
                 active = {
                     if(is.BBO(mktdata)){
-                        if (orderqty>0) 
+                        if (tmpqty>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  
@@ -104,7 +133,7 @@
                 work =,
                 join = {
                     if(is.BBO(mktdata)){
-                        if (orderqty>0) 
+                        if (tmpqty>0) 
                             prefer='bid'  # we're buying, so work the bid price
                         else
                             prefer='ask'  # we're selling, so work the ask price
@@ -152,24 +181,6 @@
         if(length(orderprice>1) && !pricemethod=='maker') orderprice<-last(orderprice[timestamp])
         if(!is.null(orderprice) && !is.null(ncol(orderprice))) orderprice <- orderprice[,1]
 
-        if(is.null(orderside) & !isTRUE(orderqty == 0))
-        {
-            curqty<-getPosQty(Portfolio=portfolio, Symbol=symbol, Date=timestamp)
-            if (curqty>0 ){
-                #we have a long position
-                orderside<-'long'
-            } else if (curqty<0){
-                #we have a short position
-                orderside<-'short'
-            } else {
-                # no current position, which way are we going?
-                if (orderqty>0) 
-                    orderside<-'long'
-                else
-                    orderside<-'short'
-            }
-        }
-
         if(is.null(orderset)) orderset=NA
 
         



More information about the Blotter-commits mailing list