install.packages('curl', repos = 'http://cran.r-project.org')
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
devtools::install_github('systematicinvestor/SIT.date')
## Skipping install of 'SIT.date' from a github remote, the SHA1 (6263da60) has not changed since last install.
## Use `force = TRUE` to force installation
library(curl)
## Using libcurl 7.68.0 with GnuTLS/3.6.13
curl_download('https://github.com/systematicinvestor/SIT/raw/master/SIT.tar.gz', 'sit',mode = 'wb',quiet=T)
install.packages('sit', repos = NULL, type='source')
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
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
rm(list = ls())
library(devtools)
## Loading required package: usethis
etf3 <- read.table(file = "etf3_tw_2.csv",
fileEncoding = "UTF-8",
sep = ",", header = T)
#
head(etf3)
## id name date close
## 1 50 元大台灣50 20080102 34.8641
## 2 52 富邦科技 20080102 22.7109
## 3 56 元大高股息 20080102 10.7916
## 4 50 元大台灣50 20080103 34.2840
## 5 52 富邦科技 20080103 21.8471
## 6 56 元大高股息 20080103 10.6450
str(etf3)
## 'data.frame': 12045 obs. of 4 variables:
## $ id : int 50 52 56 50 52 56 50 52 56 50 ...
## $ name : chr "元大台灣50" "富邦科技" "元大高股息" "元大台灣50" ...
## $ date : int 20080102 20080102 20080102 20080103 20080103 20080103 20080104 20080104 20080104 20080107 ...
## $ close: num 34.9 22.7 10.8 34.3 21.8 ...
#
etf3 <- etf3[, -2]
colnames(etf3) <- c('id', 'date', 'price')
head(etf3)
## id date price
## 1 50 20080102 34.8641
## 2 52 20080102 22.7109
## 3 56 20080102 10.7916
## 4 50 20080103 34.2840
## 5 52 20080103 21.8471
## 6 56 20080103 10.6450
etf3$id <- as.factor(etf3$id)
str(etf3)
## 'data.frame': 12045 obs. of 3 variables:
## $ id : Factor w/ 3 levels "50","52","56": 1 2 3 1 2 3 1 2 3 1 ...
## $ date : int 20080102 20080102 20080102 20080103 20080103 20080103 20080104 20080104 20080104 20080107 ...
## $ price: num 34.9 22.7 10.8 34.3 21.8 ...
#
library(pacman)
p_load(reshape2, xts, quantmod)
etf3.l <- dcast(etf3, date~id)
## Using price as value column: use value.var to override.
head(etf3.l)
## date 50 52 56
## 1 20080102 34.8641 22.7109 10.7916
## 2 20080103 34.2840 21.8471 10.6450
## 3 20080104 34.2840 21.7356 10.6659
## 4 20080107 32.7177 20.2308 10.4983
## 5 20080108 33.0368 20.2308 10.6282
## 6 20080109 33.6459 20.3423 10.7120
str(etf3.l)
## 'data.frame': 4015 obs. of 4 variables:
## $ date: int 20080102 20080103 20080104 20080107 20080108 20080109 20080110 20080111 20080114 20080115 ...
## $ 50 : num 34.9 34.3 34.3 32.7 33 ...
## $ 52 : num 22.7 21.8 21.7 20.2 20.2 ...
## $ 56 : num 10.8 10.6 10.7 10.5 10.6 ...
# convert into xts
etf3.xts <- xts(etf3.l[, -1], order.by = as.Date(as.character(etf3.l$date), format = '%Y%m%d'))
class(etf3.xts)
## [1] "xts" "zoo"
head(etf3.xts)
## 50 52 56
## 2008-01-02 34.8641 22.7109 10.7916
## 2008-01-03 34.2840 21.8471 10.6450
## 2008-01-04 34.2840 21.7356 10.6659
## 2008-01-07 32.7177 20.2308 10.4983
## 2008-01-08 33.0368 20.2308 10.6282
## 2008-01-09 33.6459 20.3423 10.7120
tail(etf3.xts)
## 50 52 56
## 2024-04-23 151.90 145.90 37.12
## 2024-04-24 156.35 151.60 37.85
## 2024-04-25 153.50 148.55 37.51
## 2024-04-26 156.00 151.25 37.69
## 2024-04-29 158.80 153.40 38.34
## 2024-04-30 158.25 153.90 38.32
# SIT
# con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
# source(con)
# close(con)
#
# devtools::install_github('systematicinvestor/SIT.date', force = T)
# curl::curl_download('https://github.com/systematicinvestor/SIT/raw/master/SIT.tar.gz', 'SIT.tar.gz',mode = 'wb',quiet=T)
# install.packages('SIT.tar.gz', repos = NULL, type='source')
library(SIT)
library(quantmod)
#
# library(SIT)
#
data <- new.env()
# 1. prices; 2. weight; 3. execution.price
# buy and hold
# etf56 <- etf6.xts$`0056`
# head(etf56)
# data$prices = data$weight = data$execution.price = etf56
# data$weight[] <- 1
# data$execution.price[] <- NA
# names(data)
#
# etf52 <- etf6.xts$`0052`
# head(etf52)
# data$prices = data$weight = data$execution.price = etf52
# data$weight[] <- 1
# data$execution.price[] <- NA
# names(data)
#
model <- list()
#
etf3 <- etf3.xts[, 1:3]
head(etf3)
## 50 52 56
## 2008-01-02 34.8641 22.7109 10.7916
## 2008-01-03 34.2840 21.8471 10.6450
## 2008-01-04 34.2840 21.7356 10.6659
## 2008-01-07 32.7177 20.2308 10.4983
## 2008-01-08 33.0368 20.2308 10.6282
## 2008-01-09 33.6459 20.3423 10.7120
names(etf3)
## [1] "50" "52" "56"
colnames(etf3) <- c('e50', 'e52', 'e56')
names(etf3)
## [1] "e50" "e52" "e56"
md = 50
i = 'e50'
for (i in names(etf3)) {
data$prices = data$weight = data$execution.price = etf3[, i]
data$weight[] <- 1
data$execution.price[] <- NA
model[[i]] <- bt.run(data)
sma <- SMA(data$prices, md)
data$weight[] <- iif(data$prices >= sma, 1, 0)
i <- paste(i, '.sma.cross', sep = '')
model[[i]] <- bt.run(data)
}
## Latest weights :
## e50
## 2024-04-30 100
##
## Performance summary :
## CAGR Best Worst
## 9.7 8 -7
##
## Latest weights :
## e50
## 2024-04-30 100
##
## Performance summary :
## CAGR Best Worst
## 8.6 7 -6.4
##
## Latest weights :
## e52
## 2024-04-30 100
##
## Performance summary :
## CAGR Best Worst
## 12.4 10 -9.2
##
## Latest weights :
## e52
## 2024-04-30 100
##
## Performance summary :
## CAGR Best Worst
## 6.2 10 -9.2
##
## Latest weights :
## e56
## 2024-04-30 100
##
## Performance summary :
## CAGR Best Worst
## 8.1 7 -6.9
##
## Latest weights :
## e56
## 2024-04-30 100
##
## Performance summary :
## CAGR Best Worst
## 12 7 -5.3
#-------------------------------------------------
# plotbt(model, plotX = T, log = 'y', LeftMargin = 3)
# strategy.performance.snapshoot(model)
plotbt(model, plotX = T, log = 'y', LeftMargin = 3)
mtext('Cumulative Performance', side = 2, line = 1)

plotbt.strategy.sidebyside(model, return.table=F, make.plot = T)

# --------------------------------------------------
# Add equal-weighted portfolio for 3 ETFs
# --------------------------------------------------
data$prices = data$weight = data$execution.price = etf3
data$execution.price[] <- NA
prices <- data$prices
n <- ncol(prices)
data$weight <- ntop(prices, n)
model$etf3.EqWeight.bh <- bt.run(data)
## Latest weights :
## e50 e52 e56
## 2024-04-30 33.33 33.33 33.33
##
## Performance summary :
## CAGR Best Worst
## 10.5 7.5 -7
#
# strategy.performance.snapshoot(model, T)
plotbt(model, plotX = T, log = 'y', LeftMargin = 3)
mtext('Cumulative Performance', side = 2, line = 1)

plotbt.strategy.sidebyside(model, return.table=F, make.plot = T)

#=============================================================
# MVP investment strategy
#=============================================================
# monthly rebalance
# covariance matrix
# use monthly returns to compute monthly covariance matrix
etf3.m <- to.monthly(etf3, indexAt = 'lastof', OHLC = FALSE)
head(etf3.m)
## e50 e52 e56
## 2008-01-31 31.8765 19.5007 9.5260
## 2008-02-29 35.2991 21.4012 10.3726
## 2008-03-31 35.0671 21.0668 10.6869
## 2008-04-30 37.0685 22.4044 11.0221
## 2008-05-31 36.0823 21.8749 10.7833
## 2008-06-30 31.9056 19.3948 9.5972
#etf3.w <- to.weekly(etf3, indexAt = 'lastof', OHLC = FALSE)
# head(etf3.w)
#
#=================================================================
# MVP portfolio
#=================================================================
# Reset inputs to SIT bt function
data$prices = data$weight = data$execution.price = etf3.m
#data$prices <- industry.price.sample
#data$weight <- industry.price.sample
#data$execution.price <- industry.price.sample
data$execution.price[] <- NA
prices <- data$prices
n <- ncol(prices)
#*****************************************************************
# Create Constraints
#*****************************************************************
constraints = new.constraints(n, lb = 0, ub = +Inf)
# SUM x.i = 1
constraints = add.constraints(rep(1, n), 1, type = '=', constraints)
#
ret = prices / mlag(prices) - 1
weight = coredata(prices)
weight[] = NA
i = 36
for (i in 36:dim(weight)[1]) {
# using 36 historical monthly returns
hist = ret[ (i- 36 +1):i, ]
hist = na.omit(hist)
# create historical input assumptions
ia = create.historical.ia(hist, 12)
# s0 = apply(coredata(hist),2, sd)
ia$cov = cov(coredata(hist))
#ia$cov = cor(coredata(hist), use='complete.obs',method='kendall') * (s0 %*% t(s0))
# use min.risk.portfolio() to compute MVP weights
weight[i,] = min.risk.portfolio(ia, constraints)
}
## Loading required package: kernlab
##
## Attaching package: 'kernlab'
## The following object is masked from 'package:SIT':
##
## cross
#
weight
## e50 e52 e56
## [1,] NA NA NA
## [2,] NA NA NA
## [3,] NA NA NA
## [4,] NA NA NA
## [5,] NA NA NA
## [6,] NA NA NA
## [7,] NA NA NA
## [8,] NA NA NA
## [9,] NA NA NA
## [10,] NA NA NA
## [11,] NA NA NA
## [12,] NA NA NA
## [13,] NA NA NA
## [14,] NA NA NA
## [15,] NA NA NA
## [16,] NA NA NA
## [17,] NA NA NA
## [18,] NA NA NA
## [19,] NA NA NA
## [20,] NA NA NA
## [21,] NA NA NA
## [22,] NA NA NA
## [23,] NA NA NA
## [24,] NA NA NA
## [25,] NA NA NA
## [26,] NA NA NA
## [27,] NA NA NA
## [28,] NA NA NA
## [29,] NA NA NA
## [30,] NA NA NA
## [31,] NA NA NA
## [32,] NA NA NA
## [33,] NA NA NA
## [34,] NA NA NA
## [35,] NA NA NA
## [36,] 3.548886e-01 3.237502e-06 0.645108202
## [37,] 3.501719e-01 3.371590e-06 0.649824747
## [38,] 3.955476e-01 5.845765e-05 0.604393943
## [39,] 4.193681e-01 5.485313e-05 0.580577010
## [40,] 4.492954e-01 5.851273e-05 0.550646065
## [41,] 4.661502e-01 5.863724e-05 0.533791121
## [42,] 4.141586e-01 5.619904e-05 0.585785200
## [43,] 3.559079e-01 3.316326e-06 0.644088824
## [44,] 4.181301e-01 6.959500e-05 0.581800280
## [45,] 6.708864e-05 9.697044e-06 0.999923214
## [46,] 3.603715e-05 1.248981e-05 0.999951473
## [47,] 9.262953e-05 3.980694e-05 0.999867564
## [48,] 6.370006e-05 2.457721e-05 0.999911723
## [49,] 1.876784e-03 5.337385e-05 0.998069842
## [50,] 4.321656e-03 3.737781e-04 0.995304566
## [51,] 9.489326e-05 1.326406e-01 0.867264546
## [52,] 7.678244e-05 1.870735e-03 0.998052482
## [53,] 1.143791e-01 8.590152e-05 0.885534966
## [54,] 1.739879e-01 1.544863e-04 0.825857650
## [55,] 7.085075e-05 1.040851e-01 0.895844078
## [56,] 7.197902e-05 5.314195e-02 0.946786069
## [57,] 3.706475e-04 3.921633e-02 0.960413022
## [58,] 4.625782e-02 2.348454e-01 0.718896771
## [59,] 3.469799e-03 2.582036e-01 0.738326627
## [60,] 1.464753e-04 3.034223e-01 0.696431248
## [61,] 3.356465e-04 3.536539e-01 0.646010479
## [62,] 4.471144e-04 3.586956e-01 0.640857323
## [63,] 1.003832e-03 3.804358e-01 0.618560328
## [64,] 4.807511e-02 3.130304e-01 0.638894508
## [65,] 1.751864e-01 2.911032e-01 0.533710386
## [66,] 2.384369e-01 2.394956e-01 0.522067475
## [67,] 2.175954e-01 2.529502e-01 0.529454451
## [68,] 2.068550e-01 2.894996e-01 0.503645398
## [69,] 2.915622e-01 3.034744e-01 0.404963444
## [70,] 2.104095e-01 3.132877e-01 0.476302796
## [71,] 2.114930e-01 3.252756e-01 0.463231335
## [72,] 4.200134e-01 2.296121e-01 0.350374504
## [73,] 3.936768e-01 2.602442e-01 0.346079065
## [74,] 3.876097e-01 3.277084e-01 0.284681908
## [75,] 3.725245e-01 3.010987e-01 0.326376761
## [76,] 4.408153e-01 2.597189e-01 0.299465735
## [77,] 5.567711e-01 1.737988e-01 0.269430050
## [78,] 5.921316e-01 2.791455e-01 0.128722941
## [79,] 5.952516e-01 2.751253e-01 0.129623145
## [80,] 6.209277e-01 2.236617e-01 0.155410586
## [81,] 9.200605e-01 1.086924e-02 0.069070289
## [82,] 8.867016e-01 4.936605e-03 0.108361829
## [83,] 9.773389e-01 3.769583e-04 0.022284162
## [84,] 9.253928e-01 4.504549e-04 0.074156794
## [85,] 8.359432e-01 1.977320e-03 0.162079484
## [86,] 8.055986e-01 1.027075e-03 0.193374335
## [87,] 7.457613e-01 2.739401e-04 0.253964786
## [88,] 8.361428e-01 1.396284e-04 0.163717530
## [89,] 8.933057e-01 2.794787e-04 0.106414860
## [90,] 9.348257e-01 1.380535e-04 0.065036271
## [91,] 9.924477e-01 4.335043e-05 0.007508917
## [92,] 9.400144e-01 2.131663e-05 0.059964259
## [93,] 9.921810e-01 4.075428e-05 0.007778260
## [94,] 8.963547e-01 2.206661e-05 0.103623250
## [95,] 8.537362e-01 2.406309e-05 0.146239710
## [96,] 8.302736e-01 2.671106e-05 0.169699657
## [97,] 7.757099e-01 2.469677e-05 0.224265390
## [98,] 8.180026e-01 2.320268e-05 0.181974232
## [99,] 6.736075e-01 1.864814e-05 0.326373824
## [100,] 6.110869e-01 1.686790e-05 0.388896277
## [101,] 6.049238e-01 1.555521e-05 0.395060679
## [102,] 6.156674e-01 1.817319e-05 0.384314436
## [103,] 6.775212e-01 1.233506e-05 0.322466448
## [104,] 6.636290e-01 1.163400e-05 0.336359400
## [105,] 6.601118e-01 1.073415e-05 0.339877449
## [106,] 6.879250e-01 1.125448e-05 0.312063794
## [107,] 6.844020e-01 1.024090e-05 0.315587786
## [108,] 6.914410e-01 1.011338e-05 0.308548881
## [109,] 7.266165e-01 1.101551e-05 0.273372477
## [110,] 7.543835e-01 1.120117e-05 0.245605292
## [111,] 7.633356e-01 1.320876e-05 0.236651164
## [112,] 7.673357e-01 1.341102e-05 0.232650860
## [113,] 7.323947e-01 1.468769e-05 0.267590610
## [114,] 7.468767e-01 1.429642e-05 0.253108988
## [115,] 7.487640e-01 1.293841e-05 0.251223081
## [116,] 7.216900e-01 1.149596e-05 0.278298457
## [117,] 6.290320e-01 1.247669e-05 0.370955484
## [118,] 5.882463e-01 9.406280e-06 0.411744300
## [119,] 5.762847e-01 1.353364e-05 0.423701716
## [120,] 5.742455e-01 1.493529e-05 0.425739558
## [121,] 5.854451e-01 1.184909e-05 0.414543085
## [122,] 5.759300e-01 1.261682e-05 0.424057409
## [123,] 5.930626e-01 1.465525e-05 0.406922741
## [124,] 5.781434e-01 1.058398e-05 0.421846007
## [125,] 5.938489e-01 1.178859e-05 0.406139340
## [126,] 5.677424e-01 1.256882e-05 0.432245019
## [127,] 4.302895e-01 1.376616e-05 0.569696720
## [128,] 4.635827e-01 2.088276e-05 0.536396416
## [129,] 4.976804e-01 2.056396e-05 0.502299023
## [130,] 8.947379e-02 1.760297e-05 0.910508604
## [131,] 1.644738e-01 1.174712e-05 0.835514430
## [132,] 1.230167e-01 1.322916e-05 0.876970077
## [133,] 1.981355e-01 1.110977e-05 0.801853364
## [134,] 1.536468e-01 1.271186e-05 0.846340478
## [135,] 1.360154e-01 1.363487e-05 0.863970983
## [136,] 1.625343e-01 1.377092e-05 0.837451927
## [137,] 1.589549e-01 1.096985e-05 0.841034171
## [138,] 1.632834e-01 1.098826e-05 0.836705652
## [139,] 2.056710e-02 1.548970e-05 0.979417412
## [140,] 1.562493e-02 1.528029e-05 0.984359791
## [141,] 1.036249e-02 1.519714e-05 0.989622316
## [142,] 9.693825e-04 1.160587e-05 0.999019012
## [143,] 5.004232e-04 1.216369e-05 0.999487413
## [144,] 1.938151e-04 1.063837e-05 0.999795546
## [145,] 1.408365e-04 9.699758e-06 0.999849464
## [146,] 7.660626e-05 8.423709e-06 0.999914970
## [147,] 2.532970e-05 6.007352e-06 0.999968663
## [148,] 2.019550e-05 5.205370e-06 0.999974599
## [149,] 2.326435e-05 5.667714e-06 0.999971068
## [150,] 2.545194e-05 6.127601e-06 0.999968420
## [151,] 1.959785e-05 6.855741e-06 0.999973546
## [152,] 1.855900e-05 6.771670e-06 0.999974669
## [153,] 2.374066e-05 7.451966e-06 0.999968807
## [154,] 2.433702e-05 7.457959e-06 0.999968205
## [155,] 1.995273e-05 6.565592e-06 0.999973482
## [156,] 2.050017e-05 6.719415e-06 0.999972780
## [157,] 2.183549e-05 7.524107e-06 0.999970640
## [158,] 2.721367e-05 8.331817e-06 0.999964455
## [159,] 1.631050e-04 2.052702e-05 0.999816368
## [160,] 2.329141e-04 2.532380e-05 0.999741762
## [161,] 2.213085e-04 2.126776e-05 0.999757424
## [162,] 1.545597e-04 2.150569e-05 0.999823935
## [163,] 1.623713e-04 2.359679e-05 0.999814032
## [164,] 2.664623e-04 2.628054e-05 0.999707257
## [165,] 1.930782e-04 2.320714e-05 0.999783715
## [166,] 2.231309e-04 3.137422e-06 0.999773732
## [167,] 9.715701e-05 3.060148e-06 0.999899783
## [168,] 8.485609e-05 3.578605e-06 0.999911565
## [169,] 1.002302e-04 3.217596e-06 0.999896552
## [170,] 5.941459e-04 4.448021e-06 0.999401406
## [171,] 4.590488e-04 4.327134e-06 0.999536624
## [172,] 8.795851e-04 4.806911e-05 0.999072346
## [173,] 1.926344e-04 4.592619e-06 0.999802773
## [174,] 2.753464e-03 3.051244e-06 0.997243485
## [175,] 5.806769e-03 3.141265e-06 0.994190090
## [176,] 8.016562e-03 3.079769e-06 0.991980358
## [177,] 8.930891e-04 3.301903e-05 0.999073892
## [178,] 7.672786e-04 3.590050e-05 0.999196821
## [179,] 5.682719e-05 1.494630e-05 0.999928227
## [180,] 5.623361e-05 1.553912e-05 0.999928227
## [181,] 4.417057e-05 1.341312e-05 0.999942416
## [182,] 6.544436e-05 1.628126e-05 0.999918274
## [183,] 1.768005e-04 2.410461e-05 0.999799095
## [184,] 1.276646e-04 2.385000e-05 0.999848485
## [185,] 1.504305e-04 2.172972e-05 0.999827840
## [186,] 6.327863e-05 1.695255e-06 0.999935026
## [187,] 2.849134e-01 1.314787e-05 0.715073415
## [188,] 2.849927e-01 1.335211e-05 0.714993965
## [189,] 2.620385e-01 1.495491e-05 0.737946563
## [190,] 2.788320e-01 1.341597e-05 0.721154587
## [191,] 3.128532e-01 9.410615e-06 0.687137398
## [192,] 3.753674e-01 6.667125e-06 0.624625953
## [193,] 4.190292e-01 6.866300e-06 0.580963899
## [194,] 3.844874e-01 6.613471e-06 0.615505970
## [195,] 2.386372e-01 9.711716e-06 0.761353066
## [196,] 2.531307e-01 9.307840e-06 0.746859957
weight <- round(weight, digits = 2)
#format(round(weight, 2), nsmall = 2)
#apply(weight, 1, sum)
data$weight[] = weight
#capital = 100000
#data$weight[] = (capital / prices) * data$weight
model$mvp.month = bt.run(data, type = "weight")
## Latest weights :
## e50 e52 e56
## 2024-04-30 24 0 76
##
## Performance summary :
## CAGR Best Worst
## 8.2 12.5 -12.2
#
plotbt(model, plotX = T, log = 'y', LeftMargin = 3)
mtext('Cumulative Performance', side = 2, line = 1)

plotbt.strategy.sidebyside(model, return.table=T, make.plot = T)

## e50 e50.sma.cross e52
## Period "Jan2008 - Apr2024" "Jan2008 - Apr2024" "Jan2008 - Apr2024"
## Cagr "9.7" "8.58" "12.43"
## Sharpe "0.58" "0.73" "0.6"
## DVR "0.49" "0.58" "0.45"
## Volatility "19.69" "12.69" "25.35"
## MaxDD "-52.38" "-23.13" "-59.41"
## AvgDD "-3.55" "-2.84" "-4.87"
## VaR "-1.91" "-1.21" "-2.49"
## CVaR "-2.92" "-1.95" "-3.7"
## Exposure "99.98" "63.51" "99.98"
## e52.sma.cross e56 e56.sma.cross
## Period "Jan2008 - Apr2024" "Jan2008 - Apr2024" "Jan2008 - Apr2024"
## Cagr "6.15" "8.07" "12.02"
## Sharpe "0.44" "0.57" "1.17"
## DVR "0.16" "0.47" "1.01"
## Volatility "17.65" "16.36" "10.37"
## MaxDD "-56.12" "-54.36" "-14.94"
## AvgDD "-6.11" "-3.12" "-2.02"
## VaR "-1.71" "-1.56" "-0.93"
## CVaR "-2.8" "-2.67" "-1.62"
## Exposure "62.76" "99.98" "63.79"
## etf3.EqWeight.bh mvp.month
## Period "Jan2008 - Apr2024" "Jan2008 - Apr2024"
## Cagr "10.53" "8.19"
## Sharpe "0.65" "0.67"
## DVR "0.53" "0.53"
## Volatility "18.52" "13.02"
## MaxDD "-54.89" "-25.1"
## AvgDD "-2.81" "-6.59"
## VaR "-1.83" "-5.25"
## CVaR "-2.81" "-8.75"
## Exposure "99.98" "81.63"
plotbt(model)

#====================
# multiple models
#====================
#*****************************************************************
# Create Constraints
#*****************************************************************
constraints = new.constraints(n, lb = 0, ub = 1)
# SUM x.i = 1
constraints = add.constraints(rep(1, n), 1, type = '=', constraints)
#*****************************************************************
# Create Portfolios
#*****************************************************************
ret = prices / mlag(prices) - 1
weight = coredata(prices)
weight[] = NA
#
weights = list()
# Equal Weight 1/N Benchmark
weights$equal.weight = weight
weights$equal.weight[] = ntop(prices, n)
start.i = 35
weights$equal.weight[1:start.i,] = NA
#
weights$min.var = weight
weights$min.maxloss = weight
# weights$min.mad = weight
# weights$min.cvar = weight
# weights$min.cdar = weight
# weights$min.cor.insteadof.cov = weight
#weights$min.mad.downside = weight
#weights$min.risk.downside = weight
#
#
for (i in 36:dim(weight)[1]) {
# using 36 historical monthly returns
hist = ret[ (i- 36 +1):i, ]
hist = na.omit(hist)
# create historical input assumptions
ia = create.historical.ia(hist, 12)
s0 = apply(coredata(hist),2, sd)
# ia$cov = cov(coredata(hist))
ia$cov = cor(coredata(hist), use='complete.obs',method='kendall') * (s0 %*% t(s0))
# use min.risk.portfolio() to compute MVP weights
weights$min.var[i,] = min.risk.portfolio(ia, constraints)
weights$min.maxloss[i,] = min.maxloss.portfolio(ia, constraints)
#weights$min.mad[i,] = min.mad.portfolio(ia, constraints)
#weights$min.cvar[i,] = min.cvar.portfolio(ia, constraints)
#weights$min.cdar[i,] = min.cdar.portfolio(ia, constraints)
#weights$min.cor.insteadof.cov[i,] = min.cor.insteadof.cov.portfolio(ia, constraints)
#weights$min.mad.downside[i,] = min.mad.downside.portfolio(ia, constraints)
# weights$min.risk.downside[i,] = min.risk.downside.portfolio(ia, constraints)
}
##
## Attaching package: 'corpcor'
## The following object is masked from 'package:SIT':
##
## cov.shrink
models = list()
# i = "equal.weight"
for(i in names(weights)) {
data$weight[] = NA
data$weight[] = weights[[i]]
models[[i]] = bt.run.share(data, clean.signal = F)
}
## Latest weights :
## e50 e52 e56
## 2024-04-30 33.33 33.33 33.33
##
## Performance summary :
## CAGR Best Worst
## 9.6 17.4 -13.4
##
## Latest weights :
## e50 e52 e56
## 2024-04-30 36.66 0 63.34
##
## Performance summary :
## CAGR Best Worst
## 8.2 12.2 -12.6
##
## Latest weights :
## e50 e52 e56
## 2024-04-30 58.73 0 41.27
##
## Performance summary :
## CAGR Best Worst
## 8.1 12.1 -12.2
# Plot perfromance
plotbt(models, plotX = T, log = 'y', LeftMargin = 3)
mtext('Cumulative Performance', side = 2, line = 1)

# Plot Strategy Statistics Side by Side
plotbt.strategy.sidebyside(models)
