forked from englianhu/binary.com-interview-question
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-Q1.Rmd
3065 lines (2568 loc) · 170 KB
/
binary-Q1.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
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "<img src='www/binary-logo-resize.jpg' width='240'>"
subtitle: "[<span style='color:blue'>binary.com</span>](https://github.com/englianhu/binary.com-interview-question) Interview Question I"
author: "[<span style='color:blue'>®γσ, Lian Hu</span>](https://englianhu.github.io/) <img src='www/ENG.jpg' width='24'> <img src='www/RYO.jpg' width='24'>®"
date: "`r lubridate::now()`"
output:
html_document:
number_sections: yes
toc: yes
toc_depth: 4
toc_float:
collapsed: no
smooth_scroll: no
tufte::tufte_html:
toc: yes
toc_depth: 4
self_contained: no
tufte::tufte_book:
citation_package: natbib
latex_engine: xelatex
tufte::tufte_handout:
citation_package: natbib
latex_engine: xelatex
link-citations: yes
---
```{r setup, warning = FALSE, include = FALSE}
suppressPackageStartupMessages(library('BBmisc'))
pkgs <- c('knitr', 'kableExtra', 'tint', 'devtools', 'lubridate', 'plyr', 'stringr', 'magrittr', 'dplyr', 'tidyr', 'tidyverse', 'tidyquant', 'turner', 'readr', 'quantmod', 'htmltools', 'highcharter', 'googleVis', 'formattable', 'ggfortify', 'DT', 'forecast', 'MCMCpack', 'PerformanceAnalytics', 'broom', 'microbenchmark', 'doParallel', 'Boruta', 'fBasics', 'fPortfolio', 'rugarch', 'parma', 'rmgarch')#, 'Mcomp', 'bsts')
suppressAll(lib(pkgs))
#'@ install.packages(pkgs, lib = 'C:/Program Files/R/R-3.4.0/library')
suppressAll(l_ply(c('last.R', 'Mn.R', 'has.Mn.R', 'simAutoArima.R', 'simStakesAutoArima.R', 'simETS.R', 'simStakesETS.R', 'plotChart2.R', 'armaSearch.R', 'simGarch.R', 'simStakesGarch.R'), function(pkg) source(paste0('./function/', pkg))))
## Directly install the developing packages.
#'@ require(devtools)
#'@ install_bitbucket("rugarch","alexiosg")
#'@ install_bitbucket("rmgarch","alexiosg")
#'@ install_bitbucket("racd","alexiosg")
#'@ install_bitbucket("twinkle","alexiosg")
#'@ install_bitbucket("spd","alexiosg")
#'@ install_bitbucket("parma","alexiosg")
## Set option to below if you want to plot an independent webpage with graph
#'@ op <- options(gvis.plot.tag=NULL)
op <- options(gvis.plot.tag = 'chart')
options(gvis.plot.tag = 'chart', warn = -1)
#'@ options(rpubs.upload.method = 'internal')
## R: llply fully reproducible results in parallel
## https://stackoverflow.com/questions/34946177/r-llply-fully-reproducible-results-in-parallel
#'@ cl <- makeCluster(detectCores())
#'@ registerDoParallel(cl)
# Create a cluster object to be used for rugarcgh and rmgarch models.
#'@ cluster = makePSOCKcluster(16)
# invalidate cache when the package version changes
#'@ knitr::opts_chunk$set(tidy = FALSE, cache.extra = packageVersion('tint'))
rm(pkgs)
#'@ options(htmltools.dir.version = FALSE)
```
# Introduction
Below are the questionaire. Here I created this file to apply `MCMCpack` and `forecast` to compelete the questions prior to completed the `Ridge`, `ElasticNet` and `LASSO` regression (quite alot of models for comparison)^[We can use `cv.glmnet()` in `glmnet` package or `caret` package for cross validation models. You can refer to [<span style='color:blue'>Algorithmic Trading</span>](https://robotwealth.com/caterory/algorithmic-trading/page/4/) and [<span style='color:blue'>Successful Algorithmic Trading</span>](https://raw.githubusercontent.com/englianhu/binary.com-interview-question/fcad2844d7f10c486f3601af9932f49973548e4b/reference/Successful%20Algorithmic%20Trading.pdf) which applied cross-validation in focasting in financial market. You can buy the ebook with full Python coding of [<span style='color:blue'>Successful Algorithmic Trading</span>](https://www.quantstart.com/successful-algorithmic-trading-ebook) as well.].
<iframe width="560" height="315" src="https://www.youtube.com/embed/Aw77aMLj9uM" frameborder="0" allowfullscreen></iframe>
<center><iframe src="https://raw.githubusercontent.com/englianhu/binary.com-interview-question/ff20ee95aa60ef5cca3cf797066089103eb62acf/reference/quant-analyst-skills-test.pdf" width="600" height="900"></iframe></center>
# Content
## Question 1
### Read Data
I use 3 years data for the question as experiment, 1st year data is burn-in data for statistical modelling and prediction purpose while following 2 years data for forecasting and staking. There have 252 trading days within a year.
```{r read-data, echo = FALSE, warning = FALSE, eval = FALSE}
## ================== eval = FALSE =============================
## Do not execute...
##
## Remove all objects include hidden objects.
rm(list = ls(all.names = TRUE))
## get currency dataset online.
getFX('USD/JPY', from = '2014-01-01', to = '2017-01-20') #oanda only provides 180 days data. getSymbols()
#'@ USDJPY <- readRDS('./data/USDJPY.rds')
USDJPY <- xts(USDJPY[, -1], order.by = USDJPY$Date)
## dateID
dateID <- index(mbase)
dateID0 <- ymd('2015-01-01')
dateID <- dateID[dateID > dateID0]
obs.data <- USDJPY[index(USDJPY) > dateID0]
## Now we try to use the daily mean value which is (Hi + Lo) / 2.
pred.data <- ldply(dateID, function(dt) {
smp = USDJPY
dtr = last(index(smp[index(smp) < dt]))
smp = smp[paste0(dtr %m-% years(1), '/', dtr)]
frd = as.numeric(difftime(dt, dtr), units = 'days')
fit = ets(smp) #https://www.otexts.org/fpp/7/7
data.frame(Date = dt, forecast(fit, h = frd)) %>% tbl_df
}, .parallel = FALSE) %>% tbl_df
cmp.data <- xts(pred.data[, -1], order.by = pred.data$Date)
cmp.data <- cbind(cmp.data, obs.data)
rm(obs.data, pred.data)
# Test the models
lm(Point.Forecast~ USD.JPY, data = cmp.data)
MCMCregress(Point.Forecast~ USD.JPY, data = cmp.data)
plot(forecast(fit))
forecast(fit, h = 4)
```
```{r read-data2, warning = FALSE}
## get currency dataset online.
## http://stackoverflow.com/questions/24219694/get-symbols-quantmod-ohlc-currency-data
#'@ getFX('USD/JPY', from = '2014-01-01', to = '2017-01-20')
## getFX() doesn't shows Op, Hi, Lo, Cl price but only price. Therefore no idea to place bets.
#'@ USDJPY <- getSymbols('JPY=X', src = 'yahoo', from = '2014-01-01',
#'@ to = '2017-01-20', auto.assign = FALSE)
#'@ names(USDJPY) <- str_replace_all(names(USDJPY), 'JPY=X', 'USDJPY')
#'@ USDJPY <- xts(USDJPY[, -1], order.by = USDJPY$Date)
#'@ saveRDS(USDJPY, './data/USDJPY.rds')
USDJPY <- read_rds(path = './data/USDJPY.rds')
mbase <- USDJPY
## dateID
dateID <- index(mbase)
dateID0 <- ymd('2015-01-01')
dateID <- dateID[dateID > dateID0]
```
```{r data-summary, warning = FALSE}
dim(mbase)
summary(mbase) %>%
kable %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive'))
```
### Statistical Modelling
#### ARIMA vs ETS
<span style='color:red'>**Remarks :** *Here I try to predict the sell/buy price and also settled price. However just noticed the question asking about prediction of the variance^[The profit is made based on the range of variance Hi-Lo price but not the accuracy of the highest, lowest or closing price.] based on mean price. I can also use the focasted highest and forecasted lowest price for variance prediction as well. However I will conduct another study and answer for the variance with Garch models.*</span>
Below are some articles with regards exponential smoothing.
- [<span style='color:blue'>Recent Advances in Robust Statistics: Theory and Applications</span>](https://books.google.com.my/books?id=ntR5DQAAQBAJ&pg=PA174&lpg=PA174&dq=exponential+smoothing+mcmc&source=bl&ots=QANf4o9oFh&sig=JWov-64qeFTcOScG2pYj9OVCl2k&hl=ja&sa=X&redir_esc=y#v=onepage&q=exponential%20smoothing%20mcmc&f=false)
- [<span style='color:blue'>Error, trend, seasonality - ets and its forecast model friends</span>](http://ellisp.github.io/blog/2016/11/27/ets-friends)
- [<span style='color:blue'>A study of outliers in the exponential smoothing approach to forecasting</span>](https://www.statindex.org/articles/258660)
- [<span style='color:blue'>8.10 ARIMA vs ETS</span>](https://www.otexts.org/fpp/8/10)
- [<span style='color:blue'>Introduction to ARIMA : nonseasonal models</span>](http://people.duke.edu/~rnau/411arim.htm#les)
It is a common myth that ARIMA models are more general than exponential smoothing. While linear exponential smoothing models are all special cases of ARIMA models, the non-linear exponential smoothing models have no equivalent ARIMA counterparts. There are also many ARIMA models that have no exponential smoothing counterparts. In particular, every ETS model^[[<span style='color:blue'>**forecast::ets()**</span>](https://www.rdocumentation.org/packages/forecast/versions/7.3/topics/ets) : Usually a three-character string identifying method using the framework terminology of Hyndman et al. (2002) and Hyndman et al. (2008). The first letter denotes the error type ("A", "M" or "Z"); the second letter denotes the trend type ("N","A","M" or "Z"); and the third letter denotes the season type ("N","A","M" or "Z"). In all cases, "N"=none, "A"=additive, "M"=multiplicative and "Z"=automatically selected. So, for example, "ANN" is simple exponential smoothing with additive errors, "MAM" is multiplicative Holt-Winters' method with multiplicative errors, and so on.
It is also possible for the model to be of class "ets", and equal to the output from a previous call to ets. In this case, the same model is fitted to y without re-estimating any smoothing parameters. See also the use.initial.values argument.] is non-stationary, while ARIMA models can be stationary.
The ETS models with seasonality or non-damped trend or both have two unit roots (i.e., they need two levels of differencing to make them stationary). All other ETS models have one unit root (they need one level of differencing to make them stationary).
The following table gives some equivalence relationships for the two classes of models.
| ETS model | ARIMA model | Parameters |
|:-------------------:|:-------------------------------------:|:-----------------------------:|
| $ETS(A, N, N)$ | $ARIMA(0, 1, 1)$ | $θ_{1} = α − 1$ |
| $ETS(A, A, N)$ | $ARIMA(0, 2, 2)$ | $θ_{1} = α + β − 2$ |
| | | $θ_{2} = 1 − α$ |
| $ETS(A, A_{d}, N)$ | $ARIMA(1, 1, 2)$ | $ϕ_{1} = ϕ$ |
| | | $θ_{1} = α + ϕβ − 1 − ϕ$ |
| | | $θ_{2} = (1 − α)ϕ$ |
| $ETS(A, N, A)$ | $ARIMA(0, 0, m)(0, 1, 0)_{m}$ | |
| $ETS(A, A, A)$ | $ARIMA(0, 1, m+1)(0, 1, 0)_{m}$ | |
| $ETS(A, A_{d}, A)$ | $ARIMA(1, 0, m+1)(0, 1, 0)_{m}$ | |
For the seasonal models, there are a large number of restrictions on the ARIMA parameters.
Kindly refer to [<span style='color:blue'>*8.10 ARIMA vs ETS*</span>](https://www.otexts.org/fpp/8/10) for further details.
```{r build-AutoArima, warning = FALSE}
## Modelling Auto Arima focasting data.
#'@ fitAutoArima.op <- suppressAll(simAutoArima(USDJPY, .prCat = 'Op')) #will take a minute
#'@ saveRDS(fitAutoArima.op, './data/fitAutoArima.op.rds')
#'@ fitAutoArima.hi <- suppressAll(simAutoArima(USDJPY, .prCat = 'Hi')) #will take a minute
#'@ saveRDS(fitAutoArima.hi, './data/fitAutoArima.hi.rds')
#'@ fitAutoArima.mn <- suppressAll(simAutoArima(USDJPY, .prCat = 'Mn')) #will take a minute
#'@ saveRDS(fitAutoArima.mn, './data/fitAutoArima.mn.rds')
#'@ fitAutoArima.lo <- suppressAll(simAutoArima(USDJPY, .prCat = 'Lo')) #will take a minute
#'@ saveRDS(fitAutoArima.lo, './data/fitAutoArima.lo.rds')
#'@ fitAutoArima.cl <- suppressAll(simAutoArima(USDJPY, .prCat = 'Cl')) #will take a minute
#'@ saveRDS(fitAutoArima.cl, './data/fitAutoArima.cl.rds')
fitAutoArima.op <- readRDS('./data/fitAutoArima.op.rds')
fitAutoArima.hi <- readRDS('./data/fitAutoArima.hi.rds')
fitAutoArima.mn <- readRDS('./data/fitAutoArima.mn.rds')
fitAutoArima.lo <- readRDS('./data/fitAutoArima.lo.rds')
fitAutoArima.cl <- readRDS('./data/fitAutoArima.cl.rds')
```
```{r build-ETS, warning = FALSE}
## Modelling ETS focasting data.
#'@ fitETS.op <- suppressAll(simETS(USDJPY, .prCat = 'Op')) #will take a minute
#'@ saveRDS(fitETS.op, './data/fitETS.op.rds')
#'@ fitETS.hi <- suppressAll(simETS(USDJPY, .prCat = 'Hi')) #will take a minute
#'@ saveRDS(fitETS.hi, './data/fitETS.hi.rds')
#'@ fitETS.mn <- suppressAll(simETS(USDJPY, .prCat = 'Mn')) #will take a minute
#'@ saveRDS(fitETS.mn, './data/fitETS.mn.rds')
#'@ fitETS.lo <- suppressAll(simETS(USDJPY, .prCat = 'Lo')) #will take a minute
#'@ saveRDS(fitETS.lo, './data/fitETS.lo.rds')
#'@ fitETS.cl <- suppressAll(simETS(USDJPY, .prCat = 'Cl')) #will take a minute
#'@ saveRDS(fitETS.cl, './data/fitETS.cl.rds')
fitETS.op <- readRDS('./data/fitETS.op.rds')
fitETS.hi <- readRDS('./data/fitETS.hi.rds')
fitETS.mn <- readRDS('./data/fitETS.mn.rds')
fitETS.lo <- readRDS('./data/fitETS.lo.rds')
fitETS.cl <- readRDS('./data/fitETS.cl.rds')
```
**Application of MCMC**
Need to refer to MCMC since I am using exponential smoothing models...
- [<span style='color:blue'>Markov Chain Monte Carlo Method</span>](https://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_introbayes_sect007.htm)
- [<span style='color:blue'>Burn-In is Unnecessary</span>](http://users.stat.umn.edu/~geyer/mcmc/burn.html)
- [<span style='color:blue'>**Nice R Code** *Punning code better since 2013*</span>](https://nicercode.github.io/guides/mcmc/)
```{r lm-ETS, warning = FALSE}
## Here I test the accuracy of forecasting of ets ZZZ model 1.
## Test the models
## opened price fit data
summary(lm(Point.Forecast~ USDJPY.Close, data = fitETS.op))
summary(MCMCregress(Point.Forecast~ USDJPY.Close, data = fitETS.op))
## highest price fit data
summary(lm(Point.Forecast~ USDJPY.Close, data = fitETS.hi))
summary(MCMCregress(Point.Forecast~ USDJPY.Close, data = fitETS.hi))
## mean price fit data (mean price of daily highest and lowest price)
summary(lm(Point.Forecast~ USDJPY.Close, data = fitETS.mn))
summary(MCMCregress(Point.Forecast~ USDJPY.Close, data = fitETS.mn))
## lowest price fit data
summary(lm(Point.Forecast~ USDJPY.Close, data = fitETS.lo))
summary(MCMCregress(Point.Forecast~ USDJPY.Close, data = fitETS.lo))
## closed price fit data
summary(lm(Point.Forecast~ USDJPY.Close, data = fitETS.cl))
summary(MCMCregress(Point.Forecast~ USDJPY.Close, data = fitETS.cl))
```
**Mean Squared Error**
```{r combine-dataAutoArima, warning = FALSE}
fcdataAA <- do.call(cbind, list(USDJPY.FPOP.Open = fitAutoArima.op$Point.Forecast,
USDJPY.FPHI.High = fitAutoArima.hi$Point.Forecast,
USDJPY.FPMN.Mean = fitAutoArima.mn$Point.Forecast,
USDJPY.FPLO.Low = fitAutoArima.lo$Point.Forecast,
USDJPY.FPCL.Close = fitAutoArima.cl$Point.Forecast,
USDJPY.Open = fitAutoArima.op$USDJPY.Open,
USDJPY.High = fitAutoArima.op$USDJPY.High,
USDJPY.Low = fitAutoArima.op$USDJPY.Low,
USDJPY.Close = fitAutoArima.op$USDJPY.Close))
fcdataAA %<>% na.omit
names(fcdataAA) <- c('USDJPY.FPOP.Open', 'USDJPY.FPHI.High', 'USDJPY.FPMN.Mean',
'USDJPY.FPLO.Low', 'USDJPY.FPCL.Close', 'USDJPY.Open',
'USDJPY.High', 'USDJPY.Low', 'USDJPY.Close')
## Mean Squared Error : comparison of accuracy
paste('Open = ', mean((fcdataAA$USDJPY.FPOP.Open - fcdataAA$USDJPY.Open)^2))
paste('High = ', mean((fcdataAA$USDJPY.FPHI.High - fcdataAA$USDJPY.High)^2))
paste('Mean = ', mean((fcdataAA$USDJPY.FPMN.Mean - (fcdataAA$USDJPY.High + fcdataAA$USDJPY.Low)/2)^2))
paste('Low = ', mean((fcdataAA$USDJPY.FPLO.Low - fcdataAA$USDJPY.Low)^2))
paste('Close = ', mean((fcdataAA$USDJPY.FPCL.Close - fcdataAA$USDJPY.Close)^2))
```
```{r combine-dataETS, warning = FALSE}
fcdata <- do.call(cbind, list(USDJPY.FPOP.Open = fitETS.op$Point.Forecast,
USDJPY.FPHI.High = fitETS.hi$Point.Forecast,
USDJPY.FPMN.Mean = fitETS.mn$Point.Forecast,
USDJPY.FPLO.Low = fitETS.lo$Point.Forecast,
USDJPY.FPCL.Close = fitETS.cl$Point.Forecast,
USDJPY.Open = fitETS.op$USDJPY.Open,
USDJPY.High = fitETS.op$USDJPY.High,
USDJPY.Low = fitETS.op$USDJPY.Low,
USDJPY.Close = fitETS.op$USDJPY.Close))
fcdata %<>% na.omit
names(fcdata) <- c('USDJPY.FPOP.Open', 'USDJPY.FPHI.High', 'USDJPY.FPMN.Mean',
'USDJPY.FPLO.Low', 'USDJPY.FPCL.Close', 'USDJPY.Open',
'USDJPY.High', 'USDJPY.Low', 'USDJPY.Close')
## Mean Squared Error : comparison of accuracy
paste('Open = ', mean((fcdata$USDJPY.FPOP.Open - fcdata$USDJPY.Open)^2))
paste('High = ', mean((fcdata$USDJPY.FPHI.High - fcdata$USDJPY.High)^2))
paste('Mean = ', mean((fcdata$USDJPY.FPMN.Mean - (fcdata$USDJPY.High + fcdata$USDJPY.Low)/2)^2))
paste('Low = ', mean((fcdata$USDJPY.FPLO.Low - fcdata$USDJPY.Low)^2))
paste('Close = ', mean((fcdata$USDJPY.FPCL.Close - fcdata$USDJPY.Close)^2))
```
#### Garch vs EWMA
Basically for volatility analyzing, we can using RSY Volatility mesure, kindly refer to *Analyzing Financial Data and Implementing Financial Models using R*^[paper 22] for more information. Well, Garch model is designate for forecast volatility.
Now we look at Garch model, *Figlewski (2004)*^[*Paper 19th*] applied few models and also using different length of data for comparison. Now I use daily Hi-Lo and 365 days data in order to predict the next market price. The author applid Garch on SAP200, 10-years-bond and 20-years-bond and concludes that the Garch model is better than eGarch but implied volatility model better than Garch and eGarch, and the monthly Hi-Lo data is better accurate than daily Hi-Lo for long term investment.
<iframe width="560" height="315" src="https://www.youtube.com/embed/wsYXKh_xmSs" frameborder="0" allowfullscreen></iframe>
$$\begin{equation}
h_{t} = {\omega} + \sum_{i=1}^q{{\alpha}_{i} {\epsilon}_{t-i}^2} + \sum_{j=1}^p{{\gamma}_{j} h_{t-j}}\ \dots equation\ 2.1.2.2.1
\end{equation}$$
- [<span style='color:blue'>Volatility Forecasting Using GARCH(1,1)</span>](https://quantumfinancier.wordpress.com/2010/08/26/volatility-forecasting-using-garch11/)
- [<span style='color:blue'>Regime Switching System Using Volatility Forecast</span>](https://quantumfinancier.wordpress.com/2010/08/27/regime-switching-system-using-volatility-forecast/)
- [<span style='color:blue'>Normalized Price Spread Strategy</span>](https://quantumfinancier.wordpress.com/2010/06/17/normalized-price-spread-strategy/)
- [<span style='color:blue'>Volatility Autocorrelation in Different Markets</span>](https://quantumfinancier.wordpress.com/2010/05/27/volatility-autocorrelation-in-different-markets/)
- [<span style='color:blue'>Basic Introduction to GARCH and EGARCH (part 1)</span>](https://quantumfinancier.wordpress.com/2010/09/12/381/)
- [<span style='color:blue'>Basic Introduction to GARCH and EGARCH (part 2)</span>](https://quantumfinancier.wordpress.com/2010/09/14/basic-introduction-to-garch-and-egarch-part-2/)
- [<span style='color:blue'>Basic Introduction to GARCH and EGARCH (Part 3)</span>](http://quantumfinancier.wordpress.com/2010/09/23/basic-introduction-to-garch-and-egarch-part-3/amp)^[Using this EGARCH model, we can epect a better estimate the volatility for assets returns due to how the EGARCH counteracts the limitations on the classic GARCH model.Here is the final part of the series of posts on the volatility modelling where I will briefly talk about one of the many variant of the GARCH model: the exponential GARCH (abbreviated EGARCH). I chose this variant because it improves the GARCH model and better model some market mechanics. In the GARCH post, I didn’t mention any of the limitation of the model as I kept them for today’s post. First of all, the GARCH model assume that only the magnitude of unanticipated excess returns determines $\sigma^2_t$. Intuitively, we can question this assumption; I, for one, would argue that not only the magnitude but also the direction of the returns affects volatility.]
- [<span style='color:blue'>Analysis List</span>](https://vlab.stern.nyu.edu/doc?topic=mdls)
- Volatility Analysis
+ [<span style='color:blue'>AGARCH](https://vlab.stern.nyu.edu/doc/6?topic=mdls</span>)
+ [<span style='color:blue'>APARCH</span>](https://vlab.stern.nyu.edu/doc/5?topic=mdls)
+ [<span style='color:blue'>Asy. MEM</span>](https://vlab.stern.nyu.edu/doc/10?topic=mdls)
+ [<span style='color:blue'>Asy. Power MEM</span>](https://vlab.stern.nyu.edu/doc/11?topic=mdls)
+ [<span style='color:blue'>CDS-GARCH</span>](https://vlab.stern.nyu.edu/doc/25?topic=mdls)
+ [<span style='color:blue'>CDS-GARCH-DYN</span>](https://vlab.stern.nyu.edu/doc/26?topic=mdls)
+ [<span style='color:blue'>EGARCH</span>](https://vlab.stern.nyu.edu/doc/4?topic=mdls)
+ [<span style='color:blue'>GARCH</span>](https://vlab.stern.nyu.edu/doc/2?topic=mdls)
+ [<span style='color:blue'>GAS-GARCH Student T</span>](https://vlab.stern.nyu.edu/doc/23?topic=mdls)
+ [<span style='color:blue'>GJR-GARCH</span>](https://vlab.stern.nyu.edu/doc/3?topic=mdls)
+ [<span style='color:blue'>MEM</span>](https://vlab.stern.nyu.edu/doc/9?topic=mdls)
+ [<span style='color:blue'>Spline-GARCH</span>](https://vlab.stern.nyu.edu/doc/7?topic=mdls)
+ [<span style='color:blue'>Zero Slope Spline-GARCH</span>](https://vlab.stern.nyu.edu/doc/8?topic=mdls)
- Correlation Analysis
+ [<span style='color:blue'>EWMA Covariance</span>](https://vlab.stern.nyu.edu/doc/12?topic=mdls)
+ [<span style='color:blue'>GARCH-DCC</span>](https://vlab.stern.nyu.edu/doc/13?topic=mdls)
+ [<span style='color:blue'>GARCH-DECO</span>](https://vlab.stern.nyu.edu/doc/20?topic=mdls)
+ [<span style='color:blue'>GJR-DCC</span>](https://vlab.stern.nyu.edu/doc/14?topic=mdls)
+ [<span style='color:blue'>GJR-DECO</span>](https://vlab.stern.nyu.edu/doc/21?topic=mdls)
- Systemic Risk Analysis
+ [<span style='color:blue'>Domestic MES</span>](https://vlab.stern.nyu.edu/doc/40?topic=mdls)
+ [<span style='color:blue'>Dynamic MES</span>](https://vlab.stern.nyu.edu/doc/16?topic=mdls)
+ [<span style='color:blue'>Dynamic MES with Simulation</span>](https://vlab.stern.nyu.edu/doc/22?topic=mdls)
+ [<span style='color:blue'>Global Dynamic MES</span>](https://vlab.stern.nyu.edu/doc/17?topic=mdls)
- Long Run Value at Risk
+ [<span style='color:blue'>Long Term GJR-GARCH Forecast</span>](https://vlab.stern.nyu.edu/doc/18?topic=mdls)
+ [<span style='color:blue'>Long Term GJR-GARCH Forecast with Options</span>](https://vlab.stern.nyu.edu/doc/19?topic=mdls)
- Liquidity Analysis
+ [<span style='color:blue'>Asymmetric ILLIQ</span>](https://vlab.stern.nyu.edu/doc/28?topic=mdls)
+ [<span style='color:blue'>Historical ILLIQ</span>](https://vlab.stern.nyu.edu/doc/29?topic=mdls)
+ [<span style='color:blue'>Spline ILLIQ</span>](https://vlab.stern.nyu.edu/doc/27?topic=mdls)
- Fixed Income Analysis
+ [<span style='color:blue'>ESR Interest Rate Forecast</span>](https://vlab.stern.nyu.edu/doc/39?topic=mdls)
- [<span style='color:blue'>Extensions of the GARCH Model</span>](http://sfb649.wiwi.hu-berlin.de/fedc_homepage/xplore/tutorials/sfehtmlnode67.html)^[Comparison among the GARCH, TGARCH (Threshold GARCH) and EGARCH (Exponential GARCH) models.]
- [<span style='color:blue'>How to fit ARMA+GARCH Model In R?</span>](https://quant.stackexchange.com/questions/4948/how-to-fit-armagarch-model-in-r?answertab=votes#tab-top)
- [<span style='color:blue'>A short introduction to the rugarch package</span>](http://unstarched.net/r-examples/rugarch/a-short-introduction-to-the-rugarch-package/)
- [<span style='color:blue'>A practical introduction to garch modeling</span>](https://www.r-bloggers.com/a-practical-introduction-to-garch-modeling/)
- [<span style='color:blue'>ARCH-GARCH Example with R</span>](http://yunus.hacettepe.edu.tr/~iozkan/eco665/archgarch.html)
- [<span style='color:blue'>Financial Econometrics Practical *Practical 6: Univariate Volatility Modelling*</span>](http://curiousquant.com/ClassNotes/FinMetrics/Practicals/Practical_6/Practical_6.html)
- [<span style='color:blue'>Multivariate GARCH(1,1) in R</span>](https://stackoverflow.com/questions/35035857/multivariate-garch1-1-in-r?answertab=votes#tab-top)
- [<span style='color:blue'>R - Modelling Multivariate GARCH (rugarch and ccgarch)</span>](https://stackoverflow.com/questions/16874375/r-modelling-multivariate-garch-rugarch-and-ccgarch?answertab=votes#tab-top)
- [<span style='color:blue'>Introduction to some R package</span>](./reference/Introduction to some R packages.pdf)
- [<span style='color:blue'>Introduction to the ruGarch package</span>](./reference/Introduction to the rugarch package.pdf)
- [<span style='color:blue'>The rmgarch models: Background and properties. - R Project</span>](./reference/The rmgarch models - Background and Properties.pdf)
- [<span style='color:blue'>rmgarch - How to Multivariate GARCH Models in R | R-How.com</span>](https://r-how.com/packages/rmgarch)
- [<span style='color:blue'>Volatility forecast evaluation in R</span>](https://eranraviv.com/volatility-forecast-evaluation-in-r/)
Firstly we use `rugarch` <s>and then `rmgarch`</s>^[Due to file loading heavily, here I leave the multivariate Garch models for future works.] to compare the result.
```{r intro-Garch, warning = FALSE}
## http://www.unstarched.net/r-examples/rugarch/a-short-introduction-to-the-rugarch-package/
ugarchspec()
## This defines a basic ARMA(1,1)-GARCH(1,1) model, though there are many more options to choose from ranging from the type of GARCH model, the ARFIMAX-arch-in-mean specification and conditional distribution. In fact, and considering only the (1,1) order for the GARCH and ARMA models, there are 13440 possible combinations of models and model options to choose from:
## possible Garch models.
nrow(expand.grid(GARCH = 1:14, VEX = 0:1, VT = 0:1, Mean = 0:1, ARCHM = 0:2, ARFIMA = 0:1, MEX = 0:1, DISTR = 1:10))
spec = ugarchspec(variance.model = list(model = 'eGARCH', garchOrder = c(2, 1)), distribution = 'std')
```
There will be `r nrow(expand.grid(GARCH = 1:14, VEX = 0:1, VT = 0:1, Mean = 0:1, ARCHM = 0:2, ARFIMA = 0:1, MEX = 0:1, DISTR = 1:10))` possible combination Garch models. Here I tried to filter few among them.
Now we try to build a Garch model and will build some Garch models to get the best fit in later section.
<iframe width="560" height="315" src="https://www.youtube.com/embed/0q3gSJKJUs8" frameborder="0" allowfullscreen></iframe>
```{r build-Garch, warning = FALSE}
## Modelling Garch ('sGarch' model) focasting data.
#'@ fitGM.op <- suppressAll(simGarch(USDJPY, .prCat = 'Op')) #will take a minute
#'@ saveRDS(fitGM.op, './data/fitGM.op.rds')
#'@ fitGM.hi <- suppressAll(simGarch(USDJPY, .prCat = 'Hi')) #will take a minute
#'@ saveRDS(fitGM.hi, './data/fitGM.hi.rds')
#'@ fitGM.mn <- suppressAll(simGarch(USDJPY, .prCat = 'Mn')) #will take a minute
#'@ saveRDS(fitGM.mn, './data/fitGM.mn.rds')
#'@ fitGM.lo <- suppressAll(simGarch(USDJPY, .prCat = 'Lo')) #will take a minute
#'@ saveRDS(fitGM.lo, './data/fitGM.lo.rds')
#'@ fitGM.cl <- suppressAll(simGarch(USDJPY, .prCat = 'Cl')) #will take a minute
#'@ saveRDS(fitGM.cl, './data/fitGM.cl.rds')
fitGM.op <- readRDS('./data/fitGM.op.rds')
fitGM.hi <- readRDS('./data/fitGM.hi.rds')
fitGM.mn <- readRDS('./data/fitGM.mn.rds')
fitGM.lo <- readRDS('./data/fitGM.lo.rds')
fitGM.cl <- readRDS('./data/fitGM.cl.rds')
```
```{r intro-EWMA, warning = FALSE}
## ======================== eval = FALSE ==============================
## Exponential Weighted Moving Average model - EWMA fixed parameters
#'@ ewma.spec.fixed <- llply(pp, function(y) {
#'@ z = simStakesGarch(mbase, .solver = .solver.par[1], .prCat = y[1],
#'@ .prCat.method = 'CSS-ML', .baseDate = ymd('2015-01-01'),
#'@ .parallel = FALSE, .progress = 'text',
#'@ .setPrice = y[2], .setPrice.method = 'CSS-ML',
#'@ .initialFundSize = 1000, .fundLeverageLog = FALSE,
#'@ .filterBets = FALSE, .variance.model = list(
#'@ model = .variance.model.par[6], garchOrder = c(1, 1),
#'@ submodel = NULL, external.regressors = NULL,
#'@ variance.targeting = FALSE),
#'@ .mean.model = list(armaOrder = c(1, 1),
#'@ include.mean = TRUE,
#'@ archm = FALSE, archpow = 1,
#'@ arfima = FALSE,
#'@ external.regressors = NULL,
#'@ archex = FALSE),
#'@ .dist.model = .dist.model.par[1], start.pars = list(),
#'@ fixed.pars = list(alpha1 = 1 - 0.94, omega = 0))
#'@
#'@ txt1 <- paste0('saveRDS(z', ', file = \'./data/',
#'@ .variance.model.par[6], '.EWMA.fixed.',
#'@ y[1], '.', y[2], '.', .dist.model.par[1], '.',
#'@ .solver.par[1], '.rds\')')
#'@ eval(parse(text = txt1))
#'@ cat(paste0(txt1, ' done!', '\n'))
#'@ rm(z)
#'@ })
## Exponential Weighted Moving Average model - EWMA estimated parameters
#'@ ewma.spec.est <- llply(pp, function(y) {
#'@ z = simStakesGarch(mbase, .solver = .solver.par[1], .prCat = y[1],
#'@ .prCat.method = 'CSS-ML', .baseDate = ymd('2015-01-01'),
#'@ .parallel = FALSE, .progress = 'text',
#'@ .setPrice = y[2], .setPrice.method = 'CSS-ML',
#'@ .initialFundSize = 1000, .fundLeverageLog = FALSE,
#'@ .filterBets = FALSE, .variance.model = list(
#'@ model = .variance.model.par[6], garchOrder = c(1, 1),
#'@ submodel = NULL, external.regressors = NULL,
#'@ variance.targeting = FALSE),
#'@ .mean.model = list(armaOrder = c(1, 1),
#'@ include.mean = TRUE,
#'@ archm = FALSE, archpow = 1,
#'@ arfima = FALSE,
#'@ external.regressors = NULL,
#'@ archex = FALSE),
#'@ .dist.model = .dist.model.par[1], start.pars = list(),
#'@ fixed.pars = list(omega = 0))
#'@
#'@ txt1 <- paste0('saveRDS(z', ', file = \'./data/',
#'@ .variance.model.par[6], '.EWMA.est.',
#'@ y[1], '.', y[2], '.', .dist.model.par[1], '.',
#'@ .solver.par[1], '.rds\')')
#'@ eval(parse(text = txt1))
#'@ cat(paste0(txt1, ' done!', '\n'))
#'@ rm(z)
#'@ })
## itegration Garch model - iGarch
#'@ igarch.spec <- llply(pp, function(y) {
#'@ z = simStakesGarch(mbase, .solver = .solver.par[1], .prCat = y[1],
#'@ .prCat.method = 'CSS-ML', .baseDate = ymd('2015-01-01'),
#'@ .parallel = FALSE, .progress = 'text',
#'@ .setPrice = y[2], .setPrice.method = 'CSS-ML',
#'@ .initialFundSize = 1000, .fundLeverageLog = FALSE,
#'@ .filterBets = FALSE, .variance.model = list(
#'@ model = .variance.model.par[6], garchOrder = c(1, 1),
#'@ submodel = NULL, external.regressors = NULL,
#'@ variance.targeting = FALSE),
#'@ .mean.model = list(armaOrder = c(1, 1),
#'@ include.mean = TRUE,
#'@ archm = FALSE, archpow = 1,
#'@ arfima = FALSE,
#'@ external.regressors = NULL,
#'@ archex = FALSE),
#'@ .dist.model = .dist.model.par[1], start.pars = list(),
#'@ fixed.pars = list())
#'@
#'@ txt1 <- paste0('saveRDS(z', ', file = \'./data/',
#'@ .variance.model.par[6], '.', y[1], '.', y[2], '.',
#'@ .dist.model.par[1], '.', .solver.par[1], '.rds\')')
#'@ eval(parse(text = txt1))
#'@ cat(paste0(txt1, ' done!', '\n'))
#'@ rm(z)
#'@ })
```
**Application of MCMC**
Need to refer to MCMC since I am using Garch models...
- [<span style='color:blue'>Markov Chain Monte Carlo Method</span>](https://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_introbayes_sect007.htm)
- [<span style='color:blue'>Burn-In is Unnecessary</span>](http://users.stat.umn.edu/~geyer/mcmc/burn.html)
- [<span style='color:blue'>**Nice R Code** *Punning code better since 2013*</span>](https://nicercode.github.io/guides/mcmc/)
```{r lm-Garch, warning = FALSE}
## Here I test the accuracy of forecasting of univariate Garch ('sGarch' model) models.
## Test the models
## opened price fit data
summary(lm(Point.Forecast~ USDJPY.Close, data = fitGM.op))
summary(MCMCregress(Point.Forecast~ USDJPY.Close, data = fitGM.op))
## highest price fit data
summary(lm(Point.Forecast~ USDJPY.Close, data = fitGM.hi))
summary(MCMCregress(Point.Forecast~ USDJPY.Close, data = fitGM.hi))
## mean price fit data (mean price of daily highest and lowest price)
summary(lm(Point.Forecast~ USDJPY.Close, data = fitGM.mn))
summary(MCMCregress(Point.Forecast~ USDJPY.Close, data = fitGM.mn))
## lowest price fit data
summary(lm(Point.Forecast~ USDJPY.Close, data = fitGM.lo))
summary(MCMCregress(Point.Forecast~ USDJPY.Close, data = fitGM.lo))
## closed price fit data
summary(lm(Point.Forecast~ USDJPY.Close, data = fitGM.cl))
summary(MCMCregress(Point.Forecast~ USDJPY.Close, data = fitGM.cl))
```
**Mean Squared Error**
```{r combine-dataGM, warning = FALSE}
## Univariate Garch models.
fcdataGM <- do.call(cbind, list(USDJPY.FPOP.Open = fitGM.op$Point.Forecast,
USDJPY.FPHI.High = fitGM.hi$Point.Forecast,
USDJPY.FPMN.Mean = fitGM.mn$Point.Forecast,
USDJPY.FPLO.Low = fitGM.lo$Point.Forecast,
USDJPY.FPCL.Close = fitGM.cl$Point.Forecast,
USDJPY.Open = fitGM.op$USDJPY.Open,
USDJPY.High = fitGM.op$USDJPY.High,
USDJPY.Low = fitGM.op$USDJPY.Low,
USDJPY.Close = fitGM.op$USDJPY.Close))
fcdataGM %<>% na.omit
names(fcdataGM) <- c('USDJPY.FPOP.Open', 'USDJPY.FPHI.High', 'USDJPY.FPMN.Mean',
'USDJPY.FPLO.Low', 'USDJPY.FPCL.Close', 'USDJPY.Open',
'USDJPY.High', 'USDJPY.Low', 'USDJPY.Close')
## Mean Squared Error : comparison of accuracy
paste('Open = ', mean((fcdataGM$USDJPY.FPOP.Open - fcdataGM$USDJPY.Open)^2))
paste('High = ', mean((fcdataGM$USDJPY.FPHI.High - fcdataGM$USDJPY.High)^2))
paste('Mean = ', mean((fcdataGM$USDJPY.FPMN.Mean - (fcdataGM$USDJPY.High + fcdataGM$USDJPY.Low)/2)^2))
paste('Low = ', mean((fcdataGM$USDJPY.FPLO.Low - fcdataGM$USDJPY.Low)^2))
paste('Close = ', mean((fcdataGM$USDJPY.FPCL.Close - fcdataGM$USDJPY.Close)^2))
```
#### MCMC vs Bayesian Time Series
Generally, we can write a Bayesian structural model like this:
$$\begin{equation}
Y_{t} = \mu_{t} + x_{t}\beta + S_{t} + \varepsilon_{t}\ ;\ e_{t}\sim N(0,\sigma_{e}^{2})
\end{equation}$$
$$\begin{equation}
\mu_{t} + 1 = \mu_{t} + \nu_{t}\ ,\ \nu_{t}\sim N(0,\sigma_{\nu}^{2})
\end{equation}$$
For bayesian and MCMC, I leave it for future works.
```{r bsts}
## Sorry ARIMA, but I’m Going Bayesian (packages : bsts, arm)
## http://multithreaded.stitchfix.com/blog/2016/04/21/forget-arima/
## https://stackoverflow.com/questions/11839886/r-predict-glm-equivalent-for-mcmcpackmcmclogit
```
#### MIDAS
For Midas, I also leave it for future works. Kindly refer to [<span style='color:blue'>Mixed Frequency Data Sampling Regression Models - The R Package midasr</span>](https://raw.githubusercontent.com/englianhu/binary.com-interview-question/757b27e1e93132368b0898152078be4961b05a28/reference/Mixed%20Frequency%20Data%20Sampling%20Regression%20Models%20-%20The%20R%20Package%20midasr.pdf) for more information about Midas.
### Data Visualization
Plot graph.
#### ARIMA vs ETS
```{r plotETS-1, echo = FALSE, warning = FALSE}
## Plot the models
## opened price fit data
autoplot(forecast(ets(fitETS.op$Point.Forecast), h = 4), facets = TRUE) + geom_forecast(color = '#ffcccc', show.legend = FALSE) + labs(x = 'Day', y = 'Forex Price', title = 'Forecasts from ETS model [A,N,N]', subtitle = 'Opening Price Forecast', caption = 'Source : USDJPY')
#'@ ggplot(data = pd, aes(x = date, y = observed)) + geom_line(color = 'red') + geom_line(aes(y = fitted), color = "blue") + geom_line(aes(y = forecast)) + geom_ribbon(aes(ymin = lo95, ymax = hi95), alpha = .25) + scale_x_date(name = "Time in Decades") + scale_y_continuous(name = "GDP per capita (current US$)") + theme(axis.text.x = element_text(size = 10), legend.justification=c(0,1), legend.position=c(0,1)) + ggtitle("Arima(0,1,1) Fit and Forecast of GDP per capita for Brazil (1960-2013)") + scale_color_manual(values = c("Blue", "Red"), breaks = c("Fitted", "Data", "Forecast")) + ggsave((filename = "gdp_forecast_ggplot.pdf"), width=330, height=180, units=c("mm"), dpi = 300, limitsize = TRUE)
## highest price fit data
autoplot(forecast(ets(fitETS.hi$Point.Forecast), h = 4), facets = TRUE) + geom_forecast(color = '#FFCCCC', show.legend = FALSE) + labs(x = 'Day', y = 'Forex Price', title = 'Forecasts from ETS model [A,N,N]', subtitle = 'Highest Price Forecast', caption = 'Source : USDJPY')
## mean price fit data (mean price of daily highest and lowest price)
autoplot(forecast(ets(fitETS.mn$Point.Forecast), h = 4), facets = TRUE) + geom_forecast(color = '#FFCCCC', show.legend = FALSE) + labs(x = 'Day', y = 'Forex Price', title = 'Forecasts from ETS model [A,N,N]', subtitle = 'Mean Price Forecast', caption = 'Source : USDJPY')
## lowest price fit data
autoplot(forecast(ets(fitETS.lo$Point.Forecast), h = 4), facets = TRUE) + geom_forecast(color = '#FFCCCC', show.legend = FALSE) + labs(x = 'Day', y = 'Forex Price', title = 'Forecasts from ETS model [A,N,N]', subtitle = 'Lowest Price Forecast', caption = 'Source : USDJPY')
## opened price fit data
autoplot(forecast(ets(fitETS.cl$Point.Forecast), h = 4), facets = TRUE) + geom_forecast(color = '#FFCCCC', show.legend = FALSE) + labs(x = 'Day', y = 'Forex Price', title = 'Forecasts from ETS model [A,N,N]', subtitle = 'Closing Price Forecast', caption = 'Source : USDJPY')
```
```{r plotETS-2, echo = FALSE, warning = FALSE}
#'@ source('./function/plotChart2.R', local = TRUE)
suppressAll(rm(fitETS.op, fitETS.hi, fitETS.mn, fitETS.lo, fitETS.cl))
plotChart2(fcdata, initialName = 'FP', chart.type = 'FP', graph.title = 'ETS Model : USDJPY')
```
#### Garch vs EWMA
```{r plotGM-1, echo = FALSE, warning = FALSE}
#'@ source('./function/plotChart2.R', local = TRUE)
suppressAll(rm(fitGM.op, fitGM.hi, fitGM.mn, fitGM.lo, fitGM.cl))
plotChart2(fcdataGM, initialName = 'FP', chart.type = 'FP', graph.title = 'Garch Model : USDJPY')
```
#### MCMC vs Bayesian Time Series
#### MIDAS
### Staking Model
#### ARIMA vs ETS
Staking function. Here I apply Kelly criterion as the betting strategy. I don't pretend to know the order of price flutuation flow from the Hi-Lo price range, therefore I just using Closing price for settlement while the staking price restricted within the variance (Hi-Lo) to made the transaction stand. The settled price can only be closing price unless staking price is opening price which sellable within the Hi-Lo range.
Due to we cannot know the forecasted sell/buy price and also forecasted closing price which is coming first solely from Hi-Lo data, therefore the Profit&Loss will slidely different (sell/buy price = forecasted sell/buy price).
- Forecasted profit = edge based on forecasted sell/buy price - forecasted settled price.
- If the forecasted sell/buy price doesn't exist within the Hi-Lo price, then the transaction is not stand.
- If the forecasted settled price does not exist within the Hi-Lo price, then the settled price will be the real closing price.
Kindly refer to [<span style='color:blue'>**Quintuitive** ARMA Models for Trading</span>](http://www.quintutive.com2012/08/22/arma-models-for-trading) to know how to determine PULL or CALL with ARMA models^[The author compare the ROI between **Buy-and-Hold** with **GARCH** model.].
Here I set an application of leverage while it is very risky (the variance of ROI is very high) as we can know from later comparison.
**Staking Model**
For Buy-Low-Sell-High tactic, I placed two limit order for tomorrow now, which are buy and sell. The transaction will be standed once the price hit in tomorrow. If the buy price doesn't met, there will be no transaction made, while sell price doesn't occur will use closing price for settlement.^[Using Kelly criterion staking model]
For variance betting, I used both focasted highest minus the forecasted lowest price to get the range. After that placed two limit orders as well. If one among the buy or sell price doesn't appear will use closing price as final settlement.^[Place $100 for every single bet.]
#### Garch vs EWMA
The staking models same with what I applied onto ETS modelled dataset.
#### MCMC vs Bayesian Time Series
#### MIDAS
### Return of Investment
#### ARIMA vs ETS
```{r simStaking-AutoArima, eval = FALSE, warning = FALSE, include = FALSE}
##============================ EVAL = FALSE ================================
##
## Model 1 without leverage.
##
## Placed orders - Fund size without log
#'@ mbase <- USDJPY
## settled with highest price.
fundAutoArimaOPHI <- simStakesAutoArima(mbase, .prCat = 'Op', .setPrice = 'Hi', .initialFundSize = 1000)
saveRDS(fundAutoArimaOPHI, file = './data/fundAutoArimaOPHI.rds')
fundAutoArimaHIHI <- simStakesAutoArima(mbase, .prCat = 'Hi', .setPrice = 'Hi', .initialFundSize = 1000)
saveRDS(fundAutoArimaHIHI, file = './data/fundAutoArimaHIHI.rds')
fundAutoArimaMNHI <- simStakesAutoArima(mbase, .prCat = 'Mn', .setPrice = 'Hi', .initialFundSize = 1000)
saveRDS(fundAutoArimaMNHI, file = './data/fundAutoArimaMNHI.rds')
fundAutoArimaLOHI <- simStakesAutoArima(mbase, .prCat = 'Lo', .setPrice = 'Hi', .initialFundSize = 1000)
saveRDS(fundAutoArimaLOHI, file = './data/fundAutoArimaLOHI.rds')
fundAutoArimaCLHI <- simStakesAutoArima(mbase, .prCat = 'Cl', .setPrice = 'Hi', .initialFundSize = 1000)
saveRDS(fundAutoArimaCLHI, file = './data/fundAutoArimaCLHI.rds')
## settled with mean price.
fundAutoArimaOPMN <- simStakesAutoArima(mbase, .prCat = 'Op', .setPrice = 'Mn', .initialFundSize = 1000)
saveRDS(fundAutoArimaOPMN, file = './data/fundAutoArimaOPMN.rds')
fundAutoArimaHIMN <- simStakesAutoArima(mbase, .prCat = 'Hi', .setPrice = 'Mn', .initialFundSize = 1000)
saveRDS(fundAutoArimaHIMN, file = './data/fundAutoArimaHIMN.rds')
fundAutoArimaMNMN <- simStakesAutoArima(mbase, .prCat = 'Mn', .setPrice = 'Mn', .initialFundSize = 1000)
saveRDS(fundAutoArimaMNMN, file = './data/fundAutoArimaMNMN.rds')
fundAutoArimaLOMN <- simStakesAutoArima(mbase, .prCat = 'Lo', .setPrice = 'Mn', .initialFundSize = 1000)
saveRDS(fundAutoArimaLOMN, file = './data/fundAutoArimaLOMN.rds')
fundAutoArimaCLMN <- simStakesAutoArima(mbase, .prCat = 'Cl', .setPrice = 'Mn', .initialFundSize = 1000)
saveRDS(fundAutoArimaCLMN, file = './data/fundAutoArimaCLMN.rds')
## settled with opening price.
fundAutoArimaOPLO <- simStakesAutoArima(mbase, .prCat = 'Op', .setPrice = 'Lo', .initialFundSize = 1000)
saveRDS(fundAutoArimaOPLO, file = './data/fundAutoArimaOPLO.rds')
fundAutoArimaHILO <- simStakesAutoArima(mbase, .prCat = 'Hi', .setPrice = 'Lo', .initialFundSize = 1000)
saveRDS(fundAutoArimaHILO, file = './data/fundAutoArimaHILO.rds')
fundAutoArimaMNLO <- simStakesAutoArima(mbase, .prCat = 'Mn', .setPrice = 'Lo', .initialFundSize = 1000)
saveRDS(fundAutoArimaMNLO, file = './data/fundAutoArimaMNLO.rds')
fundAutoArimaLOLO <- simStakesAutoArima(mbase, .prCat = 'Lo', .setPrice = 'Lo', .initialFundSize = 1000)
saveRDS(fundAutoArimaLOLO, file = './data/fundAutoArimaLOLO.rds')
fundAutoArimaCLLO <- simStakesAutoArima(mbase, .prCat = 'Cl', .setPrice = 'Lo', .initialFundSize = 1000)
saveRDS(fundAutoArimaCLLO, file = './data/fundAutoArimaCLLO.rds')
## settled with closing price.
fundAutoArimaOPCL <- simStakesAutoArima(mbase, .prCat = 'Op', .setPrice = 'Cl', .initialFundSize = 1000)
saveRDS(fundAutoArimaOPCL, file = './data/fundAutoArimaOPCL.rds')
fundAutoArimaHICL <- simStakesAutoArima(mbase, .prCat = 'Hi', .setPrice = 'Cl', .initialFundSize = 1000)
saveRDS(fundAutoArimaHICL, file = './data/fundAutoArimaHICL.rds')
fundAutoArimaMNCL <- simStakesAutoArima(mbase, .prCat = 'Mn', .setPrice = 'Cl', .initialFundSize = 1000)
saveRDS(fundAutoArimaMNCL, file = './data/fundAutoArimaMNCL.rds')
fundAutoArimaLOCL <- simStakesAutoArima(mbase, .prCat = 'Lo', .setPrice = 'Cl', .initialFundSize = 1000)
saveRDS(fundAutoArimaLOCL, file = './data/fundAutoArimaLOCL.rds')
fundAutoArimaCLCL <- simStakesAutoArima(mbase, .prCat = 'Cl', .setPrice = 'Cl', .initialFundSize = 1000)
saveRDS(fundAutoArimaCLCL, file = './data/fundAutoArimaCLCL.rds')
## Placed orders - Fund size without log
fundList <- list(fundOPHI = fundAutoArimaOPHI, fundHIHI = fundAutoArimaHIHI, fundMNHI = fundAutoArimaMNHI, fundLOHI = fundAutoArimaLOHI, fundCLHI = fundAutoArimaCLHI,
fundOPMN = fundAutoArimaOPMN, fundHIMN = fundAutoArimaHIMN, fundMNMN = fundAutoArimaMNMN, fundLOMN = fundAutoArimaLOMN, fundCLMN = fundAutoArimaCLMN,
fundOPLO = fundAutoArimaOPLO, fundHILO = fundAutoArimaHILO, fundMNLO = fundAutoArimaMNLO, fundLOLO = fundAutoArimaLOLO, fundCLLO = fundAutoArimaCLLO,
fundOPCL = fundAutoArimaOPCL, fundHICL = fundAutoArimaHICL, fundMNCL = fundAutoArimaMNCL, fundLOCL = fundAutoArimaLOCL, fundCLCL = fundAutoArimaCLCL)
ldply(fundList, function(x) { x %>% mutate(StartDate = first(Date), LatestDate = last(Date), InitFund = first(BR), LatestFund = last(Bal), Profit = sum(Profit), RR = LatestFund/InitFund) %>% dplyr::select(StartDate, LatestDate, InitFund, LatestFund, Profit, RR) %>% unique }) %>% tbl_df
# A tibble: 20 x 7
# .id StartDate LatestDate InitFund LatestFund Profit RR
# <chr> <date> <date> <dbl> <dbl> <dbl> <dbl>
# 1 fundOPHI 2015-01-02 2017-01-20 1000 1325.983 325.98313 1.325983
# 2 fundHIHI 2015-01-02 2017-01-20 1000 1000.000 0.00000 1.000000
# 3 fundMNHI 2015-01-02 2017-01-20 1000 1236.199 236.19900 1.236199
# 4 fundLOHI 2015-01-02 2017-01-20 1000 1716.985 716.98492 1.716985
# 5 fundCLHI 2015-01-02 2017-01-20 1000 1323.688 323.68809 1.323688
# 6 fundOPMN 2015-01-02 2017-01-20 1000 1304.819 304.81916 1.304819
# 7 fundHIMN 2015-01-02 2017-01-20 1000 1363.714 363.71443 1.363714
# 8 fundMNMN 2015-01-02 2017-01-20 1000 1000.000 0.00000 1.000000
# 9 fundLOMN 2015-01-02 2017-01-20 1000 1440.170 440.16965 1.440170
# 10 fundCLMN 2015-01-02 2017-01-20 1000 1292.947 292.94694 1.292947
# 11 fundOPLO 2015-01-02 2017-01-20 1000 1307.610 307.60951 1.307610
# 12 fundHILO 2015-01-02 2017-01-20 1000 1637.251 637.25113 1.637251
# 13 fundMNLO 2015-01-02 2017-01-20 1000 1250.375 250.37547 1.250375
# 14 fundLOLO 2015-01-02 2017-01-20 1000 1000.000 0.00000 1.000000
# 15 fundCLLO 2015-01-02 2017-01-20 1000 1261.157 261.15684 1.261157
# 16 fundOPCL 2015-01-02 2017-01-20 1000 1047.563 47.56281 1.047563
# 17 fundHICL 2015-01-02 2017-01-20 1000 1401.694 401.69378 1.401694
# 18 fundMNCL 2015-01-02 2017-01-20 1000 1158.790 158.79028 1.158790
# 19 fundLOCL 2015-01-02 2017-01-20 1000 1499.818 499.81773 1.499818
# 20 fundCLCL 2015-01-02 2017-01-20 1000 1000.000 0.00000 1.000000
```
```{r ROI-AutoArima, warning = FALSE, echo = FALSE}
## load the pre-run and saved models.
## Profit and Loss of Arima models.
flsAutoArima <- dir('./data', pattern = 'fundAutoArima')
fundList <- llply(flsAutoArima, function(dt) {
cbind(Model = str_replace_all(dt, '.rds', ''),
readRDS(file = paste0('./data/', dt))) %>% tbl_df
})
names(fundList) <- sapply(fundList, function(x) xts::first(x$Model))
## Summary of ROI
aa.tbl <- ldply(fundList, function(x) { x %>% mutate(StartDate = xts::first(Date), LatestDate = last(Date), InitFund = xts::first(BR), LatestFund = last(Bal), Profit = sum(Profit), RR = LatestFund/InitFund) %>% dplyr::select(StartDate, LatestDate, InitFund, LatestFund, Profit, RR) %>% unique }) %>% tbl_df
aa.tbl %>%
kable %>%
kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive')) %>%
scroll_box(width = '100%', height = '400px')
```
The return of investment from best fitted Auto Arima model.
```
7 fundAutoArimaHILO 2015-01-02 2017-01-20 1000 1637.251 637.25113 1.637251
10 fundAutoArimaLOHI 2015-01-02 2017-01-20 1000 1716.985 716.98492 1.716985
```
Profit and Loss of default `ZZZ` ets models.
```{r simStaking-woutLog, warning = FALSE}
##
## Model 1 without leverage.
##
## Placed orders - Fund size without log
#'@ mbase <- USDJPY
## settled with highest price.
#'@ fundOPHI <- simStakesETS(mbase, .prCat = 'Op', .setPrice = 'Hi', .initialFundSize = 1000)
#'@ saveRDS(fundOPHI, file = './data/fundOPHI.rds')
#'@ fundHIHI <- simStakesETS(mbase, .prCat = 'Hi', .setPrice = 'Hi', .initialFundSize = 1000)
#'@ saveRDS(fundHIHI, file = './data/fundHIHI.rds')
#'@ fundMNHI <- simStakesETS(mbase, .prCat = 'Mn', .setPrice = 'Hi', .initialFundSize = 1000)
#'@ saveRDS(fundMNHI, file = './data/fundMNHI.rds')
#'@ fundLOHI <- simStakesETS(mbase, .prCat = 'Lo', .setPrice = 'Hi', .initialFundSize = 1000)
#'@ saveRDS(fundLOHI, file = './data/fundLOHI.rds')
#'@ fundCLHI <- simStakesETS(mbase, .prCat = 'Cl', .setPrice = 'Hi', .initialFundSize = 1000)
#'@ saveRDS(fundCLHI, file = './data/fundCLHI.rds')
## settled with mean price.
#'@ fundOPMN <- simStakesETS(mbase, .prCat = 'Op', .setPrice = 'Mn', .initialFundSize = 1000)
#'@ saveRDS(fundOPMN, file = './data/fundOPMN.rds')
#'@ fundHIMN <- simStakesETS(mbase, .prCat = 'Hi', .setPrice = 'Mn', .initialFundSize = 1000)
#'@ saveRDS(fundHIMN, file = './data/fundHIMN.rds')
#'@ fundMNMN <- simStakesETS(mbase, .prCat = 'Mn', .setPrice = 'Mn', .initialFundSize = 1000)
#'@ saveRDS(fundMNMN, file = './data/fundMNMN.rds')
#'@ fundLOMN <- simStakesETS(mbase, .prCat = 'Lo', .setPrice = 'Mn', .initialFundSize = 1000)
#'@ saveRDS(fundLOMN, file = './data/fundLOMN.rds')
#'@ fundCLMN <- simStakesETS(mbase, .prCat = 'Cl', .setPrice = 'Mn', .initialFundSize = 1000)
#'@ saveRDS(fundCLMN, file = './data/fundCLMN.rds')
## settled with opening price.
#'@ fundOPLO <- simStakesETS(mbase, .prCat = 'Op', .setPrice = 'Lo', .initialFundSize = 1000)
#'@ saveRDS(fundOPLO, file = './data/fundOPLO.rds')
#'@ fundHILO <- simStakesETS(mbase, .prCat = 'Hi', .setPrice = 'Lo', .initialFundSize = 1000)
#'@ saveRDS(fundHILO, file = './data/fundHILO.rds')
#'@ fundMNLO <- simStakesETS(mbase, .prCat = 'Mn', .setPrice = 'Lo', .initialFundSize = 1000)
#'@ saveRDS(fundMNLO, file = './data/fundMNLO.rds')
#'@ fundLOLO <- simStakesETS(mbase, .prCat = 'Lo', .setPrice = 'Lo', .initialFundSize = 1000)
#'@ saveRDS(fundLOLO, file = './data/fundLOLO.rds')
#'@ fundCLLO <- simStakesETS(mbase, .prCat = 'Cl', .setPrice = 'Lo', .initialFundSize = 1000)
#'@ saveRDS(fundCLLO, file = './data/fundCLLO.rds')
## settled with closing price.
#'@ fundOPCL <- simStakesETS(mbase, .prCat = 'Op', .setPrice = 'Cl', .initialFundSize = 1000)
#'@ saveRDS(fundOPCL, file = './data/fundOPCL.rds')
#'@ fundHICL <- simStakesETS(mbase, .prCat = 'Hi', .setPrice = 'Cl', .initialFundSize = 1000)
#'@ saveRDS(fundHICL, file = './data/fundHICL.rds')
#'@ fundMNCL <- simStakesETS(mbase, .prCat = 'Mn', .setPrice = 'Cl', .initialFundSize = 1000)
#'@ saveRDS(fundMNCL, file = './data/fundMNCL.rds')
#'@ fundLOCL <- simStakesETS(mbase, .prCat = 'Lo', .setPrice = 'Cl', .initialFundSize = 1000)
#'@ saveRDS(fundLOCL, file = './data/fundLOCL.rds')
#'@ fundCLCL <- simStakesETS(mbase, .prCat = 'Cl', .setPrice = 'Cl', .initialFundSize = 1000)
#'@ saveRDS(fundCLCL, file = './data/fundCLCL.rds')
## Placed orders - Fund size without log
#'@ fundList <- list(fundOPHI = fundOPHI, fundHIHI = fundHIHI, fundMNHI = fundMNHI, fundLOHI = fundLOHI, fundCLHI = fundCLHI,
#'@ fundOPMN = fundOPMN, fundHIMN = fundHIMN, fundMNMN = fundMNMN, fundLOMN = fundLOMN, fundCLMN = fundCLMN,
#'@ fundOPLO = fundOPLO, fundHILO = fundHILO, fundMNLO = fundMNLO, fundLOLO = fundLOLO, fundCLLO = fundCLLO,
#'@ fundOPCL = fundOPCL, fundHICL = fundHICL, fundMNCL = fundMNCL, fundLOCL = fundLOCL, fundCLCL = fundCLCL)
#'@ ldply(fundList, function(x) { x %>% mutate(StartDate = first(Date), LatestDate = last(Date), InitFund = first(BR), LatestFund = last(Bal), Profit = sum(Profit), RR = LatestFund/InitFund) %>% dplyr::select(StartDate, LatestDate, InitFund, LatestFund, Profit, RR) %>% unique }) %>% tbl_df
## A tibble: 20 × 5
# .id StartDate LatestDate InitFund LatestFund Profit RR
# <chr> <date> <date> <dbl> <dbl> <dbl> <dbl>
#1 fundOPHI 2015-01-02 2017-01-20 1000 326.83685 1326.837 1.326837
#2 fundHIHI 2015-01-02 2017-01-20 1000 0.00000 1000.000 1.000000
#3 fundMNHI 2015-01-02 2017-01-20 1000 152.30210 1152.302 1.152302
#4 fundLOHI 2015-01-02 2017-01-20 1000 816.63808 1816.638 1.816638
#5 fundCLHI 2015-01-02 2017-01-20 1000 323.18564 1323.186 1.323186
#6 fundOPMN 2015-01-02 2017-01-20 1000 246.68001 1246.680 1.246680
#7 fundHIMN 2015-01-02 2017-01-20 1000 384.90915 1384.909 1.384909
#8 fundMNMN 2015-01-02 2017-01-20 1000 0.00000 1000.000 1.000000
#9 fundLOMN 2015-01-02 2017-01-20 1000 529.34170 1529.342 1.529342
#10 fundCLMN 2015-01-02 2017-01-20 1000 221.03926 1221.039 1.221039
#11 fundOPLO 2015-01-02 2017-01-20 1000 268.31155 1268.312 1.268312
#12 fundHILO 2015-01-02 2017-01-20 1000 649.35074 1649.351 1.649351
#13 fundMNLO 2015-01-02 2017-01-20 1000 298.28509 1298.285 1.298285
#14 fundLOLO 2015-01-02 2017-01-20 1000 0.00000 1000.000 1.000000
#15 fundCLLO 2015-01-02 2017-01-20 1000 208.85690 1208.857 1.208857
#16 fundOPCL 2015-01-02 2017-01-20 1000 30.55969 1030.560 1.030560
#17 fundHICL 2015-01-02 2017-01-20 1000 400.59057 1400.591 1.400591
#18 fundMNCL 2015-01-02 2017-01-20 1000 117.96808 1117.968 1.117968
#19 fundLOCL 2015-01-02 2017-01-20 1000 530.68975 1530.690 1.530690
#20 fundCLCL 2015-01-02 2017-01-20 1000 0.00000 1000.000 1.000000
## load fund files which is from chunk `r simStaking-woutLog`.
fundOPHI <- readRDS('./data/fundOPHI.rds')
fundHIHI <- readRDS('./data/fundHIHI.rds')
fundMNHI <- readRDS('./data/fundMNHI.rds')
fundLOHI <- readRDS('./data/fundLOHI.rds')
fundCLHI <- readRDS('./data/fundCLHI.rds')
fundOPMN <- readRDS('./data/fundOPMN.rds')
fundHIMN <- readRDS('./data/fundHIMN.rds')
fundMNMN <- readRDS('./data/fundMNMN.rds')
fundLOMN <- readRDS('./data/fundLOMN.rds')
fundCLMN <- readRDS('./data/fundCLMN.rds')
fundOPLO <- readRDS('./data/fundOPLO.rds')
fundHILO <- readRDS('./data/fundHILO.rds')
fundMNLO <- readRDS('./data/fundMNLO.rds')
fundLOLO <- readRDS('./data/fundLOLO.rds')
fundCLLO <- readRDS('./data/fundCLLO.rds')
fundOPCL <- readRDS('./data/fundOPCL.rds')
fundHICL <- readRDS('./data/fundHICL.rds')
fundMNCL <- readRDS('./data/fundMNCL.rds')
fundLOCL <- readRDS('./data/fundLOCL.rds')
fundCLCL <- readRDS('./data/fundCLCL.rds')
## Placed orders - Fund size without log
fundList <- list(fundOPHI = fundOPHI, fundHIHI = fundHIHI, fundMNHI = fundMNHI, fundLOHI = fundLOHI, fundCLHI = fundCLHI,
fundOPMN = fundOPMN, fundHIMN = fundHIMN, fundMNMN = fundMNMN, fundLOMN = fundLOMN, fundCLMN = fundCLMN,
fundOPLO = fundOPLO, fundHILO = fundHILO, fundMNLO = fundMNLO, fundLOLO = fundLOLO, fundCLLO = fundCLLO,
fundOPCL = fundOPCL, fundHICL = fundHICL, fundMNCL = fundMNCL, fundLOCL = fundLOCL, fundCLCL = fundCLCL)
```
```{r simStaking-withLog, eval = FALSE, warning = FALSE, include = FALSE}
##============================ EVAL = FALSE ================================
##
## Leveraged model 2
##
## Placed orders - Fund size with log
fundOPHI <- simStakesETS(mbase, .prCat = 'Op', .setPrice = 'Hi', .initialFundSize = log(1000))
fundHIHI <- simStakesETS(mbase, .prCat = 'Hi', .setPrice = 'Hi', .initialFundSize = log(1000))
fundMNHI <- simStakesETS(mbase, .prCat = 'Mn', .setPrice = 'Hi', .initialFundSize = log(1000))
fundLOHI <- simStakesETS(mbase, .prCat = 'Lo', .setPrice = 'Hi', .initialFundSize = log(1000))
fundCLHI <- simStakesETS(mbase, .prCat = 'Cl', .setPrice = 'Hi', .initialFundSize = log(1000))
fundOPMN <- simStakesETS(mbase, .prCat = 'Op', .setPrice = 'Mn', .initialFundSize = log(1000))
fundHIMN <- simStakesETS(mbase, .prCat = 'Hi', .setPrice = 'Mn', .initialFundSize = log(1000))
fundMNMN <- simStakesETS(mbase, .prCat = 'Mn', .setPrice = 'Mn', .initialFundSize = log(1000))
fundLOMN <- simStakesETS(mbase, .prCat = 'Lo', .setPrice = 'Mn', .initialFundSize = log(1000))
fundCLMN <- simStakesETS(mbase, .prCat = 'Cl', .setPrice = 'Mn', .initialFundSize = log(1000))
fundOPLO <- simStakesETS(mbase, .prCat = 'Op', .setPrice = 'Lo', .initialFundSize = log(1000))
fundHILO <- simStakesETS(mbase, .prCat = 'Hi', .setPrice = 'Lo', .initialFundSize = log(1000))
fundMNLO <- simStakesETS(mbase, .prCat = 'Mn', .setPrice = 'Lo', .initialFundSize = log(1000))
fundLOLO <- simStakesETS(mbase, .prCat = 'Lo', .setPrice = 'Lo', .initialFundSize = log(1000))
fundCLLO <- simStakesETS(mbase, .prCat = 'Cl', .setPrice = 'Lo', .initialFundSize = log(1000))
fundOPCL <- simStakesETS(mbase, .prCat = 'Op', .setPrice = 'Cl', .initialFundSize = log(1000))
fundHICL <- simStakesETS(mbase, .prCat = 'Hi', .setPrice = 'Cl', .initialFundSize = log(1000))
fundMNCL <- simStakesETS(mbase, .prCat = 'Mn', .setPrice = 'Cl', .initialFundSize = log(1000))
fundLOCL <- simStakesETS(mbase, .prCat = 'Lo', .setPrice = 'Cl', .initialFundSize = log(1000))
fundCLCL <- simStakesETS(mbase, .prCat = 'Cl', .setPrice = 'Cl', .initialFundSize = log(1000))
## Placed orders - Fund size with log
#'@ fundList <- list(fundOPHI = fundOPHI, fundHIHI = fundHIHI, fundMNHI = fundMNHI, fundLOHI = fundLOHI, fundCLHI = fundCLHI,
#'@ fundOPMN = fundOPMN, fundHIMN = fundHIMN, fundMNMN = fundMNMN, fundLOMN = fundLOMN, fundCLMN = fundCLMN,
#'@ fundOPLO = fundOPLO, fundHILO = fundHILO, fundMNLO = fundMNLO, fundLOLO = fundLOLO, fundCLLO = fundCLLO,
#'@ fundOPCL = fundOPCL, fundHICL = fundHICL, fundMNCL = fundMNCL, fundLOCL = fundLOCL, fundCLCL = fundCLCL)
#'@
#'@ ldply(fundList, function(x) { x %>% mutate(StartDate = first(Date), LatestDate = last(Date), InitFund = first(BR), LatestFund = last(Bal), Profit = sum(Profit), RR = LatestFund/InitFund) %>% dplyr::select(StartDate, LatestDate, InitFund, LatestFund, Profit, RR) %>% unique }) %>% tbl_df
## A tibble: 20 × 7
# .id StartDate LatestDate InitFund LatestFund Profit RR