#1-Exponentially Weighted Moving Average (EWMA)

library("Quandl")
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library("tseries")
er<-Quandl("ECB/EURUSD", type="zoo")
ler<-log(er)
par(mfrow=c(1,2))
plot(ler,xlab="Time", ylab="Log of EURUSD")
dler <- diff(ler) 
plot(dler,xlab="Time", ylab="Log Differences of EURUSD")

Edler <- mean(dler)
t <- index(dler)
library(fTrading)
## Loading required package: timeDate
## Loading required package: timeSeries
## 
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
## 
##     time<-
## Loading required package: fBasics
## 
## Rmetrics Package fBasics
## Analysing Markets and calculating Basic Statistics
## Copyright (C) 2005-2014 Rmetrics Association Zurich
## Educational Software for Financial Engineering and Computational Science
## Rmetrics is free software and comes with ABSOLUTELY NO WARRANTY.
## https://www.rmetrics.org --- Mail to: info@rmetrics.org
dler.ewma <- EWMA((dler-Edler)^2,lambda=.05)
plot(dler.ewma, type="l", main="Exponentially Weighted Moving Average")

#2- GARCH(1,1)
library("urca")
library("fGarch")
m1 <- garchFit( ~ garch(1,1), data=dler, trace=FALSE) 
# Checking Adequacy
par(mfrow=c(2,2), cex=0.6, mar=c(2,2,3,1))
plot(m1@residuals, type="l", xlab="", ylab="", main="residuals")
plot(m1@sigma.t, type="l", xlab="", ylab="", main="conditional standard deviation") 
plot(m1@residuals/m1@sigma.t, type="l", xlab="", ylab="", main="standardized residuals")
plot((m1@residuals/m1@sigma.t)^2, type="l", xlab="", ylab="", main="squared standardized residuals")

library("tseries")
Quandl.auth(" 3HQPzReHC1WfzuSG41gy")
## Warning: 'Quandl.auth' is deprecated.
## Use 'Quandl.api_key' instead.
## See help("Deprecated")
Box.test( (m1@residuals/m1@sigma.t)^2, lag = 15, type = "Ljung")
## 
##  Box-Ljung test
## 
## data:  (m1@residuals/m1@sigma.t)^2
## X-squared = 7.4533, df = 15, p-value = 0.9438
#(b) Consider first the 5% VaR. For each of the models plot the log change and the in the sample 5% VaR. Calculate the fraction of times in the sample where the log change falls below the 5% VaR. Repeat the
#same with 1% VaR.
#1-With 5%
VaR.5.EWMA <- Edler - 1.645*sqrt(dler.ewma)
VaR.5.garch <- Edler - 1.645*m1@sigma.t 
t<-index(dler)
par(mfrow=c(1,2), cex=0.8)
plot(t, dler, type="l", col="steelblue", main="5% VaR - Exponentially Weighted Moving Average") 
lines(t, VaR.5.EWMA, type="l", col="red")
plot(t, dler, type="l", col="steelblue", main="5% VaR - garch")
lines(t, VaR.5.garch, type="l", col="red")

#now we will Calculate the fraction of times in the sample where the log change falls below the 5% VaR in both models.
sum(dler < VaR.5.EWMA) / length(dler)
## [1] 0.046269
sum(dler < VaR.5.garch ) / length(dler)
## [1] 0.04672261

fraction of sample where loss exceeds 5% VaR for both models.the var of 5% is good.

#1-With 1%
VaR.1.EWMA <- Edler - 2.326*sqrt(dler.ewma)
VaR.1.garch <- Edler - 2.326*m1@sigma.t 
t<-index(dler)
par(mfrow=c(1,2), cex=0.8)
plot(t, dler, type="l", col="steelblue", main="1% VaR - Exponentially Weighted Moving Average") 
lines(t, VaR.1.EWMA, type="l", col="red")
plot(t, dler, type="l", col="steelblue", main="1% VaR - garch")
lines(t, VaR.1.garch, type="l", col="red")

#now we will Calculate the fraction of times in the sample where the log change falls below the 1% VaR in both models.
sum(dler < VaR.1.EWMA) / length(dler)
## [1] 0.00748469
sum(dler < VaR.1.garch ) / length(dler)
## [1] 0.01315491
#  fraction of sample where loss exceeds 5% VaR for both models.the var of 1% is good.