This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
#1.
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(timetk)
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
library(quantmod)
library(tidyr)
#IMPORT DATA
rm(list=ls())
stock_day_3_year <- read.delim("C:/Users/hiits/Downloads/tej_2016_2018.txt")
#DATA WRANGLING
stock_day <- stock_day_3_year %>%
rename(id = CO_ID,
name = CoName,
date = Date,
cap_share = "MV."
) %>%
mutate(id = as.character(id)) %>%
mutate(date = as.Date(as.character(date), '%Y%m%d')) %>%
mutate(cap_share = as.numeric(cap_share))
#FROM LONG TO WIDE
price_close <- stock_day %>%
select(id, date, Close) %>%
spread(key = id, value = Close)
#
#3.
ret_day <- stock_day %>%
tk_xts(select = -date, date_var = date) %>%
Return.calculate(method = "log")
ret_day[is.na(ret_day)] <- 0
head(ret_day)
## TSE.Sector Open High Low Close
## 2016-01-04 0 0.00000000 0.00000000 0.00000000 0.00000000
## 2016-01-04 0 0.19795970 0.19795970 0.18658596 0.20007537
## 2016-01-04 0 -1.02031861 -1.02031861 -0.98128205 -0.99477146
## 2016-01-04 0 0.72762328 0.72762328 0.71819321 0.71819321
## 2016-01-04 0 -0.69487181 -0.69487181 -0.67911186 -0.67911186
## 2016-01-04 0 0.09854795 0.09854795 0.09836748 0.09836748
## Volume Market.Cap. Shares cap_share
## 2016-01-04 0.0000000 0.00000000 0.00000000 0.00000000
## 2016-01-04 -0.4208166 -0.09003463 -0.09384418 -0.09015110
## 2016-01-04 -3.9419467 -2.49462083 -1.46754770 -2.48221486
## 2016-01-04 0.5520686 0.62481129 -0.20957887 0.60906406
## 2016-01-04 -1.0479686 -1.21224097 -0.43976031 -1.20983792
## 2016-01-04 -1.6863990 -0.06587373 -0.06596763 -0.06062462
#4
ret_mon <-stock_day %>%
tk_xts(select = -date, date_var = date) %>%
to.period(period = "months",
indexAt = "lastof",
OHLC = FALSE) %>%
Return.calculate(method = 'log')
ret_mon[is.na(ret_mon)] <- 0
ret_mon[1:10,]
## TSE.Sector Open High Low Close
## 2016-01-31 0 0.000000000 0.000000000 0.000000000 0.000000000
## 2016-02-29 0 0.081081669 0.105360516 0.099263988 0.094687321
## 2016-03-31 0 0.000000000 -0.018349139 -0.009389740 -0.005205634
## 2016-04-30 0 -0.014644613 -0.023947006 -0.014783796 -0.018967903
## 2016-05-31 0 -0.034301326 -0.034301326 -0.036840569 -0.029143995
## 2016-06-30 0 -0.012068166 -0.009863094 -0.008869238 -0.007696575
## 2016-07-31 0 0.008791265 0.019629856 0.016565812 0.009884759
## 2016-08-31 0 0.038631413 0.026639828 0.032330402 0.030142217
## 2016-09-30 0 0.001052078 0.000000000 -0.002123143 0.004232811
## 2016-10-31 0 0.014614039 0.019782009 0.025184962 0.023996978
## Volume Market.Cap. Shares cap_share
## 2016-01-31 0.0000000 0.000000000 0 0.0000000
## 2016-02-29 1.8640801 0.094237684 0 0.1335314
## 2016-03-31 -1.3785723 -0.004609483 0 0.0000000
## 2016-04-30 0.9132091 -0.019176540 0 0.0000000
## 2016-05-31 -0.5337194 -0.029741969 0 -0.1335314
## 2016-06-30 -1.2580400 -0.008119124 0 0.0000000
## 2016-07-31 0.4883528 -0.030347156 0 0.0000000
## 2016-08-31 0.6346510 0.030347156 0 0.0000000
## 2016-09-30 -0.7552790 0.004338402 0 0.0000000
## 2016-10-31 -0.4446858 0.023530497 0 0.0000000
#5
largest20_2017 <- stock_day %>%
select(id, name, date, Shares) %>%
filter(date == "2017-12-29") %>%
mutate(year1 = year(date)) %>%
select(date, year1, Shares, id, name) %>%
arrange(desc(Shares)) %>%
slice(1:20) %>%
ungroup()
glimpse(largest20_2017)
## Rows: 20
## Columns: 5
## $ date <date> 2017-12-29, 2017-12-29, 2017-12-29, 2017-12-29, 2017-12-29, 20~
## $ year1 <dbl> 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 201~
## $ Shares <int> 25930380, 19496989, 17328738, 15734860, 14976819, 13599823, 126~
## $ id <chr> "2330", "2891", "2317", "2002", "2883", "2886", "2303", "2882",~
## $ name <chr> "TSMC ", "CTBC Holding ", "Hon Hai Precision", "China S~
#6.
largest20_2018 <- stock_day %>%
select(id, name, date, Shares) %>%
filter(date == "2018-12-28") %>%
mutate(year2 = year(date)) %>%
select(date, year2, Shares, id, name) %>%
arrange(desc(Shares)) %>%
slice(1:20) %>%
ungroup()
glimpse(largest20_2018)
## Rows: 20
## Columns: 5
## $ date <date> 2018-12-28, 2018-12-28, 2018-12-28, 2018-12-28, 2018-12-28, 20~
## $ year2 <dbl> 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 201~
## $ Shares <int> 25930380, 19496989, 15734860, 14963355, 13862990, 13599823, 125~
## $ id <chr> "2330", "2891", "2002", "2883", "2317", "2886", "5880", "2882",~
## $ name <chr> "TSMC ", "CTBC Holding ", "China Steel ", "China Deve.~
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.