Skip to content

Instantly share code, notes, and snippets.

View quantra-go-algo's full-sized avatar

Algorithmic Trading quantra-go-algo

View GitHub Profile
@quantra-go-algo
quantra-go-algo / cointegration_check_gold_price_prediction.py
Created November 19, 2024 05:47
In this code, you will check whether the non-stationary independent variables are cointegrated or not
S_3 = Df['S_3'].dropna()
S_9 = Df['S_9'].dropna()
# Since S_3 and S_9 are moving averages, we could check their cointegration with the original Close series
close_price = Df['next_day_price']
# Conduct Engle-Granger Cointegration Test between S_3 and Close price
coint_result_3 = coint(S_3, close_price)
# Conduct Engle-Granger Cointegration Test between S_9 and Close price
# 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(
# 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))
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
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)))
# 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
# 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
# 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,])
# 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
library('TTR')
library('quantmod')
library('stats')
library('lubridate')
library('dplyr')
library('ggplot2')
library('forecast')
library('vars')
library('openxlsx')
library('bvarsv')