library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(xts)
library(zoo)
price_bbri <- getSymbols("BBRI.JK", auto.assign=FALSE, from="2020-01-01", to="2024-12-31")
head(price_bbri)
##            BBRI.JK.Open BBRI.JK.High BBRI.JK.Low BBRI.JK.Close BBRI.JK.Volume
## 2020-01-02     3999.931     4009.022    3963.568      4009.022       45886302
## 2020-01-03     4018.112     4036.294    3990.840      4018.112       91189705
## 2020-01-06     3963.568     3990.840    3927.205      3972.659       48648450
## 2020-01-07     4009.022     4009.022    3981.750      3999.931      114344885
## 2020-01-08     3981.750     3999.931    3945.386      3981.750      188929583
## 2020-01-09     3999.931     4018.112    3972.659      3999.931       79280569
##            BBRI.JK.Adjusted
## 2020-01-02         3132.644
## 2020-01-03         3139.747
## 2020-01-06         3104.230
## 2020-01-07         3125.541
## 2020-01-08         3111.334
## 2020-01-09         3125.541
# Candle stick chart
chartSeries(price_bbri, name = "BBRI Price 2020-2024")

# Take only the closing price
closing_pr <- Cl(to.monthly(price_bbri))
# Decompose it
dc <- decompose(as.ts(closing_pr, start=c(2014,1)))
plot(dc)

# Seasonal component 
dc$seasonal
##             Jan        Feb        Mar        Apr        May        Jun
## 2014   32.16683  362.23941  272.35321   19.35166  -47.89471 -221.67321
## 2015   32.16683  362.23941  272.35321   19.35166  -47.89471 -221.67321
## 2016   32.16683  362.23941  272.35321   19.35166  -47.89471 -221.67321
## 2017   32.16683  362.23941  272.35321   19.35166  -47.89471 -221.67321
## 2018   32.16683  362.23941  272.35321   19.35166  -47.89471 -221.67321
##             Jul        Aug        Sep        Oct        Nov        Dec
## 2014 -164.31454 -106.03695 -245.81725 -159.37059   84.90595  174.09021
## 2015 -164.31454 -106.03695 -245.81725 -159.37059   84.90595  174.09021
## 2016 -164.31454 -106.03695 -245.81725 -159.37059   84.90595  174.09021
## 2017 -164.31454 -106.03695 -245.81725 -159.37059   84.90595  174.09021
## 2018 -164.31454 -106.03695 -245.81725 -159.37059   84.90595  174.09021
library(highcharter)
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
highchart(type="stock") %>% 
  hc_add_series(price_bbri) %>% 
  hc_add_series(SMA(na.omit(Cl(price_bbri)),n=50),name="SMA(50)") %>% 
  hc_add_series(SMA(na.omit(Cl(price_bbri)),n=200),name="SMA(200)") %>% 
  hc_title(text="<b>BBRI Price Candle Stick Chart 2014-2019</b>")
# Fetch BBNI, BMRI, and IHSG stock prices
price_bbni <- getSymbols("BBNI.JK",auto.assign=FALSE,from="2020-01-01",to="2024-12-31")
price_bmri <- getSymbols("BMRI.JK",auto.assign=FALSE,from="2020-01-01",to="2024-12-31")
price_ihsg <- getSymbols("^JKSE",auto.assign=FALSE,from="2020-01-01",to="2024-12-31")

# Compare the stock prices
highchart(type="stock") %>% 
  hc_add_series(Cl(price_bbri), name="BBRI") %>% 
  hc_add_series(Cl(price_bbni), name="BBNI") %>% 
  hc_add_series(Cl(price_bmri), name="BMRI") %>% 
  hc_add_series(Cl(price_ihsg), name="IHSG") %>% 
  hc_title(text="<b>BBRI vs BBNI vs BMRI vs IHSG Closing Price</b>")
# Calculate the stocks return
return_bbri <- dailyReturn(Cl(price_bbri))
return_bbni <- dailyReturn(Cl(price_bbni))
return_bmri <- dailyReturn(Cl(price_bmri))

# Combine the returns as one data frame
returns <- data.frame(return_bbri,return_bbni,return_bmri)
names(returns) <- c("return_bbri","return_bbni","return_bmri")
returns <- as.xts(returns)

# Plot the returns
library(PerformanceAnalytics)
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
charts.PerformanceSummary(returns,main="Daily Return BBRI vs BBNI vs BMRI 2020-2024")

# Number of period we want to forecast
n <- 100

# Splitting the data
train <- head(Cl(price_bbri), length(Cl(price_bbri))-n)
test <- tail(Cl(price_bbri), n)
library(forecast)

# Forecast the data
fc_na <- naive(train, h=n)

# Plot the result
autoplot(fc_na) +
  autolayer(ts(test, start=length(train)), series = "Test Data")

# Create the Model
model_non <- auto.arima(train, seasonal=FALSE)

# Forecast n periods of the data
fc_non <- forecast(model_non, h=n)

# Plot the result
autoplot(fc_non)+
  autolayer(ts(test, start= length(train)), series="Test Data")

# Create the Model
model_s <- auto.arima(train)

# Forecast n periods of the data
fc_s <- forecast(model_s, h=n)

# Plot the result
autoplot(fc_s)+
  autolayer(ts(test, start= length(train)), series="Test Data")

checkresiduals(fc_na)

## 
##  Ljung-Box test
## 
## data:  Residuals from Naive method
## Q* = 16.284, df = 10, p-value = 0.09179
## 
## Model df: 0.   Total lags used: 10
checkresiduals(fc_non)

## 
##  Ljung-Box test
## 
## data:  Residuals from ARIMA(0,1,0)
## Q* = 16.303, df = 10, p-value = 0.09129
## 
## Model df: 0.   Total lags used: 10
accuracy(fc_na)
##                     ME    RMSE      MAE         MPE    MAPE MASE        ACF1
## Training set 0.5324129 81.6353 60.32806 -0.01047284 1.48613    1 -0.04243523
accuracy(fc_non)
##                     ME     RMSE      MAE         MPE     MAPE      MASE
## Training set 0.5355421 81.59864 60.27737 -0.01037341 1.484882 0.9991597
##                    ACF1
## Training set -0.0424311