-
Notifications
You must be signed in to change notification settings - Fork 27
/
quantstratTutorial_Basic.Rnw
662 lines (612 loc) · 21.2 KB
/
quantstratTutorial_Basic.Rnw
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
\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}}
\begin{center}
\includegraphics[width=0.9\textwidth,keepaspectratio]{images/CFRM_Logo.png} \\ \vspace{2.0\baselineskip}
\LARGE {\textbf{Getting started with quantstrat}} \\ \vspace{2.0\baselineskip}
\large{Guy Yollin} \\ \vq
\scriptsize{Applied Mathematics\\University of Washington}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{Outline}
\tableofcontents
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\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)
}
@
\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}[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}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Basic quantstrat strategy example}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\frametitle{Quantstrat object model}
\begin{center}
\includegraphics[width=1.0\textwidth,keepaspectratio]{images/qsObjectModel.png}
\end{center}
Generic Signal-Based Strategy Modeling:
\begin{itemize}
\item Instruments contain market data
\item Indicators are quantitative values derived from market data
\item Interaction between indicators and market data are used to generate signals (e.g. crossovers, thresholds)
\item Rules use market data, indicators, signals, and current account/portfolio characteristics to generate orders
\item Interaction between orders and market data generates transactions
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Basic strategy backtesting workflow for quantstrat}
\begin{center}
\includegraphics[width=1.0\textwidth,keepaspectratio]{images/QSflowchart2}
\end{center}
\end{frame}
\begin{frame}
\frametitle{Key \texttt{blotter} functions} \vm
\begin{scriptsize}
\begin{center}
\begin{tabular}{ll}
\multicolumn{2}{c}{\color{blue} \textbf{Initialization}} \\ [0.1em] \hline
& \\ [-0.9em]
initPortf & initializes a portfolio object \\ [0.1em]
& \\ [-0.9em]
initAcct & initializes an account object \\ [0.1em]
& \\ [-0.9em] \\ [-0.6em]
\multicolumn{2}{c}{\color{blue} \textbf{Processing}} \\ [0.1em] \hline
& \\ [-0.9em]
addTxn & add transactions to a portfolio \\ [0.1em]
& \\ [-0.9em]
updatePortf & calculate P\&L for each symbol for each period \\ [0.1em]
& \\ [-0.9em]
updateAcct & calculate equity from portfolio data \\ [0.1em]
& \\ [-0.9em]
updateEndEq & update ending equity for an account \\ [0.1em]
& \\ [-0.9em]
getEndEq & retrieves the most recent value of the capital account \\ [0.1em]
& \\ [-0.9em]
getPosQty & gets position at Date \\ [0.1em]
& \\ [-0.9em] \\ [-0.6em]
\multicolumn{2}{c}{\color{blue} \textbf{Analysis}} \\ [0.1em] \hline
& \\ [-0.9em]
chart.Posn & chart market data, position size, and cumulative P\&L \\ [0.1em]
& \\ [-0.9em]
PortfReturns & calculate portfolio instrument returns \\ [0.1em]
& \\ [-0.9em]
getAccount & get an account object from the .blotter environment \\ [0.1em]
& \\ [-0.9em]
getPortfolio & get a portfolio object from the .blotter environment \\ [0.1em]
& \\ [-0.9em]
getTxns & retrieve transactions from a portfolio \\ [0.1em]
& \\ [-0.9em]
tradeStats & calculate trade statistics \\ [0.1em]
& \\ [-0.9em]
perTradeStats & calculate flat to flat per-trade statistics
\end{tabular}
\end{center}
\end{scriptsize}
\end{frame}
\begin{frame}
\frametitle{Key \texttt{quantstrat} functions}
\begin{scriptsize}
\begin{center}
\begin{tabular}{ll}
\multicolumn{2}{c}{\color{blue} \textbf{Initialization}} \\ [0.1em] \hline
& \\ [-0.9em]
initOrders & initialize order container \\ [0.1em]
& \\ [-0.9em]
strategy & constructor for strategy object \\ [0.1em]
& \\ [-0.9em] \\
\multicolumn{2}{c}{\color{blue} \textbf{Strategy definition}} \\ [0.1em] \hline
& \\ [-0.9em]
add.indicator & add an indicator to a strategy \\ [0.1em]
& \\ [-0.9em]
add.signal & add a signal to a strategy \\ [0.1em]
& \\ [-0.9em]
add.rule & add a rule to a strategy \\ [0.1em]
& \\ [-0.9em]
add.distribution & add a distribution to a paramset in a strategy \\ [0.1em]
& \\ [-0.9em]
add.constraint & add a constraint on 2 distributions within a paramset \\ [0.1em]
& \\ [-0.9em] \\
\multicolumn{2}{c}{\color{blue} \textbf{Processing}} \\ [0.1em] \hline
& \\ [-0.9em]
applyStrategy & apply the strategy to arbitrary market data \\ [0.1em]
& \\ [-0.9em]
addPosLimit & add position and level limits at timestamp \\ [0.1em]
& \\ [-0.9em]
apply.paramset & apply a paramset to the strategy \\ [0.1em]
& \\ [-0.9em]
applyStrategy.rebalancing & apply the strategy to data with periodic rebalancing \\ [0.1em]
\end{tabular}
\end{center}
\end{scriptsize}
\footnotetext{The functions in quantstrat are used in conjunction with the functions in \texttt{blotter}}
\end{frame}
\begin{frame}[fragile]
\frametitle{Initialize instruments}
\begin{center}
\includegraphics[width=1.0\textwidth,keepaspectratio]{images/qsFlow1}
\end{center} \vspace{-1.5\baselineskip}
<<results='hide'>>=
library(quantstrat)
initDate <- '2009-12-31'
initEq <- 1e6
currency("USD")
stock(symbols, currency="USD", multiplier=1)
@
\begin{itemize}
\item Initialize currency instrument first and then stock instrument
\item Important that portfolio, account, and orderbook initialization date be before start of data
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Initialize portfolio, account, and orders object}
\begin{center}
\includegraphics[width=1.0\textwidth,keepaspectratio]{images/qsFlow2}
\end{center} \vspace{-1.5\baselineskip}
<<results='hide'>>=
rm.strat("multiAsset.bb1") # remove portfolio, account, orderbook if re-run
initPortf(name="multiAsset.bb1", symbols, initDate=initDate)
initAcct(name="multiAsset.bb1", portfolios="multiAsset.bb1",
initDate=initDate, initEq=initEq)
initOrders(portfolio="multiAsset.bb1", initDate=initDate)
@
<<results='hide'>>=
strategy("bbands", store=TRUE)
@
\begin{itemize}
\item The function \texttt{rm.strat} removes any existing portfolio, account, or orderbook objects which facilitates re-running the code
\item The function \texttt{strategy} initializes and new strategy object
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{The \texttt{add.indicator} function}
\begin{itemize}
\item Indicators are typically standard technical or statistical analysis outputs, such as moving averages, bands, or pricing models
\item Indicators are applied before signals and rules, and the output of indicators may be used as inputs to construct signals or fire rules
\end{itemize}
<<>>=
args(add.indicator)
@
\vq
\textmd{Main arguments:}
\begin{mydescription}{arguments}
\item [strategy] strategy object
\item [name] name of the indicator (must be an R function)
\item [arguments] arguments to be passed to the indicator function
\item [label] name to reference the indicator
\end{mydescription}
\end{frame}
\begin{frame}[fragile]
\frametitle{Define indicators}
\begin{center}
\includegraphics[width=1.0\textwidth,keepaspectratio]{images/qsFlow3}
\end{center} \vspace{-1.5\baselineskip}
<<>>=
args(BBands)
@
<<results='hide'>>=
add.indicator("bbands", name = "BBands",
arguments = list(HLC = quote(HLC(mktdata)), maType='SMA'), label='bbInd')
@
\begin{itemize}
\item \texttt{quote()} returns it's argument without evaluating
\item \texttt{mktdata} is the time series object that holds the current symbols data during evaluation
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{The \texttt{add.signals} function}
quantstrat supports the following signal types:
\begin{mydescription}{sigComparison}
\item [sigCrossover] crossover signal ("gt", "lt", "eq", "gte", "lte")
\item [sigComparison] comparison signal ("gt", "lt", "eq", "gte", "lte")
\item [sigThreshold] threshold signal ("gt", "lt", "eq", "gte", "lte")
\item [sigPeak] peak/valley signals ("peak", "bottom")
\item [sigFormula] signal calculated from a formula
\end{mydescription}
<<>>=
args(add.signal)
@
\vq
\textmd{Main arguments:}
\begin{mydescription}{arguments}
\item [strategy] strategy object
\item [name] name of the signal, must correspond to an R function
\item [arguments] arguments to be passed to the signal function
\end{mydescription}
\end{frame}
\begin{frame}[fragile]
\frametitle{Define signals}
\begin{center}
\includegraphics[width=1.0\textwidth,keepaspectratio]{images/qsFlow3}
\end{center} \vspace{-1.5\baselineskip}
<<results='hide'>>=
add.signal("bbands", name="sigCrossover",
arguments=list(columns=c("High","up"),relationship="gt"),
label="H.gt.UpperBand")
@
<<results='hide'>>=
add.signal("bbands", name="sigCrossover",
arguments=list(columns=c("Low","dn"),relationship="lt"),
label="L.lt.LowerBand")
@
\end{frame}
\begin{frame}[fragile]
\frametitle{The \texttt{add.rules} function}
The function \texttt{add.rule} adds a rule to a strategy \\
<<>>=
args(add.rule)
@
\vh
\textmd{Main arguments:}
\begin{mydescription}{arguments}
\item [strategy] strategy object
\item [name] name of the rule (typically \texttt{ruleSignal})
\item [arguments] arguments to be passed to the rule function
\item [type] type of rule ("risk","order","rebalance","exit","enter")
\item [label] user supplied text label for rule
\end{mydescription}
\end{frame}
\begin{frame}[fragile]
\frametitle{The \texttt{ruleSignal} function}
\texttt{ruleSignal} is the default rule to generate a trade order on a signal \\
<<>>=
args(ruleSignal)
@
\textmd{Main arguments:}
\begin{mydescription}{ordertype}
\item [sigcol] column name to check for signal
\item [sigval] signal value to match
\item [orderqty] quantity for order or 'all', modified by osFUN
\item [ordertype] "market","limit","stoplimit","stoptrailing","iceberg"
\item [orderside] "long", "short", or NULL
\item [osFUN] function or name of order sizing function (default is osNoOp)
\end{mydescription}
\end{frame}
\begin{frame}[fragile]
\frametitle{Add rules}
\begin{center}
\includegraphics[width=1.0\textwidth,keepaspectratio]{images/qsFlow3}
\end{center} \vspace{-1.5\baselineskip}
<<results='hide'>>=
add.rule("bbands", name='ruleSignal',
arguments=list(sigcol="H.gt.UpperBand",sigval=TRUE,
orderqty=+100, ordertype='market', orderside='long'),
type='enter',
label='LongEntry')
@
<<results='hide'>>=
add.rule("bbands", name='ruleSignal',
arguments=list(sigcol="L.lt.LowerBand",sigval=TRUE,
orderqty= 'all', ordertype='market', orderside='long'),
type='exit',
label='LongExit')
@
\begin{itemize}
\item Long-only channel breakout system with pyramiding
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{The \texttt{applyStrategy} function}
The \texttt{applyStrategy} function applies the strategy to a portfolio and generates transactions according to the strategy rules and the market data \\
<<>>=
args(applyStrategy)
@
\vh
\textmd{Main arguments:}
\begin{mydescription}{parameters}
\item [strategy] an object of type 'strategy'
\item [portfolios] a list of portfolios to apply the strategy to
\item [parameters] named list of parameters to be applied during evaluation of the strategy
\end{mydescription}
\end{frame}
\begin{frame}[fragile]
\frametitle{Applying strategy to a multi-asset portfolio}
\begin{center}
\includegraphics[width=1.0\textwidth,keepaspectratio]{images/qsFlow4}
\end{center} \vspace{-1.5\baselineskip}
<<results='hide'>>=
nSD = 2
nMA = 20
@
<<results='hide'>>=
out <- applyStrategy("bbands",
portfolios="multiAsset.bb1",parameters=list(sd=nSD,n=nMA))
@
\begin{itemize}
\item Indicator parameters can be passed when applying the strategy; for this run the length of the moving average is \Sexpr{nMA} and the standard deviation multiplier is \Sexpr{nSD}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Apply the strategy}
Calling \texttt{applyStrategy} generates transactions in the specified portfolio.
<<echo=F>>=
options(width=120)
@
<<size='tiny'>>=
getTxns(Portfolio="multiAsset.bb1", Symbol="XLK")
@
<<echo=F>>=
options(width=81)
@
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{The \texttt{mktdata} object}
\texttt{mktdata} is a special variable constructed during the execution of \texttt{applyStrategy}. It is a time series object which contains the historic price data for the current symbol being evaluated as well as the calculated indicators and signals:\\ \vq
<<echo=FALSE>>=
options(width=180,digits=6)
@
<<size='Tiny'>>=
mktdata["2015"]
@
<<echo=FALSE>>=
options(width=81,digits=8)
@
\begin{itemize}
\item Inspecting \texttt{mktdata} can be very helpful in understanding strategy processing and debugging
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Update portfolio and account}
\begin{center}
\includegraphics[width=1.0\textwidth,keepaspectratio]{images/qsFlow5}
\end{center} \vspace{-1.5\baselineskip}
<<results='hide'>>=
updatePortf("multiAsset.bb1")
updateAcct("multiAsset.bb1")
updateEndEq("multiAsset.bb1")
@
\end{frame}
\begin{frame}[fragile]
\frametitle{Data integrity check}
<<size='Tiny'>>=
checkBlotterUpdate <- function(port.st,account.st,verbose=TRUE)
{
ok <- TRUE
p <- getPortfolio(port.st)
a <- getAccount(account.st)
syms <- names(p$symbols)
port.tot <- sum(sapply(syms,FUN = function(x) eval(parse(
text=paste("sum(p$symbols",x,"posPL.USD$Net.Trading.PL)",sep="$")))))
port.sum.tot <- sum(p$summary$Net.Trading.PL)
if( !isTRUE(all.equal(port.tot,port.sum.tot)) ) {
ok <- FALSE
if( verbose )
print("portfolio P&L doesn't match sum of symbols P&L")
}
initEq <- as.numeric(first(a$summary$End.Eq))
endEq <- as.numeric(last(a$summary$End.Eq))
if( !isTRUE(all.equal(port.tot,endEq-initEq)) ) {
ok <- FALSE
if( verbose )
print("portfolio P&L doesn't match account P&L")
}
if( sum(duplicated(index(p$summary))) ) {
ok <- FALSE
if( verbose )
print("duplicate timestamps in portfolio summary")
}
if( sum(duplicated(index(a$summary))) ) {
ok <- FALSE
if( verbose )
print("duplicate timestamps in account summary")
}
return(ok)
}
checkBlotterUpdate("multiAsset.bb1","multiAsset.bb1")
@
\end{frame}
\begin{frame}
\frametitle{How the \texttt{blotter\_portfolio} object gets updated} \vm \vm
\begin{center}
\includegraphics[width=0.95\textwidth,keepaspectratio]{images/blotterClassDiagram2.pdf}
\end{center}
\end{frame}
\begin{frame}[fragile]
\frametitle{Generate position plots}
\begin{center}
\includegraphics[width=1.0\textwidth,keepaspectratio]{images/qsFlow6}
\end{center} \vspace{-1.5\baselineskip}
<<CHARTPOSNPANEL,fig.width=24,fig.height=18>>=
par(mfrow=c(3,3))
for(symbol in symbols)
{
chart.Posn(Portfolio="multiAsset.bb1",Symbol=symbol,theme=myTheme,
TA="add_BBands(n=20,sd=2)")
}
par(mfrow=c(1,1))
@
<<XLBCP,cache=FALSE>>=
chart.Posn("multiAsset.bb1","XLU",TA="add_BBands(n=20,sd=2)",theme=myTheme)
@
\end{frame}
\begin{frame}
\frametitle{Position plots} \vm
\begin{center}
\includegraphics[width=0.90\textwidth,keepaspectratio]{figures/CHARTPOSNPANEL-1}
\end{center}
\end{frame}
\begin{frame}
\frametitle{BBands strategy for XLU} \vm
\begin{center}
\includegraphics[width=0.95\textwidth,keepaspectratio]{figures/XLBCP-1}
\end{center}
\end{frame}
\begin{frame}[containsverbatim]
\frametitle{Trade stats by instrument}
<<MULTITRADESTATS,echo=TRUE,fig.width=12,fig.height=9>>=
textplot(t(tradeStats("multiAsset.bb1")))
@
\vspace{-2.25\baselineskip}
\begin{center}
\includegraphics[width=1.0\textwidth,keepaspectratio]{figures/MULTITRADESTATS-1}
\end{center}
\end{frame}
\begin{frame}[fragile]
\frametitle{Individual asset returns}
<<echo=FALSE>>=
options(width=78)
@
<<IASSRET,cache=FALSE>>=
rets.multi <- PortfReturns("multiAsset.bb1")
colnames(rets.multi) <- sort(symbols)
round(tail(rets.multi,5),6)
chart.CumReturns(rets.multi, colorset= rich10equal, legend.loc = "topleft",
main="SPDR Cumulative Returns",minor.ticks=FALSE)
@
<<echo=FALSE>>=
options(width=81)
@
\end{frame}
\begin{frame}
\frametitle{Cumulative returns by asset} \vm
\begin{center}
\includegraphics[width=0.95\textwidth,keepaspectratio]{figures/IASSRET-1}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\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}