Professional Documents
Culture Documents
Iapm Codes
Iapm Codes
library(tidyquant)
library(flextable)
library(kableExtra)
stocks <-
c("ICICIBANK.NS","AXISBANK.NS","SBIN.NS","FEDERALBNK.NS","BHEL.NS","NTPC.NS","HINDA
LCO.NS","TATAMOTORS.NS","TATASTEEL.NS") %>%
tq_get(get = "stock.prices",
from = "2008-04-01",
to = "2020-06-30") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "weekly")
# Measures how well the stock has performed in comparison to the benchmark.High
alpha is good.
alpha <- ir %>% group_by(symbol) %>%
tq_performance(
Ra = weekly.returns.x,
Rb = weekly.returns.y,
performance_fun = CAPM.alpha
)
# Jenson's alpha
j.alpha <- ir %>% group_by(symbol) %>%
tq_performance(
Ra = weekly.returns.x,
Rb = weekly.returns.y,
performance_fun = CAPM.jensenAlpha
)
# Systematic risk
sys.risk <- ir %>% group_by(symbol) %>%
tq_performance(
Ra = weekly.returns.x,
Rb = weekly.returns.y,
performance_fun = SystematicRisk
)
# Specific risk
sp.risk <- ir %>% group_by(symbol) %>%
tq_performance(
Ra = weekly.returns.x,
Rb = weekly.returns.y,
performance_fun = SpecificRisk
)
# Treynor ratio: Similar to Sharpe ratio, but uses beta as a measure of risk.
treynor <- ir %>% group_by(symbol) %>%
tq_performance(
Ra = weekly.returns.x,
Rb = weekly.returns.y,
performance_fun = TreynorRatio
)
# Tracking error
tracking <- ir %>% group_by(symbol) %>%
tq_performance(
Ra = weekly.returns.x,
Rb = weekly.returns.y,
performance_fun = TrackingError
)
# Information ratio: ratio of degree to which investment has beaten the benchmark
to the consistency.
information <- ir %>% group_by(symbol) %>%
tq_performance(
Ra = weekly.returns.x,
Rb = weekly.returns.y,
performance_fun = InformationRatio
)
# Downside frequency
sml <- ir %>% group_by(symbol) %>%
tq_performance(
Ra = weekly.returns.x,
performance_fun = DownsideFrequency,
MAR = 0.005
)
alpha,j.alpha,beta,sys.risk,sp.risk,tot.risk,treynor,appraisal,tracking,information
) %>% purrr::reduce(inner_join,by = "symbol")
###################################################################################
###########
---
title: "portfolio"
author: "Gopikumar V"
date: "11/08/2021"
output: html_document
---
```{r}
library(tidyverse)
library(tidyquant)
library(PortfolioAnalytics)
library(ROI)
library(ROI.plugin.quadprog)
library(ROI.plugin.glpk)
library(timetk)
rm(list = ls())
```
```{r}
stocks <-
c("ICICIBANK.NS","AXISBANK.NS","SBIN.NS","FEDERALBNK.NS","BHEL.NS","NTPC.NS","HINDA
LCO.NS","TATAMOTORS.NS","TATASTEEL.NS","RELIANCE.NS") %>%
tq_get(get = "stock.prices",
from = "2008-04-01",
to = "2020-06-30") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "weekly")
# Add a long only constraint such that the weight of an asset is between 0 and 1
port_spec <- add.constraint(portfolio = port_spec, type = "long_only")
```
```{r}
# Add a long only constraint such that the weight of an asset is between 0 and 1.
# This is a box constraint.
port_spec <- add.constraint(portfolio = port_spec, type = "long_only")
```
```{r}
# Diversification constraint
# port_spec <- add.constraint(portfolio=port_spec, type="diversification",
div_target=0.7)
```
```{r}
# Efficient frontier
ef <- extractEfficientFrontier(
opt,
match.col = "StdDev",
n.portfolios = 25,
risk_aversion = NULL
)
chart.EfficientFrontier(ef,
match.col="StdDev", # which column to use for risk
type="l",
RAR.text="Sharpe Ratio",
tangent.line = TRUE,
chart.assets=TRUE,
labels.assets=TRUE,
rf = 0.06/52)
```
```{r}
# Backtesting
# Nifty returns
# Compare performance
charts.PerformanceSummary(bktest)
```