This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.6 ✓ dplyr 1.0.8
## ✓ tidyr 1.2.0 ✓ stringr 1.4.0
## ✓ readr 2.1.2 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(pacman)
library(xts)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
##
## first, last
library(dplyr)
library(ggplot2)
library(devtools)
## Loading required package: usethis
library(curl)
## Using libcurl 7.68.0 with OpenSSL/1.1.1f
##
## Attaching package: 'curl'
## The following object is masked from 'package:readr':
##
## parse_date
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)
library(SIT)
## Loading required package: SIT.date
## Loading required package: quantmod
## 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 objects are masked from 'package:dplyr':
##
## count, lst
## The following object is masked from 'package:purrr':
##
## cross
## The following object is masked from 'package:tibble':
##
## lst
## The following object is masked from 'package:base':
##
## close
Data <- read.csv("/cloud/project/HW5.csv", header=FALSE)
nrow(Data)
## [1] 4674
p_load(quantmod, quadprog, lpSolve)
cols <- 1:11
Data[cols] <- lapply(Data[cols], as.numeric)
## Warning in lapply(Data[cols], as.numeric): NAs introduced by coercion
## Warning in lapply(Data[cols], as.numeric): NAs introduced by coercion
## Warning in lapply(Data[cols], as.numeric): NAs introduced by coercion
## Warning in lapply(Data[cols], as.numeric): NAs introduced by coercion
## Warning in lapply(Data[cols], as.numeric): NAs introduced by coercion
## Warning in lapply(Data[cols], as.numeric): NAs introduced by coercion
## Warning in lapply(Data[cols], as.numeric): NAs introduced by coercion
## Warning in lapply(Data[cols], as.numeric): NAs introduced by coercion
## Warning in lapply(Data[cols], as.numeric): NAs introduced by coercion
## Warning in lapply(Data[cols], as.numeric): NAs introduced by coercion
## Warning in lapply(Data[cols], as.numeric): NAs introduced by coercion
date <- seq(as.Date("1930-01-01"), length= 4674, by="1 month") -1
Data <- xts(coredata(Data[,-1])/100, order.by = date)
head(Data)
## V2 V3 V4 V5 V6 V7 V8 V9
## 1929-12-31 NA NA NA NA NA NA NA NA
## 1930-01-31 0.1933 0.1521 0.0560 0.1072 0.1007 0.0894 0.0721 0.0834
## 1930-02-28 0.1734 0.1604 0.1250 0.0878 0.1100 0.0611 0.0546 0.0694
## 1930-03-31 0.0279 -0.0367 -0.0344 -0.0980 -0.1023 -0.0899 -0.1162 -0.0550
## 1930-04-30 -0.0983 -0.1752 -0.1306 -0.1268 -0.1169 -0.1011 -0.1038 -0.0606
## 1930-05-31 -0.2398 -0.1644 -0.1852 -0.1285 -0.1365 -0.1359 -0.1113 -0.1198
## V10 V11
## 1929-12-31 NA NA
## 1930-01-31 0.0547 0.0362
## 1930-02-28 0.1141 0.1589
## 1930-03-31 -0.0475 -0.0467
## 1930-04-30 -0.1231 -0.0914
## 1930-05-31 -0.1655 -0.1296
Data.price <- (Data +1 )*100
head(Data.price)
## V2 V3 V4 V5 V6 V7 V8 V9 V10
## 1929-12-31 NA NA NA NA NA NA NA NA NA
## 1930-01-31 119.33 115.21 105.60 110.72 110.07 108.94 107.21 108.34 105.47
## 1930-02-28 117.34 116.04 112.50 108.78 111.00 106.11 105.46 106.94 111.41
## 1930-03-31 102.79 96.33 96.56 90.20 89.77 91.01 88.38 94.50 95.25
## 1930-04-30 90.17 82.48 86.94 87.32 88.31 89.89 89.62 93.94 87.69
## 1930-05-31 76.02 83.56 81.48 87.15 86.35 86.41 88.87 88.02 83.45
## V11
## 1929-12-31 NA
## 1930-01-31 103.62
## 1930-02-28 115.89
## 1930-03-31 95.33
## 1930-04-30 90.86
## 1930-05-31 87.04
Data.1 <- Data.price['199912/202003']
head(Data.1)
## V2 V3 V4 V5 V6 V7 V8 V9 V10
## 1999-12-31 107.95 106.76 106.52 108.60 107.84 109.09 106.80 100.86 104.83
## 2000-01-31 107.72 105.20 98.53 98.66 95.91 98.92 100.20 98.29 100.29
## 2000-02-29 101.23 101.23 103.05 98.46 102.46 99.75 99.05 98.25 95.14
## 2000-03-31 95.41 96.50 98.58 98.36 99.77 95.13 96.84 96.31 95.65
## 2000-04-30 105.29 106.17 105.78 106.75 103.66 103.68 103.80 104.01 104.91
## 2000-05-31 105.64 106.65 104.32 101.77 102.90 104.31 103.23 102.05 101.35
## V11
## 1999-12-31 97.03
## 2000-01-31 108.21
## 2000-02-29 84.77
## 2000-03-31 89.99
## 2000-04-30 112.59
## 2000-05-31 98.97
data <- new.env()
data$prices <- Data.1
data$weight <- Data.1
data$execution.price <- data$prices
data$execution.price[] <- NA
data$symbolnames <- colnames(data$prices)
prices <- data$prices
n <- ncol(prices)
data$weight <- ntop(prices, n)
# model <- list()
# model$equal.weight <- bt.run(data$weight, trade.summary = T)
# head(equal.weight)
# plotbt.monthly.table(model$equal.weight$equity)
# plotbt.transition.map(model$equal.weight$weight)
# strategy.performance.snapshoot(model, T)
#prices.2 <- Data.price["199701/202003"]
#head(prices.2)
#tail(prices.2)
#data$prices <- prices.2
#data$weight <- prices.2
#data$execution.price <-prices.2 * NA
#prices.3 <- data$prices
#constraints = new.constraints(n, lb = -Inf, ub = +Inf)
#constraints = add.constraints(rep(1, n), 1, type = '=', constraints)
#ret = prices.3 / mlag(prices) - 1
#weight = coredata(prices.3)
#weight[] = NA
#nrow(prices.3)
#hist <- na.omit(ret[1:36,])
#cov(hist)
#ia = create.historical.ia(hist,12)
#s0 = apply(coredata(hist), 2 , sd)
#ia$cov = cor(coredata(hist), use='complete.obs',method='pearson') * (s0 %*% t(s0))
#weight[36,] = min.risk.portfolio(ia, constraints)
#weight[36,]
#sum(weight[36,])
#model$min.var.monthly <- bt.run(data, trade.summary = T)
#sum(as.numeric(weight[36,])*as.numeric(ret[37,]))
#model$min.var.monthly$ret[37, ]
#plotbt.custom.report.part1(model$min.var.monthly, model$equal.weight)
#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)
#model <- rev(model)
#plotbt.custom.report(model)
Note that the echo = FALSE
parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.