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
require(curl)
## Loading required package: curl
## Using libcurl 7.68.0 with OpenSSL/1.1.1f
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.1'
## (as 'lib' is unspecified)
require(SIT)
## Loading required package: 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)
library(readxl)
library(xts)
library(openxlsx)
library(SIT)
p_load(quantmod, quadprog,lpSolve)
p_load(xts)
p_load(TTR)
X10_Industry_Portfolios <- read_excel("10_Industry_Portfolios.xlsx", range = "A12:K1160")
str(X10_Industry_Portfolios)
## tibble [1,148 × 11] (S3: tbl_df/tbl/data.frame)
## $ 192705 : num [1:1148] 192706 192707 192708 192709 192710 ...
## $ 7.89 : num [1:1148] -2.46 4.64 2.53 4.59 -0.98 6.57 1.52 0.42 -4.47 3.72 ...
## $ 4.57 : num [1:1148] -3.25 12.82 6.29 5.45 -4.53 ...
## $ 4.93 : num [1:1148] -3.66 11.79 2.49 4.27 -6.92 ...
## $ 4.3099999999999996: num [1:1148] -3.64 2.71 -0.64 2.95 -2.31 3.74 -0.1 -2.02 -0.75 5.03 ...
## $ 7.02 : num [1:1148] 2.46 12.16 5.42 0.6 -7.27 ...
## $ 3.35 : num [1:1148] -3.47 3.73 0.43 5.21 -2.09 2.83 0.09 0.29 -0.3 0.71 ...
## $ 2.23 : num [1:1148] -0.79 9.59 6.93 7.34 -5.16 ...
## $ 3.48 : num [1:1148] 0.23 9.3 -0.28 5.46 4.3 3.12 -0.77 2.25 -1.58 9.9 ...
## $ 8.83 : num [1:1148] -3.96 4.97 3.09 7.93 -4.6 5.99 1.95 5.58 -0.97 6.35 ...
## $ 6.08 : num [1:1148] -2.75 5.65 -1.38 4.05 -3.99 4.14 1.26 -1.27 -2.37 5.56 ...
date <- seq(as.Date("1926-08-01"), length=1148, by="1 month") - 1
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] "2021-09-30" "2021-10-31" "2021-11-30" "2021-12-31" "2022-01-31"
## [6] "2022-02-28"
class(date)
## [1] "Date"
X10_Industry_Portfolios <- xts(coredata(X10_Industry_Portfolios[, -1]/100), order.by = date)
head(X10_Industry_Portfolios)
## 7.89 4.57 4.93 4.3099999999999996 7.02 3.35 2.23
## 1926-07-31 -0.0246 -0.0325 -0.0366 -0.0364 0.0246 -0.0347 -0.0079
## 1926-08-31 0.0464 0.1282 0.1179 0.0271 0.1216 0.0373 0.0959
## 1926-09-30 0.0253 0.0629 0.0249 -0.0064 0.0542 0.0043 0.0693
## 1926-10-31 0.0459 0.0545 0.0427 0.0295 0.0060 0.0521 0.0734
## 1926-11-30 -0.0098 -0.0453 -0.0692 -0.0231 -0.0727 -0.0209 -0.0516
## 1926-12-31 0.0657 0.0441 0.1024 0.0374 0.1051 0.0283 0.1128
## 3.48 8.83 6.08
## 1926-07-31 0.0023 -0.0396 -0.0275
## 1926-08-31 0.0930 0.0497 0.0565
## 1926-09-30 -0.0028 0.0309 -0.0138
## 1926-10-31 0.0546 0.0793 0.0405
## 1926-11-30 0.0430 -0.0460 -0.0399
## 1926-12-31 0.0312 0.0599 0.0414
industry.price <- cumprod(X10_Industry_Portfolios+1)*100
industry.price.sample <- industry.price['2000-01/2020-03']
data <- new.env()
data$prices = data$weight = data$execution.price = industry.price.sample
data$execution.price[] <- NA
data$symbolnames <- colnames(data$prices)
prices <- data$prices
n <- ncol(prices)
names(data)
## [1] "prices" "weight" "symbolnames" "execution.price"
data$weight <- ntop(prices, n)
models <- list()
models$equal.weight <- bt.run(data, trade.summary = T)
## Latest weights :
## 7.89 4.57 4.93 4.3099999999999996 7.02 3.35 2.23 3.48 8.83 6.08
## 2020-03-31 10 10 10 10 10 10 10 10 10 10
##
## Performance summary :
## CAGR Best Worst
## 6.6 14.8 -17.6
bt.detail.summary(models$equal.weight)
## $System
## $System$Period
## [1] "Jan2000 - Mar2020"
##
## $System$Cagr
## [1] 6.57
##
## $System$Sharpe
## [1] 0.5
##
## $System$DVR
## [,1]
## 7.89 0.42
##
## $System$Volatility
## [1] 15.15
##
## $System$MaxDD
## [1] -49.8
##
## $System$AvgDD
## [1] -7.64
##
## $System$VaR
## 5%
## -7.33
##
## $System$CVaR
## [1] -9.96
##
## $System$Exposure
## [1] 99.59
##
##
## $Trade
## $Trade$Win.Percent
## [1] 100
##
## $Trade$Avg.Trade
## [1] 24.4
##
## $Trade$Avg.Win
## [1] 24.4
##
## $Trade$Avg.Loss
## [1] NaN
##
## $Trade$Best.Trade
## [1] 50.49
##
## $Trade$Worst.Trade
## [1] 6.53
##
## $Trade$WinLoss.Ratio
## [1] NaN
##
## $Trade$Avg.Len
## [1] 242
##
## $Trade$Num.Trades
## [1] 10
##
##
## $Period
## $Period$Win.Percent.Day
## [1] 64.2
##
## $Period$Best.Day
## [1] 14.8
##
## $Period$Worst.Day
## [1] -17.6
##
## $Period$Win.Percent.Month
## [1] 64.2
##
## $Period$Best.Month
## [1] 14.8
##
## $Period$Worst.Month
## [1] -17.6
##
## $Period$Win.Percent.Year
## [1] 81
##
## $Period$Best.Year
## [1] 29.5
##
## $Period$Worst.Year
## [1] -37.9
industry.price.sample <- industry.price['2000-01/2020-03']
data$prices = data$execution.price = data$weight = industry.price.sample
data$execution.price[] <- NA
data$symbolnames <- colnames(prices)
constraints = new.constraints(n, lb = -Inf, ub = +Inf)
constraints = add.constraints(rep(1, n), 1, type = "=", constraints)
weight <- coredata(prices)
head(weight)
## 7.89 4.57 4.93 4.3099999999999996 7.02 3.35 2.23
## [1,] 10455.612 4208.015 9302.074 12386.27 41470.37 3390.873 15227.40
## [2,] 10247.545 4745.799 9197.891 11963.90 48296.39 3867.629 16217.18
## [3,] 10255.743 4671.290 8841.012 11917.24 35773.14 3482.027 15343.07
## [4,] 9743.982 4525.546 8286.681 11896.98 30904.41 3276.935 14901.19
## [5,] 9827.780 4889.400 8893.266 13091.44 36445.57 3356.893 15737.15
## [6,] 10145.217 4761.297 9096.032 13056.09 35239.22 3312.582 15966.91
## 3.48 8.83 6.08
## [1,] 54083.25 940.3540 3703.366
## [2,] 49199.54 837.4793 3705.958
## [3,] 48919.10 885.7181 3538.449
## [4,] 44760.97 897.9410 3382.403
## [5,] 46511.13 945.5319 3580.274
## [6,] 47832.04 941.1825 3705.941
weight[] <- NA
head(weight)
## 7.89 4.57 4.93 4.3099999999999996 7.02 3.35 2.23 3.48 8.83 6.08
## [1,] NA NA NA NA NA NA NA NA NA NA
## [2,] NA NA NA NA NA NA NA NA NA NA
## [3,] NA NA NA NA NA NA NA NA NA NA
## [4,] NA NA NA NA NA NA NA NA NA NA
## [5,] NA NA NA NA NA NA NA NA NA NA
## [6,] NA NA NA NA NA NA NA NA NA NA
prices <- data$prices
ret <- prices / mlag(prices) - 1
head(ret)
## 7.89 4.57 4.93 4.3099999999999996 7.02 3.35 2.23
## 2000-01-31 NA NA NA NA NA NA NA
## 2000-02-29 -0.0199 0.1278 -0.0112 -0.0341 0.1646 0.1406 0.0650
## 2000-03-31 0.0008 -0.0157 -0.0388 -0.0039 -0.2593 -0.0997 -0.0539
## 2000-04-30 -0.0499 -0.0312 -0.0627 -0.0017 -0.1361 -0.0589 -0.0288
## 2000-05-31 0.0086 0.0804 0.0732 0.1004 0.1793 0.0244 0.0561
## 2000-06-30 0.0323 -0.0262 0.0228 -0.0027 -0.0331 -0.0132 0.0146
## 3.48 8.83 6.08
## 2000-01-31 NA NA NA
## 2000-02-29 -0.0903 -0.1094 0.0007
## 2000-03-31 -0.0057 0.0576 -0.0452
## 2000-04-30 -0.0850 0.0138 -0.0441
## 2000-05-31 0.0391 0.0530 0.0585
## 2000-06-30 0.0284 -0.0046 0.0351
hist <- ret[1:36, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[2:37, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[3:38, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[4:39, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[5:40, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[6:41, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[7:42, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[8:43, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[9:44, ]
hist <- na.omit(hist)
data$weight[] = weight
capital = 100000
data$weight[] = (capital / prices) * data$weight
min.var = bt.run(data, type='share', capital=capital)
## Latest weights :
## 7.89 4.57 4.93 4.3099999999999996 7.02 3.35 2.23 3.48 8.83
## 2020-03-31 -24.84 -19.02 68.95 21.22 20.99 6.59 59.5 22.01 -31.68
## 6.08
## 2020-03-31 -23.73
##
## Performance summary :
## CAGR Best Worst
## 5.2 20.7 -34.5
industry.price.sample <- industry.price['2000-01/2020-03']
data$prices = data$execution.price = data$weight = industry.price.sample
data$execution.price[] <- NA
data$symbolnames <- colnames(prices)
constraints = new.constraints(n, lb = 0, ub = +Inf)
constraints = add.constraints(rep(1, n), 1, type = "=", constraints)
weight <- coredata(prices)
head(weight)
## 7.89 4.57 4.93 4.3099999999999996 7.02 3.35 2.23
## [1,] 10455.612 4208.015 9302.074 12386.27 41470.37 3390.873 15227.40
## [2,] 10247.545 4745.799 9197.891 11963.90 48296.39 3867.629 16217.18
## [3,] 10255.743 4671.290 8841.012 11917.24 35773.14 3482.027 15343.07
## [4,] 9743.982 4525.546 8286.681 11896.98 30904.41 3276.935 14901.19
## [5,] 9827.780 4889.400 8893.266 13091.44 36445.57 3356.893 15737.15
## [6,] 10145.217 4761.297 9096.032 13056.09 35239.22 3312.582 15966.91
## 3.48 8.83 6.08
## [1,] 54083.25 940.3540 3703.366
## [2,] 49199.54 837.4793 3705.958
## [3,] 48919.10 885.7181 3538.449
## [4,] 44760.97 897.9410 3382.403
## [5,] 46511.13 945.5319 3580.274
## [6,] 47832.04 941.1825 3705.941
weight[] <- NA
head(weight)
## 7.89 4.57 4.93 4.3099999999999996 7.02 3.35 2.23 3.48 8.83 6.08
## [1,] NA NA NA NA NA NA NA NA NA NA
## [2,] NA NA NA NA NA NA NA NA NA NA
## [3,] NA NA NA NA NA NA NA NA NA NA
## [4,] NA NA NA NA NA NA NA NA NA NA
## [5,] NA NA NA NA NA NA NA NA NA NA
## [6,] NA NA NA NA NA NA NA NA NA NA
prices <- data$prices
ret <- prices / mlag(prices) - 1
head(ret)
## 7.89 4.57 4.93 4.3099999999999996 7.02 3.35 2.23
## 2000-01-31 NA NA NA NA NA NA NA
## 2000-02-29 -0.0199 0.1278 -0.0112 -0.0341 0.1646 0.1406 0.0650
## 2000-03-31 0.0008 -0.0157 -0.0388 -0.0039 -0.2593 -0.0997 -0.0539
## 2000-04-30 -0.0499 -0.0312 -0.0627 -0.0017 -0.1361 -0.0589 -0.0288
## 2000-05-31 0.0086 0.0804 0.0732 0.1004 0.1793 0.0244 0.0561
## 2000-06-30 0.0323 -0.0262 0.0228 -0.0027 -0.0331 -0.0132 0.0146
## 3.48 8.83 6.08
## 2000-01-31 NA NA NA
## 2000-02-29 -0.0903 -0.1094 0.0007
## 2000-03-31 -0.0057 0.0576 -0.0452
## 2000-04-30 -0.0850 0.0138 -0.0441
## 2000-05-31 0.0391 0.0530 0.0585
## 2000-06-30 0.0284 -0.0046 0.0351
hist <- ret[1:36, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[2:37, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[3:38, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[4:39, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[5:40, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[6:41, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[7:42, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[8:43, ]
hist <- na.omit(hist)
ia <- create.historical.ia(hist, 12)
weight = min.risk.portfolio(ia, constraints)
hist <- ret[9:44, ]
hist <- na.omit(hist)
data$weight[] = weight
capital = 100000
data$weight[] = (capital / prices) * data$weight
min.var.positive = bt.run(data, type='share', capital=capital)
## Latest weights :
## 7.89 4.57 4.93 4.3099999999999996 7.02 3.35 2.23 3.48 8.83 6.08
## 2020-03-31 0 0 47.94 42.19 9.86 0 0 0 0 0
##
## Performance summary :
## CAGR Best Worst
## 7 20.3 -16.5
library(ggplot2)
plotbt.custom.report.part1(models$equal.weight, min.var, min.var.positive)

plotbt.strategy.sidebyside(models$equal.weight, min.var, min.var.positive)
