1. Introduction

This assignment analyzes daily stock prices of companies listed on the Taiwan stock market during the last two years.

The dataset contains company ID, date, opening price, highest price, lowest price, and closing price.

2. Load the Data

library(readr)
library(dplyr)
library(ggplot2)

taiwan_stocks_hw3 <- read_csv(
  "Downloads/Example1/taiwan_stocks_hw3.csv"
)

taiwan_stocks_hw3$Date <- as.Date(taiwan_stocks_hw3$Date)

3. Basic Information

dim(taiwan_stocks_hw3)
## [1] 1048575       6
head(taiwan_stocks_hw3)
## # A tibble: 6 × 6
##   CO_ID                    Date       Open    High    Low     Close
##   <chr>                    <date>     <chr>   <chr>   <chr>   <dbl>
## 1 0053 PTE                 0010-04-20 95.4244 95.4244 94.4654  94.5
## 2 0050 Yuanta Taiwan Top50 0010-04-20 44.1573 44.4692 43.9173  44.1
## 3 0056 PTD                 0010-04-20 31.0855 31.1824 30.8513  31.0
## 4 0051 TMT                 0010-04-20 76.4016 76.4016 74.9954  75.3
## 5 0057 FB MSCI Taiwan      0010-04-20 136.2   136.95  135.7   136. 
## 6 0052 FB Technology       0010-04-20 24.566  24.7366 24.3681  24.5
summary(taiwan_stocks_hw3)
##        CO_ID              Date                   Open        
##  Length   :1048575   Min.   :0001-02-20   Length   :1048575  
##  N.unique :   3599   1st Qu.:0004-02-20   N.unique : 325004  
##  N.blank  :      0   Median :0008-04-20   N.blank  :      0  
##  Min.nchar:      7   Mean   :0007-09-10   Min.nchar:      1  
##  Max.nchar:     38   3rd Qu.:0011-04-20   Max.nchar:      9  
##                      Max.   :0012-12-20                      
##                      NAs    :630951                          
##         High                Low              Close          
##  Length   :1048575   Length   :1048575   Min.   :     0.79  
##  N.unique : 325043   N.unique : 320325   1st Qu.:    23.92  
##  N.blank  :      0   N.blank  :      0   Median :    49.01  
##  Min.nchar:      1   Min.nchar:      1   Mean   :   945.13  
##  Max.nchar:      9   Max.nchar:      9   3rd Qu.:   111.88  
##                                          Max.   :173067.45  
## 

4. Number of Companies

taiwan_stocks_hw3 %>%
  summarise(
    Number_of_Companies = n_distinct(CO_ID)
  )
## # A tibble: 1 × 1
##   Number_of_Companies
##                 <int>
## 1                3599

5. Data Period

taiwan_stocks_hw3 %>%
  summarise(
    First_Date = min(Date, na.rm = TRUE),
    Last_Date = max(Date, na.rm = TRUE)
  )
## # A tibble: 1 × 2
##   First_Date Last_Date 
##   <date>     <date>    
## 1 0001-02-20 0012-12-20

6. Number of Observations for Each Company

company_summary <- taiwan_stocks_hw3 %>%
  group_by(CO_ID) %>%
  summarise(
    Observations = n(),
    First_Date = min(Date, na.rm = TRUE),
    Last_Date = max(Date, na.rm = TRUE),
    .groups = "drop"
  ) %>%
  arrange(desc(Observations))
## Warning: There were 22 warnings in `summarise()`.
## The first warning was:
## ℹ In argument: `First_Date = min(Date, na.rm = TRUE)`.
## ℹ In group 1501: `CO_ID = "370201 WPG-EB1"`.
## Caused by warning in `min.default()`:
## ! no non-missing arguments to min; returning Inf
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 21 remaining warnings.
head(company_summary, 20)
## # A tibble: 20 × 4
##    CO_ID                                  Observations First_Date Last_Date 
##    <chr>                                         <int> <date>     <date>    
##  1 IN0111 ITP NTR INDEX                            325 0001-02-20 0012-12-20
##  2 KRTWIT Korea/Taiwan IT Premier Index(           325 0001-02-20 0012-12-20
##  3 KRTWITK Korea/Taiwan IT Premier Index(          325 0001-02-20 0012-12-20
##  4 KRTWITT Korea/Taiwan IT Premier Index           325 0001-02-20 0012-12-20
##  5 KTITKR Korea/Taiwan IT Premier Total            325 0001-02-20 0012-12-20
##  6 KTITR Korea/Taiwan IT Premier Total             325 0001-02-20 0012-12-20
##  7 KTITTR Korea/Taiwan IT Premier Total            325 0001-02-20 0012-12-20
##  8 0050 Yuanta Taiwan Top50                        317 0001-02-20 0012-12-20
##  9 0051 TMT                                        317 0001-02-20 0012-12-20
## 10 0052 FB Technology                              317 0001-02-20 0012-12-20
## 11 0055 PTF                                        317 0001-02-20 0012-12-20
## 12 0057 FB MSCI Taiwan                             317 0001-02-20 0012-12-20
## 13 0061 PWC300                                     317 0001-02-20 0012-12-20
## 14 006201 Yuanta TW TPEx 50                        317 0001-02-20 0012-12-20
## 15 006205 FB SSE180                                317 0001-02-20 0012-12-20
## 16 006206 YT SSE50                                 317 0001-02-20 0012-12-20
## 17 00625K FB SSE180+R                              317 0001-02-20 0012-12-20
## 18 00631L T50Bull2X                                317 0001-02-20 0012-12-20
## 19 00632R T50Bear1X                                317 0001-02-20 0012-12-20
## 20 00633L Fubon SSE180 L2X ETF                     317 0001-02-20 0012-12-20

7. Average Closing Price by Company

average_close <- taiwan_stocks_hw3 %>%
  group_by(CO_ID) %>%
  summarise(
    Average_Close = mean(Close, na.rm = TRUE),
    .groups = "drop"
  ) %>%
  arrange(desc(Average_Close))

head(average_close, 20)
## # A tibble: 20 × 2
##    CO_ID                               Average_Close
##    <chr>                                       <dbl>
##  1 EDRL2 Eelectronics 2X                      90581.
##  2 WIT02 TW Technology (TR)                   87227.
##  3 TDRL2 TAIEX 2X                             66602.
##  4 TRTEJ TRTEJ                                55792.
##  5 Y9997 Total Return Index                   53740.
##  6 FDRL2 Fin. leveraged 2X Index              48642.
##  7 Y5556 TSE Non-Elec. TR                     47981.
##  8 IR127 TIP APL TR 2X                        46721.
##  9 TRI50 TW 50 INDEX (TR)                     46480.
## 10 TR100 TW MID-CAP (TR)                      41934.
## 11 WIT01 TW Technology Index                  41464.
## 12 FRMSR Formosa Total Return Index           40635.
## 13 IR123 TW 50 30% Cap TR                     39992.
## 14 Y8887 TR NonF NonE                         32542.
## 15 TF002 TWDP(TR)                             29017.
## 16 FRMSA Formosa Index                        26971.
## 17 SA50R Small/Mid-Cap Alpha Mom. 50TR        26730.
## 18 IR141 TPEx 200 TR 2X Index                 26543.
## 19 Y9998 TEJ Index                            23994.
## 20 Y9999 TSE Taiex                            23994.

8. Highest Closing Price by Company

highest_close <- taiwan_stocks_hw3 %>%
  group_by(CO_ID) %>%
  summarise(
    Highest_Close = max(Close, na.rm = TRUE),
    .groups = "drop"
  ) %>%
  arrange(desc(Highest_Close))

head(highest_close, 20)
## # A tibble: 20 × 2
##    CO_ID                            Highest_Close
##    <chr>                                    <dbl>
##  1 EDRL2 Eelectronics 2X                  173067.
##  2 WIT02 TW Technology (TR)               126992.
##  3 TDRL2 TAIEX 2X                         111279.
##  4 IR127 TIP APL TR 2X                     91203.
##  5 TRTEJ TRTEJ                             73518.
##  6 Y9997 Total Return Index                72029.
##  7 TRI50 TW 50 INDEX (TR)                  65434.
##  8 FDRL2 Fin. leveraged 2X Index           61429.
##  9 WIT01 TW Technology Index               59589.
## 10 TR100 TW MID-CAP (TR)                   55525.
## 11 FRMSR Formosa Total Return Index        54016.
## 12 IR123 TW 50 30% Cap TR                  52742.
## 13 Y5556 TSE Non-Elec. TR                  51363.
## 14 IR141 TPEx 200 TR 2X Index              41411.
## 15 Y8887 TR NonF NonE                      36215.
## 16 FRMSA Formosa Index                     35271.
## 17 TF002 TWDP(TR)                          32817.
## 18 E99TR EMP99 TR                          32013.
## 19 Y9998 TEJ Index                         31639.
## 20 Y9999 TSE Taiex                         31639.

9. Lowest Closing Price by Company

lowest_close <- taiwan_stocks_hw3 %>%
  group_by(CO_ID) %>%
  summarise(
    Lowest_Close = min(Close, na.rm = TRUE),
    .groups = "drop"
  ) %>%
  arrange(Lowest_Close)

head(lowest_close, 20)
## # A tibble: 20 × 2
##    CO_ID                                 Lowest_Close
##    <chr>                                        <dbl>
##  1 02001R Fubon APL TR Daily Return Inve         0.79
##  2 6495 Nanoplus                                 1.05
##  3 911868 Neo-Neon Holdings Limited              1.19
##  4 00686R CTaiex-1X                              1.47
##  5 00685L CTaiex2X                               1.89
##  6 6434 TacBright                                2.1 
##  7 5314 Myson                                    2.43
##  8 00664R Cathay TAIEX Inv 1X                    2.43
##  9 912000 SIM Technology Group Limited           2.43
## 10 911608 BH Global Corporation Limited          2.59
## 11 00643K CSZSME+R                               2.7 
## 12 00671R Fubon NASDAQ-100 -1X                   2.77
## 13 6626 Game Hours                               2.84
## 14 9110 Vietnam Manufacturing and Expo           2.9 
## 15 4804 Da Lue                                   2.93
## 16 00634R FubonSSE180 INVR.ETF                   3.15
## 17 4529 EPTECH                                   3.33
## 18 020033 President HSTECH Futures Index         3.5 
## 19 2443 Lead Data                                3.57
## 20 6734 iXensor                                  3.60

10. TSMC Daily Closing Price

tsmc <- taiwan_stocks_hw3 %>%
  filter(CO_ID == "2330")

ggplot(tsmc, aes(x = Date, y = Close)) +
  geom_line() +
  labs(
    title = "TSMC (2330) Daily Closing Price",
    x = "Date",
    y = "Closing Price (NT$)"
  )

11. TSMC Daily Price Range

ggplot(tsmc, aes(x = Date)) +
  geom_line(aes(y = High), linetype = "dashed") +
  geom_line(aes(y = Low), linetype = "dashed") +
  geom_line(aes(y = Close)) +
  labs(
    title = "TSMC (2330) Daily High, Low and Closing Prices",
    x = "Date",
    y = "Price (NT$)"
  )

12. Monthly Average Closing Price

monthly_close <- taiwan_stocks_hw3 %>%
  mutate(
    Year_Month = format(Date, "%Y-%m")
  ) %>%
  group_by(Year_Month) %>%
  summarise(
    Average_Close = mean(Close, na.rm = TRUE),
    .groups = "drop"
  )

head(monthly_close, 20)
## # A tibble: 20 × 2
##    Year_Month Average_Close
##    <chr>              <dbl>
##  1 0001-02            1010.
##  2 0001-03             926.
##  3 0001-05            1109.
##  4 0001-06            1038.
##  5 0001-07            1042.
##  6 0001-08            1035.
##  7 0001-09            1026.
##  8 0001-10             924.
##  9 0001-12            1135.
## 10 0002-03             913.
## 11 0002-04             913.
## 12 0002-05             930.
## 13 0002-06             937.
## 14 0002-07             943.
## 15 0002-10             936.
## 16 0002-11             940.
## 17 0002-12             937.
## 18 0003-03             935.
## 19 0003-04             933.
## 20 0003-05             942.

13. Monthly Average Closing Price Chart

ggplot(monthly_close, aes(x = Year_Month, y = Average_Close, group = 1)) +
  geom_line() +
  labs(
    title = "Monthly Average Closing Price",
    x = "Month",
    y = "Average Closing Price (NT$)"
  ) +
  theme(
    axis.text.x = element_text(angle = 90, hjust = 1)
  )

14. Conclusion

The dataset contains daily stock price information for companies listed on the Taiwan stock market during the selected two-year period. The main variables are opening price, highest price, lowest price, and closing price.

The analysis summarizes the number of companies, observations, data period, average closing prices, highest closing prices, and lowest closing prices. TSMC (stock code 2330) is also used as an example to visualize daily stock price movements.