Skip to content

Instantly share code, notes, and snippets.

@quantra-go-algo
Created August 28, 2024 20:20
Show Gist options
  • Save quantra-go-algo/f7b1e5b11b0cf4abda476334b53efab3 to your computer and use it in GitHub Desktop.
Save quantra-go-algo/f7b1e5b11b0cf4abda476334b53efab3 to your computer and use it in GitHub Desktop.
# 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
df_forecasts <- open_xlsx("df_results_tvp_var.xlsx")
# If data is a Null value
if (is.null(df_forecasts)) {
# Subset the df2 dataframe to only our forecast data
df_forecasts <- subset(var_data, var_data$date>=as.Date(initial_date))
# Create names for each ticker's VAR forecasts
ticker_var_forecasts <- lapply(tickers, paste0, "_var_signal")
# Create names for each ticker's TVP-VAR-SV forecasts
ticker_tvp_var_forecasts <- lapply(tickers, paste0, "_tvp_var_signal")
# Create a new temp_df dataframe for the tickers' forecasts
temp_df <- data.frame(matrix(ncol = 2*length(tickers), nrow = length(df_forecasts$date)))
colnames(temp_df) <- c(ticker_var_forecasts,ticker_tvp_var_forecasts)
# Join both dataframes in the df_forecasts dataframe
df_forecasts <- cbind(df_forecasts,temp_df)
df_forecasts$trade_done <- 0
# Set the initial index number for the forecast data
initial_iloc_to_forecast <- which(df_forecasts$date==as.Date(initial_date))
# If data was obtained from the Excel file
} else {
# Create names for each ticker's VAR forecasts
ticker_var_forecasts <- lapply(tickers, paste0, "_var_signal")
# Create names for each ticker's TVP-VAR-SV forecasts
ticker_tvp_var_forecasts <- lapply(tickers, paste0, "_tvp_var_signal")
# Set the initial index number for the forecast data
initial_iloc_to_forecast <- which(df_forecasts$date == head(subset(df_forecasts, trade_done == 0) ,1)$date)
}
# Set the span to 6 years of data
span <- 1500
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment