This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Plot the both buy-and-hold and the strategy cumulative returns | |
ggplot(data=df_forecasts, aes(x = date)) + | |
geom_line(aes(y = var_stra_cum_returns, color="VAR")) + | |
geom_line(aes(y = tvp_var_sv_stra_cum_returns, color="TVP-VAR-SV")) + | |
geom_line(aes(y = bnh_cum_returns,color="B&H")) + | |
geom_line(aes(y = stra_improved_cum_returns, color="Improved TVP-VAR-SV")) + | |
ggtitle("Buy and Hold and Strategies' Cumulative Returns") + xlab("Date") + ylab("Returns") + | |
theme(plot.title = element_text(hjust = 0.5, size=25), legend.position="bottom", legend.text = element_text(size=20)) + | |
scale_x_date(date_labels = "%b %y") + | |
theme( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create the VAR-based equally-weighted portfolio returns | |
df_forecasts$var_stra_returns <- rowMeans((df_forecasts[,match(tickers,colnames(df_forecasts))] * | |
df_forecasts[,match(ticker_var_forecasts,colnames(df_forecasts))]), | |
na.rm=TRUE) | |
# Set the NaN values of the strategy returns to zero | |
df_forecasts$var_stra_returns[is.na(df_forecasts$var_stra_returns)] = 0.0 | |
# Create the strategy cumulative returns | |
df_forecasts$var_stra_cum_returns <- exp(cumsum(df_forecasts$var_stra_returns)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print(paste0(strrep('=',50))) | |
print(paste0(strrep('=',50))) | |
print(paste0(strrep('=',50))) | |
print(paste0('Estimation of TVP-VAR-SV forecasts')) | |
if (length(initial_iloc_to_forecast<nrow(df_forecasts))!=0) { | |
# The for loop to estimate the model each day | |
for (i in initial_iloc_to_forecast:nrow(df_forecasts)) { | |
# Set the current iteration date |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print(paste0(strrep('=',50))) | |
print(paste0(strrep('=',50))) | |
print(paste0(strrep('=',50))) | |
print(paste0('Estimation of basic-VAR forecasts')) | |
# Check if VAR forecasts have already been estimated | |
if (length(as.numeric(rownames(tail(subset(df_forecasts, trade_done == 1) ,1))))==0) { | |
print(paste0(strrep('=',50))) | |
print(paste0(strrep('=',50))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Group the data dates by year and month | |
options(dplyr.summarise.inform = FALSE) | |
dates <- var_data %>% | |
mutate(month = format(date, "%m"), year = format(date, "%Y")) %>% | |
group_by(year, month) %>% summarise(first_dates = first(date)) | |
# Get the first date of Oct-2021 | |
initial_date = subset(dates, (dates$year=='2019') & (dates$month=='01'))$first_dates | |
# Import the Excel file in case it exists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to open an XLSX file with error handling | |
open_xlsx <- function(file_path) { | |
# Output the Excel file in case it exists | |
result <- tryCatch({ | |
# Read the Excel file | |
data <- read.xlsx(file_path) | |
# Set the date column as datetime type | |
data$date <- as.Date(data$date, origin = "1899-12-30") | |
# Return the data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set the seed to estimate the TVP-VAR-SV model | |
set.seed(2024) | |
# Estimate the TVP-VAR-SV model | |
bv <- bvar.sv.tvp(tvp_var_data, tau= 250, nf=1, nrep = 300, nburn=20) | |
# Obtain the forecasts of the model based on the mean of the posterior-distribution draws | |
forecast_ys <- rowMeans(bv$fc.ydraws[1:7,1,]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set tickers | |
tickers <- c('MSFT', 'AAPL', 'TSLA', 'NFLX', 'META', 'AMZN','GOOGL') | |
# Set start and end dates | |
start = "1990-01-01" | |
end = "2024-08-01" | |
df <- new.env() | |
# Import the data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library('TTR') | |
library('quantmod') | |
library('stats') | |
library('lubridate') | |
library('dplyr') | |
library('ggplot2') | |
library('forecast') | |
library('vars') | |
library('openxlsx') | |
library('bvarsv') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages('TTR') | |
install.packages('quantmod') | |
install.packages('stats') | |
install.packages('lubridate') | |
install.packages('dplyr') | |
install.packages('ggplot2') | |
install.packages('forecast') | |
install.packages('vars') | |
install.packages('openxlsx') | |
install.packages('bvarsv') |
NewerOlder