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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Mar 25 21:04:37 CEST 2012


Author: braverock
Date: 2012-03-25 21:04:37 +0200 (Sun, 25 Mar 2012)
New Revision: 986

Modified:
   pkg/quantstrat/DESCRIPTION
   pkg/quantstrat/R/orders.R
   pkg/quantstrat/R/ruleOrderProc.R
   pkg/quantstrat/R/ruleSignal.R
   pkg/quantstrat/R/rules.R
Log:
- allow orderqty='all' for 'risk' orders
- fix 'stoplimit' orders, should be working now, use stoplimit if order price would be through the market on that side at time of order entry, 'limit' otherwise.

Modified: pkg/quantstrat/DESCRIPTION
===================================================================
--- pkg/quantstrat/DESCRIPTION	2012-03-25 17:07:06 UTC (rev 985)
+++ pkg/quantstrat/DESCRIPTION	2012-03-25 19:04:37 UTC (rev 986)
@@ -1,7 +1,7 @@
 Package: quantstrat
 Type: Package
 Title: Quantitative Strategy Model Framework
-Version: 0.6.3
+Version: 0.6.4
 Date: $Date$
 Author: Peter Carl, Dirk Eddelbuettel, Brian G. Peterson,
     Jeffrey A. Ryan, Joshua Ulrich, Garrett See, Yu Chen
@@ -24,5 +24,7 @@
     'rules.R'
     'signals.R'
     'strategy.R'
-    'traderules.R'
     'wrapup.R'
+    'osFUNs.R'
+    'ruleOrderProc.R'
+    'ruleSignal.R'

Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R	2012-03-25 17:07:06 UTC (rev 985)
+++ pkg/quantstrat/R/orders.R	2012-03-25 19:04:37 UTC (rev 986)
@@ -215,14 +215,15 @@
     #if(!length(grep(symbol,names(orderbook[[portfolio]])))==1) stop(paste("symbol",symbol,"does not exist in portfolio",portfolio,"having symbols",names(orderbook[[portfolio]])))
 
     #data quality checks
-    if(!is.numeric(qty)) stop (paste("Quantity must be numeric:",qty))
-    if(qty==0) stop("qty",qty,"must be positive or negative")
+    if(!is.numeric(qty) && !(qty=='all')) stop (paste("Quantity must be numeric:",qty))
+    if(qty==0) stop("qty",qty,"must positive, negative, or 'all'")
     if(is.null(qty)) stop("qty",qty,"must not be NULL")
     if(is.na(qty)) stop("qty",qty,"must not be NA")
     if(!is.numeric(price)) stop (paste("Price must be numeric:",price))
     if(is.null(price)) stop("price ",price," must not be NULL")
     if(is.na(price)) stop("order at timestamp ", timestamp, " must not have price of NA")
-    if(price==0) warning(paste(ordertype, "order for", qty, "has a price of zero."))
+    #spreads can have a zero price
+    #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"'))
@@ -287,7 +288,7 @@
     else ordertime<-as.POSIXct(timestamp)+delay
     orders<-NULL
     for (i in 1:length(price)){
-        neworder<-xts(as.matrix(t(c(as.numeric(qty[i]), price[i], ordertype[i], side, threshold[i], status, statustimestamp, order.set,TxnFees,label))),order.by=(ordertime))
+        neworder<-xts(as.matrix(t(c(qty[i], price[i], ordertype[i], side, threshold[i], status, statustimestamp, order.set,TxnFees,label))),order.by=(ordertime))
         if(is.null(orders)) orders<-neworder
         else orders <- rbind(orders,neworder)
     }
@@ -298,7 +299,8 @@
         return()
     }
 
-    qtysign <- sign(drop(coredata(qty)))
+    if(is.numeric(qty)) qtysign <- sign(drop(coredata(qty)))
+    else qtysign <- NULL
     
     if(!isTRUE(return)){
         if(isTRUE(replace)) updateOrders(portfolio=portfolio, symbol=symbol,timespan=timespan, side=side, qtysign=qtysign, oldstatus="open", newstatus="replaced", statustimestamp=timestamp)

Modified: pkg/quantstrat/R/ruleOrderProc.R
===================================================================
--- pkg/quantstrat/R/ruleOrderProc.R	2012-03-25 17:07:06 UTC (rev 985)
+++ pkg/quantstrat/R/ruleOrderProc.R	2012-03-25 19:04:37 UTC (rev 986)
@@ -81,7 +81,10 @@
             txnprice=NULL
             txnfees=ordersubset[ii,"Txn.Fees"]
             orderPrice <- as.numeric(ordersubset[ii,"Order.Price"])
-            orderQty <- as.numeric(ordersubset[ii,"Order.Qty"])
+            orderQty <- ordersubset[ii,"Order.Qty"]
+            if(orderQty=='all') orderQty <- osNoOp(timestamp=mktdataTimestamp, orderqty=orderQty, portfolio=portfolio, symbol=symbol,ruletype='exit' )
+            orderQty<-as.numeric(orderQty)
+            if(orderQty==0) next()
             orderThreshold <- as.numeric(ordersubset[ii,"Order.Threshold"])
             # mktdataTimestamp <- mktdata[timestamp]
             #FIXME Should we only keep the last observation per time stamp?

Modified: pkg/quantstrat/R/ruleSignal.R
===================================================================
--- pkg/quantstrat/R/ruleSignal.R	2012-03-25 17:07:06 UTC (rev 985)
+++ pkg/quantstrat/R/ruleSignal.R	2012-03-25 19:04:37 UTC (rev 986)
@@ -142,6 +142,7 @@
         
         ## now size the order
         #TODO add fancy formals matching for osFUN
+        if(!(ruletype=='risk') && (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))
         
         

Modified: pkg/quantstrat/R/rules.R
===================================================================
--- pkg/quantstrat/R/rules.R	2012-03-25 17:07:06 UTC (rev 985)
+++ pkg/quantstrat/R/rules.R	2012-03-25 19:04:37 UTC (rev 986)
@@ -396,7 +396,14 @@
 
                     for (slorder in stoplimitorders) {
                         dindex <- get.dindex()
-                        tmpqty <- as.numeric(ordersubset[oo.idx[slorder],'Order.Qty'])
+                        tmpqty <- ordersubset[oo.idx[slorder],'Order.Qty']
+                        if (tmpqty=='all') tmpqty<-osNoOp(timestamp=timestamp, orderqty=tmpqty, portfolio=portfolio, symbol=symbol,ruletype='exit' )
+                        if (tmpqty==0) {
+                            #no position, so do some slight of hand to figure out when the index may be needed
+                            side <- ordersubset[oo.idx[slorder],'Order.Side']
+                            if(side=='long') tmpqty==-1
+                            else tmpqty==1
+                        }
                         tmpprice <- as.numeric(ordersubset[oo.idx[slorder],'Order.Price'])
                         if (tmpqty > 0) { #buy if mktprice moves above stoplimitorder price
                             relationship='gte'  #if the Ask or Hi go above threshold our stop will be filled



More information about the Blotter-commits mailing list