#==============================================================================================
# QUESTION 1: 10 Industry Portfolio Returns (average value-weighted monthly returns)
#==============================================================================================
industry10 <- read.table(file = "D:/(1) ADRIN/10_Industry_Portfolios.txt", header = TRUE)
str(industry10)
## 'data.frame': 1136 obs. of 11 variables:
## $ Date : int 192607 192608 192609 192610 192611 192612 192701 192702 192703 192704 ...
## $ NoDur: num 1.45 3.97 1.14 -1.24 5.2 0.82 -0.67 3.37 2.73 3.35 ...
## $ Durbl: num 15.55 3.68 4.8 -8.23 -0.19 ...
## $ Manuf: num 4.69 2.81 1.15 -3.63 4.1 3.74 -0.08 5.81 1.43 0.77 ...
## $ Enrgy: num -1.18 3.47 -3.39 -0.78 0.01 2.82 1.29 1.47 -6.01 -5.17 ...
## $ HiTec: num 2.9 2.66 -0.38 -4.58 4.71 -0.02 -1.13 4.45 1.45 5.4 ...
## $ Telcm: num 0.83 2.17 2.41 -0.11 1.63 1.99 1.88 3.97 5.56 -2.13 ...
## $ Shops: num 0.11 -0.71 0.21 -2.29 6.43 0.62 -2.55 3.61 -0.41 4.46 ...
## $ Hlth : num 1.77 4.25 0.69 -0.57 5.42 0.11 5.05 1.71 1.01 2.74 ...
## $ Utils: num 7.04 -1.69 2.04 -2.63 3.71 -0.17 -1.79 4.53 0.37 1.71 ...
## $ Other: num 2.16 4.38 0.29 -2.85 2.11 3.4 1.51 5.06 1.27 0.86 ...
#==============================================================================================
library(SIT)
## Loading required package: SIT.date
## Loading required package: quantmod
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
##
## Attaching package: 'SIT'
## The following object is masked from 'package:TTR':
##
## DVI
## The following object is masked from 'package:base':
##
## close
library(pacman)
p_load(quantmod, quadprog,lpSolve)
p_load(xts)
p_load(TTR)
#==============================================================================================
date <- seq(as.Date('1926-08-01'), length = 1136, by = '1 month') - 1
tail(date)
## [1] "2020-09-30" "2020-10-31" "2020-11-30" "2020-12-31" "2021-01-31"
## [6] "2021-02-28"
industry10.xts <- xts(coredata(industry10[, -1])/100, order.by = date)
head(industry10.xts)
## NoDur Durbl Manuf Enrgy HiTec Telcm Shops Hlth
## 1926-07-31 0.0145 0.1555 0.0469 -0.0118 0.0290 0.0083 0.0011 0.0177
## 1926-08-31 0.0397 0.0368 0.0281 0.0347 0.0266 0.0217 -0.0071 0.0425
## 1926-09-30 0.0114 0.0480 0.0115 -0.0339 -0.0038 0.0241 0.0021 0.0069
## 1926-10-31 -0.0124 -0.0823 -0.0363 -0.0078 -0.0458 -0.0011 -0.0229 -0.0057
## 1926-11-30 0.0520 -0.0019 0.0410 0.0001 0.0471 0.0163 0.0643 0.0542
## 1926-12-31 0.0082 0.0989 0.0374 0.0282 -0.0002 0.0199 0.0062 0.0011
## Utils Other
## 1926-07-31 0.0704 0.0216
## 1926-08-31 -0.0169 0.0438
## 1926-09-30 0.0204 0.0029
## 1926-10-31 -0.0263 -0.0285
## 1926-11-30 0.0371 0.0211
## 1926-12-31 -0.0017 0.0340
#==============================================================================================
# QUESTION 2: Equal Weight Portfolio Returns EACH Month Starting From 2000/01 to 2020/03.
# Denote this strategy as the Benchmark portfolio and create its backtesting report using SIT.
#==============================================================================================
# Converting into Prices
industry10.price <- cumprod(industry10.xts+1)*100
head(industry10.price)
## NoDur Durbl Manuf Enrgy HiTec Telcm Shops
## 1926-07-31 101.4500 115.5500 104.6900 98.82000 102.9000 100.8300 100.11000
## 1926-08-31 105.4776 119.8022 107.6318 102.24905 105.6371 103.0180 99.39922
## 1926-09-30 106.6800 125.5527 108.8696 98.78281 105.2357 105.5007 99.60796
## 1926-10-31 105.3572 115.2198 104.9176 98.01231 100.4159 105.3847 97.32694
## 1926-11-30 110.8358 115.0008 109.2192 98.02211 105.1455 107.1025 103.58506
## 1926-12-31 111.7446 126.3744 113.3040 100.78633 105.1245 109.2338 104.22728
## Hlth Utils Other
## 1926-07-31 101.7700 107.0400 102.1600
## 1926-08-31 106.0952 105.2310 106.6346
## 1926-09-30 106.8273 107.3777 106.9438
## 1926-10-31 106.2184 104.5537 103.8959
## 1926-11-30 111.9754 108.4326 106.0882
## 1926-12-31 112.0986 108.2483 109.6952
tail(industry10.price)
## NoDur Durbl Manuf Enrgy HiTec Telcm Shops Hlth
## 2020-09-30 1504265 1194217 1099803 413827.4 1963845 455588.0 1723666 3514315
## 2020-10-31 1465004 1156360 1091334 395081.0 1926925 437820.0 1680057 3363551
## 2020-11-30 1611797 1549407 1242702 508074.2 2139272 501041.3 1821014 3687461
## 2020-12-31 1690775 1788480 1274143 539574.8 2246235 527546.3 1848147 3860771
## 2021-01-31 1621115 1998627 1243818 565096.6 2257242 509768.0 1849440 3970803
## 2021-02-28 1644297 1837337 1311358 696594.6 2297646 532452.7 1820959 3927125
## Utils Other
## 2020-09-30 323657.1 268617.8
## 2020-10-31 338189.3 263164.8
## 2020-11-30 347083.7 304455.4
## 2020-12-31 349270.3 320287.1
## 2021-01-31 347873.3 311255.0
## 2021-02-28 331871.1 340917.6
industry10.price.sample <- industry10.price['199912/202003']
# Three Inputs Needed for Backtesting:
# 1. prices; 2. weight; 3. execution.price
#==============================================================================================
data <- new.env()
data$prices <- industry10.price.sample
data$weight <- industry10.price.sample
data$execution.price <- industry10.price.sample
data$execution.price[] <- NA
data$symbolnames <- colnames(data$prices)
prices <- data$prices
n <- ncol(prices)
# Equal Weight
data$weight <- ntop(prices, n)
# Backtesting
#==============================================================================================
model <- list()
model$equal.weight <- bt.run(data, trade.summary = T)
## Latest weights :
## NoDur Durbl Manuf Enrgy HiTec Telcm Shops Hlth Utils Other
## 2020-03-31 10 10 10 10 10 10 10 10 10 10
##
## Performance summary :
## CAGR Best Worst
## 6.2 12 -17.5
names(model$equal.weight)
## [1] "weight" "type" "ret" "best"
## [5] "worst" "equity" "cagr" "dates.index"
## [9] "trade.summary"
model$equal.weight$ret
## NoDur
## 1999-12-31 0.00000
## 2000-01-31 -0.02404
## 2000-02-29 -0.03021
## 2000-03-31 0.08384
## 2000-04-30 -0.00699
## 2000-05-31 -0.01062
## 2000-06-30 0.00540
## 2000-07-31 -0.00798
## 2000-08-31 0.05216
## 2000-09-30 -0.00498
## 2000-10-31 0.00111
## 2000-11-30 -0.05731
## 2000-12-31 0.03200
## 2001-01-31 0.02567
## 2001-02-28 -0.04508
## 2001-03-31 -0.04839
## 2001-04-30 0.06950
## 2001-05-31 0.00779
## 2001-06-30 -0.02341
## 2001-07-31 -0.00625
## 2001-08-31 -0.05124
## 2001-09-30 -0.08415
## 2001-10-31 0.02058
## 2001-11-30 0.06530
## 2001-12-31 0.02087
## 2002-01-31 -0.01077
## 2002-02-28 -0.00006
## 2002-03-31 0.04814
## 2002-04-30 -0.03922
## 2002-05-31 -0.01079
## 2002-06-30 -0.06948
## 2002-07-31 -0.09331
## 2002-08-31 0.00866
## 2002-09-30 -0.09801
## 2002-10-31 0.06694
## 2002-11-30 0.05996
## 2002-12-31 -0.04448
## 2003-01-31 -0.03034
## 2003-02-28 -0.02320
## 2003-03-31 0.00899
## 2003-04-30 0.07855
## 2003-05-31 0.06425
## 2003-06-30 0.01497
## 2003-07-31 0.01275
## 2003-08-31 0.03313
## 2003-09-30 -0.01560
## 2003-10-31 0.05560
## 2003-11-30 0.01509
## 2003-12-31 0.05929
## 2004-01-31 0.01287
## 2004-02-29 0.01821
## 2004-03-31 -0.00985
## 2004-04-30 -0.00568
## 2004-05-31 0.00333
## 2004-06-30 0.02419
## 2004-07-31 -0.03226
## 2004-08-31 0.00153
## 2004-09-30 0.01994
## 2004-10-31 0.01402
## 2004-11-30 0.04884
## 2004-12-31 0.03361
## 2005-01-31 -0.02142
## 2005-02-28 0.02903
## 2005-03-31 -0.01790
## 2005-04-30 -0.03003
## 2005-05-31 0.03807
## 2005-06-30 0.01217
## 2005-07-31 0.04349
## 2005-08-31 -0.00647
## 2005-09-30 0.00433
## 2005-10-31 -0.03037
## 2005-11-30 0.02924
## 2005-12-31 0.00114
## 2006-01-31 0.04013
## 2006-02-28 -0.00031
## 2006-03-31 0.01566
## 2006-04-30 0.01119
## 2006-05-31 -0.01763
## 2006-06-30 0.00783
## 2006-07-31 0.00331
## 2006-08-31 0.02144
## 2006-09-30 0.01808
## 2006-10-31 0.04064
## 2006-11-30 0.02268
## 2006-12-31 0.00844
## 2007-01-31 0.01954
## 2007-02-28 -0.01007
## 2007-03-31 0.01679
## 2007-04-30 0.04317
## 2007-05-31 0.03699
## 2007-06-30 -0.01224
## 2007-07-31 -0.03438
## 2007-08-31 0.00950
## 2007-09-30 0.03383
## 2007-10-31 0.02054
## 2007-11-30 -0.03925
## 2007-12-31 -0.00278
## 2008-01-31 -0.06152
## 2008-02-29 -0.02282
## 2008-03-31 -0.00596
## 2008-04-30 0.04770
## 2008-05-31 0.02713
## 2008-06-30 -0.07961
## 2008-07-31 -0.01304
## 2008-08-31 0.01914
## 2008-09-30 -0.09382
## 2008-10-31 -0.17461
## 2008-11-30 -0.06554
## 2008-12-31 0.01660
## 2009-01-31 -0.07092
## 2009-02-28 -0.09766
## 2009-03-31 0.08219
## 2009-04-30 0.12003
## 2009-05-31 0.04255
## 2009-06-30 0.00946
## 2009-07-31 0.08182
## 2009-08-31 0.02471
## 2009-09-30 0.04095
## 2009-10-31 -0.01792
## 2009-11-30 0.05904
## 2009-12-31 0.03364
## 2010-01-31 -0.03388
## 2010-02-28 0.03292
## 2010-03-31 0.06087
## 2010-04-30 0.02445
## 2010-05-31 -0.07546
## 2010-06-30 -0.05393
## 2010-07-31 0.07799
## 2010-08-31 -0.04104
## 2010-09-30 0.09344
## 2010-10-31 0.04232
## 2010-11-30 0.01177
## 2010-12-31 0.06367
## 2011-01-31 0.01448
## 2011-02-28 0.03514
## 2011-03-31 0.01216
## 2011-04-30 0.03363
## 2011-05-31 -0.00750
## 2011-06-30 -0.01401
## 2011-07-31 -0.02736
## 2011-08-31 -0.05467
## 2011-09-30 -0.07302
## 2011-10-31 0.11508
## 2011-11-30 -0.00375
## 2011-12-31 0.00949
## 2012-01-31 0.04405
## 2012-02-29 0.04164
## 2012-03-31 0.02425
## 2012-04-30 -0.00737
## 2012-05-31 -0.05274
## 2012-06-30 0.03186
## 2012-07-31 0.01452
## 2012-08-31 0.01875
## 2012-09-30 0.02871
## 2012-10-31 -0.00805
## 2012-11-30 0.00866
## 2012-12-31 0.01244
## 2013-01-31 0.05548
## 2013-02-28 0.01381
## 2013-03-31 0.04302
## 2013-04-30 0.02273
## 2013-05-31 0.01956
## 2013-06-30 -0.00724
## 2013-07-31 0.05643
## 2013-08-31 -0.02819
## 2013-09-30 0.03691
## 2013-10-31 0.04045
## 2013-11-30 0.02205
## 2013-12-31 0.02329
## 2014-01-31 -0.03248
## 2014-02-28 0.04877
## 2014-03-31 0.00610
## 2014-04-30 0.00838
## 2014-05-31 0.01821
## 2014-06-30 0.02736
## 2014-07-31 -0.02722
## 2014-08-31 0.04295
## 2014-09-30 -0.02884
## 2014-10-31 0.02588
## 2014-11-30 0.02234
## 2014-12-31 -0.00088
## 2015-01-31 -0.02484
## 2015-02-28 0.05390
## 2015-03-31 -0.01167
## 2015-04-30 0.00819
## 2015-05-31 0.00837
## 2015-06-30 -0.01880
## 2015-07-31 0.00838
## 2015-08-31 -0.05771
## 2015-09-30 -0.03076
## 2015-10-31 0.07484
## 2015-11-30 0.00111
## 2015-12-31 -0.02554
## 2016-01-31 -0.04644
## 2016-02-29 0.00612
## 2016-03-31 0.07154
## 2016-04-30 0.01420
## 2016-05-31 0.00999
## 2016-06-30 0.00935
## 2016-07-31 0.03279
## 2016-08-31 -0.00427
## 2016-09-30 0.00167
## 2016-10-31 -0.02465
## 2016-11-30 0.04058
## 2016-12-31 0.02259
## 2017-01-31 0.01693
## 2017-02-28 0.02872
## 2017-03-31 0.00221
## 2017-04-30 0.00833
## 2017-05-31 0.00613
## 2017-06-30 0.00517
## 2017-07-31 0.01710
## 2017-08-31 -0.00451
## 2017-09-30 0.02559
## 2017-10-31 0.01109
## 2017-11-30 0.03119
## 2017-12-31 0.01158
## 2018-01-31 0.04118
## 2018-02-28 -0.05093
## 2018-03-31 -0.01569
## 2018-04-30 0.00698
## 2018-05-31 0.01923
## 2018-06-30 0.01696
## 2018-07-31 0.02755
## 2018-08-31 0.02140
## 2018-09-30 0.00420
## 2018-10-31 -0.05908
## 2018-11-30 0.02392
## 2018-12-31 -0.09437
## 2019-01-31 0.08110
## 2019-02-28 0.03220
## 2019-03-31 0.00981
## 2019-04-30 0.03247
## 2019-05-31 -0.06745
## 2019-06-30 0.07249
## 2019-07-31 0.00626
## 2019-08-31 -0.01967
## 2019-09-30 0.02102
## 2019-10-31 0.01735
## 2019-11-30 0.02681
## 2019-12-31 0.03249
## 2020-01-31 -0.00562
## 2020-02-29 -0.08483
## 2020-03-31 -0.15370
model$equal.weight$equity
## NoDur
## 1999-12-31 1.0000000
## 2000-01-31 0.9759600
## 2000-02-29 0.9464762
## 2000-03-31 1.0258288
## 2000-04-30 1.0186583
## 2000-05-31 1.0078401
## 2000-06-30 1.0132825
## 2000-07-31 1.0051965
## 2000-08-31 1.0576275
## 2000-09-30 1.0523605
## 2000-10-31 1.0535286
## 2000-11-30 0.9931509
## 2000-12-31 1.0249318
## 2001-01-31 1.0512417
## 2001-02-28 1.0038518
## 2001-03-31 0.9552754
## 2001-04-30 1.0216670
## 2001-05-31 1.0296258
## 2001-06-30 1.0055223
## 2001-07-31 0.9992378
## 2001-08-31 0.9480368
## 2001-09-30 0.8682595
## 2001-10-31 0.8861283
## 2001-11-30 0.9439925
## 2001-12-31 0.9636936
## 2002-01-31 0.9533146
## 2002-02-28 0.9532574
## 2002-03-31 0.9991472
## 2002-04-30 0.9599607
## 2002-05-31 0.9496027
## 2002-06-30 0.8836243
## 2002-07-31 0.8011733
## 2002-08-31 0.8081115
## 2002-09-30 0.7289085
## 2002-10-31 0.7777016
## 2002-11-30 0.8243326
## 2002-12-31 0.7876663
## 2003-01-31 0.7637685
## 2003-02-28 0.7460491
## 2003-03-31 0.7527560
## 2003-04-30 0.8118850
## 2003-05-31 0.8640486
## 2003-06-30 0.8769834
## 2003-07-31 0.8881650
## 2003-08-31 0.9175899
## 2003-09-30 0.9032755
## 2003-10-31 0.9534976
## 2003-11-30 0.9678859
## 2003-12-31 1.0252718
## 2004-01-31 1.0384671
## 2004-02-29 1.0573776
## 2004-03-31 1.0469624
## 2004-04-30 1.0410157
## 2004-05-31 1.0444822
## 2004-06-30 1.0697483
## 2004-07-31 1.0352382
## 2004-08-31 1.0368221
## 2004-09-30 1.0574963
## 2004-10-31 1.0723224
## 2004-11-30 1.1246947
## 2004-12-31 1.1624956
## 2005-01-31 1.1375950
## 2005-02-28 1.1706194
## 2005-03-31 1.1496653
## 2005-04-30 1.1151408
## 2005-05-31 1.1575943
## 2005-06-30 1.1716822
## 2005-07-31 1.2226386
## 2005-08-31 1.2147282
## 2005-09-30 1.2199879
## 2005-10-31 1.1829369
## 2005-11-30 1.2175260
## 2005-12-31 1.2189140
## 2006-01-31 1.2678290
## 2006-02-28 1.2674359
## 2006-03-31 1.2872840
## 2006-04-30 1.3016887
## 2006-05-31 1.2787399
## 2006-06-30 1.2887525
## 2006-07-31 1.2930182
## 2006-08-31 1.3207405
## 2006-09-30 1.3446195
## 2006-10-31 1.3992649
## 2006-11-30 1.4310002
## 2006-12-31 1.4430778
## 2007-01-31 1.4712756
## 2007-02-28 1.4564598
## 2007-03-31 1.4809138
## 2007-04-30 1.5448448
## 2007-05-31 1.6019887
## 2007-06-30 1.5823803
## 2007-07-31 1.5279781
## 2007-08-31 1.5424939
## 2007-09-30 1.5946764
## 2007-10-31 1.6274311
## 2007-11-30 1.5635544
## 2007-12-31 1.5592077
## 2008-01-31 1.4632853
## 2008-02-29 1.4298931
## 2008-03-31 1.4213709
## 2008-04-30 1.4891703
## 2008-05-31 1.5295715
## 2008-06-30 1.4078023
## 2008-07-31 1.3894446
## 2008-08-31 1.4160386
## 2008-09-30 1.2831858
## 2008-10-31 1.0591288
## 2008-11-30 0.9897135
## 2008-12-31 1.0061427
## 2009-01-31 0.9347871
## 2009-02-28 0.8434958
## 2009-03-31 0.9128227
## 2009-04-30 1.0223888
## 2009-05-31 1.0658914
## 2009-06-30 1.0759747
## 2009-07-31 1.1640110
## 2009-08-31 1.1927737
## 2009-09-30 1.2416178
## 2009-10-31 1.2193680
## 2009-11-30 1.2913595
## 2009-12-31 1.3348008
## 2010-01-31 1.2895778
## 2010-02-28 1.3320307
## 2010-03-31 1.4131114
## 2010-04-30 1.4476620
## 2010-05-31 1.3384214
## 2010-06-30 1.2662403
## 2010-07-31 1.3649944
## 2010-08-31 1.3089750
## 2010-09-30 1.4312857
## 2010-10-31 1.4918577
## 2010-11-30 1.5094168
## 2010-12-31 1.6055214
## 2011-01-31 1.6287694
## 2011-02-28 1.6860043
## 2011-03-31 1.7065061
## 2011-04-30 1.7638959
## 2011-05-31 1.7506667
## 2011-06-30 1.7261399
## 2011-07-31 1.6789127
## 2011-08-31 1.5871265
## 2011-09-30 1.4712345
## 2011-10-31 1.6405442
## 2011-11-30 1.6343922
## 2011-12-31 1.6499026
## 2012-01-31 1.7225808
## 2012-02-29 1.7943090
## 2012-03-31 1.8378210
## 2012-04-30 1.8242763
## 2012-05-31 1.7280639
## 2012-06-30 1.7831201
## 2012-07-31 1.8090110
## 2012-08-31 1.8429299
## 2012-09-30 1.8958404
## 2012-10-31 1.8805789
## 2012-11-30 1.8968647
## 2012-12-31 1.9204617
## 2013-01-31 2.0270089
## 2013-02-28 2.0550019
## 2013-03-31 2.1434081
## 2013-04-30 2.1921278
## 2013-05-31 2.2350058
## 2013-06-30 2.2188244
## 2013-07-31 2.3440326
## 2013-08-31 2.2779543
## 2013-09-30 2.3620336
## 2013-10-31 2.4575779
## 2013-11-30 2.5117675
## 2013-12-31 2.5702666
## 2014-01-31 2.4867843
## 2014-02-28 2.6080648
## 2014-03-31 2.6239740
## 2014-04-30 2.6459629
## 2014-05-31 2.6941459
## 2014-06-30 2.7678577
## 2014-07-31 2.6925166
## 2014-08-31 2.8081602
## 2014-09-30 2.7271728
## 2014-10-31 2.7977521
## 2014-11-30 2.8602539
## 2014-12-31 2.8577368
## 2015-01-31 2.7867507
## 2015-02-28 2.9369565
## 2015-03-31 2.9026822
## 2015-04-30 2.9264552
## 2015-05-31 2.9509496
## 2015-06-30 2.8954718
## 2015-07-31 2.9197358
## 2015-08-31 2.7512379
## 2015-09-30 2.6666098
## 2015-10-31 2.8661789
## 2015-11-30 2.8693603
## 2015-12-31 2.7960769
## 2016-01-31 2.6662271
## 2016-02-29 2.6825444
## 2016-03-31 2.8744536
## 2016-04-30 2.9152708
## 2016-05-31 2.9443944
## 2016-06-30 2.9719245
## 2016-07-31 3.0693739
## 2016-08-31 3.0562677
## 2016-09-30 3.0613716
## 2016-10-31 2.9859088
## 2016-11-30 3.1070770
## 2016-12-31 3.1772659
## 2017-01-31 3.2310570
## 2017-02-28 3.3238529
## 2017-03-31 3.3311986
## 2017-04-30 3.3589475
## 2017-05-31 3.3795379
## 2017-06-30 3.3970101
## 2017-07-31 3.4550990
## 2017-08-31 3.4395165
## 2017-09-30 3.5275337
## 2017-10-31 3.5666540
## 2017-11-30 3.6778980
## 2017-12-31 3.7204880
## 2018-01-31 3.8736977
## 2018-02-28 3.6764103
## 2018-03-31 3.6187274
## 2018-04-30 3.6439862
## 2018-05-31 3.7140600
## 2018-06-30 3.7770505
## 2018-07-31 3.8811082
## 2018-08-31 3.9641639
## 2018-09-30 3.9808134
## 2018-10-31 3.7456270
## 2018-11-30 3.8352224
## 2018-12-31 3.4732924
## 2019-01-31 3.7549764
## 2019-02-28 3.8758867
## 2019-03-31 3.9139091
## 2019-04-30 4.0409938
## 2019-05-31 3.7684287
## 2019-06-30 4.0416021
## 2019-07-31 4.0669025
## 2019-08-31 3.9869066
## 2019-09-30 4.0707114
## 2019-10-31 4.1413382
## 2019-11-30 4.2523675
## 2019-12-31 4.3905269
## 2020-01-31 4.3658521
## 2020-02-29 3.9954969
## 2020-03-31 3.3813890
# Trade Summary Report
#==============================================================================================
bt.detail.summary(model$equal.weight)
## $System
## $System$Period
## [1] "Dec1999 - Mar2020"
##
## $System$Cagr
## [1] 6.2
##
## $System$Sharpe
## [1] 0.49
##
## $System$DVR
## [,1]
## NoDur 0.42
##
## $System$Volatility
## [1] 14.5
##
## $System$MaxDD
## [1] -48.17
##
## $System$AvgDD
## [1] -7.02
##
## $System$VaR
## 5%
## -7.07
##
## $System$CVaR
## [1] -9.8
##
## $System$Exposure
## [1] 99.59
##
##
## $Trade
## $Trade$Win.Percent
## [1] 100
##
## $Trade$Avg.Trade
## [1] 23.2
##
## $Trade$Avg.Win
## [1] 23.2
##
## $Trade$Avg.Loss
## [1] NaN
##
## $Trade$Best.Trade
## [1] 44.02
##
## $Trade$Worst.Trade
## [1] 2.83
##
## $Trade$WinLoss.Ratio
## [1] NaN
##
## $Trade$Avg.Len
## [1] 243
##
## $Trade$Num.Trades
## [1] 10
##
##
## $Period
## $Period$Win.Percent.Day
## [1] 63.5
##
## $Period$Best.Day
## [1] 12
##
## $Period$Worst.Day
## [1] -17.5
##
## $Period$Win.Percent.Month
## [1] 63.5
##
## $Period$Best.Month
## [1] 12
##
## $Period$Worst.Month
## [1] -17.5
##
## $Period$Win.Percent.Year
## [1] 68.2
##
## $Period$Best.Year
## [1] 33.8
##
## $Period$Worst.Year
## [1] -35.5
plotbt.monthly.table(model$equal.weight$equity)

## Jan Feb Mar Apr May Jun Jul Aug Sep
## 1999 " NA" " NA" " NA" " NA" " NA" " NA" " NA" " NA" " NA"
## 2000 " -2.4" " -3.0" " 8.4" " -0.7" " -1.1" " 0.5" " -0.8" " 5.2" " -0.5"
## 2001 " 2.6" " -4.5" " -4.8" " 6.9" " 0.8" " -2.3" " -0.6" " -5.1" " -8.4"
## 2002 " -1.1" " 0.0" " 4.8" " -3.9" " -1.1" " -6.9" " -9.3" " 0.9" " -9.8"
## 2003 " -3.0" " -2.3" " 0.9" " 7.9" " 6.4" " 1.5" " 1.3" " 3.3" " -1.6"
## 2004 " 1.3" " 1.8" " -1.0" " -0.6" " 0.3" " 2.4" " -3.2" " 0.2" " 2.0"
## 2005 " -2.1" " 2.9" " -1.8" " -3.0" " 3.8" " 1.2" " 4.3" " -0.6" " 0.4"
## 2006 " 4.0" " 0.0" " 1.6" " 1.1" " -1.8" " 0.8" " 0.3" " 2.1" " 1.8"
## 2007 " 2.0" " -1.0" " 1.7" " 4.3" " 3.7" " -1.2" " -3.4" " 1.0" " 3.4"
## 2008 " -6.2" " -2.3" " -0.6" " 4.8" " 2.7" " -8.0" " -1.3" " 1.9" " -9.4"
## 2009 " -7.1" " -9.8" " 8.2" " 12.0" " 4.3" " 0.9" " 8.2" " 2.5" " 4.1"
## 2010 " -3.4" " 3.3" " 6.1" " 2.4" " -7.5" " -5.4" " 7.8" " -4.1" " 9.3"
## 2011 " 1.4" " 3.5" " 1.2" " 3.4" " -0.8" " -1.4" " -2.7" " -5.5" " -7.3"
## 2012 " 4.4" " 4.2" " 2.4" " -0.7" " -5.3" " 3.2" " 1.5" " 1.9" " 2.9"
## 2013 " 5.5" " 1.4" " 4.3" " 2.3" " 2.0" " -0.7" " 5.6" " -2.8" " 3.7"
## 2014 " -3.2" " 4.9" " 0.6" " 0.8" " 1.8" " 2.7" " -2.7" " 4.3" " -2.9"
## 2015 " -2.5" " 5.4" " -1.2" " 0.8" " 0.8" " -1.9" " 0.8" " -5.8" " -3.1"
## 2016 " -4.6" " 0.6" " 7.2" " 1.4" " 1.0" " 0.9" " 3.3" " -0.4" " 0.2"
## 2017 " 1.7" " 2.9" " 0.2" " 0.8" " 0.6" " 0.5" " 1.7" " -0.5" " 2.6"
## 2018 " 4.1" " -5.1" " -1.6" " 0.7" " 1.9" " 1.7" " 2.8" " 2.1" " 0.4"
## 2019 " 8.1" " 3.2" " 1.0" " 3.2" " -6.7" " 7.2" " 0.6" " -2.0" " 2.1"
## 2020 " -0.6" " -8.5" "-15.4" " NA" " NA" " NA" " NA" " NA" " NA"
## Avg " -0.1" " -0.1" " 1.1" " 2.2" " 0.3" " -0.2" " 0.7" " -0.1" " -0.5"
## Oct Nov Dec Year MaxDD
## 1999 " NA" " NA" " NA" " 0.0" " 0.0"
## 2000 " 0.1" " -5.7" " 3.2" " 2.5" " -6.1"
## 2001 " 2.1" " 6.5" " 2.1" " -6.0" "-17.4"
## 2002 " 6.7" " 6.0" " -4.4" "-18.3" "-27.0"
## 2003 " 5.6" " 1.5" " 5.9" " 30.2" " -5.3"
## 2004 " 1.4" " 4.9" " 3.4" " 13.4" " -3.2"
## 2005 " -3.0" " 2.9" " 0.1" " 4.9" " -4.7"
## 2006 " 4.1" " 2.3" " 0.8" " 18.4" " -1.8"
## 2007 " 2.1" " -3.9" " -0.3" " 8.0" " -4.6"
## 2008 "-17.5" " -6.6" " 1.7" "-35.5" "-36.5"
## 2009 " -1.8" " 5.9" " 3.4" " 32.7" "-16.2"
## 2010 " 4.2" " 1.2" " 6.4" " 20.3" "-12.5"
## 2011 " 11.5" " -0.4" " 0.9" " 2.8" "-16.6"
## 2012 " -0.8" " 0.9" " 1.2" " 16.4" " -6.0"
## 2013 " 4.0" " 2.2" " 2.3" " 33.8" " -2.8"
## 2014 " 2.6" " 2.2" " -0.1" " 11.2" " -3.2"
## 2015 " 7.5" " 0.1" " -2.6" " -2.2" " -9.6"
## 2016 " -2.5" " 4.1" " 2.3" " 13.6" " -4.6"
## 2017 " 1.1" " 3.1" " 1.2" " 17.1" " -0.5"
## 2018 " -5.9" " 2.4" " -9.4" " -6.6" "-12.7"
## 2019 " 1.7" " 2.7" " 3.2" " 26.4" " -6.7"
## 2020 " NA" " NA" " NA" "-23.0" "-23.0"
## Avg " 1.2" " 1.6" " 1.1" " 7.3" "-10.1"
plotbt.transition.map(model$equal.weight$weight)

strategy.performance.snapshoot(model, T)

## NULL
## NULL
#==============================================================================================
# QUESTION 3: Compute MVP portfolio returns by rebalancing EACH month starting from 2000/01 to 2020/03.
# Use in-sample data range of 36 months to compute covariance matrix. Denote this strategy
# as the MVP portfolio and create its backtesting report using SIT.
#==============================================================================================
industry10.price.sample2 <- industry10.price['199701/202003']
head(industry10.price.sample2)
## NoDur Durbl Manuf Enrgy HiTec Telcm Shops
## 1997-01-31 202589.8 191273.4 125768.1 165710.3 186237.0 101825.93 121840.3
## 1997-02-28 210875.7 191942.9 126786.8 156281.4 172381.0 104147.56 125593.0
## 1997-03-31 200078.8 184380.3 122045.0 164423.7 162813.9 96232.34 123972.9
## 1997-04-30 210462.9 188639.5 128562.2 164505.9 176083.2 98676.65 126712.7
## 1997-05-31 222859.2 201221.8 138602.9 176564.2 193938.0 105228.78 134366.1
## 1997-06-30 230726.1 209069.4 145824.1 178877.1 196536.8 109206.42 141111.3
## Hlth Utils Other
## 1997-01-31 379713.9 45784.85 59247.35
## 1997-02-28 383283.2 45523.88 60787.78
## 1997-03-31 354920.2 44267.42 57207.38
## 1997-04-30 376783.3 43652.10 60199.33
## 1997-05-31 402819.0 45297.79 63955.77
## 1997-06-30 438629.7 46683.90 67390.19
data$prices <- industry10.price.sample2
data$weight <- industry10.price.sample2
data$execution.price <- industry10.price.sample2
data$execution.price[] <- NA
prices2 <- data$prices
# Creating Constraints
#==============================================================================================
constraints = new.constraints(n, lb = -Inf, ub = +Inf)
# SUM x.i = 1
constraints = add.constraints(rep(1, n), 1, type = '=', constraints)
ret = prices2 / mlag(prices) - 1
weight = coredata(prices2)
weight[] = NA
#Historical Inputs
#==============================================================================================
historical <- na.omit(ret[1:36,])
cov(historical)
## NoDur Durbl Manuf Enrgy HiTec
## NoDur 0.0016404657 0.0007069172 0.0013045727 0.0012482727 0.0002766129
## Durbl 0.0007069172 0.0065133071 0.0030994922 0.0015503356 0.0052892281
## Manuf 0.0013045727 0.0030994922 0.0029752026 0.0019617135 0.0040590777
## Enrgy 0.0012482727 0.0015503356 0.0019617135 0.0033391237 0.0017921274
## HiTec 0.0002766129 0.0052892281 0.0040590777 0.0017921274 0.0168679085
## Telcm 0.0008490343 0.0032259138 0.0020862924 0.0012376727 0.0073248547
## Shops 0.0013868583 0.0030578080 0.0023807928 0.0017300386 0.0037599481
## Hlth 0.0007697315 0.0003594804 0.0009048168 0.0008885056 0.0018035585
## Utils 0.0010967613 0.0015042943 0.0013647130 0.0026571277 -0.0002615759
## Other 0.0013518406 0.0028319887 0.0024663729 0.0023239853 0.0037939938
## Telcm Shops Hlth Utils Other
## NoDur 0.0008490343 0.0013868583 0.0007697315 0.0010967613 0.0013518406
## Durbl 0.0032259138 0.0030578080 0.0003594804 0.0015042943 0.0028319887
## Manuf 0.0020862924 0.0023807928 0.0009048168 0.0013647130 0.0024663729
## Enrgy 0.0012376727 0.0017300386 0.0008885056 0.0026571277 0.0023239853
## HiTec 0.0073248547 0.0037599481 0.0018035585 -0.0002615759 0.0037939938
## Telcm 0.0065815573 0.0027416122 0.0012498068 -0.0001640651 0.0025273785
## Shops 0.0027416122 0.0032279802 0.0004610245 0.0009212748 0.0024406072
## Hlth 0.0012498068 0.0004610245 0.0023774438 0.0012219342 0.0009128341
## Utils -0.0001640651 0.0009212748 0.0012219342 0.0038002946 0.0018514697
## Other 0.0025273785 0.0024406072 0.0009128341 0.0018514697 0.0031256521
ia = create.historical.ia(historical,12)
s0 = apply(coredata(historical), 2 , sd)
ia$cov = cor(coredata(historical), use='complete.obs',method='pearson') * (s0 %*% t(s0))
# Minimum Variance
#==============================================================================================
weight[36,] = min.risk.portfolio(ia, constraints)
weight[36,]
## NoDur Durbl Manuf Enrgy HiTec Telcm
## 0.678593028 0.206524966 -0.209443810 0.168419497 0.034940313 -0.006751355
## Shops Hlth Utils Other
## 0.014619785 0.323925805 -0.019410847 -0.191417381
sum(weight[36,])
## [1] 1
model$min.var.monthly <- bt.run(data, trade.summary = T)
## Latest weights :
## NoDur Durbl Manuf Enrgy HiTec Telcm Shops
## 2020-03-31 140741374 65892460 98742702 59615281 146869796 43271974 127551579
## Hlth Utils Other
## 2020-03-31 304006133 34423075 26733504
##
## Performance summary :
## CAGR Best Worst
## -100 0 -100
# data$weight[] = weight
# capital = 100000
# data$weight[] = (capital / prices) * data$weight
sum(as.numeric(weight[36,])*as.numeric(ret[37,]))
## [1] -0.01512735
model$min.var.monthly$ret[37, ]
## NoDur
## 2000-01-31 -1
#==============================================================================================
# QUESTION 4: Plot both strategies side by side and compare their performance and comment.
#==============================================================================================
plotbt.custom.report.part1(model$min.var.monthly, model$equal.weight)
## Warning in xy.coords(x, y, xlabel, ylabel, log): 278 y values <= 0 omitted from
## logarithmic plot
## Warning in plot.window(...): nonfinite axis limits [GScale(-inf,0.642517,2, .);
## log=1]

# Ploting Monthly MVP and Equal Weight Portfolio Transition Maps
layout(1:2)
plotbt.transition.map(model$min.var.monthly$weight)
legend('topright', legend = 'min.var.monthly', bty = 'n')
plotbt.transition.map(model$equal.weight$weight)
legend('topright', legend = 'equal weight', bty = 'n')

strategy.performance.snapshoot(model, T)
## Warning in plot.window(...): nonfinite axis limits [GScale(-inf,0.642517,2, .);
## log=1]
## Warning in max(mret, na.rm = T): no non-missing arguments to max; returning -Inf
## Warning in min(mret, na.rm = T): no non-missing arguments to min; returning Inf

## NULL
# Comment
#==============================================================================================
# Based on article https://www.cxoadvisory.com/strategic-allocation/mean-variance-optimization-versus-equal-weight/
# where Massimiliano Caporin and Loriana Pelizzon investigating the conditions under which mean-variance (including global minimum variance) optimized portfolios outperform an
# equal-weight portfolio.One of the results is:
# Over the entire sample period, some mean-variance optimization strategies outperform equal weighting, but outperformance depends on the asset set, the optimization model applied
# and the return/volatility forecast window size. Optimization strategies work best when the return/volatility forecast method includes detection of mean reversion in asset returns.