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
Quandl.api_key("4-KG5x_Vo7rXzmZNAHch")
library("tseries")
library("urca")
library(fGarch)
## 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
library("fTrading")
eusd <- Quandl("ECB/EURUSD", type ="zoo")
head(eusd)
## 1999-01-04 1999-01-05 1999-01-06 1999-01-07 1999-01-08 1999-01-11
## 1.1789 1.1790 1.1743 1.1632 1.1659 1.1569
tail(eusd)
## 2016-03-15 2016-03-16 2016-03-17 2016-03-18 2016-03-21 2016-03-22
## 1.1109 1.1064 1.1311 1.1279 1.1271 1.1212
leusd <- log(eusd)
dleusd <- diff(leusd)
mdleusd <- mean(dleusd)
idleus <- index(dleusd)
dleusd1 <- SMA((dleusd-mdleusd)^2,n=30)
dleusd2 <- SMA((dleusd-mdleusd)^2,n=180)
dleusdw <- EWMA((dleusd - mdleusd)^2, lambda=0.05)
par(mfrow=c(2,2), cex=0.5, mar = c(2,2,3,1))
plot((dleusd-mdleusd)^2)
plot(dleusd1)
plot(dleusd2)
plot(dleusdw)

garch1 <- garchFit(~ garch(1,1), data=dleusd, trace=F)
garch1
##
## Title:
## GARCH Modelling
##
## Call:
## garchFit(formula = ~garch(1, 1), data = dleusd, trace = F)
##
## Mean and Variance Equation:
## data ~ garch(1, 1)
## <environment: 0x7f8336b2b2d8>
## [data = dleusd]
##
## Conditional Distribution:
## norm
##
## Coefficient(s):
## mu omega alpha1 beta1
## 4.9386e-05 1.2312e-07 2.8629e-02 9.6888e-01
##
## Std. Errors:
## based on Hessian
##
## Error Analysis:
## Estimate Std. Error t value Pr(>|t|)
## mu 4.939e-05 8.533e-05 0.579 0.56276
## omega 1.231e-07 4.207e-08 2.926 0.00343 **
## alpha1 2.863e-02 3.076e-03 9.307 < 2e-16 ***
## beta1 9.689e-01 3.155e-03 307.084 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Log Likelihood:
## 16242.7 normalized: 3.683987
##
## Description:
## Wed Mar 23 17:35:36 2016 by user:
summary(garch1)
##
## Title:
## GARCH Modelling
##
## Call:
## garchFit(formula = ~garch(1, 1), data = dleusd, trace = F)
##
## Mean and Variance Equation:
## data ~ garch(1, 1)
## <environment: 0x7f8336b2b2d8>
## [data = dleusd]
##
## Conditional Distribution:
## norm
##
## Coefficient(s):
## mu omega alpha1 beta1
## 4.9386e-05 1.2312e-07 2.8629e-02 9.6888e-01
##
## Std. Errors:
## based on Hessian
##
## Error Analysis:
## Estimate Std. Error t value Pr(>|t|)
## mu 4.939e-05 8.533e-05 0.579 0.56276
## omega 1.231e-07 4.207e-08 2.926 0.00343 **
## alpha1 2.863e-02 3.076e-03 9.307 < 2e-16 ***
## beta1 9.689e-01 3.155e-03 307.084 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Log Likelihood:
## 16242.7 normalized: 3.683987
##
## Description:
## Wed Mar 23 17:35:36 2016 by user:
##
##
## Standardised Residuals Tests:
## Statistic p-Value
## Jarque-Bera Test R Chi^2 466.1973 0
## Shapiro-Wilk Test R W 0.9898003 0
## Ljung-Box Test R Q(10) 6.211497 0.7971921
## Ljung-Box Test R Q(15) 15.93686 0.3862601
## Ljung-Box Test R Q(20) 18.90362 0.5280986
## Ljung-Box Test R^2 Q(10) 7.002786 0.7251819
## Ljung-Box Test R^2 Q(15) 7.453321 0.9438293
## Ljung-Box Test R^2 Q(20) 12.13936 0.9111956
## LM Arch Test R TR^2 7.083393 0.8520537
##
## Information Criterion Statistics:
## AIC BIC SIC HQIC
## -7.366160 -7.360361 -7.366161 -7.364115
v5 <- mdleusd - 1.645 * sqrt(dleusdw)
v5g <- mdleusd - 1.645 * garch1@sigma.t
plot(idleus, dleusd, main ="5% VAR")
lines(idleus, v5, col = "blue")
plot(idleus, dleusd, main ="5% VAR garch")
lines(idleus, v5g, col ="blue")
ve <-mdleusd - 2.326 * sqrt(dleusd)
## Warning in sqrt(dleusd): NaNs produced
vg <- mdleusd - 2.326 * garch1@sigma.t
plot(idleus, dleusd, main = "1% VAR garch")
lines(idleus, vg, col = "blue")
plot(idleus, dleusd, main = "1% VAR garch")
lines(idleus, vg, col ="blue")

min(dleusd)
## [1] -0.04735441
min(vg)
## [1] -0.0345306
min(v5)
## [1] -0.02911416
index(dleusd[dleusd==min(dleusd)])
## [1] "2008-12-19"
min(v5g)
## [1] -0.02442415
min(ve)
## [1] NaN