Repository navigation
Expand file tree
/
Copy pathquantstratTutorial_Intro.Rnw
More file actions
511 lines (469 loc) · 17.1 KB
/
Copy pathquantstratTutorial_Intro.Rnw
File metadata and controls
511 lines (469 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
\documentclass[fleqn,xcolor=pdftex,dvipsnames,table]{beamer}
\input{boilerPlateUW}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\beamer@sectionintoc}{\vskip1.5em}{\vskip1.3em}{}{}
\makeatother
\AtBeginSection[]
{
\begin{frame}
\frametitle{Outline}
% sectionstyle=<style for current section>/<style for other sections>
% subsectionstyle=<style for current subsection>/<style for other subsections in current section>/<style for subsections in other sections>
\tableofcontents[sectionstyle=show/shaded,subsectionstyle=show/show/hide]
\end{frame}
}
\AtBeginSubsection[]
{
\begin{frame}
\frametitle{Outline}
% sectionstyle=<style for current section>/<style for other sections>
% subsectionstyle=<style for current subsection>/<style for other subsections in current section>/<style for subsections in other sections>
\tableofcontents[sectionstyle=show/shaded,subsectionstyle=show/shaded/hide]
\end{frame}
}
\title{Getting started with quantstrat}
\author{\href{http://faculty.washington.edu/gyollin/}{Guy Yollin}}
\date{R/Finance 2015}
\institute[Copyright \copyright \ 2015]{}
%\logo{\includegraphics[width=0.25in]{logo}}
<<echo=FALSE>>=
#########################################################################
# Copyright (C) 2011-2015 Guy Yollin #
# License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher #
#########################################################################
@
<<include=FALSE>>=
library(knitr)
opts_chunk$set(tidy=FALSE,cache=FALSE,size='scriptsize',
fig.path='figures/',fig.show='hide',fig.keep='last',
fig.align='center', fig.width=7, fig.height=5,
message=FALSE,warning=FALSE)
@
<<echo=FALSE,cache=FALSE>>=
options(width=81,continue=" ",digits=8)
@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\normalem
\lstset{language=Pascal,basicstyle=\scriptsize,tabsize=2}
%\frame{\titlepage}
\begin{frame}
\vb
\frametitle{\textcolor{purple}{Computational Finance and Risk Management}} \vm
\begin{center}
\includegraphics[width=0.5\textwidth,keepaspectratio]{images/stockMarket.jpg} \\ \vspace{1.0\baselineskip}
\LARGE {\textbf{Getting started with quantstrat}} \\ \vspace{1.0\baselineskip}
\large{Guy Yollin} \\ \vq
\scriptsize{University of Washington} \\ \vh
\large{Joshua Ulrich} \\ \vq
\scriptsize{DV Trading}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{Legal Disclaimer}
\begin{itemize}
\item This presentation is for informational purposes only
\item This presentation should not be construed as a solicitation or offering of investment services
\item The presentation does not intend to provide investment advice
\item The information in this presentation should not be construed as an offer to buy or sell, or the solicitation of an offer to buy or sell any security, or as a recommendation or advice about the purchase or sale of any security
\item The presenter(s) shall not shall be liable for any errors or inaccuracies in the information presented
\item There are no warranties, expressed or implied, as to accuracy, completeness, or results obtained from any information presented
\end{itemize}
\begin{center}
\color{red}\textbf{INVESTING ALWAYS INVOLVES RISK}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Guy Yollin}
\begin{itemize}
\item{Professional Experience}
\begin{itemize}
\item{Software Engineering}
\begin{itemize}
\item{r-programming.org}
\item{Insightful Corporation}
\item{Electro Scientific Industries, Vision Products Division}
\end{itemize}
\item{Hedge Fund}
\begin{itemize}
\item{Rotella Capital Management}
\item{J.E. Moody, LLC}
\end{itemize}
\item{Academic}
\begin{itemize}
\item{University of Washington}
\item{Oregon Graduate Institute}
\end{itemize}
\end{itemize} \vh
\item{Education}
\begin{itemize}
\item{Oregon Graduate Institute, Computational Finance}
\item{Drexel University, Electrical Engineering}
\end{itemize}\vh
\item{Contact Info}
\begin{itemize}
\item{\url{gyollin@uw.edu}}
\item{\url{http://www.linkedin.com/in/guyyollin}}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Joshua Ulrich}
\begin{itemize}
\item{Professional Experience}
\begin{itemize}
\item{Programming}
\begin{itemize}
\item{fossfinance.com}
\item{R packages: TTR, xts, quantmod, blotter, quantstrat, pack, LSPM}
\end{itemize} %% Programming
\item{Finance}
\begin{itemize}
\item{DV Trading}
\item{Enterprise Bank \& Trust}
\item{Wells Fargo Home Mortgage}
\end{itemize} %% Finance
\end{itemize}\vh %% Professional experience
\item{Education}
\begin{itemize}
\item{University of Missouri--St. Louis, Economics}
\end{itemize}\vh
\item{Contact Info}
\begin{itemize}
\item{\url{josh.m.ulrich@gmail.com}}
\item{\url{http://about.me/joshuaulrich}}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Lecture references}
\begin{itemize}
\item TradeAnalytics project page on R-forge:\\
\url{http://r-forge.r-project.org/projects/blotter/}
\begin{itemize}
\item documents and demos for:
\begin{itemize}
\item blotter package
\item quantstrat package\\
\end{itemize}
\end{itemize} \vb
\item Using quantstrat by Jan Humme \& Brian Peterson\\
{\footnotesize \url{http://www.rinfinance.com/agenda/2013/workshop/Humme+Peterson.pdf} \vb}
\item R-SIG-FINANCE:\\
\url{https://stat.ethz.ch/mailman/listinfo/r-sig-finance} \vb
\end{itemize}
\footnotetext[2]{demos are located in the directory: \texttt{.../R-3.x.x/library/quantstrat/demo}}
\end{frame}
\begin{frame}
\frametitle{Outline}
\tableofcontents
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{Packages for quantitative finance in R} \vspace{-0.5\baselineskip}
\begin{center}
\includegraphics[width=0.95\textwidth,keepaspectratio]{images/quantAnalPackageHierarchy}
\end{center}
\end{frame}
\begin{frame}
\frametitle{About \texttt{blotter} and \texttt{quantstrat}}
\begin{itemize}
\item Provides support for multi-asset class and multi-currency portfolios for backtesting and other financial research. \textcolor{red}{\textbf{Still in heavy development}}. \vh
\item The software is in an beta stage
\begin{itemize}
\item some things are not completely implemented (or documented)
\item some things invariably have errors
\item some implementations will change in the future
\end{itemize} \vh
\item Software has been in development for a number of years
\begin{itemize}
\item blotter: Dec-2008
\item quantstrat: Feb-2010
\end{itemize} \vh
\item Software is used everyday by working professions in asset management
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{The \texttt{blotter} package}
Description\\
{\addtolength{\leftskip}{5 mm}
Transaction infrastructure for defining instruments, transactions, portfolios and accounts for trading systems and simulation. Provides portfolio support for multi-asset class and multi-currency portfolios. Still in heavy development.\\*
}\vh
Key features
\begin{itemize}
\item supports portfolios of multiple assets
\item supports accounts of multiple portfolios
\item supports P\&L calculation and roll-up across instruments and portfolios (i.e. \texttt{blotter} does low-level trading system accounting)
\end{itemize} \vh
Authors
\begin{itemize}
\item Peter Carl
\item Brian Peterson
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{The \texttt{quantstrat} package}
Description\\
{\addtolength{\leftskip}{5 mm}
\texttt{quantstrat} provides a generic infrastructure to model and backtest signal-based quantitative strategies. It is a high-level abstraction layer (built on xts, FinancialInstrument, blotter, etc.) that allows you to build and test strategies in very few lines of code. \\*
}\vh
Key features
\begin{itemize}
\item Supports strategies which include indicators, signals, and rules
\item Allows strategies to be applied to multi-asset portfolios
\item Supports market, limit, stoplimit, and stoptrailing order types
\item Supports order sizing and parameter optimization
\end{itemize} \vh
Authors
\begin{itemize}
\item Peter Carl, Brian Peterson
\item Joshua Ulrich, Jan Humme
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{The TTR package}
The \texttt{TTR} package is a comprehensive collection of technical analysis indicators for R \\ \vh
\textmd{Key features:}
\begin{itemize}
\item moving averages
\item oscillators
\item price channels
\item trend indicators
\end{itemize} \vh
\textmd{Author:}
\begin{itemize}
\item Joshua Ulrich
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Selected technical analysis indicators in TTR}
\begin{tiny}
\begin{center}
\begin{tabular}{ l | l || l | l }
\textbf{Function} & \textbf{Description} & \textbf{Function} & \textbf{Description} \\ [0.5em] \hline
& & & \\ [-0.5em]
stoch & stochastic oscillator & ADX & Directional Movement Index \\ [0.5em]
aroon & Aroon indicator & ATR & Average True Range \\ [0.5em]
BBands & Bollinger bands & CCI & Commodity Channel Index \\ [0.5em]
chaikinAD & Chaikin Acc/Dist & chaikinVolatility & Chaikin Volatility \\ [0.5em]
ROC & rate of change & momentum & momentum indicator \\ [0.5em]
CLV & Close Location Value & CMF & Chaikin Money Flow \\ [0.5em]
CMO & Chande Momentum Oscillator & SMA & simple moving average \\ [0.5em]
EMA & exponential moving average & DEMA & double exp mov avg \\ [0.5em]
VWMA & volume weighted MA & VWAP & volume weighed avg price \\ [0.5em]
DonchianChannel & Donchian Channel & DPO & Detrended Price Oscillator \\ [0.5em]
EMV & Ease of Movement Value & volatility & volatility estimators \\ [0.5em]
MACD & MA converge/diverge & MFI & Money Flow Index \\ [0.5em]
RSI & Relative Strength Index & SAR & Parabolic Stop-and-Reverse \\ [0.5em]
TDI & Trend Detection Index & TRIX & Triple Smoothed Exponential Osc \\ [0.5em]
VHF & Vertical Horizontal Filter & williamsAD & Williams Acc/Dist \\ [0.5em]
WPR & William's \% R & ZigZag & Zig Zag trend line \\ [0.5em]
\end{tabular}
\end{center}
\end{tiny}
\footnotetext{see Technical Analysis from A to Z by Steven Achelis}
\end{frame}
\begin{frame}[fragile]
\frametitle{Install trading system development packages}
<<eval=FALSE>>=
#
# install these packages from CRAN (or r-forge)
#
install.packages("xts")
install.packages("PerformanceAnalytics")
install.packages("quantmod")
install.packages("TTR")
#
# Install these package from r-forge
#
install.packages("FinancialInstrument", repos = "http://R-Forge.R-project.org")
install.packages("blotter", repos = "http://R-Forge.R-project.org")
install.packages("quantstrat", repos = "http://R-Forge.R-project.org")
@
\begin{itemize}
\item R-Forge packages can be installed by setting the repos argument to \url{http://R-Forge.R-project.org}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{ETF Portfolio}
In the following examples, we'll use a 9-asset portfolio composed of the 9 Select Sector SPDRs that divide the S\&P 500 into nine sector index funds: \\ \vb
\begin{small}
\begin{center}
\begin{tabular}{ l l }
\textbf{Symbol} & \textbf{Sector} \\ [0.1em] \hline
& \\ [-0.1em]
XLY & Consumer Discretionary \\ [0.1em]
XLP & Consumer Staples \\ [0.1em]
XLE & Energy \\ [0.1em]
XLF & Financial \\ [0.1em]
XLV & Health Care \\ [0.1em]
XLI & Industrial \\ [0.1em]
XLB & Materials \\ [0.1em]
XLK & Technology \\ [0.1em]
XLU & Utilities \\ [0.1em]
\end{tabular}
\end{center}
\end{small}
\end{frame}
\begin{frame}[fragile]
\frametitle{Download data}
<<>>=
library(PerformanceAnalytics)
library(quantmod)
library(lattice)
startDate <- '2010-01-01' # start of data
endDate <- '2015-05-01' # end of data
Sys.setenv(TZ="UTC") # set time zone
symbols = c("XLF", "XLP", "XLE", "XLY", "XLV", "XLI", "XLB", "XLK", "XLU")
@
\vm
<<echo=FALSE>>=
if(file.exists("XLX.RData"))
{
load("XLX.RData")
} else {
getSymbols(symbols, from=startDate, to=endDate, index.class="POSIXct")
for(symbol in symbols) {
x<-get(symbol)
x<-adjustOHLC(x,symbol.name=symbol)
x<-to.weekly(x,indexAt='lastof',drop.time=TRUE)
indexFormat(x)<-'%Y-%m-%d'
colnames(x)<-gsub("x",symbol,colnames(x))
assign(symbol,x)
}
save(list=symbols,file="XLX.RData")
}
@
<<eval=FALSE>>=
getSymbols(symbols, from=startDate, to=endDate, index.class="POSIXct")
for(symbol in symbols) {
x<-get(symbol)
x<-adjustOHLC(x,symbol.name=symbol)
x<-to.weekly(x,indexAt='lastof',drop.time=TRUE)
indexFormat(x)<-'%Y-%m-%d'
colnames(x)<-gsub("x",symbol,colnames(x))
assign(symbol,x)
}
@
\begin{itemize}
\item Set timezone
\item Use \texttt{POSIXct} as index class for historic quotes
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Compute returns}
<<plotETF,echo=TRUE, fig.keep='all', tidy=FALSE>>=
prices <- NULL
for(i in 1:length(symbols))
prices <- cbind(prices,Cl(get(symbols[i])))
colnames(prices) <- symbols
returns <- diff(log(prices))[-1, ]
num.ass <- ncol(returns)
xyplot(prices, xlab = "", layout = c(3, 3),type=c("l","g"))
stacked.df <- stack(as.data.frame(returns))
colnames(stacked.df) <- c("returns", "symbol")
densityplot(~returns | symbol, stacked.df, cex = 0.25, xlab="",type=c("l","g"))
@
\end{frame}
\begin{frame}
\frametitle{Sector Select SPDRs} \vm \vm
\begin{center}
\includegraphics[width=1.0\textwidth,keepaspectratio]{figures/plotETF-1}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Sector Select SPDRs} \vm \vm
\begin{center}
\includegraphics[width=1.0\textwidth,keepaspectratio]{figures/plotETF-2}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Bollinger bands}
\begin{itemize}
\item Bollinger bands are a volatility-sensitive price channel \vh
\item Published by John Bollinger in the early 1980s \vh
\item RSI Calculation
\begin{itemize}
\item MA(nMA) = simple moving average of the weighted-close
\item Upper Band = MA(nMA) + nSD $\times$ StdDev(C)
\item Lower Band = MA(nMA) - nSD $\times$ StdDev(C)
\item nMA typically 20
\item nSD typically in the range of 2 to 3
\end{itemize} \vh
\item Interpretation
\begin{itemize}
\item Trade channel reversals between the upper and lower bands
\item Trade channel break-outs above/below the bands
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Long-only Bollinger Band breakout strategy}
Buy rule:
\begin{itemize}
\item Buy long when the High crosses above the upper band
\end{itemize} \vb
Exit rule:
\begin{itemize}
\item Exit when the Low crosses below the lower band
\end{itemize} \vb
Pyramiding:
\begin{itemize}
\item Multiple orders in the same direction
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Calculate and plot Bollinger bands}
<<XLFBB,cache=FALSE>>=
args(BBands)
b <- BBands(HLC=HLC(XLF["2013"]), n=20, sd=2)
tail(b)
myTheme<-chart_theme()
myTheme$col$dn.col<-'lightblue'
myTheme$col$dn.border <- 'lightgray'
myTheme$col$up.border <- 'lightgray'
chart_Series(XLF,TA='add_BBands(lwd=2)',theme=myTheme,name="XLF")
@
\end{frame}
\begin{frame}
\frametitle{Bollinger bands} \vm
\begin{center}
\includegraphics[width=0.95\textwidth,keepaspectratio]{figures/XLFBB-1}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Basic quantstrat strategy example}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Position sizing}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Stop orders}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Parameter optimization}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{\textcolor{purple}{Computational Finance and Risk Management}}
\begin{center}
\includegraphics[width=0.75\textwidth,keepaspectratio]{images/CFRM_Logo.png} \\ \vb
\url{http://depts.washington.edu/compfin}
\end{center}
\end{frame}
\end{document}