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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Mar 25 22:29:30 CEST 2012


Author: braverock
Date: 2012-03-25 22:29:29 +0200 (Sun, 25 Mar 2012)
New Revision: 988

Modified:
   pkg/quantstrat/R/rules.R
   pkg/quantstrat/demo/macd.R
Log:
- make stoplimit more stable by making qty numeric
- add same basic handling for stoptrailing, not sure this functionality is completely working again yet
- add version of macd demo with example rules

Modified: pkg/quantstrat/R/rules.R
===================================================================
--- pkg/quantstrat/R/rules.R	2012-03-25 19:14:04 UTC (rev 987)
+++ pkg/quantstrat/R/rules.R	2012-03-25 20:29:29 UTC (rev 988)
@@ -404,6 +404,7 @@
                             if(side=='long') tmpqty==-1
                             else tmpqty==1
                         }
+                        tmpqty<-as.numeric(tmpqty)
                         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
@@ -493,7 +494,15 @@
                         neworders<-NULL
                         onum<-oo.idx[torder]
                         orderThreshold <- as.numeric(ordersubset[onum,'Order.Threshold'])
-                        tmpqty<-as.numeric(ordersubset[onum,'Order.Qty'])
+                        tmpqty<-ordersubset[onum,'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[onum,'Order.Side']
+                            if(side=='long') tmpqty==-1
+                            else tmpqty==1
+                        }
+                        tmpqty<-as.numeric(tmpqty)
                         tmpprice<-as.numeric(ordersubset[onum,'Order.Price'])
                         tmpidx<-as.character(index(ordersubset[onum,])) #this is the time the order was entered
                         #print(tmpidx)

Modified: pkg/quantstrat/demo/macd.R
===================================================================
--- pkg/quantstrat/demo/macd.R	2012-03-25 19:14:04 UTC (rev 987)
+++ pkg/quantstrat/demo/macd.R	2012-03-25 20:29:29 UTC (rev 988)
@@ -41,23 +41,38 @@
 initAcct(account.st,portfolios=portfolio.st, initDate=initDate)
 initOrders(portfolio=portfolio.st,initDate=initDate)
 
+strat.st<-portfolio.st
+# define the strategy
+strategy(strat.st, store=TRUE)
 
-stratMACD <- strategy(portfolio.st)
+#one indicator
+add.indicator(strat.st, name = "MACD", arguments = list(x=quote(Cl(mktdata))) )
 
-stratMACD <- add.indicator(strategy = stratMACD, name = "MACD", arguments = list(x=quote(Cl(mktdata))) )
+#two signals
+add.signal(strat.st,name="sigThreshold",arguments = list(column="signal",relationship="gt",threshold=0,cross=TRUE),label="signal.gt.zero")
+add.signal(strat.st,name="sigThreshold",arguments = list(column="signal",relationship="lt",threshold=0,cross=TRUE),label="signal.lt.zero")
 
-stratMACD <- add.signal(strategy = stratMACD,name="sigThreshold",arguments = list(column="signal",relationship="gt",threshold=0,cross=TRUE),label="signal.gt.zero")
-stratMACD <- add.signal(strategy = stratMACD,name="sigThreshold",arguments = list(column="signal",relationship="lt",threshold=0,cross=TRUE),label="signal.lt.zero")
+####
+# add rules
 
-stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = list(sigcol="signal.gt.zero",sigval=TRUE, orderqty=100, ordertype='market', orderside='long', threshold=NULL),type='enter')
-#stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = list(sigcol="signal.gt.zero",sigval=TRUE, orderqty=-100, ordertype='stoplimit', orderside='long', threshold=.85,tmult=TRUE),type='risk')
-# alternately, use a trailing order
-# stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = list(sigcol="signal.gt.zero",sigval=TRUE, orderqty=-100, ordertype='stoptrailing', orderside='long', threshold=.9,tmult=TRUE),type='risk')
-stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = list(sigcol="signal.lt.zero",sigval=TRUE, orderqty='all', ordertype='market', orderside='long', threshold=NULL),type='exit')
+# entry
+add.rule(strat.st,name='ruleSignal', arguments = list(sigcol="signal.gt.zero",sigval=TRUE, orderqty=100, ordertype='market', orderside='long', threshold=NULL),type='enter',label='enter',storefun=FALSE)
 
+#alternatives for risk stops:
+# simple stoplimit order, with threshold multiplier
+# add.rule(strat.st,name='ruleSignal', arguments = list(sigcol="signal.gt.zero",sigval=TRUE, orderqty='all', ordertype='stoplimit', orderside='long', threshold=-.05,tmult=TRUE),type='risk',label='risk',storefun=FALSE)
+# alternately, use a trailing order, also with a threshold multiplier
+# add.rule(strat.st,name='ruleSignal', arguments = list(sigcol="signal.gt.zero",sigval=TRUE, orderqty='all', ordertype='stoptrailing', orderside='long', threshold=-.1,tmult=TRUE),type='risk',label='trailingexit')
+
+# exit
+add.rule(strat.st,name='ruleSignal', arguments = list(sigcol="signal.lt.zero",sigval=TRUE, orderqty='all', ordertype='market', orderside='long', threshold=NULL),type='exit',label='exit')
+
+#end rules
+####
+
 getSymbols(stock.str,from=initDate)
 start_t<-Sys.time()
-out<-try(applyStrategy(strategy=stratMACD , portfolios=portfolio.st,parameters=list(nFast=fastMA, nSlow=slowMA, nSig=signalMA,maType=maType)))
+out<-try(applyStrategy(strat.st , portfolios=portfolio.st,parameters=list(nFast=fastMA, nSlow=slowMA, nSig=signalMA,maType=maType)))
 end_t<-Sys.time()
 print(end_t-start_t)
 



More information about the Blotter-commits mailing list