Value at Risk (VaR) in R Programming Language

Maung Agus Sutikno
3 min readSep 2, 2022

--

Value at risk, or VaR is a statistic that quantifies the extent of possible financial losses within a firm, portfolio, or position over a specific time frame (Investopedia, 2022). This writing is provided to give technical guidance in conducting this analysis in R programming language. Case study presented here is a risk measurement in financial sector — single asset. Enjoy!

Value at Risk (VaR)

Conceptually Value at Risk represents the projected level of losses a trading desk needs to protect itself against under extreme conditions (financetrainingcourse.com, 2011). It is used as a proxy for the amount of capital required to support such losses.

Preparation

Firstly, prepare the required library to perform the analysis.

install.packages("PerformanceAnalytics", repos = "http://cran.us.r-project.org")
install.packages("dplyr", repos = "http://cran.us.r-project.org")
install.packages("tidyquant", repos = "http://cran.us.r-project.org")
install.packages("quantmod", repos = "http://cran.us.r-project.org")
install.packages("tseries", repos = "http://cran.us.r-project.org")
install.packages("tidyverse", repos = "http://cran.us.r-project.org")
library(PerformanceAnalytics)
library(dplyr)
library(tidyquant)
library(quantmod)
library(tseries)
library(tidyverse)

Secondly, call all the stocks that we are going to analyze. Instead, using “read_csv” or putting the source link, thanks to quantmod library in making simple to get the historical stock price. Find about quantmod here.

symbol_name2 <- c("AAPL", "GOOG", "TLKM.JK", "ADHI.JK", "BBRI.JK", "GOTO.JK")

Next, determine the analysis period and choose the value we are going to download (adjusted closing price of stock).

price2 <- getSymbols(symbol_name2, from = "2020-03-01", to = "2022-08-31")%>%
map(~Ad(get(.))) %>%
reduce(merge) %>%
'colnames<-'(symbol_name2)

Analysis

Lastly, now we can get the value of the return using the logarithm calculation method and the value at risk. The VaR in this article uses historical method, which is one of the methodologies in calculating VaR other than the parametric method and Monte Carlo method. The historical method tries to sort all the returns within a certain period and gives the potential most severe losses in a certain value along the X-axis on the graph below.

VaR Historical Simulation Approach. Source: financetrainingcourse.com
return_a <- CalculateReturns(price2, method="log")

return2_a <- return_a[-1,]

hist_var_2 <- VaR(return2_a, p=0.95, method="historical")

hist_var_2

The output:

In the last pandemic period, TLKM became a stock with least risk. Its VaR is only -2.88%; this means the risk of loss in the more than 2 years is -2.88%. While AAPL and GOOG are in the VaR -3.4% of VaR, another tech stock from Indonesia, GOTO, has the most severe risk among the analyzed stocks. Its VaR is -6.8%. The risk GOTO has is higher than another Indonesian tech company, TLKM.

If we conduct the same analysis for 2 years period before COVID-19 official announcement by WHO, it is found that this pandemic made the VaR more severe. AAPL, GOOG, TLKM, ADHI, and BBRI have VaR of -3.01%, -2.7%, -2.65%, -3.86%, and -3.29% respectively.

--

--