forked from timtrice/backtesting-strategies
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparameter-optimization.html
444 lines (398 loc) · 37.3 KB
/
parameter-optimization.html
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
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Backtesting Strategies with R</title>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<meta name="description" content="Backtesting strategies with R">
<meta name="generator" content="bookdown 0.0.62 and GitBook 2.6.7">
<meta property="og:title" content="Backtesting Strategies with R" />
<meta property="og:type" content="book" />
<meta property="og:description" content="Backtesting strategies with R" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Backtesting Strategies with R" />
<meta name="twitter:description" content="Backtesting strategies with R" />
<meta name="author" content="Tim Trice">
<meta name="date" content="2016-05-06">
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: { equationNumbers: { autoNumber: "AMS" } }
});
</script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="prev" href="data-quality.html">
<link rel="next" href="stop-loss.html">
<script src="assets/jquery-2.2.3/jquery.min.js"></script>
<link href="assets/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="assets/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="assets/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="assets/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="assets/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<script src="assets/htmlwidgets-0.6/htmlwidgets.js"></script>
<script src="assets/datatables-binding-0.1/datatables.js"></script>
<script src="assets/datatables-1.10.7/jquery.dataTables.min.js"></script>
<link href="assets/datatables-default-1.10.7/dataTables.extra.css" rel="stylesheet" />
<link href="assets/datatables-default-1.10.7/jquery.dataTables.min.css" rel="stylesheet" />
<link href="assets/datatables-scroller-1.10.7/dataTables.scroller.min.css" rel="stylesheet" />
<script src="assets/datatables-scroller-1.10.7/dataTables.scroller.min.js"></script>
<link href="assets/datatables-fixedColumns-1.10.7/dataTables.fixedColumns.min.css" rel="stylesheet" />
<script src="assets/datatables-fixedColumns-1.10.7/dataTables.fixedColumns.min.js"></script>
<link href="assets/nouislider-7.0.10/jquery.nouislider.min.css" rel="stylesheet" />
<script src="assets/nouislider-7.0.10/jquery.nouislider.min.js"></script>
<link href="assets/selectize-0.12.0/selectize.bootstrap3.css" rel="stylesheet" />
<script src="assets/selectize-0.12.0/selectize.min.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-1507390-25', 'auto');
ga('send', 'pageview');
</script>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>
<link rel="stylesheet" href="custom_styles.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter" data-level="1" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i><b>1</b> Introduction</a><ul>
<li class="chapter" data-level="1.1" data-path="index.html"><a href="index.html#r-resources"><i class="fa fa-check"></i><b>1.1</b> R Resources</a></li>
<li class="chapter" data-level="1.2" data-path="index.html"><a href="index.html#libraries"><i class="fa fa-check"></i><b>1.2</b> Libraries</a></li>
<li class="chapter" data-level="1.3" data-path="index.html"><a href="index.html#sessioninfo"><i class="fa fa-check"></i><b>1.3</b> SessionInfo</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="terminology.html"><a href="terminology.html"><i class="fa fa-check"></i><b>2</b> Terminology</a></li>
<li class="chapter" data-level="3" data-path="using-quantstrat.html"><a href="using-quantstrat.html"><i class="fa fa-check"></i><b>3</b> Using Quantsrat</a><ul>
<li class="chapter" data-level="3.1" data-path="using-quantstrat.html"><a href="using-quantstrat.html#settings-and-variables"><i class="fa fa-check"></i><b>3.1</b> Settings and Variables</a></li>
<li class="chapter" data-level="3.2" data-path="using-quantstrat.html"><a href="using-quantstrat.html#symbols"><i class="fa fa-check"></i><b>3.2</b> Symbols</a></li>
<li class="chapter" data-level="3.3" data-path="using-quantstrat.html"><a href="using-quantstrat.html#checkBlotterUpdate"><i class="fa fa-check"></i><b>3.3</b> checkBlotterUpdate()</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="get-symbols.html"><a href="get-symbols.html"><i class="fa fa-check"></i><b>4</b> Get Symbols</a><ul>
<li class="chapter" data-level="4.1" data-path="get-symbols.html"><a href="get-symbols.html#yahoo"><i class="fa fa-check"></i><b>4.1</b> Yahoo!</a></li>
<li class="chapter" data-level="4.2" data-path="get-symbols.html"><a href="get-symbols.html#google"><i class="fa fa-check"></i><b>4.2</b> Google</a></li>
<li class="chapter" data-level="4.3" data-path="get-symbols.html"><a href="get-symbols.html#mysql"><i class="fa fa-check"></i><b>4.3</b> MySQL</a></li>
<li class="chapter" data-level="4.4" data-path="get-symbols.html"><a href="get-symbols.html#fred"><i class="fa fa-check"></i><b>4.4</b> FRED</a></li>
<li class="chapter" data-level="4.5" data-path="get-symbols.html"><a href="get-symbols.html#oanda"><i class="fa fa-check"></i><b>4.5</b> OANDA</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="basic-strategy.html"><a href="basic-strategy.html"><i class="fa fa-check"></i><b>5</b> Basic Strategy</a><ul>
<li class="chapter" data-level="5.1" data-path="basic-strategy.html"><a href="basic-strategy.html#strategy-setup"><i class="fa fa-check"></i><b>5.1</b> Strategy Setup</a></li>
<li class="chapter" data-level="5.2" data-path="basic-strategy.html"><a href="basic-strategy.html#add-indicators"><i class="fa fa-check"></i><b>5.2</b> Add Indicators</a></li>
<li class="chapter" data-level="5.3" data-path="basic-strategy.html"><a href="basic-strategy.html#add-signals"><i class="fa fa-check"></i><b>5.3</b> Add Signals</a></li>
<li class="chapter" data-level="5.4" data-path="basic-strategy.html"><a href="basic-strategy.html#add-rules"><i class="fa fa-check"></i><b>5.4</b> Add Rules</a></li>
<li class="chapter" data-level="5.5" data-path="basic-strategy.html"><a href="basic-strategy.html#apply-strategy"><i class="fa fa-check"></i><b>5.5</b> Apply Strategy</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="data-quality.html"><a href="data-quality.html"><i class="fa fa-check"></i><b>6</b> Data Quality</a><ul>
<li class="chapter" data-level="6.1" data-path="data-quality.html"><a href="data-quality.html#yahoo-vs.google"><i class="fa fa-check"></i><b>6.1</b> Yahoo! vs. Google</a></li>
<li class="chapter" data-level="6.2" data-path="data-quality.html"><a href="data-quality.html#examining-trades"><i class="fa fa-check"></i><b>6.2</b> Examining Trades</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="parameter-optimization.html"><a href="parameter-optimization.html"><i class="fa fa-check"></i><b>7</b> Parameter Optimization</a><ul>
<li class="chapter" data-level="7.1" data-path="parameter-optimization.html"><a href="parameter-optimization.html#add-distribution"><i class="fa fa-check"></i><b>7.1</b> Add Distribution</a></li>
<li class="chapter" data-level="7.2" data-path="parameter-optimization.html"><a href="parameter-optimization.html#add-distribution-constraint"><i class="fa fa-check"></i><b>7.2</b> Add Distribution Constraint</a></li>
<li class="chapter" data-level="7.3" data-path="parameter-optimization.html"><a href="parameter-optimization.html#running-parallel"><i class="fa fa-check"></i><b>7.3</b> Running Parallel</a></li>
<li class="chapter" data-level="7.4" data-path="parameter-optimization.html"><a href="parameter-optimization.html#apply-paramset"><i class="fa fa-check"></i><b>7.4</b> Apply Paramset</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="stop-loss.html"><a href="stop-loss.html"><i class="fa fa-check"></i><b>8</b> Stop-Loss Orders</a><ul>
<li class="chapter" data-level="8.1" data-path="stop-loss.html"><a href="stop-loss.html#add-indicators-1"><i class="fa fa-check"></i><b>8.1</b> Add Indicators</a></li>
<li class="chapter" data-level="8.2" data-path="stop-loss.html"><a href="stop-loss.html#add-signals-1"><i class="fa fa-check"></i><b>8.2</b> Add Signals</a></li>
<li class="chapter" data-level="8.3" data-path="stop-loss.html"><a href="stop-loss.html#add-rules-1"><i class="fa fa-check"></i><b>8.3</b> Add Rules</a></li>
<li class="chapter" data-level="8.4" data-path="stop-loss.html"><a href="stop-loss.html#add-position-limit"><i class="fa fa-check"></i><b>8.4</b> Add Position Limit</a></li>
<li class="chapter" data-level="8.5" data-path="stop-loss.html"><a href="stop-loss.html#enable-rules"><i class="fa fa-check"></i><b>8.5</b> Enable Rules</a></li>
<li class="chapter" data-level="8.6" data-path="stop-loss.html"><a href="stop-loss.html#apply-strategy-1"><i class="fa fa-check"></i><b>8.6</b> Apply Strategy</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="stop-loss-optimization.html"><a href="stop-loss-optimization.html"><i class="fa fa-check"></i><b>9</b> Stop Loss Optimization</a><ul>
<li class="chapter" data-level="9.1" data-path="stop-loss-optimization.html"><a href="stop-loss-optimization.html#add-indicators-2"><i class="fa fa-check"></i><b>9.1</b> Add Indicators</a></li>
<li class="chapter" data-level="9.2" data-path="stop-loss-optimization.html"><a href="stop-loss-optimization.html#add-signals-2"><i class="fa fa-check"></i><b>9.2</b> Add Signals</a></li>
<li class="chapter" data-level="9.3" data-path="stop-loss-optimization.html"><a href="stop-loss-optimization.html#add-rules-2"><i class="fa fa-check"></i><b>9.3</b> Add Rules</a></li>
<li class="chapter" data-level="9.4" data-path="stop-loss-optimization.html"><a href="stop-loss-optimization.html#add-position-limit-1"><i class="fa fa-check"></i><b>9.4</b> Add Position Limit</a></li>
<li class="chapter" data-level="9.5" data-path="stop-loss-optimization.html"><a href="stop-loss-optimization.html#add-distribution-1"><i class="fa fa-check"></i><b>9.5</b> Add Distribution</a></li>
<li class="chapter" data-level="9.6" data-path="stop-loss-optimization.html"><a href="stop-loss-optimization.html#add-distribution-constraint-1"><i class="fa fa-check"></i><b>9.6</b> Add Distribution Constraint</a></li>
<li class="chapter" data-level="9.7" data-path="stop-loss-optimization.html"><a href="stop-loss-optimization.html#enable-rules-1"><i class="fa fa-check"></i><b>9.7</b> Enable Rules</a></li>
<li class="chapter" data-level="9.8" data-path="stop-loss-optimization.html"><a href="stop-loss-optimization.html#apply-paramset-1"><i class="fa fa-check"></i><b>9.8</b> Apply Paramset</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="trailing-stops.html"><a href="trailing-stops.html"><i class="fa fa-check"></i><b>10</b> Trailing Stops</a><ul>
<li class="chapter" data-level="10.1" data-path="trailing-stops.html"><a href="trailing-stops.html#osfixeddollar"><i class="fa fa-check"></i><b>10.1</b> osFixedDollar</a></li>
<li class="chapter" data-level="10.2" data-path="trailing-stops.html"><a href="trailing-stops.html#add-indicators-3"><i class="fa fa-check"></i><b>10.2</b> Add Indicators</a></li>
<li class="chapter" data-level="10.3" data-path="trailing-stops.html"><a href="trailing-stops.html#add-signals-3"><i class="fa fa-check"></i><b>10.3</b> Add Signals</a></li>
<li class="chapter" data-level="10.4" data-path="trailing-stops.html"><a href="trailing-stops.html#add-rules-3"><i class="fa fa-check"></i><b>10.4</b> Add Rules</a></li>
<li class="chapter" data-level="10.5" data-path="trailing-stops.html"><a href="trailing-stops.html#enable-rules-2"><i class="fa fa-check"></i><b>10.5</b> Enable Rules</a></li>
<li class="chapter" data-level="10.6" data-path="trailing-stops.html"><a href="trailing-stops.html#save-strategy"><i class="fa fa-check"></i><b>10.6</b> Save Strategy</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="obtaining-resources.html"><a href="obtaining-resources.html"><i class="fa fa-check"></i><b>11</b> Obtaining Resources</a><ul>
<li class="chapter" data-level="11.1" data-path="obtaining-resources.html"><a href="obtaining-resources.html#amazon-web-services"><i class="fa fa-check"></i><b>11.1</b> Amazon Web Services</a></li>
<li class="chapter" data-level="11.2" data-path="obtaining-resources.html"><a href="obtaining-resources.html#getting-started"><i class="fa fa-check"></i><b>11.2</b> Getting Started</a><ul>
<li class="chapter" data-level="11.2.1" data-path="obtaining-resources.html"><a href="obtaining-resources.html#installing-quantstrat"><i class="fa fa-check"></i><b>11.2.1</b> Installing Quantstrat</a></li>
</ul></li>
<li class="chapter" data-level="11.3" data-path="obtaining-resources.html"><a href="obtaining-resources.html#testing-resources"><i class="fa fa-check"></i><b>11.3</b> Testing Resources</a></li>
<li class="chapter" data-level="11.4" data-path="obtaining-resources.html"><a href="obtaining-resources.html#changing-instances"><i class="fa fa-check"></i><b>11.4</b> Changing Instances</a></li>
<li class="chapter" data-level="11.5" data-path="obtaining-resources.html"><a href="obtaining-resources.html#stop-the-server"><i class="fa fa-check"></i><b>11.5</b> Stop the Server</a></li>
<li class="chapter" data-level="11.6" data-path="obtaining-resources.html"><a href="obtaining-resources.html#reading-resources"><i class="fa fa-check"></i><b>11.6</b> Reading Resources</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="analyzing-results.html"><a href="analyzing-results.html"><i class="fa fa-check"></i><b>12</b> Analyzing Results</a><ul>
<li class="chapter" data-level="12.1" data-path="analyzing-results.html"><a href="analyzing-results.html#apply-strategy-2"><i class="fa fa-check"></i><b>12.1</b> Apply Strategy</a></li>
<li class="chapter" data-level="12.2" data-path="analyzing-results.html"><a href="analyzing-results.html#chart-positions"><i class="fa fa-check"></i><b>12.2</b> Chart Positions</a></li>
<li class="chapter" data-level="12.3" data-path="analyzing-results.html"><a href="analyzing-results.html#trade-statistics"><i class="fa fa-check"></i><b>12.3</b> Trade Statistics</a><ul>
<li class="chapter" data-level="12.3.1" data-path="analyzing-results.html"><a href="analyzing-results.html#trade-related"><i class="fa fa-check"></i><b>12.3.1</b> Trade Related</a></li>
<li class="chapter" data-level="12.3.2" data-path="analyzing-results.html"><a href="analyzing-results.html#profit-related"><i class="fa fa-check"></i><b>12.3.2</b> Profit Related</a></li>
<li class="chapter" data-level="12.3.3" data-path="analyzing-results.html"><a href="analyzing-results.html#averages"><i class="fa fa-check"></i><b>12.3.3</b> Averages</a></li>
<li class="chapter" data-level="12.3.4" data-path="analyzing-results.html"><a href="analyzing-results.html#performance-summary"><i class="fa fa-check"></i><b>12.3.4</b> Performance Summary</a></li>
<li class="chapter" data-level="12.3.5" data-path="analyzing-results.html"><a href="analyzing-results.html#per-trade-statistics"><i class="fa fa-check"></i><b>12.3.5</b> Per Trade Statistics</a></li>
<li class="chapter" data-level="12.3.6" data-path="analyzing-results.html"><a href="analyzing-results.html#performance-statistics"><i class="fa fa-check"></i><b>12.3.6</b> Performance Statistics</a></li>
<li class="chapter" data-level="12.3.7" data-path="analyzing-results.html"><a href="analyzing-results.html#risk-statistics"><i class="fa fa-check"></i><b>12.3.7</b> Risk Statistics</a></li>
<li class="chapter" data-level="12.3.8" data-path="analyzing-results.html"><a href="analyzing-results.html#buy-and-hold-performance"><i class="fa fa-check"></i><b>12.3.8</b> Buy and Hold Performance</a></li>
<li class="chapter" data-level="12.3.9" data-path="analyzing-results.html"><a href="analyzing-results.html#strategy-vs.market"><i class="fa fa-check"></i><b>12.3.9</b> Strategy vs. Market</a></li>
<li class="chapter" data-level="12.3.10" data-path="analyzing-results.html"><a href="analyzing-results.html#riskreturn-scatterplot"><i class="fa fa-check"></i><b>12.3.10</b> Risk/Return Scatterplot</a></li>
<li class="chapter" data-level="12.3.11" data-path="analyzing-results.html"><a href="analyzing-results.html#relative-performance"><i class="fa fa-check"></i><b>12.3.11</b> Relative Performance</a></li>
<li class="chapter" data-level="12.3.12" data-path="analyzing-results.html"><a href="analyzing-results.html#portfolio-summary"><i class="fa fa-check"></i><b>12.3.12</b> Portfolio Summary</a></li>
<li class="chapter" data-level="12.3.13" data-path="analyzing-results.html"><a href="analyzing-results.html#order-book"><i class="fa fa-check"></i><b>12.3.13</b> Order Book</a></li>
<li class="chapter" data-level="12.3.14" data-path="analyzing-results.html"><a href="analyzing-results.html#maximum-adverse-excursion"><i class="fa fa-check"></i><b>12.3.14</b> Maximum Adverse Excursion</a></li>
<li class="chapter" data-level="12.3.15" data-path="analyzing-results.html"><a href="analyzing-results.html#maximum-favorable-excursion"><i class="fa fa-check"></i><b>12.3.15</b> Maximum Favorable Excursion</a></li>
</ul></li>
<li class="chapter" data-level="12.4" data-path="analyzing-results.html"><a href="analyzing-results.html#account-summary"><i class="fa fa-check"></i><b>12.4</b> Account Summary</a><ul>
<li class="chapter" data-level="12.4.1" data-path="analyzing-results.html"><a href="analyzing-results.html#equity-curve"><i class="fa fa-check"></i><b>12.4.1</b> Equity Curve</a></li>
<li class="chapter" data-level="12.4.2" data-path="analyzing-results.html"><a href="analyzing-results.html#account-performance-summary"><i class="fa fa-check"></i><b>12.4.2</b> Account Performance Summary</a></li>
<li class="chapter" data-level="12.4.3" data-path="analyzing-results.html"><a href="analyzing-results.html#cumulative-returns"><i class="fa fa-check"></i><b>12.4.3</b> Cumulative Returns</a></li>
<li class="chapter" data-level="12.4.4" data-path="analyzing-results.html"><a href="analyzing-results.html#distribution-analysis"><i class="fa fa-check"></i><b>12.4.4</b> Distribution Analysis</a></li>
<li class="chapter" data-level="12.4.5" data-path="analyzing-results.html"><a href="analyzing-results.html#annualized-returns"><i class="fa fa-check"></i><b>12.4.5</b> Annualized Returns</a></li>
<li class="chapter" data-level="12.4.6" data-path="analyzing-results.html"><a href="analyzing-results.html#performance-scatter-plot"><i class="fa fa-check"></i><b>12.4.6</b> Performance Scatter Plot</a></li>
<li class="chapter" data-level="12.4.7" data-path="analyzing-results.html"><a href="analyzing-results.html#notional-costs"><i class="fa fa-check"></i><b>12.4.7</b> Notional Costs</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="13" data-path="walk-forward-analysis.html"><a href="walk-forward-analysis.html"><i class="fa fa-check"></i><b>13</b> Walk Forward Analysis</a></li>
<li class="chapter" data-level="14" data-path="monte-carlo-analysis.html"><a href="monte-carlo-analysis.html"><i class="fa fa-check"></i><b>14</b> Monte Carlo Analysis</a></li>
<li class="chapter" data-level="15" data-path="warnings-and-errors.html"><a href="warnings-and-errors.html"><i class="fa fa-check"></i><b>15</b> Warnings and Errors</a><ul>
<li class="chapter" data-level="15.1" data-path="warnings-and-errors.html"><a href="warnings-and-errors.html#must-use-auto.assigntrue-for-multiple-symbols-requests"><i class="fa fa-check"></i><b>15.1</b> Must use auto.assign=TRUE for multiple Symbols requests</a></li>
<li class="chapter" data-level="15.2" data-path="warnings-and-errors.html"><a href="warnings-and-errors.html#missing-in-call-to-function-add.distribution"><i class="fa fa-check"></i><b>15.2</b> Missing in call to function add.distribution</a></li>
</ul></li>
<li class="chapter" data-level="16" data-path="about-backtesting-strategies-with-r.html"><a href="about-backtesting-strategies-with-r.html"><i class="fa fa-check"></i><b>16</b> About “Backtesting Strategies with R”</a><ul>
<li class="chapter" data-level="16.1" data-path="about-backtesting-strategies-with-r.html"><a href="about-backtesting-strategies-with-r.html#tim-trice"><i class="fa fa-check"></i><b>16.1</b> Tim Trice</a></li>
<li class="chapter" data-level="16.2" data-path="about-backtesting-strategies-with-r.html"><a href="about-backtesting-strategies-with-r.html#to-you-the-reader"><i class="fa fa-check"></i><b>16.2</b> To You, the Reader</a></li>
</ul></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Backtesting Strategies with R</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="parameter-optimization" class="section level1">
<h1><span class="header-section-number">Chapter 7</span> Parameter Optimization</h1>
<p>One of the important aspects of backtesting is being able to test out various parameters. After all, what if you’re Luxor strategy doesn’t do well with 10/30 SMA indicators but does spectacular with 17/28 SMA indicators?</p>
<p><code>quantstrat</code> helps us do this by adding distributions to our parameters. We can create a range of SMA values for our <code>nFast</code> and <code>nSlow</code> variables. We then examine the results to find the combination that gives us the best results.</p>
<p>We’ll assign the range for each of our SMA’s to two new variables: <code>.fastSMA</code> and <code>.slowSMA</code>. Both are just simple integer vectors. You can make them as narrow or wide as you want. However, this is an intensive process. The wider your parameters, the longer it will run. I’ll address this later.</p>
<p>We also introduce <code>.nsamples</code>. <code>.nsamples</code> will be our value for the input parameter <code>nsamples</code> in <code>apply.paramset()</code>. This tells <code>quantstrat</code> that of the x-number of combinations of <code>.fastSMA</code> and <code>.slowSMA</code> to test to only take a random sample of those combinations. By default <code>apply.paramset(nsamples = 0)</code> which means all combinations will be tested. This can be fine provided you have the computational resources to do so - it can be a very intensive process (we’ll deal with resources later).</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">.fastSMA <-<span class="st"> </span>(<span class="dv">1</span>:<span class="dv">30</span>)
.slowSMA <-<span class="st"> </span>(<span class="dv">20</span>:<span class="dv">80</span>)
.nsamples <-<span class="st"> </span><span class="dv">5</span></code></pre></div>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">portfolio.st <-<span class="st"> "Port.Luxor.MA.Opt"</span>
account.st <-<span class="st"> "Acct.Luxor.MA.Opt"</span>
strategy.st <-<span class="st"> "Strat.Luxor.MA.Opt"</span>
<span class="kw">rm.strat</span>(portfolio.st)
<span class="kw">rm.strat</span>(account.st)
<span class="kw">initPortf</span>(<span class="dt">name =</span> portfolio.st,
<span class="dt">symbols =</span> symbols,
<span class="dt">initDate =</span> init_date)</code></pre></div>
<pre><code>## [1] "Port.Luxor.MA.Opt"</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">initAcct</span>(<span class="dt">name =</span> account.st,
<span class="dt">portfolios =</span> portfolio.st,
<span class="dt">initDate =</span> init_date,
<span class="dt">initEq =</span> init_equity)</code></pre></div>
<pre><code>## [1] "Acct.Luxor.MA.Opt"</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">initOrders</span>(<span class="dt">portfolio =</span> portfolio.st,
<span class="dt">symbols =</span> symbols,
<span class="dt">initDate =</span> init_date)
<span class="kw">strategy</span>(strategy.st, <span class="dt">store =</span> <span class="ot">TRUE</span>)</code></pre></div>
<p>Next we’ll go through and re-initiate our portfolio and account objects as we did prior.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">rm.strat</span>(portfolio.st)
<span class="kw">rm.strat</span>(account.st)</code></pre></div>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">initPortf</span>(<span class="dt">name =</span> portfolio.st,
<span class="dt">symbols =</span> symbols,
<span class="dt">initDate =</span> init_date)</code></pre></div>
<pre><code>## [1] "Port.Luxor.MA.Opt"</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">initAcct</span>(<span class="dt">name =</span> account.st,
<span class="dt">portfolios =</span> portfolio.st,
<span class="dt">initDate =</span> init_date)</code></pre></div>
<pre><code>## [1] "Acct.Luxor.MA.Opt"</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">initOrders</span>(<span class="dt">portfolio =</span> portfolio.st,
<span class="dt">initDate =</span> init_date)</code></pre></div>
<div id="add-distribution" class="section level2">
<h2><span class="header-section-number">7.1</span> Add Distribution</h2>
<p>We already saved our indicators, signals and rules - <code>strategy.st</code> - and loaded the strategy; we do not need to rewrite that code.</p>
<p>We use <code>add.distribution</code> to distribute our range of values across the two indicators. Again, our first parameter the name of our strategy <code>strategy.st</code>.</p>
<ul>
<li><p><code>paramset.label</code>: name of the function to which the parameter range will be applied; in this case <code>TTR:SMA()</code>.</p></li>
<li><p><code>component.type</code>: indicator</p></li>
<li><p><code>component.label</code>: label of the indicator when we added it (<code>nFast</code> and <code>nSlow</code>)</p></li>
<li><p><code>variable</code>: the parameter of <code>SMA()</code> to which our integer vectors (<code>.fastSMA</code> and <code>.slowSMA</code>) will be applied; <code>n</code>.</p></li>
<li><p><code>label</code>: unique identifier for the distribution.</p></li>
</ul>
<p>This ties our distribution parameters to our indicators. When we run the strategy, each possible value for <code>.fastSMA</code> will be applied to <code>nFAST</code> and <code>.slowSMA</code> to <code>nSLOW</code>.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">add.distribution</span>(strategy.st,
<span class="dt">paramset.label =</span> <span class="st">"SMA"</span>,
<span class="dt">component.type =</span> <span class="st">"indicator"</span>,
<span class="dt">component.label =</span> <span class="st">"nFast"</span>,
<span class="dt">variable =</span> <span class="kw">list</span>(<span class="dt">n =</span> .fastSMA),
<span class="dt">label =</span> <span class="st">"nFAST"</span>)</code></pre></div>
<pre><code>## [1] "Strat.Luxor.MA.Opt"</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">add.distribution</span>(strategy.st,
<span class="dt">paramset.label =</span> <span class="st">"SMA"</span>,
<span class="dt">component.type =</span> <span class="st">"indicator"</span>,
<span class="dt">component.label =</span> <span class="st">"nSlow"</span>,
<span class="dt">variable =</span> <span class="kw">list</span>(<span class="dt">n =</span> .slowSMA),
<span class="dt">label =</span> <span class="st">"nSLOW"</span>)</code></pre></div>
<pre><code>## [1] "Strat.Luxor.MA.Opt"</code></pre>
</div>
<div id="add-distribution-constraint" class="section level2">
<h2><span class="header-section-number">7.2</span> Add Distribution Constraint</h2>
<p>What we do not want is to abandon our initial rules which were to buy only when SMA(10) was greater than or equal to SMA(30), otherwise short. In other words, go long when our faster SMA is greater than or equal to our slower SMA and go short when the faster SMA was less than the slower SMA.</p>
<p>We keep this in check by using <code>add.distribution.constraint</code>. We pass in the <code>paramset.label</code> as we did in <code>add.distribution</code>. We assign <code>nFAST</code> to <code>distribution.label.1</code> and <code>nSLOW</code> to <code>distribution.label.2</code>.</p>
<p>Our operator will be one of <code>c("<", ">", "<=", ">=", "=")</code>. Here, we’re issuing a constraint to always keep <code>nFAST</code> less than <code>nSLOW</code>.</p>
<p>We’ll name this constraint <code>SMA.Con</code> by applying it to the <code>label</code> parameter.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">add.distribution.constraint</span>(strategy.st,
<span class="dt">paramset.label =</span> <span class="st">"SMA"</span>,
<span class="dt">distribution.label.1 =</span> <span class="st">"nFAST"</span>,
<span class="dt">distribution.label.2 =</span> <span class="st">"nSLOW"</span>,
<span class="dt">operator =</span> <span class="st">"<"</span>,
<span class="dt">label =</span> <span class="st">"SMA.Constraint"</span>)</code></pre></div>
<pre><code>## [1] "Strat.Luxor.MA.Opt"</code></pre>
</div>
<div id="running-parallel" class="section level2">
<h2><span class="header-section-number">7.3</span> Running Parallel</h2>
<p><code>quantstrat</code> includes the <code>foreach</code> library for purposes such as this. <code>foreach</code> allows us to execute our strategy in parallel on multicore processors which can save some time.</p>
<p>On my current setup it is using one virtual core which doesn’t help much for large tasks such as this. However, if you are running on a system with more than one core processor you can use the follinwg if/else statement courtesy of <a href="http://www.r-programming.org/papers">Guy Yollin</a>. It requires the <code>parallel</code> library and <code>doParallel</code> for Windows users and <code>doMC</code> for non-Windows users.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(parallel)
if( <span class="kw">Sys.info</span>()[<span class="st">'sysname'</span>] ==<span class="st"> "Windows"</span>) {
<span class="kw">library</span>(doParallel)
<span class="kw">registerDoParallel</span>(<span class="dt">cores=</span><span class="kw">detectCores</span>())
} else {
<span class="kw">library</span>(doMC)
<span class="kw">registerDoMC</span>(<span class="dt">cores=</span><span class="kw">detectCores</span>())
}</code></pre></div>
<pre><code>## Loading required package: iterators</code></pre>
</div>
<div id="apply-paramset" class="section level2">
<h2><span class="header-section-number">7.4</span> Apply Paramset</h2>
<p>When we ran our original strategy we used <code>applyStrategy()</code>. When running distributions we use <code>apply.paramset()</code>.</p>
<p>For our current strategy we only need to pass in our portfolio and account objects.</p>
<p>I’ve also used an if/else statement to avoid running this strategy repeatedly when making updates to the book which, again, is time-consuming. The results are saved to a RData file we’ll analyze later.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">cwd <-<span class="st"> </span><span class="kw">getwd</span>()
<span class="kw">setwd</span>(<span class="st">"./_data/"</span>)
results_file <-<span class="st"> </span><span class="kw">paste</span>(<span class="st">"results"</span>, strategy.st, <span class="st">"RData"</span>, <span class="dt">sep =</span> <span class="st">"."</span>)
if( <span class="kw">file.exists</span>(results_file) ) {
<span class="kw">load</span>(results_file)
} else {
results <-<span class="st"> </span><span class="kw">apply.paramset</span>(strategy.st,
<span class="dt">paramset.label =</span> <span class="st">"SMA"</span>,
<span class="dt">portfolio.st =</span> portfolio.st,
<span class="dt">account.st =</span> account.st,
<span class="dt">nsamples =</span> .nsamples)
if(<span class="kw">checkBlotterUpdate</span>(portfolio.st, account.st, <span class="dt">verbose =</span> <span class="ot">TRUE</span>)) {
<span class="kw">save</span>(<span class="dt">list =</span> <span class="st">"results"</span>, <span class="dt">file =</span> results_file)
<span class="kw">save.strategy</span>(strategy.st)
}
}
<span class="kw">setwd</span>(cwd)</code></pre></div>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="data-quality.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="stop-loss.html" class="navigation navigation-next " aria-label="Next page""><i class="fa fa-angle-right"></i></a>
<script src="assets/gitbook-2.6.7/js/app.js"></script>
<script src="assets/gitbook-2.6.7/js/lunr.js"></script>
<script src="assets/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="assets/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="assets/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="assets/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="assets/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script>
require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"facebook": true,
"twitter": true,
"google": false,
"weibo": false,
"instapper": false,
"vk": false,
"all": ["facebook", "google", "twitter", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/timtrice/stock-book/edit/master/parameter-optimization.Rmd",
"text": "Edit This Page"
},
"download": null,
"toc": {
"collapse": "subsection"
},
"toolbar": {
"position": "static"
}
});
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
if (location.protocol !== "file:" && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>