library(tidyquant)
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(DT)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr   1.1.4     ✔ readr   2.1.4
## ✔ forcats 1.0.0     ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4     ✔ tibble  3.2.1
## ✔ purrr   1.0.2     ✔ tidyr   1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first()  masks xts::first()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ dplyr::last()   masks xts::last()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Load necessary libraries
library(tidyquant)

# Import IMOEX.ME.csv data
symbols <- c('IMOEX.ME')
stocks.prices <- tq_get(symbols, get = 'stock.prices',
                        from = '2022-01-31',
                        to = '2022-02-20')

# Change column values
stocks.prices$symbol[stocks.prices$symbol == 'IMOEX.ME'] <- 'MOEX'  # Replace 'MOEX' with the appropriate name
# Add more lines if you have other symbols to rename

# Print the first few rows of the modified data
head(stocks.prices)
## # A tibble: 6 × 8
##   symbol date        open  high   low close volume adjusted
##   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>  <dbl>    <dbl>
## 1 MOEX   2022-01-31 3524. 3566. 3519. 3530.      0    3530.
## 2 MOEX   2022-02-01 3565. 3585. 3531. 3548.      0    3548.
## 3 MOEX   2022-02-02 3575. 3580. 3516. 3544.      0    3544.
## 4 MOEX   2022-02-03 3499. 3519. 3447. 3471.      0    3471.
## 5 MOEX   2022-02-04 3499. 3556. 3464. 3471.      0    3471.
## 6 MOEX   2022-02-07 3511. 3521. 3438. 3471.      0    3471.
# Load necessary libraries
library(tidyquant)
library(ggplot2)

# Import IMOEX.ME.csv data
symbols <- c('IMOEX.ME')
stocks.prices <- tq_get(symbols, get = 'stock.prices',
                        from = '2022-01-31',
                        to = '2022-02-20')

# Change column values
stocks.prices$symbol[stocks.prices$symbol == 'IMOEX.ME'] <- 'MOEX'  # Replace 'MOEX' with the appropriate name
# Add more lines if you have other symbols to rename

# Visualize
stocks.prices %>%
  ggplot(aes(x = date, y = adjusted)) +
  geom_line(size = 2, color = 'steelblue') +
  theme_minimal() +
  facet_wrap(~symbol, scales = 'free_y', nrow = 2) +
  labs(x = 'Date', y = 'Price') +
  ggtitle('Price Chart for KOSPI and SKhynix')
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

# Load necessary libraries
library(tidyquant)
library(dplyr)  # Add this line to load the dplyr library

# Import IMOEX.ME.csv data
symbols <- c('IMOEX.ME')
stocks.prices <- tq_get(symbols, get = 'stock.prices',
                        from = '2022-01-31',
                        to = '2022-02-20')

# Change column values
stocks.prices$symbol[stocks.prices$symbol == 'IMOEX.ME'] <- 'MOEX'  # Replace 'MOEX' with the appropriate name
# Add more lines if you have other symbols to rename

# Calculate daily return
multpl_stock_daily_ret <- stocks.prices %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = 'daily',
               col_rename = 'returns')

# Print the first few rows of the calculated daily returns
head(multpl_stock_daily_ret)
## # A tibble: 6 × 3
## # Groups:   symbol [1]
##   symbol date          returns
##   <chr>  <date>          <dbl>
## 1 MOEX   2022-01-31  0        
## 2 MOEX   2022-02-01  0.00504  
## 3 MOEX   2022-02-02 -0.00123  
## 4 MOEX   2022-02-03 -0.0205   
## 5 MOEX   2022-02-04 -0.0000490
## 6 MOEX   2022-02-07 -0.0000663
# Load necessary libraries
library(tidyquant)
library(dplyr)
library(ggplot2)

# Import IMOEX.ME.csv data
symbols <- c('IMOEX.ME')
stocks.prices <- tq_get(symbols, get = 'stock.prices',
                        from = '2022-01-31',
                        to = '2022-02-20')

# Change column values
stocks.prices$symbol[stocks.prices$symbol == 'IMOEX.ME'] <- 'MOEX'  # Replace 'MOEX' with the appropriate name
# Add more lines if you have other symbols to rename

# Calculate daily return
multpl_stock_daily_ret <- stocks.prices %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = 'daily',
               col_rename = 'returns')

# Visualize Daily Return
multpl_stock_daily_ret %>%
  ggplot(aes(x = date, y = returns, color = symbol)) +
  geom_bar(stat = 'identity', position = 'dodge', width = 0.5, size = 2) +
  scale_y_continuous(labels = scales::percent) +
  labs(x = 'Year', y = 'Daily Returns') +
  theme_minimal() +
  scale_color_brewer(palette = 'Set2') +
  facet_wrap(~symbol, nrow = 2) +
  ggtitle('Daily Return of MOEX')  # Update the title accordingly

# Install and load the DT package
install.packages("DT")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(DT)

# Load necessary libraries
library(tidyquant)
library(dplyr)
library(tidyr)

# Import IMOEX.ME.csv data
symbols <- c('IMOEX.ME')
stocks.prices <- tq_get(symbols, get = 'stock.prices',
                        from = '2022-01-31',
                        to = '2022-02-20')

# Change column values
stocks.prices$symbol[stocks.prices$symbol == 'IMOEX.ME'] <- 'MOEX'  # Replace 'MOEX' with the appropriate name
# Add more lines if you have other symbols to rename

# Calculate daily return
multpl_stock_daily_ret <- stocks.prices %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = 'daily',
               col_rename = 'returns')

# Spread the data for regression
Data <- multpl_stock_daily_ret %>%
  spread(symbol, value = returns) 

# Print the spread data using DT::datatable
DT::datatable(Data)
# Load necessary libraries
library(tidyquant)
library(dplyr)
library(tidyr)

# Import IMOEX.ME.csv data
symbols <- c('IMOEX.ME')
stocks.prices <- tq_get(symbols, get = 'stock.prices',
                        from = '2022-01-31',
                        to = '2022-02-20')

# Change column values
stocks.prices$symbol[stocks.prices$symbol == 'IMOEX.ME'] <- 'MOEX'  # Replace 'MOEX' with the appropriate name
# Add more lines if you have other symbols to rename

# Calculate daily return
multpl_stock_daily_ret <- stocks.prices %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = 'daily',
               col_rename = 'returns')

# Spread the data for regression
Data <- multpl_stock_daily_ret %>%
  spread(symbol, value = returns) 

# Applying one-factor model
reg <- lm(data = Data, MOEX ~ MOEX)  # Adjust variable names accordingly
## Warning in model.matrix.default(mt, mf, contrasts): the response appeared on
## the right-hand side and was dropped
## Warning in model.matrix.default(mt, mf, contrasts): problem with term 1 in
## model.matrix: no columns are assigned
summary(reg)
## 
## Call:
## lm(formula = MOEX ~ MOEX, data = Data)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.034697 -0.017028  0.002367  0.011341  0.036572 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.002416   0.005602  -0.431    0.673
## 
## Residual standard error: 0.0217 on 14 degrees of freedom
# Load necessary libraries
library(tidyquant)
library(dplyr)
library(tidyr)

# Import IMOEX.ME.csv data
symbols <- c('IMOEX.ME')
stocks.prices <- tq_get(symbols, get = 'stock.prices',
                        from = '2022-01-31',
                        to = '2022-02-20')

# Change column values
stocks.prices$symbol[stocks.prices$symbol == 'IMOEX.ME'] <- 'MOEX'  # Replace 'MOEX' with the appropriate name
# Add more lines if you have other symbols to rename

# Calculate daily return
multpl_stock_daily_ret <- stocks.prices %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = 'daily',
               col_rename = 'returns')

# Spread the data for regression
Data2 <- multpl_stock_daily_ret %>%
  spread(symbol, value = returns)

# Calculate Expected Return
Data2$Expected_ret <- 1.2250926 * Data2$MOEX

# Calculate Abnormal Return
Data2$Abnormal_ret <- Data2$MOEX - Data2$Expected_ret  # Adjust variable names accordingly

# Calculate t-statistic
Data2$t_stat <- Data2$Abnormal_ret / 0.0126  # Adjust the denominator accordingly

# Print the modified data
head(Data2)
## # A tibble: 6 × 5
##   date             MOEX Expected_ret Abnormal_ret    t_stat
##   <date>          <dbl>        <dbl>        <dbl>     <dbl>
## 1 2022-01-31  0            0            0          0       
## 2 2022-02-01  0.00504      0.00617     -0.00113   -0.0900  
## 3 2022-02-02 -0.00123     -0.00151      0.000277   0.0220  
## 4 2022-02-03 -0.0205      -0.0251       0.00461    0.366   
## 5 2022-02-04 -0.0000490   -0.0000600    0.0000110  0.000875
## 6 2022-02-07 -0.0000663   -0.0000812    0.0000149  0.00118
# Load necessary libraries
library(tidyquant)
library(dplyr)
library(tidyr)

# Import IMOEX.ME.csv data
symbols <- c('IMOEX.ME')
stocks.prices <- tq_get(symbols, get = 'stock.prices',
                        from = '2022-01-31',
                        to = '2022-02-20')

# Change column values
stocks.prices$symbol[stocks.prices$symbol == 'IMOEX.ME'] <- 'MOEX'  # Replace 'MOEX' with the appropriate name
# Add more lines if you have other symbols to rename

# Calculate daily return
multpl_stock_daily_ret <- stocks.prices %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = 'daily',
               col_rename = 'returns')

# Spread the data for regression
Data2 <- multpl_stock_daily_ret %>%
  spread(symbol, value = returns)

# Calculate Expected Return
Data2$Expected_ret <- 1.2250926 * Data2$MOEX

# Calculate Abnormal Return
Data2$Abnormal_ret <- Data2$MOEX - Data2$Expected_ret  # Adjust variable names accordingly

# Calculate Cumulative Abnormal Return (CAR)
Data2$CAR <- cumprod(1 + Data2$Abnormal_ret)

# Print the modified data
head(Data2)
## # A tibble: 6 × 5
##   date             MOEX Expected_ret Abnormal_ret   CAR
##   <date>          <dbl>        <dbl>        <dbl> <dbl>
## 1 2022-01-31  0            0            0         1    
## 2 2022-02-01  0.00504      0.00617     -0.00113   0.999
## 3 2022-02-02 -0.00123     -0.00151      0.000277  0.999
## 4 2022-02-03 -0.0205      -0.0251       0.00461   1.00 
## 5 2022-02-04 -0.0000490   -0.0000600    0.0000110 1.00 
## 6 2022-02-07 -0.0000663   -0.0000812    0.0000149 1.00
# Load necessary libraries
library(tidyquant)
library(dplyr)
library(tidyr)
library(ggplot2)

# Import IMOEX.ME.csv data
symbols <- c('IMOEX.ME')
stocks.prices <- tq_get(symbols, get = 'stock.prices',
                        from = '2022-01-31',
                        to = '2022-02-20')

# Change column values
stocks.prices$symbol[stocks.prices$symbol == 'IMOEX.ME'] <- 'MOEX'  # Replace 'MOEX' with the appropriate name
# Add more lines if you have other symbols to rename

# Calculate daily return
multpl_stock_daily_ret <- stocks.prices %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = 'daily',
               col_rename = 'returns')

# Spread the data for regression
Data2 <- multpl_stock_daily_ret %>%
  spread(symbol, value = returns)

# Calculate Expected Return
Data2$Expected_ret <- 1.2250926 * Data2$MOEX

# Calculate Abnormal Return
Data2$Abnormal_ret <- Data2$MOEX - Data2$Expected_ret  # Adjust variable names accordingly

# Calculate Cumulative Abnormal Return (CAR)
Data2$CAR <- cumprod(1 + Data2$Abnormal_ret)

# Visualize Cumulative Abnormal Return (CAR)
Data2 %>%
  ggplot(aes(x = date, y = CAR)) +
  geom_point(size = 5, color = 'steelblue') +
  geom_point(aes(y = 1.0141802, x = as.Date('2022-07-27')), size = 5, color = 'red') +
  annotate('text', x = as.Date('2022-07-30'), y = 1.0141802, label = 'Earnings Release', size = 4) +
  theme_minimal() +
  labs(x = 'Date', y = 'CAR') +
  ggtitle('Cumulative Abnormal Return')

# Load necessary libraries
library(tidyquant)
library(ggplot2)

# Import IMOEX.ME.csv data
symbols <- c('IMOEX.ME', 'Symbol2', 'Symbol3')  # Replace 'Symbol2' and 'Symbol3' with the appropriate symbols
stocks.prices1 <- tq_get(symbols, get = 'stock.prices',
                         from = '2022-01-31',
                         to = '2022-02-20')
## Warning: There were 2 warnings in `dplyr::mutate()`.
## The first warning was:
## ℹ In argument: `data.. = purrr::map(...)`.
## Caused by warning:
## ! x = 'Symbol2', get = 'stock.prices': Error in getSymbols.yahoo(Symbols = "Symbol2", env = <environment>, verbose = FALSE, : Unable to import "Symbol2".
## HTTP error 404.
##  Removing Symbol2.
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
# Change column values if needed
# stocks.prices1$symbol[stocks.prices1$symbol == 'IMOEX.ME'] <- 'NewName'  # Replace 'NewName' with the appropriate name
# Add more lines if you have other symbols to rename

# Visualize Price Chart for KOSPI, SKhynix, and Industry
stocks.prices1 %>%
  ggplot(aes(x = date, y = adjusted)) +
  geom_line(size = 2, color = 'steelblue') +
  theme_minimal() +
  facet_wrap(~symbol, scales = 'free_y', nrow = 3) +
  labs(x = 'Date', y = 'Price') +
  ggtitle('Price Chart for KOSPI, SKhynix, and Industry')

# Load necessary libraries
library(tidyquant)
library(dplyr)

# Import IMOEX.ME.csv data
symbols <- c('IMOEX.ME', 'Symbol2', 'Symbol3')  # Replace 'Symbol2' and 'Symbol3' with the appropriate symbols
stocks.prices1 <- tq_get(symbols, get = 'stock.prices',
                         from = '2022-01-31',
                         to = '2022-02-20')
## Warning: There were 2 warnings in `dplyr::mutate()`.
## The first warning was:
## ℹ In argument: `data.. = purrr::map(...)`.
## Caused by warning:
## ! x = 'Symbol2', get = 'stock.prices': Error in getSymbols.yahoo(Symbols = "Symbol2", env = <environment>, verbose = FALSE, : Unable to import "Symbol2".
## HTTP error 404.
##  Removing Symbol2.
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
# Change column values if needed
# stocks.prices1$symbol[stocks.prices1$symbol == 'IMOEX.ME'] <- 'NewName'  # Replace 'NewName' with the appropriate name
# Add more lines if you have other symbols to rename

# Calculate daily return
multpl_stock_daily_ret1 <- stocks.prices1 %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = 'daily',
               col_rename = 'returns')

# Print the first few rows of the calculated daily returns
head(multpl_stock_daily_ret1)
## # A tibble: 6 × 3
## # Groups:   symbol [1]
##   symbol   date          returns
##   <chr>    <date>          <dbl>
## 1 IMOEX.ME 2022-01-31  0        
## 2 IMOEX.ME 2022-02-01  0.00504  
## 3 IMOEX.ME 2022-02-02 -0.00123  
## 4 IMOEX.ME 2022-02-03 -0.0205   
## 5 IMOEX.ME 2022-02-04 -0.0000490
## 6 IMOEX.ME 2022-02-07 -0.0000663
# Load necessary libraries
library(tidyquant)
library(dplyr)
library(ggplot2)

# Import IMOEX.ME.csv data
symbols <- c('IMOEX.ME', 'Symbol2', 'Symbol3')  # Replace 'Symbol2' and 'Symbol3' with the appropriate symbols
stocks.prices1 <- tq_get(symbols, get = 'stock.prices',
                         from = '2022-01-31',
                         to = '2022-02-20')
## Warning: There were 2 warnings in `dplyr::mutate()`.
## The first warning was:
## ℹ In argument: `data.. = purrr::map(...)`.
## Caused by warning:
## ! x = 'Symbol2', get = 'stock.prices': Error in getSymbols.yahoo(Symbols = "Symbol2", env = <environment>, verbose = FALSE, : Unable to import "Symbol2".
## HTTP error 404.
##  Removing Symbol2.
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
# Change column values if needed
# stocks.prices1$symbol[stocks.prices1$symbol == 'IMOEX.ME'] <- 'NewName'  # Replace 'NewName' with the appropriate name
# Add more lines if you have other symbols to rename

# Calculate daily return
multpl_stock_daily_ret1 <- stocks.prices1 %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = 'daily',
               col_rename = 'returns')

# Visualize Daily Return
multpl_stock_daily_ret1 %>%
  ggplot(aes(x = date, y = returns, color = symbol)) +
  geom_bar(stat = 'identity', position = 'dodge', width = 0.5, size = 2) +
  scale_y_continuous(labels = scales::percent) +
  labs(x = 'Year', y = 'Daily Returns') +
  theme_minimal() +
  scale_color_brewer(palette = 'Set2') +
  facet_wrap(~symbol, nrow = 3) +
  ggtitle('Daily Return of KOSPI, SKhynix, and Industry')

# Load necessary libraries
library(tidyquant)
library(dplyr)
library(tidyr)
library(DT)

# Import IMOEX.ME.csv data
symbols <- c('IMOEX.ME', 'Symbol2', 'Symbol3')  # Replace 'Symbol2' and 'Symbol3' with the appropriate symbols
stocks.prices1 <- tq_get(symbols, get = 'stock.prices',
                         from = '2022-01-31',
                         to = '2022-02-20')
## Warning: There were 2 warnings in `dplyr::mutate()`.
## The first warning was:
## ℹ In argument: `data.. = purrr::map(...)`.
## Caused by warning:
## ! x = 'Symbol2', get = 'stock.prices': Error in getSymbols.yahoo(Symbols = "Symbol2", env = <environment>, verbose = FALSE, : Unable to import "Symbol2".
## HTTP error 404.
##  Removing Symbol2.
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
# Change column values if needed
# stocks.prices1$symbol[stocks.prices1$symbol == 'IMOEX.ME'] <- 'NewName'  # Replace 'NewName' with the appropriate name
# Add more lines if you have other symbols to rename

# Calculate daily return
multpl_stock_daily_ret1 <- stocks.prices1 %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = 'daily',
               col_rename = 'returns')

# Convert returns to numeric
multpl_stock_daily_ret1$returns <- as.numeric(as.character(multpl_stock_daily_ret1$returns))

# Create Data3
Data3 <- multpl_stock_daily_ret1 %>%
  spread(symbol, value = returns) %>%
  drop_na()

# Print the data table using DT::datatable
DT::datatable(Data3)