R Markdown

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:

library(PerformanceAnalytics)
## 载入需要的程辑包:xts
## 载入需要的程辑包:zoo
## 
## 载入程辑包:'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## 载入程辑包:'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
library(quantmod) 
## 载入需要的程辑包:TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(xts) 
library(dygraphs)
library(tibble)
library(tidyr) 
library(ggplot2) 
library(highcharter) 
library(tidyquant)
## 载入需要的程辑包:lubridate
## 
## 载入程辑包:'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(plotly) # To create interactive charts
## 
## 载入程辑包:'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(timetk) # To manipulate the data series
library(forcats)
#NS TH HL GD
price <-read.csv("C:/Users/Lenovo/Documents/WeChat Files/wxid_bzeg6bzaoa8512/FileStorage/File/2023-04/11111111111111.csv")
price[,1] = as.POSIXct(price[,1])
price = xts(price[,-1], order.by=price[,1])
names(price)<-c("NS","TH","HL","GD")
dygraph(price) 
#plot(price)
#pr_monthly <- to.monthly(price, indexAt = "lastof", OHLC = FALSE) 
#head(pr_monthly)
#rt_monthly <- na.omit(Return.calculate(pr_monthly, method = "log")) 
head(price)
##               NS    TH    HL   GD
## 2022-01-04 25.47 10.03 20.26 9.70
## 2022-01-05 24.16  9.60 19.47 9.77
## 2022-01-06 24.53  8.50 19.49 9.66
## 2022-01-07 23.19  8.29 19.35 9.53
## 2022-01-10 22.30  8.76 19.61 9.50
## 2022-01-11 20.50  8.30 20.00 9.44
pr_daily <- to.daily(price, indexAt = "lastof", OHLC = FALSE) 
head(pr_daily)
##               NS    TH    HL   GD
## 2022-01-31 25.47 10.03 20.26 9.70
## 2022-01-31 24.16  9.60 19.47 9.77
## 2022-01-31 24.53  8.50 19.49 9.66
## 2022-01-31 23.19  8.29 19.35 9.53
## 2022-01-31 22.30  8.76 19.61 9.50
## 2022-01-31 20.50  8.30 20.00 9.44
rt_daily <- na.omit(Return.calculate(pr_daily, method = "log")) 

head(rt_daily)
##                     NS          TH           HL           GD
## 2022-01-31 -0.05280292 -0.04381750 -0.039773679  0.007190581
## 2022-01-31  0.01519849 -0.12169693  0.001026694 -0.011322818
## 2022-01-31 -0.05617571 -0.02501619 -0.007209094 -0.013548931
## 2022-01-31 -0.03913447  0.05514594  0.013347221 -0.003152919
## 2022-01-31 -0.08416179 -0.05394039  0.019692633 -0.006335818
## 2022-01-31  0.02600050  0.04706751  0.012422520  0.006335818
dygraph(rt_daily)
mean_ret <- colMeans(rt_daily)
print(round(mean_ret, 4))
##      NS      TH      HL      GD 
## -0.0023 -0.0043 -0.0021 -0.0024
cov_mat <- cov(rt_daily) * 252
print(round(cov_mat,4)) 
##        NS     TH     HL     GD
## NS 0.2788 0.1010 0.0290 0.0482
## TH 0.1010 0.3079 0.0353 0.0611
## HL 0.0290 0.0353 0.1414 0.0222
## GD 0.0482 0.0611 0.0222 0.0667
tick <- c("NS","TH","HL","GD")
wts <- runif(n = length(tick))
wts <- wts/sum(wts)
print(wts)
## [1] 0.0578379 0.4029199 0.3845357 0.1547066
port_returns <- (sum(wts * mean_ret) + 1)^252 - 1
port_risk <- sqrt(t(wts) %*% (cov_mat %*% wts))
sharpe_ratio <- port_returns/port_risk


num_port <- 100

# Creating a matrix to store the weights

all_wts <- matrix(nrow = num_port,
                  ncol = length(tick))

# Creating an empty vector to store
# Portfolio returns

port_returns <- vector('numeric', length = num_port)

# Creating an empty vector to store
# Portfolio Standard deviation

port_risk <- vector('numeric', length = num_port)

# Creating an empty vector to store
# Portfolio Sharpe Ratio

sharpe_ratio <- vector('numeric', length = num_port)

for (i in seq_along(port_returns)) {
  
  wts <- runif(length(tick))
  wts <- wts/sum(wts)
  
  # Storing weight in the matrix
  all_wts[i,] <- wts
  
  # Portfolio returns
  
  port_ret <- sum(wts * mean_ret)
  port_ret <- ((port_ret + 1)^252) - 1
  
  # Storing Portfolio Returns values
  port_returns[i] <- port_ret
  
  
  # Creating and storing portfolio risk
  port_sd <- sqrt(t(wts) %*% (cov_mat  %*% wts))
  port_risk[i] <- port_sd
  
  # Creating and storing Portfolio Sharpe Ratios
  # Assuming 0% Risk free rate
  
  sr <- port_ret/port_sd
  sharpe_ratio[i] <- sr
}

# Storing the values in the table
portfolio_values <- tibble(Return = port_returns,
                           Risk = port_risk,
                           SharpeRatio = sharpe_ratio)


# Converting matrix to a tibble and changing column names
all_wts <- tk_tbl(all_wts)
## Warning in tk_tbl.data.frame(as.data.frame(data), preserve_index, rename_index,
## : Warning: No index to preserve. Object otherwise converted to tibble
## successfully.
colnames(all_wts) <- colnames(pr_daily)
# Combing all the values together
portfolio_values <- tk_tbl(cbind(all_wts, portfolio_values))
## Warning in tk_tbl.data.frame(cbind(all_wts, portfolio_values)): Warning: No
## index to preserve. Object otherwise converted to tibble successfully.
min_var <- portfolio_values[which.min(portfolio_values$Risk),]
max_sr <- portfolio_values[which.max(portfolio_values$SharpeRatio),]


p <- min_var %>%
  gather(NS:TH, key = Asset,
         value = Weights) %>%
  mutate(Asset = as.factor(Asset)) %>%
  ggplot(aes(x = fct_reorder(Asset,Weights), y = Weights, fill = Asset)) +
  geom_bar(stat = 'identity') +
  theme_minimal() +
  labs(x = 'Assets', y = 'Weights', title = "Minimum Variance Portfolio Weights") +
  scale_y_continuous(labels = scales::percent) 


ggplotly(p)
p <- max_sr %>%
  gather(NS:TH, key = Asset,
         value = Weights) %>%
  mutate(Asset = as.factor(Asset)) %>%
  ggplot(aes(x = fct_reorder(Asset,Weights), y = Weights, fill = Asset)) +
  geom_bar(stat = 'identity') +
  theme_minimal() +
  labs(x = 'Assets', y = 'Weights', title = "Tangency Portfolio Weights") +
  scale_y_continuous(labels = scales::percent) 


ggplotly(p)
p <- portfolio_values %>%
  ggplot(aes(x = Risk, y = Return, color = SharpeRatio)) +
  geom_point() +
  theme_classic() +
  scale_y_continuous(labels = scales::percent) +
  scale_x_continuous(labels = scales::percent) +
  labs(x = 'Annualized Risk',
       y = 'Annualized Returns',
       title = "Portfolio Optimization & Efficient Frontier") +
  geom_point(aes(x = Risk,
                 y = Return), data = min_var, color = 'red') +
  geom_point(aes(x = Risk,
                 y = Return), data = max_sr, color = 'red') #+
#annotate('text', x = 3.2, y = 550, label = "Tangency Portfolio") +
#annotate('text', x = 1.3, y = 40, label = "Min Var portfolio") #+
#annotate(geom = 'segment', x = 0.2, xend = 0.185,  y = 0.03, 
#         yend = 0.11, color = 'red', arrow = arrow(type = "open")) +
#annotate(geom = 'segment', x = 0.285, xend = 0.26,  y = 0.34, 
#         yend = 0.31, color = 'red', arrow = arrow(type = "open"))

ggplotly(p)