-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBacktest.Rmd
366 lines (299 loc) · 8.59 KB
/
Backtest.Rmd
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
---
output: word_document
params:
dir:
value:x
file:
value: x
---
```{r setup, include=FALSE}
tryCatch({
#Specify the output directory of the images
knitr::opts_chunk$set(echo = TRUE,warning=FALSE,fig.path = paste0(params$dir,"/images/"))
#Import libraries
source(paste0(".","/functions/importLibrary.R"))
#Remove the images folder which was generated before
unlink(paste0(params$dir,"/images"), recursive = TRUE)
unlink(paste0(params$dir,"/strategy-statistics.json"),recursive = TRUE)
#Read price series from json file and make them ready to use
source(paste0(".","/functions/getPriceSeries.R"))
#Initialize intermediate file to keep the names of images
imageNameMapping = data.frame(From = "", To = "")
},error = function(e){
print("Setup Failed.")
})
```
<style>
.col1 {
columns: 1 1200; /* number of columns and width in pixels*/
-webkit-columns: 1 1200; /* chrome, safari */
-moz-columns: 1 1200; /* firefox */
}
.col1 table{
width: 900px;
font-size: 150%;
font-family: "Open Sans Condensed";
}
.col3 {
columns: 3 1200px;
-webkit-columns: 3 1200px;
-moz-columns: 3 1200px;
}
.col3 img{
width: 300px;
height: auto;
}
.col3 table{
width: 900px;
font-family: "Open Sans Condensed";
}
.col2 {
columns: 3 1200px;
-webkit-columns: 3 1200px;
-moz-columns: 3 1200px;
margin-left: 7%;
}
.col2 img{
width: 400px;
height: auto;
}
.col2 table{
width: 900px;
}
.dygraph-legend {
background: transparent !important;
left: 600px !important;
top: 160px !important;
}
a:link {
color:black;
}
sup {
font-family: "Open Sans Condensed";
}
<!-- table, th, td { -->
<!-- border: 1px solid black; -->
<!-- border-collapse: collapse; -->
<!-- } -->
</style>
<div class="col1">
```{r one-column-1, results='asis', echo=FALSE, out.extra=''}
cat("<table class='container'>")
cat("<tr align='center'>")
cat("<th width='100%'>")
cat("Cumulative Returns")
cat("</th></tr>")
cat("</table>")
```
</div>
<div class="col1">
```{r CumulativeReturn, echo=FALSE,warning=FALSE,fig.width=11.5, fig.height=2.5,message=FALSE,results='asis', out.extra=''}
#Cumulative return chart
tryCatch({
source(paste0(".","/functions/cumulativeReturn.R"))
imageNameMapping = rbind(imageNameMapping,data.frame(From = "CumulativeReturn-1.png",
To = "cumulative-return.png"))
},error = function(e){
print("Cumulative Return Failed.")
})
```
</div>
<div class="col1">
```{r one-column-2, results='asis', echo=FALSE, out.extra=''}
cat("<table class='container'>")
cat("<tr align='center'>")
cat("<th width='100%'>")
cat("Daily Returns")
cat("</th></tr>")
cat("</table>")
```
</div>
<div class="col1">
```{r DailyReturns, echo=FALSE,warning=FALSE,fig.width=11.5, fig.height=2.5,message=FALSE,results='asis', out.extra=''}
#Daily return chart
tryCatch({
source(paste0(".","/functions/dailyReturn.R"))
imageNameMapping = rbind(imageNameMapping,data.frame(From = "DailyReturns-1.png", To = "daily-returns.png"))
},error = function(e){
print("Daily Return Failed.")
})
```
</div>
<div class="col1">
```{r one-column-3, results='asis', echo=FALSE, out.extra=''}
cat("<table class='container'>")
cat("<tr align='center'>")
cat("<th width='100%'>")
cat("Top 5 Drawdown Period")
cat("</th></tr>")
cat("</table>")
```
</div>
<div class="col1">
```{r Drawdowns, echo=FALSE,warning=FALSE,fig.width=11.5, fig.height=2.5,message=FALSE,results='asis', out.extra=''}
#Drawdown chart
tryCatch({
source(paste0(".","/functions/drawdown.R"))
imageNameMapping = rbind(imageNameMapping,data.frame(From = "Drawdowns-1.png", To = "drawdowns.png"))
},error = function(e){
print("Drawdowns Failed.")
})
```
</div>
<br></br>
<div class="col3">
```{r three-column-1, results='asis', echo=FALSE, out.extra=''}
cat("<table class='container'>")
cat("<tr align='center'>")
cat("<th width='35%'>")
cat("Monthly Returns(%)")
cat("</th><th width='30%'>")
cat("Annual Returns(%)")
cat("</th><th width='35%'>")
cat("Distribution of Monthly Returns")
cat("</th></tr>")
cat("</table>")
```
</div>
<div class="col3">
```{r Returns, echo=FALSE,warning=FALSE,message=FALSE}
#Monthly return chart
tryCatch({
source(paste0(".","/functions/monthlyReturn.R"))
imageNameMapping = rbind(imageNameMapping,data.frame(From = "Returns-1.png", To = "monthly-returns.png"))
},error = function(e){
print("Monthly Return Failed.")
})
#Yearly return chart
tryCatch({
source(paste0(".","/functions/yearlyReturn.R"))
imageNameMapping = rbind(imageNameMapping,data.frame(From = "Returns-2.png", To = "annual-returns.png"))
},error = function(e){
print("Yearly Return Failed.")
})
#Monthly return distribution chart
tryCatch({
source(paste0(".","/functions/monthlyReturnDistribution.R"))
imageNameMapping = rbind(imageNameMapping,data.frame(From = "Returns-3.png",
To = "distribution-of-monthly-returns.png"))
},error = function(e){
print("Monthly Return Distribution Failed.")
})
```
</div>
<div class="col3">
```{r Event, echo=FALSE,warning=FALSE,message=FALSE,results='asis', out.extra=''}
#Event Charts
tryCatch({
source(paste0(".","/functions/event.R"))
},error = function(e){
print("Event Charts Failed.")
})
```
</div>
<div class="col1">
```{r RollingPortfolioBetaToEquity, echo=FALSE,warning=FALSE,fig.width=11.5, fig.height=2.5,message=FALSE,results='asis', out.extra=''}
#Rolling Beta chart
tryCatch({
source(paste0(".","/functions/rollingBeta.R"))
},error = function(e){
print("RollingPortfolioBeta Failed.")
})
```
</div>
<div class="col1">
```{r RollingSharpeRatio(6-month), echo=FALSE,warning=FALSE,fig.width=11.5, fig.height=2.5,message=FALSE,results='asis', out.extra=''}
#Rolling Sharpe Ratio chart
tryCatch({
source(paste0(".","/functions/rollingSP.R"))
},error = function(e){
print("RollingSharpeRatio Failed.")
})
```
</div>
<div class="col1">
```{r NetHoldings, echo=FALSE,warning=FALSE,fig.width=11.5, fig.height=2.5,message=FALSE,results='asis', out.extra=''}
#Net Holdings chart
tryCatch({
source(paste0(".","/functions/netHoldings.R"))
imageNameMapping = rbind(imageNameMapping,data.frame(From = "NetHoldings-1.png", To = "net-holdings.png"))
},error = function(e){
print("NetHoldings Failed.")
})
```
</div>
<div class="col1">
```{r Leverage, echo=FALSE,warning=FALSE,fig.width=11.5, fig.height=2.5,message=FALSE,results='asis', out.extra=''}
#Leverage chart
tryCatch({
source(paste0(".","/functions/leverage.R"))
imageNameMapping = rbind(imageNameMapping,data.frame(From = "Leverage-1.png", To = "leverage.png"))
},error = function(e){
print("Leverage Failed.")
})
```
</div>
<div class="col3">
```{r AssetAllocation, echo=FALSE,warning=FALSE,message=FALSE,results='asis', out.extra=''}
#Asset Allocation charts
tryCatch({
source(paste0(".","/functions/assetAllocation.R"))
},error = function(e){
print("AssetAllocation Failed.")
})
```
</div>
<div class="col1">
```{r one-column-4, results='asis', echo=FALSE, out.extra=''}
cat("<table class='container'>")
cat("<tr align='center'>")
cat("<th width='100%'>")
cat("Return Prediction")
cat("</th></tr>")
cat("</table>")
```
</div>
<div class="col1">
```{r ReturnPrediction, echo=FALSE,warning=FALSE,fig.width=11.5, fig.height=2.5,message=FALSE,results='asis', out.extra=''}
tryCatch({
source(paste0(".","/functions/returnPrediction.R"))
imageNameMapping = rbind(imageNameMapping,data.frame(From = "ReturnPrediction-1.png",
To = "return-prediction.png"))
},error = function(e){
print("ReturnPrediction Failed.")
})
```
</div>
```{r image_output, echo=FALSE,warning=FALSE,message=FALSE}
#Write the intermediate file
tryCatch({
imageNameMapping = imageNameMapping[-1,]
write.csv(imageNameMapping,paste0(params$dir,"/images/imageNameMapping.csv"))
},error = function(e){
print("Image Output Failed.")
})
```
```{r json_table, echo=FALSE,warning=FALSE,message=FALSE}
#Calculate strategy statistics and wirte json file
tryCatch({
source(paste0(".","/functions/outputJson.R"))
}, error = function(e){
print("Json Output Failed.")
}
)
```
<br></br>
<div class="col1">
```{r footnote, echo=FALSE,warning=FALSE,fig.width=11.5, fig.height=2.5,message=FALSE,results='asis', out.extra=''}
#Footnote in the html
cat("<hr></hr>")
index_array = index_array[! index_array %in% length(titles)]
for(i in 1:length(index_array))
{
cat("<sup>")
cat(i)
cat(paste0(". [From ",startDate[index_array[i]]," To ",endDate[index_array[i]]
,"] ",footnotes[index_array[i]],"</sup><br>"))
}
```
</div>