This file contains hidden or 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 for loop | |
for i in range(1,len(monthly_index)): | |
print('='*100) | |
# Set the current month-end variable | |
current_month_end = monthly_index[(i-1)] | |
# Set the next month-end variable | |
next_month_end = monthly_index[i] | |
# Set the span from the previous to the next month end | |
span = len(data.loc[current_month_end:next_month_end,:].index) |
This file contains hidden or 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
from statsmodels.tsa.arima.model import ARIMA |
This file contains hidden or 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
pip install pandas numpy matplotlib statsmodels yfinance |
This file contains hidden or 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
pip install pandas statsmodels matplotlib |
This file contains hidden or 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
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 |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
NewerOlder