Loading 02_code/R/250412_JB_DAX_returns_VaRCVaR.R 0 → 100644 +71 −0 Original line number Diff line number Diff line rm(list=ls()) # Pakete laden library("quantmod") library("lubridate") library("zoo") # Symbol symbols <- c("^GDAXI", "^GSPC","^FCHI","^FTSE") # DAX, S&P500, CAC40 Indizes start_date <- Sys.Date() - years(20) log.ret.series <- data.frame() for(symbol in symbols){ # Daten von Yahoo abrufen getSymbols(symbol, src = "yahoo", from = start_date, periodicity = "daily") # Adjusted Close Preise extrahieren prices <- Ad(get(gsub("\\^","",symbol))) prices <- na.locf(prices) # Fehlende Werte mit dem letzten Wert auffüllen # Plot the prices plot(prices, main = paste("Adjusted Close Prices of", symbol), col = "blue", lwd = 2) log.ret <- na.omit(diff(log(prices))) # logarithmische Renditen log.ret.series <- cbind(log.ret, log.ret.series) # Renditen in Dataframe speichern plot.zoo(log.ret, main = paste("Log. Returns of Adj. Close Prices of the", symbol), xlab = "Time", ylab = "Log. Returns", col = "red", type="l", lwd = 2 ) #VaR VaR <- quantile(log.ret, probs = 0.01,na.rm=TRUE)# Value at Risk (VaR) bei 1% Quantil # Horizontale Linie beim VaR abline(h = VaR, col = "blue", lty = 2, lwd = 2) # Horizontale Linie beim VaR df <- cbind(log.ret, VaR) # Renditen und VaR zusammen anzeigen) names(df) <- c("log.ret", "VaR") # Spaltennamen setzen df$less_than_VaR <- ifelse(df$log.ret < df$VaR, 1, 0) # 1 wenn log.ret < VaR, sonst 0 CVaR <- mean(df[df$less_than_VaR == 1,"log.ret"]) # Durchschnitt der Renditen, die kleiner als VaR sind cat(paste0("---------------------------",symbol,"---------------------------\n", "Anzahl der Renditen:\t\t\t", nrow(df),"\n", "Anzahl der Renditen kleiner als VaR:\t", sum(df$less_than_VaR),"\n", "Value at Risk (VaR) bei 1% Quantil:\t", round(VaR, 4),"\n", "Conditional Value at Risk (CVaR):\t", round(CVaR, 4),"\n\n" )) } colors <- c("red", "blue", "green", "purple") # Farben für die Plots # Plot der Dichte der logarithmischen Renditen plot(density(log.ret.series[,1], na.rm = TRUE), main = "Density of Logarithmic Returns", xlab = "Logarithmic Returns", ylab = "Density", xlim = c(-0.15, -0), ylim = c(0, 2), col = colors[1], lwd = 2 ) for(ii in 1:ncol(log.ret.series)){ density_x <- density(log.ret.series[,ii], na.rm = TRUE) lines(density_x, col = colors[ii], lwd = 2) } Loading
02_code/R/250412_JB_DAX_returns_VaRCVaR.R 0 → 100644 +71 −0 Original line number Diff line number Diff line rm(list=ls()) # Pakete laden library("quantmod") library("lubridate") library("zoo") # Symbol symbols <- c("^GDAXI", "^GSPC","^FCHI","^FTSE") # DAX, S&P500, CAC40 Indizes start_date <- Sys.Date() - years(20) log.ret.series <- data.frame() for(symbol in symbols){ # Daten von Yahoo abrufen getSymbols(symbol, src = "yahoo", from = start_date, periodicity = "daily") # Adjusted Close Preise extrahieren prices <- Ad(get(gsub("\\^","",symbol))) prices <- na.locf(prices) # Fehlende Werte mit dem letzten Wert auffüllen # Plot the prices plot(prices, main = paste("Adjusted Close Prices of", symbol), col = "blue", lwd = 2) log.ret <- na.omit(diff(log(prices))) # logarithmische Renditen log.ret.series <- cbind(log.ret, log.ret.series) # Renditen in Dataframe speichern plot.zoo(log.ret, main = paste("Log. Returns of Adj. Close Prices of the", symbol), xlab = "Time", ylab = "Log. Returns", col = "red", type="l", lwd = 2 ) #VaR VaR <- quantile(log.ret, probs = 0.01,na.rm=TRUE)# Value at Risk (VaR) bei 1% Quantil # Horizontale Linie beim VaR abline(h = VaR, col = "blue", lty = 2, lwd = 2) # Horizontale Linie beim VaR df <- cbind(log.ret, VaR) # Renditen und VaR zusammen anzeigen) names(df) <- c("log.ret", "VaR") # Spaltennamen setzen df$less_than_VaR <- ifelse(df$log.ret < df$VaR, 1, 0) # 1 wenn log.ret < VaR, sonst 0 CVaR <- mean(df[df$less_than_VaR == 1,"log.ret"]) # Durchschnitt der Renditen, die kleiner als VaR sind cat(paste0("---------------------------",symbol,"---------------------------\n", "Anzahl der Renditen:\t\t\t", nrow(df),"\n", "Anzahl der Renditen kleiner als VaR:\t", sum(df$less_than_VaR),"\n", "Value at Risk (VaR) bei 1% Quantil:\t", round(VaR, 4),"\n", "Conditional Value at Risk (CVaR):\t", round(CVaR, 4),"\n\n" )) } colors <- c("red", "blue", "green", "purple") # Farben für die Plots # Plot der Dichte der logarithmischen Renditen plot(density(log.ret.series[,1], na.rm = TRUE), main = "Density of Logarithmic Returns", xlab = "Logarithmic Returns", ylab = "Density", xlim = c(-0.15, -0), ylim = c(0, 2), col = colors[1], lwd = 2 ) for(ii in 1:ncol(log.ret.series)){ density_x <- density(log.ret.series[,ii], na.rm = TRUE) lines(density_x, col = colors[ii], lwd = 2) }