[Blotter-commits] r358 - pkg/quantstrat/demo

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jul 21 18:46:46 CEST 2010


Author: braverock
Date: 2010-07-21 18:46:46 +0200 (Wed, 21 Jul 2010)
New Revision: 358

Added:
   pkg/quantstrat/demo/maCross.R
Log:
- add MA-cross strategy demo from R. Raghuraman ("Raghu")

Added: pkg/quantstrat/demo/maCross.R
===================================================================
--- pkg/quantstrat/demo/maCross.R	                        (rev 0)
+++ pkg/quantstrat/demo/maCross.R	2010-07-21 16:46:46 UTC (rev 358)
@@ -0,0 +1,42 @@
+#########################################################################################################################################################################
+#A simple moving average strategy to evaluate trade efficiency
+#checks on SMA of 50 days and SMA of 200 days
+#Author: R. Raghuraman("raghu")
+#########################################################################################################################################################################
+
+require(quantstrat)
+try(rm("order_book.macross",pos=.strategy),silent=TRUE)
+try(rm("account.macross","portfolio.macross",pos=.blotter),silent=TRUE)
+try(rm("account.st","portfolio.st","stock.str","macross","initDate","initEq",'start_t','end_t'),silent=TRUE)
+stock.str='AAPL' # what are we trying it on
+currency('USD')
+stock(stock.str,currency='USD',multiplier=1)
+initDate='1999-12-31'
+initEq=1000000
+portfolio.st='macross'
+account.st='macross'
+initPortf(portfolio.st,symbols=stock.str, initDate=initDate)
+initAcct(account.st,portfolios=portfolio.st, initDate=initDate)
+initOrders(portfolio=portfolio.st,initDate=initDate)
+
+stratMACROSS<- strategy(portfolio.st)
+
+stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=50),label= "ma50" )
+stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=200),label= "ma200")
+
+stratMACROSS <- add.signal(strategy = stratMACROSS,name="sigCrossover",arguments = list(data=quote(mktdata),columns=c("ma50","ma200"),relationship="gt"),label="ma50.gt.ma200")
+stratMACROSS <- add.signal(strategy = stratMACROSS,name="sigCrossover",arguments = list(data=quote(mktdata),column=c("ma50","ma200"),relationship="lt"),label="ma50.lt.ma200")
+
+stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="ma50.gt.ma200",sigval=TRUE, orderqty=100, ordertype='market', orderside='long'),type='enter')
+stratMACROSS <- add.rule(strategy = stratMACROSS,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="ma50.lt.ma200",sigval=TRUE, orderqty=-100, ordertype='market', orderside='long'),type='exit')
+
+getSymbols(stock.str,from=initDate)
+start_t<-Sys.time()
+out<-try(applyStrategy(strategy=stratMACROSS , portfolios=portfolio.st))
+end_t<-Sys.time()
+end_t-start_t
+updatePortf(Portfolio='macross',Dates=paste('::',as.Date(Sys.time()),sep=''))
+chart.Posn(Portfolio='macross',Symbol=stock.str)
+add_SMA(n=50 , on=1,col='blue')
+add_SMA(n=200, on=1)
+



More information about the Blotter-commits mailing list