I will estimate market risk with VaR model with R. I used Dell stock price.
Data and packages
library(quantmod)
library(dygraphs)
library(tseries)
library(tidyverse)
getSymbols(Symbols = "DELL",from='2016-08-17',auto.assign = T)
## [1] "DELL"
## DELL.Open DELL.High DELL.Low DELL.Close DELL.Volume DELL.Adjusted
## 2020-02-07 52.50 52.50 51.00 51.62 4722800 51.62
## 2020-02-10 51.00 52.17 50.56 52.11 2700000 52.11
## 2020-02-11 52.10 53.10 52.00 52.26 4402400 52.26
## 2020-02-12 52.73 53.52 52.58 53.30 3484100 53.30
## 2020-02-13 51.95 52.83 51.43 52.55 3426500 52.55
## 2020-02-14 52.95 53.55 52.63 52.88 2107600 52.88
## An 'xts' object on 2016-08-17/2020-02-14 containing:
## Data: num [1:880, 1:6] 24.1 24.4 23.6 23.8 24.2 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:6] "DELL.Open" "DELL.High" "DELL.Low" "DELL.Close" ...
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## List of 2
## $ src : chr "yahoo"
## $ updated: POSIXct[1:1], format: "2020-02-15 10:55:11"
Check normality
P<-DELL$DELL.Close
ra <- (P-lag(P))/lag(P)
rg <- log(P/lag(P))
ra = na.omit(ra)
rg = na.omit(rg)
returns<-data.frame(ra$DELL.Close,rg$DELL.Close)
head(returns)
## DELL.Close DELL.Close.1
## 2016-08-18 -0.005813976 -0.005830943
## 2016-08-19 0.017543800 0.017391684
## 2016-08-22 0.003448339 0.003442407
## 2016-08-23 0.030927780 0.030459154
## 2016-08-24 0.013333377 0.013245270
## 2016-08-25 -0.004166672 -0.004175376
## ra rg
## 2016-08-18 -0.005813976 -0.005830943
## 2016-08-19 0.017543800 0.017391684
## 2016-08-22 0.003448339 0.003442407
## 2016-08-23 0.030927780 0.030459154
## 2016-08-24 0.013333377 0.013245270
## 2016-08-25 -0.004166672 -0.004175376
## method return
## 1 ra -0.005813976
## 2 ra 0.017543800
## 3 ra 0.003448339
## 4 ra 0.030927780
## 5 ra 0.013333377
## 6 ra -0.004166672
##
## Jarque Bera Test
##
## data: ra
## X-squared = 17967, df = 2, p-value < 2.2e-16
##
## Jarque Bera Test
##
## data: rg
## X-squared = 34260, df = 2, p-value < 2.2e-16
Arithmetic return
## [1] -0.03076105