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)
library(fTrading)
## Warning: package 'fTrading' was built under R version 3.2.4
## Loading required package: timeDate
## Loading required package: timeSeries
##
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
##
## time<-
## Loading required package: fBasics
## Warning: package 'fBasics' was built under R version 3.2.4
##
## 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
library(fGarch)
## Warning: package 'fGarch' was built under R version 3.2.4
library(urca)
## Warning: package 'urca' was built under R version 3.2.4
eu<-Quandl("ECB/EURUSD",api_key="5NyVfHfxdScCYsskDP59", type="zoo")
str(eu)
## 'zoo' series from 1999-01-04 to 2016-03-23
## Data: num [1:4411] 1.18 1.18 1.17 1.16 1.17 ...
## Index: Date[1:4411], format: "1999-01-04" "1999-01-05" "1999-01-06" "1999-01-07" ...
leu<-log(eu)
plot(leu,xlab="Time Period", ylab="Log of EUR/USD")
dleu <- diff(leu)
plot(dleu,xlab="Time Period", ylab="Log Difference of EUR/USD")
mdleu<-mean(dleu)
ind<-index(dleu)
sdleu <- SMA((dleu-mdleu)^2,n=100)
ewma.dleu<- EWMA((dleu - mdleu)^2,lambda=0.05)
par(mfrow=c(1,2))
plot((dleu-mdleu)^2)
plot(ewma.dleu, type="l", main="EWMA")
gar11 <- garchFit( ~ garch(1,1), data=dleu, trace=FALSE)
gar11
##
## Title:
## GARCH Modelling
##
## Call:
## garchFit(formula = ~garch(1, 1), data = dleu, trace = FALSE)
##
## Mean and Variance Equation:
## data ~ garch(1, 1)
## <environment: 0x00000000186636e0>
## [data = dleu]
##
## Conditional Distribution:
## norm
##
## Coefficient(s):
## mu omega alpha1 beta1
## 4.8940e-05 1.2312e-07 2.8586e-02 9.6892e-01
##
## Std. Errors:
## based on Hessian
##
## Error Analysis:
## Estimate Std. Error t value Pr(>|t|)
## mu 4.894e-05 8.532e-05 0.574 0.5663
## omega 1.231e-07 4.203e-08 2.929 0.0034 **
## alpha1 2.859e-02 3.070e-03 9.311 <2e-16 ***
## beta1 9.689e-01 3.150e-03 307.567 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Log Likelihood:
## 16246.55 normalized: 3.684025
##
## Description:
## Wed Mar 23 19:21:32 2016 by user: ASMSHAKIL
summary(gar11)
##
## Title:
## GARCH Modelling
##
## Call:
## garchFit(formula = ~garch(1, 1), data = dleu, trace = FALSE)
##
## Mean and Variance Equation:
## data ~ garch(1, 1)
## <environment: 0x00000000186636e0>
## [data = dleu]
##
## Conditional Distribution:
## norm
##
## Coefficient(s):
## mu omega alpha1 beta1
## 4.8940e-05 1.2312e-07 2.8586e-02 9.6892e-01
##
## Std. Errors:
## based on Hessian
##
## Error Analysis:
## Estimate Std. Error t value Pr(>|t|)
## mu 4.894e-05 8.532e-05 0.574 0.5663
## omega 1.231e-07 4.203e-08 2.929 0.0034 **
## alpha1 2.859e-02 3.070e-03 9.311 <2e-16 ***
## beta1 9.689e-01 3.150e-03 307.567 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Log Likelihood:
## 16246.55 normalized: 3.684025
##
## Description:
## Wed Mar 23 19:21:32 2016 by user: ASMSHAKIL
##
##
## Standardised Residuals Tests:
## Statistic p-Value
## Jarque-Bera Test R Chi^2 466.6279 0
## Shapiro-Wilk Test R W 0.9897999 0
## Ljung-Box Test R Q(10) 6.092073 0.8074692
## Ljung-Box Test R Q(15) 15.84888 0.3921656
## Ljung-Box Test R Q(20) 18.82087 0.5334958
## Ljung-Box Test R^2 Q(10) 7.006499 0.7248313
## Ljung-Box Test R^2 Q(15) 7.460486 0.9435906
## Ljung-Box Test R^2 Q(20) 12.13262 0.9114356
## LM Arch Test R TR^2 7.093526 0.8513715
##
## Information Criterion Statistics:
## AIC BIC SIC HQIC
## -7.366235 -7.360438 -7.366237 -7.364191
With 5%:
var5.ewma <- mdleu - 1.6 * sqrt(dleu)
## Warning in sqrt(dleu): NaNs produced
var5.gar <- mdleu - 1.6 * gar11@sigma.t
par(mfrow = c(1,2))
plot(ind, dleu, main = "5% VAR-EWMA")
lines(ind, var5.ewma, col = "red")
plot(ind, dleu, main = "5% VAR garch")
lines(ind, var5.gar, col ="yellow")
Hence, fraction of sample where loss is more than 5% VaR, the var of 5% performs good.
With 1%:
var1ewma <- mdleu - 2.3 * sqrt(dleu)
## Warning in sqrt(dleu): NaNs produced
var1gar11 <- mdleu - 2.3 * gar11@sigma.t
par(mfrow=c(1,2), cex=0.9)
plot(ind, dleu, main = "VaR 1%-EWMA")
lines(ind, var1ewma, col = "red")
plot(ind, dleu, main = "VAR 1%-garch")
lines(ind, var1gar11, col ="yellow")
min(dleu)
## [1] -0.04735441
min(var1ewma)
## [1] NaN
index(dleu[dleu==min(dleu)])
## [1] "2008-12-19"
Hence, a. The day with smallest ecxchange rate was 12/19/2008. b. The daily return of highest drop day was -0.04. c. The daily return on the day was -0.04.