# Q1. Install library tidyquant, timetk and quantmod. Import data tej_2016_2018.txt.
library(timetk)
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
## ══ Need to Learn tidyquant? ════════════════════════════════════════════════════
## Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
## </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
library(quantmod)
library(tidyr)
library(ggplot2)
library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:xts':
## 
##     first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
rm(list = ls())
tej_stock <- read_tsv("tej_2015_2021.txt")
## Rows: 5920 Columns: 12
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (3): CO_ID, CoName, MV%
## dbl (9): Date, Open, High, Low, Close, Volume, Amount, Shares, Market Cap.
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
glimpse(tej_stock)
## Rows: 5,920
## Columns: 12
## $ CO_ID         <chr> "0050", "0056", "006205", "00646", "0050", "0056", "0062…
## $ CoName        <chr> "Yuanta Taiwan Top50", "PTD", "FB SSE180_0", "S&P 500_6"…
## $ Date          <dbl> 20151214, 20151214, 20151214, 20151214, 20151215, 201512…
## $ Open          <dbl> 49.4688, 15.2653, 30.7000, 19.4400, 49.8445, 15.2942, 31…
## $ High          <dbl> 49.8028, 15.3376, 31.2800, 19.6400, 50.1368, 15.4244, 31…
## $ Low           <dbl> 49.2601, 15.1206, 30.5200, 19.0000, 49.5523, 15.2942, 31…
## $ Close         <dbl> 49.5941, 15.2942, 31.0600, 19.6100, 49.6358, 15.4027, 31…
## $ Volume        <dbl> 9528, 2614, 5104, 3839, 6796, 716, 4935, 1761, 6530, 145…
## $ Amount        <dbl> 565570, 55050, 158105, 74995, 405048, 15233, 156235, 346…
## $ Shares        <dbl> 1287000, 361534, 432772, 63485, 1287000, 361534, 430772,…
## $ `Market Cap.` <dbl> 76448, 7643, 13442, 1245, 76512, 7697, 13608, 1246, 7767…
## $ `MV%`         <chr> "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "…
# Q2. Select column CO_ID, DATE, Close. Convert CO_ID into text format, DATE into date format and change data from long format to wide format. (Hint: You can use `spread()`)
tej_stock$CO_ID <- as.character(tej_stock$CO_ID)
tej_stock$Date <- as.character(tej_stock$Date)
tej_stock$Date <- as.Date(tej_stock$Date, "%Y%m%d")
tej_stock %>% 
  select(CO_ID, Date, Close) %>%
  spread(key = CO_ID, value = Close)
## # A tibble: 1,480 × 5
##    Date       `0050` `0056` `006205` `00646`
##    <date>      <dbl>  <dbl>    <dbl>   <dbl>
##  1 2015-12-14   49.6   15.3     31.1    19.6
##  2 2015-12-15   49.6   15.4     31.6    19.6
##  3 2015-12-16   50.4   15.6     31.6    19.9
##  4 2015-12-17   51.0   15.8     32.2    20.0
##  5 2015-12-18   50.7   15.9     32.2    19.8
##  6 2015-12-21   50.6   15.9     33      19.6
##  7 2015-12-22   50.8   15.9     33.1    19.7
##  8 2015-12-23   50.8   15.9     33.1    19.8
##  9 2015-12-24   51.1   15.9     32.8    20.0
## 10 2015-12-25   51.4   15.9     33.0    20.0
## # … with 1,470 more rows
#Q3. Convert data into xts format (Hint: you can use`tk_xts()`). Compute daily returns (you can use log or `Return.calculate()`). Show the first five daily returns. 
ret_day <- tej_stock %>% 
  tk_xts(select = -Date, date_var = Date) %>% 
  Return.calculate(method = "log") 
## Warning: Non-numeric columns being dropped: CO_ID, CoName, MV%
ret_day[is.na(ret_day)] <- 0
head(ret_day)
##                  Open       High        Low      Close     Volume     Amount
## 2015-12-14  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000
## 2015-12-14 -1.1757599 -1.1777639 -1.1810564 -1.1763982 -1.2933532 -2.3295922
## 2015-12-14  0.6986804  0.7126716  0.7023242  0.7084471  0.6691429  1.0550175
## 2015-12-14 -0.4569299 -0.4654106 -0.4739432 -0.4598812 -0.2848126 -0.7458379
## 2015-12-15  0.9415754  0.9371870  0.9585897  0.9286727  0.5711223  1.6865841
## 2015-12-15 -1.1814345 -1.1788046 -1.1755550 -1.1701695 -2.2504093 -3.2805415
##                Shares Market Cap.
## 2015-12-14  0.0000000   0.0000000
## 2015-12-14 -1.2697131  -2.3028206
## 2015-12-14  0.1798549   0.5645939
## 2015-12-14 -1.9194074  -2.3792486
## 2015-12-15  3.0092656   4.1183121
## 2015-12-15 -1.2697131  -2.2966169
#Q4. Select the first 10 stocks and compute monthly returns. (Hint: you can use `to.monthly()` or `to.period()` to convert daily data into monthly data. Show the first 10 monthly returns. 
ret_mon <- tej_stock %>% 
  tk_xts(select = -Date, date_var = Date) %>%
  to.period(period  = "months",
            indexAt = "lastof",
            OHLC    = FALSE)  %>%
  Return.calculate(method = 'log')
## Warning: Non-numeric columns being dropped: CO_ID, CoName, MV%
ret_mon[is.na(ret_mon)] <- 0
ret_mon[1:10,]
##                    Open          High          Low        Close     Volume
## 2015-12-31  0.000000000  0.0000000000  0.000000000  0.000000000  0.0000000
## 2016-01-31 -0.045334985 -0.0416515259 -0.045949890 -0.039659493 -1.0731332
## 2016-02-29  0.003121751 -0.0015572284  0.003650591 -0.003637312  1.4065285
## 2016-03-31  0.026654576  0.0266545764  0.024171915  0.025695144 -1.2714260
## 2016-04-30  0.006554095  0.0065540945  0.004056801  0.009593611 -1.2820334
## 2016-05-31  0.021869659  0.0218696589  0.026468462  0.021869659  1.1839721
## 2016-06-30 -0.027918064 -0.0243853835 -0.026974662 -0.026402530  0.6370577
## 2016-07-31  0.036728694  0.0331960132  0.030909739  0.030327952 -0.8157495
## 2016-08-31 -0.003906255 -0.0009751342  0.003430535  0.003910073 -1.9568392
## 2016-09-30 -0.019763489 -0.0221957324 -0.021761492 -0.023693112  2.5747630
##                Amount      Shares Market Cap.
## 2015-12-31  0.0000000  0.00000000  0.00000000
## 2016-01-31 -1.1169436  0.09747728  0.05720202
## 2016-02-29  1.4075149 -0.02899384 -0.03239455
## 2016-03-31 -1.2458149 -0.01481838  0.01066271
## 2016-04-30 -1.2776523 -0.06157211 -0.05209106
## 2016-05-31  1.2086097 -0.19194139 -0.17010597
## 2016-06-30  0.6124514 -0.12263996 -0.14864709
## 2016-07-31 -0.7850014 -0.17804670 -0.14758610
## 2016-08-31 -1.9553534 -0.09534916 -0.09188095
## 2016-09-30  2.5533146 -0.01439495 -0.03838424