Friday, 18 August 2017

How to calculate historical VAR using R Programming?

stockdata<-read.csv(choose.file(),header=TRUE)close<-stockdata$Close #assigning closing pricereturns<-(close[1:(length(close)-1)]-close[2:length(close)])/close[2:length(close)]#calculating returns
library(PerformanceAnalytics)
Var99<-(VaR(returns,p=0.99,method="historical")*100)#Value at Risk at 99% Confidence
Var95<-(VaR(returns,p=0.95, method = "historical")*100) #Value at risk at 95% Confidence
var90<-(VaR(returns,p=0.90,method = "historical")*100) #value at risk at 90% confidence
varmatrix<-cbind(var90,Var95,Var99)
colnames(varmatrix)<-c("var90","Var95","Var99")#creating head
varmatrix
write.csv(varmatrix,file = "VAR of stock") #exporting
barplot(varmatrix,ylab = "Risk in %",ylim = c(-1:-2)) #plotting VAR

1 comment:

  1. The main motive of the Big data solutions companies is to spread the knowledge so that they can give more big data engineers to the world.


    ReplyDelete