install.packages("pacman")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("devtools")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("curl", repos = "http://cran.r-project.org")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
## Warning: unable to access index for repository http://cran.r-project.org/src/contrib:
## cannot open URL 'http://cran.r-project.org/src/contrib/PACKAGES'
## Warning: package 'curl' is not available for this version of R
##
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
install.packages("curl", repos = "http://cran.r-project.org")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
## Warning: unable to access index for repository http://cran.r-project.org/src/contrib:
## cannot open URL 'http://cran.r-project.org/src/contrib/PACKAGES'
## Warning: package 'curl' is not available for this version of R
##
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
install.packages("tidyverse")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("lubridate")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("purrr")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("tidyquant")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("readxl")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("writexl")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (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 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.2'
## (as 'lib' is unspecified)
devtools::install_github('joshuaulrich/xts')
## Skipping install of 'xts' from a github remote, the SHA1 (d3d5c959) has not changed since last install.
## Use `force = TRUE` to force installation
devtools::install_github('joshuaulrich/quantmod')
## Skipping install of 'quantmod' from a github remote, the SHA1 (50e1e85c) has not changed since last install.
## Use `force = TRUE` to force installation
library(xts)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
rm(list=ls())
con = gzcon(url('https://github.com/systematicinvestor/SIT/raw/master/sit.gz', 'rb'))
source(con)
close(con)
library(pacman)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.0 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.1 ✔ tibble 3.2.0
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks xts::first()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks xts::last()
## ✖ readr::parse_date() masks curl::parse_date()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(lubridate)
library(purrr)
library(tidyquant)
## Loading required package: PerformanceAnalytics
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
## Loading required package: quantmod
## Loading required package: TTR
##
## Attaching package: 'TTR'
## The following object is masked _by_ '.GlobalEnv':
##
## DVI
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(readxl)
library(writexl)
p_load(quantmod, quadprog, lpSolve)
data <- read_csv("myetf4(1).csv")
## Rows: 751 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Index
## dbl (4): tw0050, tw0056, tw006205, tw00646
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
p_load(quantmod, quadprog, lpSolve)
data$Index <- as.Date(data$Index, format="%Y/%m/%d")
data <- data %>% filter(between(Index,as.Date('2015-12-14'), as.Date('2018-12-28')))
head(data)
## # A tibble: 6 × 5
## Index tw0050 tw0056 tw006205 tw00646
## <date> <dbl> <dbl> <dbl> <dbl>
## 1 2015-12-14 53.3 18.2 31.1 19.6
## 2 2015-12-15 53.3 18.4 31.6 19.6
## 3 2015-12-16 54.1 18.6 31.6 19.9
## 4 2015-12-17 54.8 18.8 32.2 20.0
## 5 2015-12-18 54.5 19.0 32.2 19.8
## 6 2015-12-21 54.4 19.0 33 19.6
##Question1
cov <- cov(data[,2:5])
one <- rep(1,4)
one_41<- matrix(one,ncol=1)
nume <- inv(cov)%*%one_41
deno <- t(one_41)%*%nume
mvp_daily <- nume / as.vector(deno)
as_tibble(mvp_daily)
## Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if
## `.name_repair` is omitted as of tibble 2.0.0.
## ℹ Using compatibility `.name_repair`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## # A tibble: 4 × 1
## V1
## <dbl>
## 1 -0.281
## 2 1.08
## 3 0.136
## 4 0.0648
colnames(mvp_daily) = "Weight"
mvp_daily
## Weight
## tw0050 -0.28080456
## tw0056 1.07989097
## tw006205 0.13606491
## tw00646 0.06484868
mean_return <- colMeans(data[,2:5])
mean <- t(as.matrix(mean_return))
realized_return <- mean%*%mvp_daily
as_tibble(realized_return)
## # A tibble: 1 × 1
## Weight
## <dbl>
## 1 9.89
colnames(realized_return) = "Daily Realized Return"
realized_return
## Daily Realized Return
## [1,] 9.892485
##Question2
month_data <- to.monthly(data)
colnames(month_data) = c("tw0050","tw0056","tw006205","tw00646")
head(month_data)
## tw0050 tw0056 tw006205 tw00646
## Dec 2015 53.29 19.02 31.06 20.06
## Jan 2016 53.42 18.60 25.40 19.28
## Feb 2016 53.42 19.41 25.46 19.21
## Mar 2016 55.49 19.70 25.41 19.71
## Apr 2016 57.32 19.23 26.98 19.90
## May 2016 54.50 18.95 26.02 20.34
cov_mon <- cov(month_data[1:4]) #4x4
nume_mon <- inv(cov_mon)%*%one_41
deno_mon <- t(one_41)%*%nume_mon
mvp_mon <- nume_mon / as.vector(deno_mon)
as_tibble(mvp_mon)
## # A tibble: 4 × 1
## V1
## <dbl>
## 1 -0.269
## 2 1.01
## 3 0.127
## 4 0.128
colnames(mvp_mon) = "Weight"
mvp_mon
## Weight
## tw0050 -0.2689932
## tw0056 1.0143129
## tw006205 0.1268862
## tw00646 0.1277941
mean_return_mon <- colMeans(month_data[,1:4])
mean_mon <- t(as.matrix(mean_return_mon))
realized_return_mon <- mean_mon%*%mvp_mon
as_tibble(realized_return_mon)
## # A tibble: 1 × 1
## Weight
## <dbl>
## 1 10.7
colnames(realized_return_mon) = "Monthly Realized Return"
realized_return_mon
## Monthly Realized Return
## [1,] 10.74154
##Question3
nume_t <- inv(cov_mon)%*%mean_return_mon # 4x4 x 4x1 = 4x1
deno_t <- t(one_41)%*%nume_t # 1x4 x 4x1
port_t <- nume_t/as.vector(deno_t)
as_tibble(port_t)
## # A tibble: 4 × 1
## V1
## <dbl>
## 1 -0.236
## 2 0.883
## 3 0.192
## 4 0.161
colnames(port_t)= "Weight"
port_t
## Weight
## tw0050 -0.2359199
## tw0056 0.8827621
## tw006205 0.1923066
## tw00646 0.1608512