packages = c(‘quantmod’)
install.packages(packages, dependencies = TRUE)
lapply(packages, require, character.only = TRUE)
packages = c(‘tseries’, ‘forecast’)
lapply(packages, require, character.only = TRUE)
packages = c(‘FinTS’, ‘rugarch’)
install.packages(packages, dependencies = TRUE)
lapply(packages, require, character.only = TRUE)
stock_data <- new.env()
stock_symbol <- ‘RELIANCE.NS’ #RELIANCE.NS is the ticker symbol for Reliance Ltd BSE
start_date <- as.Date(‘2019-01-01’) end_date <- as.Date(‘2023-12-31’)
getSymbols(Symbols = stock_symbol, src = ‘yahoo’, from = start_date, to = end_date, env = stock_data)
rel_prices <- Cl(stock_data[[stock_symbol]])
rel_prices <- na.omit(rel_prices)
head(rel_prices)
class(rel_prices)
adf_result <- adf.test(rel_prices)
print(adf_result)
#In this case, since the p-value (0.4087) is greater than a typical significance #level like 0.05, we fail to reject the null hypothesis. #Therefore, we do not have enough evidence to conclude that the time series is stationary.
rel_ds = diff(log(rel_prices)); plot(rel_prices) #Reliance Ltd (First)return #Difference Time-Series
rel_ds=na.omit(rel_ds) adf_test_rel_ds = adf.test(rel_ds); adf_test_rel_ds
lb_test_rel_ds = Box.test(rel_ds)# Inference : reliance Difference (Stationary) lb_test_rel_ds
#In this case, since the p-value (4.294e-05) is much smaller than a typical significance level like 0.05,
#we reject the null hypothesis. Therefore, we have evidence to suggest that there is significant autocorrelation in the time series. | NULL: No Auto correlation | Alternate: Auto Correlation
#Autocorrelation Function (ACF) | Partial Autocorrelation Function (PACF) # *****************************************************************************
acf(rel_prices) # ACF of adani Series
pacf(rel_prices) # PACF of adani Series
acf(rel_ds) # ACF of adani Series
pacf(rel_ds) # PACF of adani Difference (Stationary) Series
arma_pq_rel_ds = auto.arima(rel_ds); arma_pq_rel_ds
rel_ds_fpq = forecast(arma_pq_rel_ds, h = 500) plot(rel_ds_fpq)
lb_test_arma_pq_rel_ds = Box.test(arma_pq_rel_ds$residuals); lb_test_arma_pq_rel_ds
rel_ret_sq = arma_pq_rel_ds$residuals^2 # Residual Variance (Since Mean Returns is approx. 0) plot(rel_ret_sq)
rel_ret_sq_box_test = Box.test(rel_ret_sq,lag = 1 ) # H0: Return Variance Series is Not Serially Correlated rel_ret_sq_box_test # Inference : Return Variance Series is Autocorrelated (Has Volatility Clustering)
rel_ret_arch_test = ArchTest(arma_pq_rel_ds$residuals^2,lag = 1) # H0: No ARCH Effects rel_ret_arch_test # Inference : Return Series is Heteroskedastic (Has Volatility Clustering)
garch_model1 = ugarchspec(variance.model = list(model = ‘sGARCH’, garchOrder = c(1,1)), mean.model = list(armaOrder = c(0,1), include.mean = TRUE)) rel_ret_garch1 = ugarchfit(garch_model1, data = arma_pq_rel_ds$residuals^2); rel_ret_garch1
rel_ret_garch_forecast1 = ugarchforecast(rel_ret_garch1, n.ahead = 500); rel_ret_garch_forecast1
plot(rel_ret_garch_forecast1, which = 1)
plot(rel_ret_garch_forecast1, which = 3)