Homework

I downloaded the daily closing price of ETF 0050, 0052, and 0056 from the Taiwan Economic Journal database. And then I imported the data into R and convert it into time series. Using the price data, I calculated the daily, weekly, and monthly returns of each stock.

Here is the code I wrote on the Rstudio.

setwd(dir = "/home/rstudio2/hq")
getwd()
## [1] "/home/rstudio2/hq"
homework <- read.delim("~/hq/homework.txt", header=TRUE)
head(homework)
##   CO_ID              CoName     Date   Close
## 1    50 Yuanta Taiwan Top50 20100104 37.5723
## 2    52       FB Technology 20100104 23.1720
## 3    56       PTD           20100104 11.9283
## 4    50 Yuanta Taiwan Top50 20100105 37.5723
## 5    52       FB Technology 20100105 23.2037
## 6    56       PTD           20100105 11.8775

The table above is raw data, which I downloaded from the TEJ database. Here I show how I imported data to R.

Here is the code I wrote.

    rm(list=ls())
    library(pacman)
    p_load(tidyverse, lubridate, readxl, highcharter, tidyquant, 
    +        timetk, tibbletime, quantmod, PerformanceAnalytics, scales)
    p_load(reshape2)
    getwd()
    setwd(dir = "/home/rstudio2/hq");
    }

Firstly, I used getwd() and setwd() to set the working directory.

The result of the code

Here is the result. It shows company IDs, company names, dates, and closing prices. As we know, company IDs are 0050,0052, and 0056. Since I downloaded data between 2010 and 2021, the date starts from the 4th of January 2010.

CO_ID              CoName     Date   Close
1    50 Yuanta Taiwan Top50 20100104 37.5723
2    52       FB Technology 20100104 23.1720
3    56       PTD           20100104 11.9283
4    50 Yuanta Taiwan Top50 20100105 37.5723
5    52       FB Technology 20100105 23.2037
6    56       PTD           20100105 11.8775

Changing row and column names

homework \<- homework[, -2] colnames(homework) \<- c("id", "date","close") homework.1 \<- dcast(homework, date\~id)

head(homework.1) 

Result of changing names of rows and columns date

             50       52     56 
1 20100104 37.5723 23.1720 11.9283 
2 20100105 37.5723 23.2037 11.8775 
3 20100106 38.2705 23.7433 12.0552 
4 20100107 38.1708 23.4894 11.9537 
5 20100108 38.4035 23.4195 12.0805 
6 20100111 38.5033 23.5529 12.2074

After changing rows and columns, it looks tidier.

Converting into time series

library(xts)
homework.xts <- xts(homework.1[,-1], order.by= as.Date(as.character(homework.1$date), format="%Y%m%d"))


homework.xts <- xts(homework.1[,-1], order.by= as.Date(as.character(homework.1$date), format="%Y%m%d"))
homework.xts <- xts(homework.1[,-1], order.by= parse.Date(as.character(homework.1$date), format="%Y%m%d"))
head(homework.xts)

The result of converting into time series

After converting into time series looks easier to distinguish which shows the date of the closing price. Becaseparated, month and day are separated from each other by hyphen “-”.

             50      52      56
2010-01-04 37.5723 23.1720 11.9283
2010-01-05 37.5723 23.2037 11.8775
2010-01-06 38.2705 23.7433 12.0552
2010-01-07 38.1708 23.4894 11.9537
2010-01-08 38.4035 23.4195 12.0805
2010-01-11 38.5033 23.5529 12.2074

Converting time series into a month

Monthly = to.monthly(homework.xts, OHLC=FALSE) 
> head(Monthly, )6
              

Result

            50      52      56
Jan 2010 35.2448 21.5848 11.3445
Feb 2010 34.2805 21.0452 11.1161
Mar 2010 36.0760 22.1435 11.5730
Apr 2010 36.4418 22.3086 11.8471
May 2010 33.3163 20.4308 11.1009
Jun 2010 32.9506 19.8357 11.1161

Calculating monthly return

MonthlyReturns = diff(Monthly)/lag(Monthly)
head(MonthlyReturns, n=6)
                50           52           56
Jan 2010          NA           NA           NA
Feb 2010 -0.02736006 -0.024999073 -0.020133104
Mar 2010  0.05237672  0.052187672  0.041102545
Apr 2010  0.01013971  0.007455913  0.023684438
May 2010 -0.08576689 -0.084173816 -0.062985878
Jun 2010 -0.01097661 -0.029127592  0.001369258

Calculating weekly return

Weekly= to.weekly(homework.xts, OHLC=FALSE)
head(Weekly, 6)
weeklyReturn = diff(Weekly)/lag(Weekly)
head(weeklyReturn, n=6)
                  50          52          56
2010-01-08            NA          NA          NA
2010-01-15  0.0008671085  0.01924892  0.01345143
2010-01-22 -0.0588238355 -0.06648848 -0.04436004
2010-01-29 -0.0257354364 -0.03134200 -0.03037633
2010-02-06 -0.0339624569 -0.04852952 -0.04518489
2010-02-10  0.0175782283  0.03554995  0.02858224

Calculating daily return

Daily = to.daily(homework.xts, OHLC=FALSE)
head(Daily, 6)

DailyReturn = diff(Daily)/lag(Daily)
head(DailyReturn, n=6)
                     50           52           56
2010-01-04           NA           NA           NA
2010-01-05  0.000000000  0.001368030 -0.004258780
2010-01-06  0.018582839  0.023254912  0.014961061
2010-01-07 -0.002605140 -0.010693543 -0.008419603
2010-01-08  0.006096283 -0.002975810  0.010607594
2010-01-11  0.002598721  0.005696108  0.010504532

Comparing monthly, weekly and daily returns, weekly returns of each stock is greater than others.

This is the end of homework.