Setup

rm(list=ls())
library(tidyverse)
library(tidyquant)
library(timetk)

Questions

1. Import Data

Task: Load packages tidyquant, tidyverse and timetk. Import the data file tej_day_price_2024_20250630.txt using read_csv(), read_tsv() or read_delim().

# Import data using read_tsv() since the file is tab-delimited
tej_data <- read_tsv("tej_day_price_2024_20250630.txt")

# Show the imported results
glimpse(tej_data)
## Rows: 337,347
## Columns: 12
## $ CO_ID                 <chr> "1101 TCC", "1102 ACC", "1103 CHC", "1104 UCC", …
## $ Date                  <dbl> 20240102, 20240102, 20240102, 20240102, 20240102…
## $ `TSE ID`              <dbl> 1101, 1102, 1103, 1104, 1108, 1109, 1110, 1201, …
## $ `TSE Sector`          <chr> "01", "01", "01", "01", "01", "01", "01", "02", …
## $ `English Short Name`  <chr> "TCC", "ACC", "CHC", "UCC", "Lucky Cement", "HSI…
## $ `Open(NTD)`           <dbl> 32.5373, 37.2642, 17.7825, 26.0628, 14.1679, 16.…
## $ `High(NTD)`           <dbl> 32.5373, 37.4442, 17.7825, 26.1505, 14.1679, 16.…
## $ `Low(NTD)`            <dbl> 32.3038, 36.9492, 17.5953, 25.9750, 14.0343, 16.…
## $ `Close(NTD)`          <dbl> 32.3972, 37.0392, 17.6421, 26.0628, 14.0788, 16.…
## $ `Volume(1000S)`       <dbl> 14937, 6223, 171, 260, 442, 228, 57, 126, 48, 18…
## $ `Amount(NTD1000)`     <dbl> 518751, 256522, 3240, 7736, 6992, 4159, 1075, 24…
## $ `Market Cap.(NTD MN)` <dbl> 262026, 145941, 14896, 19995, 6395, 6209, 10754,…

Result: The dataset contains 337,347 rows and 12 columns with stock price data from the Taiwan Stock Exchange.


2. Rename Columns

Task: Replace columns 2, 3, 5, 9, and 12 with new names: “date”, “id”, “name”, “price”, “cap”.

# Rename specific columns
tej_data <- tej_data %>%
  rename(
    date = 2,
    id = 3,
    name = 5,
    price = 9,
    cap = 12
  ) %>%
  select(id, name, date, price, cap)

glimpse(tej_data)
## Rows: 337,347
## Columns: 5
## $ id    <dbl> 1101, 1102, 1103, 1104, 1108, 1109, 1110, 1201, 1203, 1210, 1213…
## $ name  <chr> "TCC", "ACC", "CHC", "UCC", "Lucky Cement", "HSINGTA", "Tuna Cem…
## $ date  <dbl> 20240102, 20240102, 20240102, 20240102, 20240102, 20240102, 2024…
## $ price <dbl> 32.3972, 37.0392, 17.6421, 26.0628, 14.0788, 16.1807, 18.3336, 1…
## $ cap   <dbl> 262026, 145941, 14896, 19995, 6395, 6209, 10754, 9640, 13992, 52…

3. Convert to Wide Format

Task: Select id, date, price columns. Change id to text, date to date format, and convert from long to wide format.

# Convert formats and reshape to wide
tej_wide <- tej_data %>%
  select(id, date, price) %>%
  mutate(
    id = as.character(id),
    date = ymd(date)
  ) %>%
  pivot_wider(names_from = id, values_from = price)

head(tej_wide, 10)
## # A tibble: 10 × 947
##    date       `1101` `1102` `1103` `1104` `1108` `1109` `1110` `1201` `1203`
##    <date>      <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
##  1 2024-01-02   32.4   37.0   17.6   26.1   14.1   16.2   18.3   18.2   55.3
##  2 2024-01-03   31.9   36.6   17.5   25.9   14.0   16.1   18.2   18.2   54.7
##  3 2024-01-04   31.9   37.0   17.5   25.5   14.1   16.1   18.4   18.1   54.1
##  4 2024-01-05   32.1   37.0   17.5   25.8   14.1   16.1   18.3   18.1   54.9
##  5 2024-01-08   32.0   37.2   17.6   25.7   14.2   16.1   18.3   18.1   54.6
##  6 2024-01-09   31.8   36.9   17.5   25.4   13.8   16.0   18.4   18.1   54.6
##  7 2024-01-10   31.5   36.6   17.4   25.4   13.7   16.0   18.5   18.0   53.4
##  8 2024-01-11   31.5   36.7   17.5   25.6   13.8   16.1   18.5   18.0   55.1
##  9 2024-01-12   31.5   36.6   17.5   25.6   13.8   16.0   18.4   18.0   54.4
## 10 2024-01-15   31.4   36.6   17.4   25.4   13.8   16.0   18.3   18.0   53.8
## # ℹ 937 more variables: `1210` <dbl>, `1213` <dbl>, `1215` <dbl>, `1216` <dbl>,
## #   `1217` <dbl>, `1218` <dbl>, `1219` <dbl>, `1220` <dbl>, `1225` <dbl>,
## #   `1227` <dbl>, `1229` <dbl>, `1231` <dbl>, `1232` <dbl>, `1233` <dbl>,
## #   `1234` <dbl>, `1235` <dbl>, `1236` <dbl>, `1301` <dbl>, `1303` <dbl>,
## #   `1304` <dbl>, `1305` <dbl>, `1307` <dbl>, `1308` <dbl>, `1309` <dbl>,
## #   `1310` <dbl>, `1312` <dbl>, `1313` <dbl>, `1314` <dbl>, `1315` <dbl>,
## #   `1316` <dbl>, `1319` <dbl>, `1321` <dbl>, `1323` <dbl>, `1324` <dbl>, …

4. Identify NA Values

Task: Show stock ids with NA values and compute the number of NAs for each stock.

# Count NA values for each stock
na_summary <- tej_wide %>%
  pivot_longer(-date, names_to = "key", values_to = "value") %>%
  filter(is.na(value)) %>%
  count(key, name = "value") %>%
  arrange(value)

na_summary
## # A tibble: 10 × 2
##    key   value
##    <chr> <int>
##  1 7722     18
##  2 7732     43
##  3 7736     53
##  4 7750    108
##  5 7765    151
##  6 3716    160
##  7 4585    177
##  8 7780    196
##  9 7788    198
## 10 7799    217

Result: 10 stocks contain NA values, ranging from 18 to 217 missing observations.


5. Fill NA Values

Task: Replace NA values with the closest available stock prices using na.locf().

# Fill NA values forward
tej_filled <- tej_wide %>%
  tk_tbl() %>%
  mutate(across(-date, ~zoo::na.locf(., na.rm = FALSE)))

head(tej_filled, 10)
## # A tibble: 10 × 947
##    date       `1101` `1102` `1103` `1104` `1108` `1109` `1110` `1201` `1203`
##    <date>      <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
##  1 2024-01-02   32.4   37.0   17.6   26.1   14.1   16.2   18.3   18.2   55.3
##  2 2024-01-03   31.9   36.6   17.5   25.9   14.0   16.1   18.2   18.2   54.7
##  3 2024-01-04   31.9   37.0   17.5   25.5   14.1   16.1   18.4   18.1   54.1
##  4 2024-01-05   32.1   37.0   17.5   25.8   14.1   16.1   18.3   18.1   54.9
##  5 2024-01-08   32.0   37.2   17.6   25.7   14.2   16.1   18.3   18.1   54.6
##  6 2024-01-09   31.8   36.9   17.5   25.4   13.8   16.0   18.4   18.1   54.6
##  7 2024-01-10   31.5   36.6   17.4   25.4   13.7   16.0   18.5   18.0   53.4
##  8 2024-01-11   31.5   36.7   17.5   25.6   13.8   16.1   18.5   18.0   55.1
##  9 2024-01-12   31.5   36.6   17.5   25.6   13.8   16.0   18.4   18.0   54.4
## 10 2024-01-15   31.4   36.6   17.4   25.4   13.8   16.0   18.3   18.0   53.8
## # ℹ 937 more variables: `1210` <dbl>, `1213` <dbl>, `1215` <dbl>, `1216` <dbl>,
## #   `1217` <dbl>, `1218` <dbl>, `1219` <dbl>, `1220` <dbl>, `1225` <dbl>,
## #   `1227` <dbl>, `1229` <dbl>, `1231` <dbl>, `1232` <dbl>, `1233` <dbl>,
## #   `1234` <dbl>, `1235` <dbl>, `1236` <dbl>, `1301` <dbl>, `1303` <dbl>,
## #   `1304` <dbl>, `1305` <dbl>, `1307` <dbl>, `1308` <dbl>, `1309` <dbl>,
## #   `1310` <dbl>, `1312` <dbl>, `1313` <dbl>, `1314` <dbl>, `1315` <dbl>,
## #   `1316` <dbl>, `1319` <dbl>, `1321` <dbl>, `1323` <dbl>, `1324` <dbl>, …

6. Remove Stocks with NAs

Task: Delete stocks that contained NA values and show updated dimensions.

# Get list of stocks with NAs
stocks_with_na <- na_summary$key

# Remove these stocks
tej_clean <- tej_wide %>%
  select(-all_of(stocks_with_na))

# Show dimensions
dim(tej_clean)
## [1] 358 937

Result: Data now has 358 rows and 937 columns (1 date column + 936 stocks).


7. Calculate Daily Returns

Task: Convert to xts time series, calculate daily returns, remove first row, and show first 5 stocks with first 5 days.

# Convert to xts
tej_xts <- tej_clean %>%
  tk_xts(date_var = date)

# Calculate daily returns
daily_returns <- Return.calculate(tej_xts, method = "discrete")

# Remove first row and show first 5 stocks, first 5 days
daily_returns[-1, 1:5] %>% head(5)
##                    1101         1102         1103         1104         1108
## 2024-01-03 -0.014408653 -0.012149290 -0.007958236 -0.006733735 -0.003160781
## 2024-01-04  0.000000000  0.012298711  0.000000000 -0.013558772  0.003170803
## 2024-01-05  0.004384536 -0.001214929  0.002674026  0.010306896  0.000000000
## 2024-01-08 -0.002909225  0.004865628  0.002666895 -0.003399291  0.006328664
## 2024-01-09 -0.005841680 -0.007263102 -0.002659801 -0.013655209 -0.025155457

8. Calculate Monthly Returns

Task: Compute monthly returns, delete first row, and show first 5 stocks with first 5 months.

# Convert to monthly and calculate returns
monthly_prices <- to.monthly(tej_xts, OHLC = FALSE)
monthly_returns <- Return.calculate(monthly_prices, method = "discrete")

# Remove first row and show first 5 stocks, first 5 months
monthly_returns[-1, 1:5] %>% head(5)
##                  1101        1102         1103        1104         1108
## Feb 2024  0.006272034  0.01760804 -0.008404066  0.01887014  0.036185231
## Mar 2024  0.001554899  0.02101398 -0.016950585  0.06397241  0.003170803
## Apr 2024 -0.003108301  0.05811288  0.057470064  0.11234002  0.072790295
## May 2024  0.029639309 -0.04920108  0.032611536 -0.03271487  0.000000000
## Jun 2024  0.036364817  0.05535680 -0.036845213  0.04852830 -0.011805133

9. Top 20 Firms by Market Cap

Task: Find the 20 largest cap firms at year-end 2024 and 2025.

# Get top 20 firms for 2024 and 2025 year-ends
top_firms <- tej_data %>%
  mutate(date = ymd(date)) %>%
  filter(date %in% as.Date(c("2024-12-31", "2025-06-30"))) %>%
  mutate(year1 = year(date)) %>%
  group_by(year1) %>%
  arrange(desc(cap)) %>%
  slice(1:20) %>%
  ungroup() %>%
  mutate(cap1 = scales::dollar(cap, prefix = "$", big.mark = ",")) %>%
  select(date, year1, cap, cap1, id, name)

top_firms
## # A tibble: 40 × 6
##    date       year1      cap cap1           id name           
##    <date>     <dbl>    <dbl> <chr>       <dbl> <chr>          
##  1 2024-12-31  2024 27877688 $27,877,688  2330 TSMC           
##  2 2024-12-31  2024  2556073 $2,556,073   2317 Hon Hai        
##  3 2024-12-31  2024  2266389 $2,266,389   2454 MediaTek       
##  4 2024-12-31  2024  1234015 $1,234,015   2881 Fubon Financial
##  5 2024-12-31  2024  1118242 $1,118,242   2308 DELTA          
##  6 2024-12-31  2024  1108574 $1,108,574   2382 QCI            
##  7 2024-12-31  2024  1001907 $1,001,907   2882 CATHAY FHC     
##  8 2024-12-31  2024   958045 $958,045     2412 CHT            
##  9 2024-12-31  2024   767154 $767,154     2891 CTBC Holding   
## 10 2024-12-31  2024   715219 $715,219     3711 ASEH           
## # ℹ 30 more rows

Result: TSMC (2330) leads with the largest market capitalization in both periods, followed by Hon Hai (2317) and MediaTek (2454).