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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Sep 6 18:33:36 CEST 2012


Author: opentrades
Date: 2012-09-06 18:33:35 +0200 (Thu, 06 Sep 2012)
New Revision: 1161

Modified:
   pkg/quantstrat/R/orders.R
   pkg/quantstrat/R/ruleOrderProc.R
   pkg/quantstrat/R/rules.R
   pkg/quantstrat/demo/luxor.orderchains.R
Log:
fixed OHLC stoptrailing orders in quantstrat
demo/luxor.orderchains.R now also demonstrating StopTrailing orders



Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R	2012-09-06 01:46:03 UTC (rev 1160)
+++ pkg/quantstrat/R/orders.R	2012-09-06 16:33:35 UTC (rev 1161)
@@ -259,15 +259,10 @@
                     stoplimit =, 
                     iceberg =, 
                     stoptrailing = {
-                        if(isTRUE(tmult)){
-                            #set the numeric theshold as threshold*price 
+                        if(isTRUE(tmult))
+                        {
                             if(threshold<1) threshold = price*threshold
-                            else{
-                                #get the difference between the threshold*price and the price
-                                threshold = price-(price*threshold) #positive threshold  
-                            } 
                             tmult=FALSE
-                            #this sets the threshold as a fixed number for later trailing orders 
                         } 
                         price = price+threshold                        
                     }

Modified: pkg/quantstrat/R/ruleOrderProc.R
===================================================================
--- pkg/quantstrat/R/ruleOrderProc.R	2012-09-06 01:46:03 UTC (rev 1160)
+++ pkg/quantstrat/R/ruleOrderProc.R	2012-09-06 16:33:35 UTC (rev 1161)
@@ -222,36 +222,78 @@
                         }
                     },
                     stoptrailing = {
-                        # if market moved through my price, execute
-                        if(orderQty > 0){ # positive quantity 'buy'
-                            if(isBBOmktdata) prefer='offer'
-                            if(orderPrice >= getPrice(mktdataTimestamp,prefer=prefer)[,1]){ #TODO maybe use last(getPrice) to catch multiple prints on timestamp?
-                                # price we're willing to pay is higher than the offer price, so execute at the prevailing price
-                                #txnprice = orderPrice
-                                txnprice = as.numeric(getPrice(mktdataTimestamp,prefer=prefer)[,1]) #presumes unique timestamps
-                                txntime = timestamp
+                        if(isBBOmktdata)    # TODO BBO needs testing after fixes for OHLC by JH
+                        {
+                            # if market moved through my price, execute
+                            if(orderQty > 0) # positive quantity 'buy'
+                            {
+                                if(isBBOmktdata) prefer='offer'
+                                if(orderPrice >= getPrice(mktdataTimestamp,prefer=prefer)[,1]){ #TODO maybe use last(getPrice) to catch multiple prints on timestamp?
+                                    # price we're willing to pay is higher than the offer price, so execute at the prevailing price
+                                    #txnprice = orderPrice
+                                    txnprice = as.numeric(getPrice(mktdataTimestamp,prefer=prefer)[,1]) #presumes unique timestamps
+                                    txntime = timestamp
+                                } 
+                            } else { # negative quantity 'sell'
+                                if(isBBOmktdata) prefer='bid'
+                                if(orderPrice <= getPrice(mktdataTimestamp,prefer=prefer)[,1]){
+                                    # we're willing to sell at a better price than the bid, so execute at the prevailing price
+                                    # txnprice = orderPrice
+                                    txnprice = as.numeric(getPrice(mktdataTimestamp,prefer=prefer)[,1]) #presumes unique timestamp
+                                    txntime = timestamp
+                                } 
                             } 
-                        } else { # negative quantity 'sell'
-                            if(isBBOmktdata) prefer='bid'
-                            if(orderPrice <= getPrice(mktdataTimestamp,prefer=prefer)[,1]){
-                                # we're willing to sell at a better price than the bid, so execute at the prevailing price
-                                # txnprice = orderPrice
-                                txnprice = as.numeric(getPrice(mktdataTimestamp,prefer=prefer)[,1]) #presumes unique timestamp
-                                txntime = timestamp
-                            } 
-                        } 
-                        if(isOHLCmktdata){
+                        }
+                        else if(isOHLCmktdata)
+                        {
                             # check to see if price moved through the limit
-                            if( orderPrice > as.numeric(Lo(mktdataTimestamp)) &&
-                                orderPrice < as.numeric(Hi(mktdataTimestamp)) ) 
+
+                            order.side <- ordersubset[ii, "Order.Side"]
+
+                            if(order.side == 'long'  && as.numeric(Lo(mktdataTimestamp)) < orderPrice
+                            || order.side == 'short' && as.numeric(Hi(mktdataTimestamp)) > orderPrice)
                             {
-                                txnprice = orderPrice
-                                txntime = timestamp
+                                txnprice <- orderPrice
+                                txntime <- timestamp
                             } 
+                            else
+                            {
+                                # do we need to change the trailing stop?
+
+                                order.threshold <- as.numeric(ordersubset[ii, "Order.Threshold"])
+
+                                if(order.side == 'long')
+                                    new.order.price <- max(orderPrice, as.numeric(Hi(mktdataTimestamp)) + order.threshold)
+                                if(order.side == 'short')
+                                    new.order.price <- min(orderPrice, as.numeric(Lo(mktdataTimestamp)) + order.threshold)
+
+                                if(new.order.price != orderPrice)
+                                {
+                                    # adjust trailing stop
+
+                                    neworder<-addOrder(portfolio=portfolio,
+                                             symbol=symbol,
+                                             timestamp=timestamp,
+                                             qty=orderQty,
+                                             price=new.order.price - order.threshold,
+                                             ordertype=orderType,
+                                             side=order.side,
+                                             threshold=order.threshold,
+                                             status="open",
+                                             replace=FALSE, return=TRUE,
+                                             orderset=ordersubset[ii,"Order.Set"],
+                                             label=ordersubset[ii,"Rule"],
+                                             ,...=..., TxnFees=txnfees)
+                                    if (is.null(neworders)) neworders=neworder else neworders = rbind(neworders,neworder)
+                                    ordersubset[ii,"Order.Status"]<-'replaced'
+                                    ordersubset[ii,"Order.StatusTime"]<-format(timestamp, "%Y-%m-%d %H:%M:%S")
+                                    next()
+                                }
+                            }
                         }
+
                         # if market is beyond price+(-threshold), replace order
                         if(is.null(txnprice)) { 
-                            #print("here")
                             # we didn't trade, so check to see if we need to move the stop
                             # first figure out how to find a price
                             if (isOHLCmktdata){
@@ -294,6 +336,7 @@
                         # else next
                     }
             )
+
             if(!is.null(txnprice) && !isTRUE(is.na(txnprice)))
             {
                 #make sure we don't cross through zero

Modified: pkg/quantstrat/R/rules.R
===================================================================
--- pkg/quantstrat/R/rules.R	2012-09-06 01:46:03 UTC (rev 1160)
+++ pkg/quantstrat/R/rules.R	2012-09-06 16:33:35 UTC (rev 1161)
@@ -519,7 +519,7 @@
 		    }
 		} # end loop over open limit orders
 
-                trailorders<-grep('^stoptrailing$', ordersubset[oo.idx,'Order.Type'])
+        trailorders<-grep('^stoptrailing$', ordersubset[oo.idx,'Order.Type'])
 		for(torder in trailorders)
 		{
 		    dindex<-get.dindex()

Modified: pkg/quantstrat/demo/luxor.orderchains.R
===================================================================
--- pkg/quantstrat/demo/luxor.orderchains.R	2012-09-06 01:46:03 UTC (rev 1160)
+++ pkg/quantstrat/demo/luxor.orderchains.R	2012-09-06 16:33:35 UTC (rev 1161)
@@ -21,14 +21,15 @@
 .timespan = 'T00:00/T23:59'
 
 .stoploss=0.001
-.takeprofit=0.005
+.stoptrailing=0.0015
+.takeprofit=0.003
 
 initDate = '2002-10-21'
 .from='2002-10-21'
 #.to='2008-07-04'
-#.to='2003-12-31'
 .to='2002-10-31'
 #.to='2002-12-31'
+#.to='2003-12-31'
 #.from='2006-01-01'
 #.to='2006-12-31'
 #.from='2007-01-01'
@@ -148,6 +149,43 @@
 	label='StopLossSHORT'
 )
 
+### stop-trailing
+
+if(TRUE)
+{
+add.rule(s, name = 'ruleSignal',
+	arguments=list(sigcol='long' , sigval=TRUE,
+		replace=FALSE,
+		orderside='long',
+		ordertype='stoptrailing',
+		tmult=TRUE,
+		threshold=-.stoptrailing,
+		TxnFees=.txn,
+		orderqty='all',
+		orderset='ocolong'
+	),
+	type='chain',
+	parent='EnterLONG',
+	label='StopTrailingLONG'
+)
+
+add.rule(s, name = 'ruleSignal',
+	arguments=list(sigcol='short' , sigval=TRUE,
+		replace=FALSE,
+		orderside='short',
+		ordertype='stoptrailing',
+		tmult=TRUE,
+		threshold=.stoptrailing,
+		TxnFees=.txn,
+		orderqty='all',
+		orderset='ocoshort'
+	),
+	type='chain',
+	parent='EnterSHORT',
+	label='StopTrailingSHORT'
+)
+}
+
 ### take-profit
 
 add.rule(s, name = 'ruleSignal',
@@ -221,6 +259,7 @@
 		TxnFees=0,
 		orderqty=+.qty,
 		osFUN=osMaxPos,
+#		osFUN=osNoOp,
 		orderset='ocolong'
 	),
 	type='enter',
@@ -238,6 +277,7 @@
 		TxnFees=0,
 		orderqty=-.qty,
 		osFUN=osMaxPos,
+#		osFUN=osNoOp,
 		orderset='ocoshort'
 	),
 	type='enter',



More information about the Blotter-commits mailing list