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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jan 7 19:12:45 CET 2015


Author: michaelguan326
Date: 2015-01-07 19:12:44 +0100 (Wed, 07 Jan 2015)
New Revision: 1667

Added:
   pkg/quantstrat/demo/signal.RSI.R
   pkg/quantstrat/demo/signal.SMA.R
Log:
- add signal demos


Added: pkg/quantstrat/demo/signal.RSI.R
===================================================================
--- pkg/quantstrat/demo/signal.RSI.R	                        (rev 0)
+++ pkg/quantstrat/demo/signal.RSI.R	2015-01-07 18:12:44 UTC (rev 1667)
@@ -0,0 +1,96 @@
+# Work Flow:
+# Example Code 2 for Running Signal Analysis in Quantstrat.
+# System: A simple RSI strategy for evaluating signals.
+# Author: Michael Guan
+###########################################################################
+rm(list=ls())
+
+# Load Packages:
+require(iterators)
+require(quantstrat)
+
+suppressWarnings(rm("order_book.RSI",pos=.strategy))
+suppressWarnings(rm("account.RSI","portfolio.RSI",pos=.blotter))
+suppressWarnings(rm("account.st","portfolio.st","stock.str","stratRSI","initDate","initEq",'start_t','end_t'))
+
+#Parameters
+n=2
+
+#Data
+currency("USD")
+currency("EUR")
+symbols = c("XLF", "XLP")
+for(symbol in symbols){ # establish trade-able instruments
+  stock(symbol, currency="USD",multiplier=1)
+  getSymbols(symbol,src='yahoo')
+}
+
+# Initialize Account, Portfolio, Strategy
+stratRSI <- strategy("RSI")
+
+initDate='1997-12-31'
+initEq=100000
+port.st<-'RSI' #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,initEq=initEq)
+initOrders(portfolio=port.st, initDate=initDate)
+for(symbol in symbols){ addPosLimit(port.st, symbol, initDate, 300, 3 ) } #set max pos 
+
+# Indicator
+stratRSI <- add.indicator(strategy = stratRSI, name = "RSI", arguments = list(price = quote(getPrice(mktdata)),n=n), label="RSI")
+
+# There are two signals:
+# The first is when RSI is greater than 90
+stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments = list(threshold=70, column="RSI",relationship="gt", cross=TRUE),label="RSI.gt.70")
+# The second is when RSI is less than 10
+stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments = list(threshold=30, column="RSI",relationship="lt",cross=TRUE),label="RSI.lt.30")
+
+#########################################################################
+#Signal Analysis
+
+#Entry Signal colname Label
+signal.label = 'RSI.lt.30'
+
+.n = seq(2,10,1)
+
+strategy.st<-add.distribution(stratRSI,
+                              paramset.label = 'RSI',
+                              component.type = 'indicator',
+                              component.label = 'RSI',
+                              variable = list(n = .n),
+                              label = 'nRSI')
+
+
+# Run Study
+results =apply.paramset.signal.analysis(strategy.st, 
+                                        paramset.label='RSI', 
+                                        port.st, 
+                                        sigcol = signal.label,
+                                        sigval = 1,
+                                        on='days',
+                                        forward.days=10,
+                                        cum.sum=TRUE,
+                                        include.day.of.signal=F,
+                                        obj.fun=signal.obj.slope,
+                                        decreasing=T,
+                                        mktdata=NULL,
+                                        verbose=TRUE)
+
+
+# Plot Paramset Combined Barchart [Subset list to plot a sub portion if too large]
+plot.signals(results$sigret.by.asset$XLF, rows=2, columns = 5)
+
+# Distributional Box Plot via gamlss
+distributional.boxplot(results$sigret.by.asset$XLF$paramset.2)
+
+
+
+
+
+
+
+
+
+
+

Added: pkg/quantstrat/demo/signal.SMA.R
===================================================================
--- pkg/quantstrat/demo/signal.SMA.R	                        (rev 0)
+++ pkg/quantstrat/demo/signal.SMA.R	2015-01-07 18:12:44 UTC (rev 1667)
@@ -0,0 +1,173 @@
+# Work Flow:
+# Example Code 1 for Running Signal Analysis in Quantstrat.
+# System: A simple moving average strategy for evaluating signal
+# Author: Michael Guan
+###########################################################################
+# Load Packages:
+rm(list=ls())
+require(iterators)
+require(quantstrat)
+
+###########################################################################
+# Configure Date Time Settings
+ttz<-Sys.getenv('TZ')
+Sys.setenv(TZ='UTC')
+
+suppressWarnings(rm("order_book.macross",pos=.strategy))
+suppressWarnings(rm("account.macross","portfolio.macross",pos=.blotter))
+suppressWarnings(rm("account.st","portfolio.st","stock.str","strategy.st",'start_t','end_t'))
+
+if(isTRUE(options('in_test')$in_test)){
+  # use test dates
+  initDate="2011-01-01" 
+  endDate="2012-12-31"   
+} else {
+  # use demo defaults
+  initDate="1999-12-31"
+  endDate=Sys.Date()
+}
+
+###########################################################################
+# Data
+stock.str=c('XLY','XLF','XLP','XLI','RTH','XLV','XLK','XLE','IYT')
+currency('USD')
+stock(stock.str,currency='USD',multiplier=1)
+getSymbols(stock.str,from=initDate,to=endDate,src = 'yahoo')
+for(i in stock.str)
+  assign(i, adjustOHLC(get(i),use.Adjusted=TRUE))
+
+###########################################################################
+# Account, Portfolio, Strategy Initialization
+initEq=1000000
+portfolio.st='macross'
+account.st='macross'
+initPortf(portfolio.st,symbols=stock.str, initDate=initDate)
+initAcct(account.st,portfolios=portfolio.st, initDate=initDate,initEq=initEq)
+initOrders(portfolio=portfolio.st,initDate=initDate)
+
+strategy.st<- strategy(portfolio.st)
+
+
+#Indicator
+strategy.st <- add.indicator(strategy = strategy.st, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=50),label= "ma50" )
+strategy.st <- add.indicator(strategy = strategy.st, name = "SMA", arguments = list(x=quote(Cl(mktdata)[,1]), n=200),label= "ma200")
+
+#Signals
+strategy.st <- add.signal(strategy = strategy.st,name="sigCrossover",arguments = list(columns=c("ma50","ma200"), relationship="gte"), label="ma50.gt.ma200")
+strategy.st <- add.signal(strategy = strategy.st,name="sigCrossover",arguments = list(column=c("ma50","ma200"),relationship="lt"), label="ma50.lt.ma200")
+
+###########################################################################
+
+# Signal Column Label to Analyze
+signal.label = 'ma50.gt.ma200'
+
+# Desired Parameter Pool
+.FastSMA = seq(1,5,1)
+.SlowSMA = seq(5,20,5)
+
+strategy.st<-add.distribution(strategy.st,
+                              paramset.label = 'SMA',
+                              component.type = 'indicator',
+                              component.label = 'ma50',
+                              variable = list(n = .FastSMA),
+                              label = 'nFAST')
+
+strategy.st<-add.distribution(strategy.st,
+                              paramset.label = 'SMA',
+                              component.type = 'indicator',
+                              component.label = 'ma200',
+                              variable = list(n = .SlowSMA),
+                              label = 'nSLOW')
+
+# Constraint: nFast < nSlow
+strategy.st<-add.distribution.constraint(strategy.st,
+                                         paramset.label = 'SMA',
+                                         distribution.label.1 = 'nFAST',
+                                         distribution.label.2 = 'nSLOW',
+                                         operator = '<',
+                                         label = 'SMA')
+
+
+# Daily Signal With Post Daily Return Analysis
+results =apply.paramset.signal.analysis(strategy.st, 
+                                        paramset.label='SMA', 
+                                        portfolio.st, 
+                                        sigcol = signal.label,
+                                        sigval = 1,
+                                        on=NULL,
+                                        forward.days=50,
+                                        cum.sum=TRUE,
+                                        include.day.of.signal=F,
+                                        obj.fun=signal.obj.slope,
+                                        decreasing=T)
+
+distributional.boxplot(signal=results$sigret.by.asset$IYT$paramset.5.20,
+                       x.val=seq(1, 50, 5),val=10,ylim=c(-20, 20),
+                       xlim=c(0, 50),mai=c(1,1,0.3,0.5),h=0)
+
+plot.signals(results$sigret.by.asset$XLE, rows=5, columns = 4)
+beanplot.signals(results$sigret.by.asset$XLE, rows=5, columns = 4)
+plot.signal.path(results$sigret.by.asset$IYT$paramset.5.20)
+
+
+# Daily Signal With Post Weekly Return Analysis
+results.w =apply.paramset.signal.analysis(strategy.st, 
+                                          paramset.label='SMA', 
+                                          portfolio.st, 
+                                          sigcol = signal.label,
+                                          sigval = 1,
+                                          on='weeks',
+                                          forward.days=10,
+                                          cum.sum=TRUE,
+                                          include.day.of.signal=F,
+                                          obj.fun=signal.obj.slope,
+                                          decreasing=T)
+
+distributional.boxplot(signal=results.w$sigret.by.asset$IYT$paramset.5.20,
+                       x.val=seq(1, 10, 2),val=10,ylim=c(-20, 20),
+                       xlim=c(0, 10),mai=c(1,1,0.3,0.5),h=0)
+
+plot.signals(results.w$sigret.by.asset$XLE, rows=5, columns = 4)
+beanplot.signals(results.w$sigret.by.asset$XLE, rows=5, columns = 4)
+
+
+# Daily Signal With Post Monthly Return Analysis
+results.m =apply.paramset.signal.analysis(strategy.st, 
+                                          paramset.label='SMA', 
+                                          portfolio.st, 
+                                          sigcol = signal.label,
+                                          sigval = 1,
+                                          on='months',
+                                          forward.days=5,
+                                          cum.sum=TRUE,
+                                          include.day.of.signal=F,
+                                          obj.fun=signal.obj.slope,
+                                          decreasing=T)
+
+distributional.boxplot(signal=results.m$sigret.by.asset$IYT$paramset.5.20,
+                       x.val=seq(1, 5, 1),val=10,ylim=c(-30, 30),
+                       xlim=c(0, 5),mai=c(1,1,0.3,0.5),h=0)
+
+plot.signals(results.m$sigret.by.asset$XLE, rows=5, columns = 4)
+beanplot.signals(results.m$sigret.by.asset$XLE, rows=5, columns = 4)
+
+
+
+
+###############################################################################
+# R (http://r-project.org/) Quantitative Strategy Model Framework
+#
+# Copyright (c) 2009-2012
+# Peter Carl, Dirk Eddelbuettel, Brian G. Peterson,
+# Jeffrey Ryan, Joshua Ulrich, and Garrett See
+#
+# This library is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+###############################################################################
+
+##### PLACE THIS BLOCK AT END OF DEMO SCRIPT ################### 
+# book  = getOrderBook(port)
+# stats = tradeStats(port)
+# rets  = PortfReturns(acct)
+################################################################
\ No newline at end of file



More information about the Blotter-commits mailing list