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(quantmod)
library(quadprog)
library(lpSolve)
rm(list = ls())
con = gzcon(url('https://github.com/systematicinvestor/SIT/raw/master/sit.gz', 'rb'))
source(con)
close(con)
industry10 <- read.table("10_Industry_Portfolios_Wout_Div.txt", head = TRUE)
date <- seq(as.Date("1926-08-01"), length=1126, by="1 month") - 1
industry10 <- xts(coredata(industry10[, -1])/100, order.by = date)
head(date)
## [1] "1926-07-31" "1926-08-31" "1926-09-30" "1926-10-31" "1926-11-30"
## [6] "1926-12-31"
tail(date)
## [1] "2019-11-30" "2019-12-31" "2020-01-31" "2020-02-29" "2020-03-31"
## [6] "2020-04-30"
class(date)
## [1] "Date"
# Convert data into XTS
industry10 <- xts(industry10[,-1]/100, order.by = date)
head(industry10)
## Durbl Manuf Enrgy HiTec Telcm Shops
## 1926-07-31 0.001507 0.000439 -0.000130 0.000284 0.000083 -0.000013
## 1926-08-31 0.000309 0.000246 0.000313 0.000237 0.000217 -0.000095
## 1926-09-30 0.000448 0.000056 -0.000386 -0.000117 0.000089 0.000002
## 1926-10-31 -0.000847 -0.000392 -0.000090 -0.000464 -0.000011 -0.000254
## 1926-11-30 -0.000237 0.000371 -0.000062 0.000446 0.000163 0.000590
## 1926-12-31 0.000956 0.000272 0.000221 -0.000083 0.000051 0.000043
## Hlth Utils Other
## 1926-07-31 0.000111 0.000689 0.000188
## 1926-08-31 0.000359 -0.000207 0.000400
## 1926-09-30 0.000052 0.000165 -0.000015
## 1926-10-31 -0.000120 -0.000288 -0.000311
## 1926-11-30 0.000478 0.000331 0.000165
## 1926-12-31 -0.000068 -0.000067 0.000252
#Convert return into price
industry10.price <- cumprod(industry10+1)*100
head(industry10.price)
## Durbl Manuf Enrgy HiTec Telcm Shops Hlth
## 1926-07-31 100.1507 100.0439 99.98700 100.02840 100.0083 99.9987 100.0111
## 1926-08-31 100.1816 100.0685 100.01830 100.05211 100.0300 99.9892 100.0470
## 1926-09-30 100.2265 100.0741 99.97969 100.04040 100.0389 99.9894 100.0522
## 1926-10-31 100.1416 100.0349 99.97069 99.99398 100.0378 99.9640 100.0402
## 1926-11-30 100.1179 100.0720 99.96449 100.03858 100.0541 100.0230 100.0880
## 1926-12-31 100.2136 100.0992 99.98658 100.03028 100.0592 100.0273 100.0812
## Utils Other
## 1926-07-31 100.0689 100.0188
## 1926-08-31 100.0482 100.0588
## 1926-09-30 100.0647 100.0573
## 1926-10-31 100.0359 100.0262
## 1926-11-30 100.0690 100.0427
## 1926-12-31 100.0623 100.0679
industry10.price.sample <- industry10.price['1999-12/2020-03']
data <- new.env()
#Create 4 input data
#data$prices <- industry10.price.sample
#data$weight <- industry10.price.sample
#data$execution.price <- industry10.price.sample
data$prices = data$weight = data$execution.price = industry10.price.sample
data$execution.price[] <- NA
data$symbolnames <- colnames(data$prices)
prices <- data$prices
n <- ncol(prices)
# Compute Equal Weight Portfolio of 10 asset
data$weight <- ntop(prices, n)
head(data$weight)
## Durbl Manuf Enrgy HiTec Telcm Shops
## 1999-12-31 0.1111111 0.1111111 0.1111111 0.1111111 0.1111111 0.1111111
## 2000-01-31 0.1111111 0.1111111 0.1111111 0.1111111 0.1111111 0.1111111
## 2000-02-29 0.1111111 0.1111111 0.1111111 0.1111111 0.1111111 0.1111111
## 2000-03-31 0.1111111 0.1111111 0.1111111 0.1111111 0.1111111 0.1111111
## 2000-04-30 0.1111111 0.1111111 0.1111111 0.1111111 0.1111111 0.1111111
## 2000-05-31 0.1111111 0.1111111 0.1111111 0.1111111 0.1111111 0.1111111
## Hlth Utils Other
## 1999-12-31 0.1111111 0.1111111 0.1111111
## 2000-01-31 0.1111111 0.1111111 0.1111111
## 2000-02-29 0.1111111 0.1111111 0.1111111
## 2000-03-31 0.1111111 0.1111111 0.1111111
## 2000-04-30 0.1111111 0.1111111 0.1111111
## 2000-05-31 0.1111111 0.1111111 0.1111111
industry10.price.sample <- industry10.price ['1997-01/2020-03']
data$prices = data$execution.price = data$weight = industry10.price.sample
data$execution.price[] <- NA
data$symbolnames <- colnames(prices)
industry10.price.sample <- industry10.price ['1997-01/2020-03']
data$prices = data$execution.price = data$weight = industry10.price.sample
data$execution.price[] <- NA
data$symbolnames <- colnames(prices)
# Constraint
constraints = new.constraints(n, lb = -Inf, ub = +Inf)
constraints = add.constraints(rep(1,n), 1, type= "=", constraints)
#create Weight Variable
weight <- coredata(prices)
head(weight)
## Durbl Manuf Enrgy HiTec Telcm Shops Hlth Utils
## [1,] 107.0431 106.7829 106.3659 109.4251 105.2123 107.1208 108.0276 103.5906
## [2,] 107.0308 106.6970 106.3753 109.3735 105.1684 107.0003 108.1085 103.6475
## [3,] 106.9422 106.6502 106.3124 109.5720 105.1301 106.9584 108.0761 103.5669
## [4,] 107.0541 106.7375 106.4424 109.6151 105.2090 107.0997 108.0784 103.6221
## [5,] 107.1503 106.7471 106.4211 109.4978 105.1244 107.0538 108.1343 103.6973
## [6,] 107.0059 106.7305 106.5170 109.3789 105.0146 107.0244 108.1752 103.7330
## Other
## [1,] 105.8052
## [2,] 105.7551
## [3,] 105.6798
## [4,] 105.8274
## [5,] 105.7901
## [6,] 105.8250
weight[] <- NA
head(weight)
## Durbl Manuf Enrgy HiTec Telcm Shops Hlth Utils Other
## [1,] NA NA NA NA NA NA NA NA NA
## [2,] NA NA NA NA NA NA NA NA NA
## [3,] NA NA NA NA NA NA NA NA NA
## [4,] NA NA NA NA NA NA NA NA NA
## [5,] NA NA NA NA NA NA NA NA NA
## [6,] NA NA NA NA NA NA NA NA NA
#Return
prices <- data$prices
ret <- prices / mlag(prices)
head(ret)
## Durbl Manuf Enrgy HiTec Telcm Shops Hlth
## 1997-01-31 NA NA NA NA NA NA NA
## 1997-02-28 1.000004 1.000063 0.999376 0.999252 1.000218 1.000299 1.000078
## 1997-03-31 0.999592 0.999610 1.000516 0.999442 0.999208 0.999862 0.999251
## 1997-04-30 1.000207 1.000527 1.000001 1.000814 1.000227 1.000214 1.000602
## 1997-05-31 1.000634 1.000763 1.000675 1.001009 1.000654 1.000595 1.000678
## 1997-06-30 1.000376 1.000511 1.000127 1.000131 1.000351 1.000494 1.000882
## Utils Other
## 1997-01-31 NA NA
## 1997-02-28 0.999894 1.000247
## 1997-03-31 0.999679 0.999395
## 1997-04-30 0.999829 1.000508
## 1997-05-31 1.000319 1.000611
## 1997-06-30 1.000260 1.000523
#MVP for 2000-2020
hist <- ret[1:36,]
hist <- na.omit(hist)
head(hist)
## Durbl Manuf Enrgy HiTec Telcm Shops Hlth
## 1997-02-28 1.000004 1.000063 0.999376 0.999252 1.000218 1.000299 1.000078
## 1997-03-31 0.999592 0.999610 1.000516 0.999442 0.999208 0.999862 0.999251
## 1997-04-30 1.000207 1.000527 1.000001 1.000814 1.000227 1.000214 1.000602
## 1997-05-31 1.000634 1.000763 1.000675 1.001009 1.000654 1.000595 1.000678
## 1997-06-30 1.000376 1.000511 1.000127 1.000131 1.000351 1.000494 1.000882
## 1997-07-31 1.000719 1.000703 1.000730 1.001594 1.000247 1.000772 1.000192
## Utils Other
## 1997-02-28 0.999894 1.000247
## 1997-03-31 0.999679 0.999395
## 1997-04-30 0.999829 1.000508
## 1997-05-31 1.000319 1.000611
## 1997-06-30 1.000260 1.000523
## 1997-07-31 1.000243 1.000909
#Input Assumptions
ia <- create.historical.ia(hist,12)
#MVP weights for 2000-01
min.risk.portfolio(ia, constraints)
## [1] 0.1112702 0.3394402 0.1385379 -0.1262141 0.1271624 0.4707548 0.3219133
## [8] 0.3907168 -0.7735816