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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jul 19 15:53:53 CEST 2010


Author: braverock
Date: 2010-07-19 15:53:53 +0200 (Mon, 19 Jul 2010)
New Revision: 356

Added:
   pkg/quantstrat/demo/MACD.R
   pkg/quantstrat/demo/RSI.R
Log:
- add RSI(2) and MACD trend strategy examples

Added: pkg/quantstrat/demo/MACD.R
===================================================================
--- pkg/quantstrat/demo/MACD.R	                        (rev 0)
+++ pkg/quantstrat/demo/MACD.R	2010-07-19 13:53:53 UTC (rev 356)
@@ -0,0 +1,54 @@
+# Simple MACD strategy
+#
+# MACD may be used in many ways, this will demonstrate a trend indicator.
+# 
+# traditionally, when the MACD signal crosses zero, this indicated a establishment of a positive trend
+#
+# we'll buy on positive treshold crossover of the 'signal' column, and sell on negative threshold crossover
+# 
+# Author: brian
+###############################################################################
+
+
+require(quantstrat)
+try(rm("order_book.macd",pos=.strategy),silent=TRUE)
+try(rm("account.macd","portfolio.macd",pos=.blotter),silent=TRUE)
+try(rm("account.st","portfolio.st","stock.str","s","initDate","initEq",'start_t','end_t'),silent=TRUE)
+
+stock.str='IBM' # what are we trying it on
+fastMA = 12 
+slowMA = 26 
+signalMA = 9
+currency('USD')
+stock(stock.str,currency='USD',multiplier=1)
+
+initDate='2006-12-31'
+initEq=1000000
+portfolio.st='macd'
+account.st='macd'
+
+initPortf(portfolio.st,symbols=stock.str, initDate=initDate)
+initAcct(account.st,portfolios=portfolio.st, initDate=initDate)
+initOrders(portfolio=portfolio.st,initDate=initDate)
+
+
+stratMACD <- strategy(portfolio.st)
+
+stratMACD <- add.indicator(strategy = stratMACD, name = "MACD", arguments = list(x=quote(Cl(mktdata)), nFast=fastMA, nSlow=slowMA, nSig=signalMA,maType="EMA") )
+
+stratMACD <- add.signal(strategy = stratMACD,name="sigThreshold",arguments = list(data=quote(mktdata),column="signal",relationship="gt",threshold=0,cross=TRUE),label="signal.gt.zero")
+stratMACD <- add.signal(strategy = stratMACD,name="sigThreshold",arguments = list(data=quote(mktdata),column="signal",relationship="lt",threshold=0,cross=TRUE),label="signal.lt.zero")
+
+stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="signal.gt.zero",sigval=TRUE, orderqty=100, ordertype='market', orderside=NULL, threshold=NULL),type='enter')
+stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="signal.lt.zero",sigval=TRUE, orderqty='all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
+
+getSymbols(stock.str,from=initDate)
+start_t<-Sys.time()
+out<-try(applyStrategy(strategy=stratMACD , portfolios=portfolio.st))
+end_t<-Sys.time()
+end_t-start_t
+updatePortf(Portfolio='macd',Dates=paste('::',as.Date(Sys.time()),sep=''))
+chart.Posn(Portfolio='macd',Symbol=stock.str)
+plot(add_MACD(fast=fastMA, slow=slowMA, signal=signalMA,maType="EMA"))
+
+

Added: pkg/quantstrat/demo/RSI.R
===================================================================
--- pkg/quantstrat/demo/RSI.R	                        (rev 0)
+++ pkg/quantstrat/demo/RSI.R	2010-07-19 13:53:53 UTC (rev 356)
@@ -0,0 +1,76 @@
+# Initialize a strategy object
+stratRSI <- strategy("RSI")
+
+# Add an indicator
+stratRSI <- add.indicator(strategy = stratRSI, name = "RSI", arguments = list(price = quote(getPrice(mktdata)), n=2), label="RSI")
+
+# There are two signals:
+# The first is when RSI is greater than 90
+stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments = list(data=quote(mktdata),threshold=90, column="RSI",relationship="gt", cross=TRUE),label="RSI.gt.90")
+# The second is when RSI is less than 10
+stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments = list(data=quote(mktdata),threshold=10, column="RSI",relationship="lt",cross=TRUE),label="RSI.lt.10")
+
+# There are two rules:
+#'## we would Use osMaxPos to put trade on in layers, or to a maximum position. 
+# The first is to sell when the RSI crosses above the threshold
+stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = list(data=quote(mktdata), sigcol="RSI.gt.90", sigval=TRUE, orderqty=-1000, ordertype='market', orderside='short', pricemethod='market'), type='enter', path.dep=TRUE)
+stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = list(data=quote(mktdata), sigcol="RSI.lt.10", sigval=TRUE, orderqty='all', ordertype='market', orderside='short', pricemethod='market'), type='exit', path.dep=TRUE)
+# The second is to buy when the RSI crosses below the threshold
+stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = list(data=quote(mktdata), sigcol="RSI.lt.10", sigval=TRUE, orderqty= 1000, ordertype='market', orderside='long', pricemethod='market'), type='enter', path.dep=TRUE)
+stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = list(data=quote(mktdata), sigcol="RSI.gt.90", sigval=TRUE, orderqty='all', ordertype='market', orderside='long', pricemethod='market'), type='enter', path.dep=TRUE)
+
+#add changeable parameters
+# add level in/out
+
+# add trailing entry
+
+# add trailing exit?
+
+currency("USD")
+currency("EUR")
+symbols = c("XLF", "XLP", "XLE", "XLY", "XLV", "XLI", "XLB", "XLK", "XLU")
+for(symbol in symbols){ # establish trade-able instruments
+    stock(symbol, currency="USD",multiplier=1)
+}
+
+
+# you can test with something like this:
+# applySignals(strategy=stratRSI, mktdata=applyIndicators(strategy=stratRSI, mktdata=symbols[1]))
+
+
+initDate='2009-02-06'
+initEq=100000
+port.st<-'RSISPX' #use a string here for easier changing of parameters and re-trying
+
+initPortf(port.st, symbols=symbols, initDate=initDate)
+initAcct(port.st, portfolios=port.st, initDate=initDate)
+initOrders(portfolio=port.st, initDate=initDate)
+
+print("setup completed")
+
+# Process the indicators and generate trades
+portfolios=c("RSISPX","RSIGDAXI","RSIboth")
+start_t<-Sys.time()
+out<-try(applyStrategy(strategy=stratRSI , portfolios=c(port.st)))
+end_t<-Sys.time()
+print("Strategy Loop:")
+print(end_t-start_t)
+
+# look at the order book
+#print(getOrderBook(port.st))
+
+start_t<-Sys.time()
+updatePortf(Portfolio=port.st,Dates=paste('::',as.Date(Sys.time()),sep=''))
+end_t<-Sys.time()
+print("trade blotter portfolio update:")
+print(end_t-start_t)
+
+# hack for new quantmod graphics, remove later
+themelist<-chart_theme()
+themelist$up.col<-'lightgreen'
+themelist$down.col<-'pink'
+for(symbol in symbols){
+    dev.new()
+    chart.Posn(Portfolio=port.st,Symbol=symbol,theme=themelist)
+    plot(add_RSI(n=2))
+}



More information about the Blotter-commits mailing list