rm(list=ls())
library(tidyquant)
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
## 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
## Registered S3 method overwritten by 'xts':
##   method     from
##   as.zoo.xts zoo
## 
## 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
## Version 0.4-0 included new data defaults. See ?getSymbols.
## ══ 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)
## 
## Attaching package: 'timetk'
## The following objects are masked from 'package:tidyquant':
## 
##     summarise_by_time, summarize_by_time
library(quantmod)
library(pacman)
p_load(tidyverse, lubridate, readxl, highcharter, tidyquant, 
       timetk, tibbletime, quantmod, PerformanceAnalytics, scales)
p_load(reshape2)
#1
library(readr)
m.price <- read_tsv("~/TEST/tej_day_price_2017_2018.txt")
## Parsed with column specification:
## cols(
##   證券代碼 = col_double(),
##   簡稱 = col_character(),
##   `TSE 產業別` = col_character(),
##   上市別 = col_character(),
##   年月日 = col_double(),
##   `開盤價(元)` = col_double(),
##   `最高價(元)` = col_double(),
##   `收盤價(元)` = col_double(),
##   `最低價(元)` = col_double(),
##   `成交值(千元)` = col_double(),
##   `市值(百萬元)` = col_double(),
##   `成交量(千股)` = col_double()
## )
etf3<-rename(m.price)
head(etf3)
## # A tibble: 6 x 12
##   證券代碼 簡稱  `TSE 產業別` 上市別 年月日 `開盤價(元)` `最高價(元)`
##      <dbl> <chr> <chr>        <chr>   <dbl>        <dbl>        <dbl>
## 1     1101 台泥  01           TSE    2.02e7        29.9         29.9 
## 2     1102 亞泥  01           TSE    2.02e7        24.9         25.0 
## 3     1103 嘉泥  01           TSE    2.02e7         8.27         8.27
## 4     1104 環泥  01           TSE    2.02e7        21.4         21.5 
## 5     1108 幸福  01           TSE    2.02e7         8.56         8.57
## 6     1109 信大  01           TSE    2.02e7        10.1         10.1 
## # … with 5 more variables: `收盤價(元)` <dbl>, `最低價(元)` <dbl>,
## #   `成交值(千元)` <dbl>, `市值(百萬元)` <dbl>, `成交量(千股)` <dbl>
#2
aes <- m.price %>% 
  rename(id    = "證券代碼", 
         name  = "簡稱", 
         date  = "年月日", 
         price = "收盤價(元)",
         cap   = "市值(百萬元)")

head(aes)
## # A tibble: 6 x 12
##      id name  `TSE 產業別` 上市別   date `開盤價(元)` `最高價(元)` price
##   <dbl> <chr> <chr>        <chr>   <dbl>        <dbl>        <dbl> <dbl>
## 1  1101 台泥  01           TSE    2.02e7        29.9         29.9  29.6 
## 2  1102 亞泥  01           TSE    2.02e7        24.9         25.0  25.0 
## 3  1103 嘉泥  01           TSE    2.02e7         8.27         8.27  8.27
## 4  1104 環泥  01           TSE    2.02e7        21.4         21.5  21.5 
## 5  1108 幸福  01           TSE    2.02e7         8.56         8.57  8.57
## 6  1109 信大  01           TSE    2.02e7        10.1         10.1  10.1 
## # … with 4 more variables: `最低價(元)` <dbl>, `成交值(千元)` <dbl>, cap <dbl>,
## #   `成交量(千股)` <dbl>
#3
aww <- m.price %>% 
  rename(id    = 證券代碼, 
         name  = 簡稱, 
         date  = 年月日, 
         price = `收盤價(元)`,
         cap   = `市值(百萬元)`
  ) %>% 
  mutate(id = as.character(id)) %>%
  mutate(date = as.Date(as.character(date), '%Y%m%d')) %>%
  select(id, date, price) %>% 
  spread(key = id, value = price) 

head(aww) 
## # A tibble: 6 x 929
##   date       `1101` `1102` `1103` `1104` `1108` `1109` `1110` `1201` `1203`
##   <date>      <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
## 1 2017-01-03   29.6   25.0   8.27   21.5   8.57   10.1   15.5   17.6   20.8
## 2 2017-01-04   29.7   24.8   8.23   21.5   8.56   10.1   15.4   17.4   20.9
## 3 2017-01-05   29.7   25.0   8.24   21.5   8.61   10.1   15.4   17.5   20.9
## 4 2017-01-06   29.7   25.0   8.21   21.4   8.57   10.1   15.4   17.4   21.1
## 5 2017-01-09   29.4   24.8   8.31   21.4   8.59   10.1   15.3   17.6   21.1
## 6 2017-01-10   29.5   24.7   8.28   21.4   8.59   10.2   15.3   17.4   20.9
## # … with 919 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>, `1256` <dbl>,
## #   `1262` <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>, `1325` <dbl>, `1326` <dbl>,
## #   `1337` <dbl>, `1338` <dbl>, `1339` <dbl>, `1340` <dbl>, `1341` <dbl>,
## #   `1402` <dbl>, `1409` <dbl>, `1410` <dbl>, `1413` <dbl>, `1414` <dbl>,
## #   `1416` <dbl>, `1417` <dbl>, `1418` <dbl>, `1419` <dbl>, `1423` <dbl>,
## #   `1432` <dbl>, `1434` <dbl>, `1435` <dbl>, `1436` <dbl>, `1437` <dbl>,
## #   `1438` <dbl>, `1439` <dbl>, `1440` <dbl>, `1441` <dbl>, `1442` <dbl>,
## #   `1443` <dbl>, `1444` <dbl>, `1445` <dbl>, `1446` <dbl>, `1447` <dbl>,
## #   `1449` <dbl>, `1451` <dbl>, `1452` <dbl>, `1453` <dbl>, `1454` <dbl>,
## #   `1455` <dbl>, `1456` <dbl>, `1457` <dbl>, `1459` <dbl>, `1460` <dbl>,
## #   `1463` <dbl>, `1464` <dbl>, `1465` <dbl>, `1466` <dbl>, `1467` <dbl>,
## #   `1468` <dbl>, `1470` <dbl>, `1471` <dbl>, `1472` <dbl>, `1473` <dbl>,
## #   `1474` <dbl>, `1475` <dbl>, `1476` <dbl>, `1477` <dbl>, `1503` <dbl>,
## #   `1504` <dbl>, `1506` <dbl>, `1507` <dbl>, `1512` <dbl>, `1513` <dbl>,
## #   `1514` <dbl>, `1515` <dbl>, …
#4
tej1.5 <- aww %>%
  select(1:6) %>%
  tk_xts(select = -date, date_var = date) %>% 
  Return.calculate(method = "log") %>%
  na.omit()
## Warning: `select_()` is deprecated as of dplyr 0.7.0.
## Please use `select()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
dim(tej1.5)
## [1] 492   5
head(tej1.5,5)
##                    1101         1102         1103         1104         1108
## 2017-01-04  0.003031837 -0.005627024 -0.004848494  0.000000000 -0.001167542
## 2017-01-05  0.000000000  0.005627024  0.001214329  0.001858737  0.005824128
## 2017-01-06  0.000000000  0.000000000 -0.003647420 -0.006053569 -0.004656586
## 2017-01-09 -0.010142075 -0.007644374  0.012106685  0.000000000  0.002331003
## 2017-01-10  0.001358235 -0.003641517 -0.003616640 -0.001870033  0.000000000
#5
ccc <- aww %>%
  select(1:6) %>%
  tk_xts(select = -date, date_var = date)  

bbb <- ccc %>% 
  to.period(period = "months", 
            indexAt = "lastof", 
            OHLC= FALSE) %>% 
  Return.calculate(method = "log") %>%
  na.omit()

dim(bbb)
## [1] 23  5
head(bbb, 10)
##                    1101         1102        1103         1104         1108
## 2017-02-28  0.061707886  0.127658684  0.19286519  0.068744779  0.102909963
## 2017-03-31 -0.036565972  0.006616775 -0.05291257  0.013370913 -0.020181248
## 2017-04-30 -0.033552392 -0.026377474 -0.02314472 -0.022969591 -0.031610261
## 2017-05-31 -0.020133763 -0.067822596 -0.01871054  0.003937874 -0.010016778
## 2017-06-30  0.022832820 -0.064589374  0.02357673 -0.042825456 -0.007860793
## 2017-07-31  0.035416576  0.022527099 -0.01368545 -0.026323386  0.001126761
## 2017-08-31 -0.004236604  0.057578197  0.13684774  0.017165812 -0.007914117
## 2017-09-30 -0.030506344 -0.018571692  0.02876679  0.008701681 -0.012564414
## 2017-10-31 -0.008792752  0.001910950 -0.02448413  0.006816659  0.001148765
## 2017-11-30  0.001357773  0.034525005  0.03691694 -0.019666767 -0.022055450
#6
tns <- read_tsv("TEST/tej_day_price_2017_2018.txt", col_names = TRUE)
## Parsed with column specification:
## cols(
##   證券代碼 = col_double(),
##   簡稱 = col_character(),
##   `TSE 產業別` = col_character(),
##   上市別 = col_character(),
##   年月日 = col_double(),
##   `開盤價(元)` = col_double(),
##   `最高價(元)` = col_double(),
##   `收盤價(元)` = col_double(),
##   `最低價(元)` = col_double(),
##   `成交值(千元)` = col_double(),
##   `市值(百萬元)` = col_double(),
##   `成交量(千股)` = col_double()
## )
glimpse(tns)
## Rows: 443,171
## Columns: 12
## $ 證券代碼       <dbl> 1101, 1102, 1103, 1104, 1108, 1109, 1110, 1201, 1203, 1210…
## $ 簡稱           <chr> "台泥", "亞泥", "嘉泥", "環泥", "幸福", "信大", "東泥", "味全", "味王", "大…
## $ `TSE 產業別`   <chr> "01", "01", "01", "01", "01", "01", "01", "02", "02", "02…
## $ 上市別         <chr> "TSE", "TSE", "TSE", "TSE", "TSE", "TSE", "TSE", "TSE", "…
## $ 年月日         <dbl> 20170103, 20170103, 20170103, 20170103, 20170103, 2017010…
## $ `開盤價(元)`   <dbl> 29.90, 24.91, 8.27, 21.41, 8.56, 10.11, 15.58, 17.55, 21.3…
## $ `最高價(元)`   <dbl> 29.90, 24.95, 8.27, 21.54, 8.57, 10.11, 15.63, 17.70, 21.3…
## $ `收盤價(元)`   <dbl> 29.64, 24.95, 8.27, 21.50, 8.57, 10.11, 15.54, 17.65, 20.8…
## $ `最低價(元)`   <dbl> 29.35, 24.76, 8.17, 21.37, 8.56, 10.11, 15.54, 17.55, 20.8…
## $ `成交值(千元)` <dbl> 101450, 33550, 2411, 3705, 182, 42, 406, 11504, 171, 64713,…
## $ `市值(百萬元)` <dbl> 129779, 89078, 6748, 15610, 3703, 3789, 9009, 8931, 5472, 2…
## $ `成交量(千股)` <dbl> 2890, 1271, 278, 150, 20, 4, 25, 653, 7, 2213, 19, 2947, 27…
aws6<-tns %>% select('證券代碼', '簡稱', '年月日', '市值(百萬元)') %>% 
  rename(id = '證券代碼', name = '簡稱', date = '年月日', cap = '市值(百萬元)') %>%      
  mutate(date = date %>% as.character %>% as.Date('%Y%m%d')) %>% 
  mutate(id = id %>% as.character) %>% 
  arrange(desc(date), desc(cap)) %>%  
  select(3,4,1,2) %>%
  slice(1:20, 224877:224896)
head(aws6)
## # A tibble: 6 x 4
##   date           cap id    name  
##   <date>       <dbl> <chr> <chr> 
## 1 2018-12-28 5847300 2330  台積電
## 2 2018-12-28 1038329 6505  台塑化
## 3 2018-12-28  981499 2317  鴻海  
## 4 2018-12-28  876591 2412  中華電
## 5 2018-12-28  642939 1301  台塑  
## 6 2018-12-28  615424 1326  台化