rm(list=ls())
#install.packages('quantmod')
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
getSymbols("GOOG", auto.assign = TRUE)
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## [1] "GOOG"
# There are two primary time series data
# xts and zoo
tickers = c("GOOG", "MSFT", "AMZN")
getSymbols(tickers, from = "2010-01-01", auto.assign = TRUE)
## [1] "GOOG" "MSFT" "AMZN"
# download multiple prices to data variable
data = new.env()
getSymbols(tickers, from = "2010-01-01", env = data , auto.assign = TRUE)
## [1] "GOOG" "MSFT" "AMZN"
ls(data)
## [1] "AMZN" "GOOG" "MSFT"
names(data)
## [1] "GOOG"        ".getSymbols" "AMZN"        "MSFT"
head(data$AMZN)
##            AMZN.Open AMZN.High AMZN.Low AMZN.Close AMZN.Volume
## 2010-01-04    136.25    136.61   133.14     133.90     7599900
## 2010-01-05    133.43    135.48   131.81     134.69     8851900
## 2010-01-06    134.60    134.73   131.65     132.25     7178800
## 2010-01-07    132.01    132.32   128.80     130.00    11030200
## 2010-01-08    130.56    133.68   129.03     133.52     9830500
## 2010-01-11    132.62    132.80   129.21     130.31     8779400
##            AMZN.Adjusted
## 2010-01-04        133.90
## 2010-01-05        134.69
## 2010-01-06        132.25
## 2010-01-07        130.00
## 2010-01-08        133.52
## 2010-01-11        130.31
str(AMZN)
## An 'xts' object on 2010-01-04/2019-03-26 containing:
##   Data: num [1:2322, 1:6] 136 133 135 132 131 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:6] "AMZN.Open" "AMZN.High" "AMZN.Low" "AMZN.Close" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
## List of 2
##  $ src    : chr "yahoo"
##  $ updated: POSIXct[1:1], format: "2019-03-27 21:57:31"
class(AMZN)
## [1] "xts" "zoo"
tail(AMZN,3)
##            AMZN.Open AMZN.High AMZN.Low AMZN.Close AMZN.Volume
## 2019-03-22   1810.17   1818.98  1763.11    1764.77     6363000
## 2019-03-25   1757.79   1782.68  1747.50    1774.26     5103800
## 2019-03-26   1793.00   1805.77  1773.36    1783.76     4838400
##            AMZN.Adjusted
## 2019-03-22       1764.77
## 2019-03-25       1774.26
## 2019-03-26       1783.76
names(AMZN)
## [1] "AMZN.Open"     "AMZN.High"     "AMZN.Low"      "AMZN.Close"   
## [5] "AMZN.Volume"   "AMZN.Adjusted"
ls(AMZN)
## [1] "AMZN.Adjusted" "AMZN.Close"    "AMZN.High"     "AMZN.Low"     
## [5] "AMZN.Open"     "AMZN.Volume"
head(AMZN$AMZN.Close)
##            AMZN.Close
## 2010-01-04     133.90
## 2010-01-05     134.69
## 2010-01-06     132.25
## 2010-01-07     130.00
## 2010-01-08     133.52
## 2010-01-11     130.31
AMZN2010_15=AMZN['2010/2015']
#Extract closing price: Cl(), 
# Adjusted price: Ad()
AMZN.ad<-Ad(AMZN)
head(AMZN.ad)
##            AMZN.Adjusted
## 2010-01-04        133.90
## 2010-01-05        134.69
## 2010-01-06        132.25
## 2010-01-07        130.00
## 2010-01-08        133.52
## 2010-01-11        130.31
class(AMZN.ad)
## [1] "xts" "zoo"
# write function to gather adjusted prices together
firm3<-merge(Ad(AMZN), Ad(GOOG), Ad(MSFT))
head(firm3)
##            AMZN.Adjusted GOOG.Adjusted MSFT.Adjusted
## 2010-01-04        133.90      311.3500      24.61580
## 2010-01-05        134.69      309.9789      24.62375
## 2010-01-06        132.25      302.1647      24.47263
## 2010-01-07        130.00      295.1305      24.21812
## 2010-01-08        133.52      299.0649      24.38515
## 2010-01-11        130.31      298.6128      24.07497
colnames(firm3)<-c("AMZN", "GOOG", "MSFG")
head(firm3)
##              AMZN     GOOG     MSFG
## 2010-01-04 133.90 311.3500 24.61580
## 2010-01-05 134.69 309.9789 24.62375
## 2010-01-06 132.25 302.1647 24.47263
## 2010-01-07 130.00 295.1305 24.21812
## 2010-01-08 133.52 299.0649 24.38515
## 2010-01-11 130.31 298.6128 24.07497
#Import data from .txt file
etf4<-read.table("ETF4_2010_2018_d1_english.txt", header = T, sep = ',')
str(etf4)
## 'data.frame':    6991 obs. of  5 variables:
##  $ CO_ID : num  50 56 50 56 50 56 50 56 50 56 ...
##  $ CoName: Factor w/ 4 levels "FB SSE180_0  ",..: 4 2 4 2 4 2 4 2 4 2 ...
##  $ Date  : int  20100104 20100104 20100105 20100105 20100106 20100106 20100107 20100107 20100108 20100108 ...
##  $ Close : num  41.9 15.5 41.9 15.4 42.7 ...
##  $ Volume: int  20083 281 16453 711 19012 402 14110 1223 11342 354 ...
# convert CO_ID into character;
etf4<-read.table("ETF4_2010_2018_d1_english.txt", header = T, sep = ',',
                 colClasses = c("CO_ID" = "character"))
str(etf4)
## 'data.frame':    6991 obs. of  5 variables:
##  $ CO_ID : chr  "0050   " "0056   " "0050   " "0056   " ...
##  $ CoName: Factor w/ 4 levels "FB SSE180_0  ",..: 4 2 4 2 4 2 4 2 4 2 ...
##  $ Date  : int  20100104 20100104 20100105 20100105 20100106 20100106 20100107 20100107 20100108 20100108 ...
##  $ Close : num  41.9 15.5 41.9 15.4 42.7 ...
##  $ Volume: int  20083 281 16453 711 19012 402 14110 1223 11342 354 ...
head(etf4)
##     CO_ID              CoName     Date Close Volume
## 1 0050    Yuanta Taiwan Top50 20100104 41.91  20083
## 2 0056          PTD           20100104 15.49    281
## 3 0050    Yuanta Taiwan Top50 20100105 41.91  16453
## 4 0056          PTD           20100105 15.42    711
## 5 0050    Yuanta Taiwan Top50 20100106 42.69  19012
## 6 0056          PTD           20100106 15.66    402
write.csv(etf4, "etf4.csv")
installed.packages('readr')
##      Package LibPath Version Priority Depends Imports LinkingTo Suggests
##      Enhances License License_is_FOSS License_restricts_use OS_type Archs
##      MD5sum NeedsCompilation Built
library(readr)
etf4_csv<-read_csv("etf4.csv")
## Warning: Missing column names filled in: 'X1' [1]
## Parsed with column specification:
## cols(
##   X1 = col_double(),
##   CO_ID = col_character(),
##   CoName = col_character(),
##   Date = col_double(),
##   Close = col_double(),
##   Volume = col_double()
## )
etf4_csv
## # A tibble: 6,991 x 6
##       X1 CO_ID CoName                  Date Close Volume
##    <dbl> <chr> <chr>                  <dbl> <dbl>  <dbl>
##  1     1 0050  Yuanta Taiwan Top50 20100104  41.9  20083
##  2     2 0056  PTD                 20100104  15.5    281
##  3     3 0050  Yuanta Taiwan Top50 20100105  41.9  16453
##  4     4 0056  PTD                 20100105  15.4    711
##  5     5 0050  Yuanta Taiwan Top50 20100106  42.7  19012
##  6     6 0056  PTD                 20100106  15.7    402
##  7     7 0050  Yuanta Taiwan Top50 20100107  42.6  14110
##  8     8 0056  PTD                 20100107  15.5   1223
##  9     9 0050  Yuanta Taiwan Top50 20100108  42.8  11342
## 10    10 0056  PTD                 20100108  15.7    354
## # ... with 6,981 more rows
#--------------------------
# clean data
etf4.c<-etf4_csv[, c(-1,-3,-6)]
etf4.c
## # A tibble: 6,991 x 3
##    CO_ID     Date Close
##    <chr>    <dbl> <dbl>
##  1 0050  20100104  41.9
##  2 0056  20100104  15.5
##  3 0050  20100105  41.9
##  4 0056  20100105  15.4
##  5 0050  20100106  42.7
##  6 0056  20100106  15.7
##  7 0050  20100107  42.6
##  8 0056  20100107  15.5
##  9 0050  20100108  42.8
## 10 0056  20100108  15.7
## # ... with 6,981 more rows
colnames(etf4.c)<-c("id", "date", "price")
etf4.c
## # A tibble: 6,991 x 3
##    id        date price
##    <chr>    <dbl> <dbl>
##  1 0050  20100104  41.9
##  2 0056  20100104  15.5
##  3 0050  20100105  41.9
##  4 0056  20100105  15.4
##  5 0050  20100106  42.7
##  6 0056  20100106  15.7
##  7 0050  20100107  42.6
##  8 0056  20100107  15.5
##  9 0050  20100108  42.8
## 10 0056  20100108  15.7
## # ... with 6,981 more rows
#install.packages("magrittr")
library(magrittr)
#install.packages("dplyr")
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
etf4.c<-etf4_csv%>%select(c(2,4,5))%>%rename("id" = "CO_ID", "date"= "Date", "price" = "Close")
etf4.c
## # A tibble: 6,991 x 3
##    id        date price
##    <chr>    <dbl> <dbl>
##  1 0050  20100104  41.9
##  2 0056  20100104  15.5
##  3 0050  20100105  41.9
##  4 0056  20100105  15.4
##  5 0050  20100106  42.7
##  6 0056  20100106  15.7
##  7 0050  20100107  42.6
##  8 0056  20100107  15.5
##  9 0050  20100108  42.8
## 10 0056  20100108  15.7
## # ... with 6,981 more rows
#install.packages("reshape2")
library(reshape2)
etf4.reorder = dcast(etf4.c, date~id)
## Using price as value column: use value.var to override.
dim(etf4.reorder)
## [1] 2223    5
head(etf4.reorder)
##       date  0050  0056 006205 00646
## 1 20100104 41.91 15.49     NA    NA
## 2 20100105 41.91 15.42     NA    NA
## 3 20100106 42.69 15.66     NA    NA
## 4 20100107 42.58 15.52     NA    NA
## 5 20100108 42.84 15.69     NA    NA
## 6 20100111 42.95 15.85     NA    NA
str(etf4.reorder)
## 'data.frame':    2223 obs. of  5 variables:
##  $ date  : num  20100104 20100105 20100106 20100107 20100108 ...
##  $ 0050  : num  41.9 41.9 42.7 42.6 42.8 ...
##  $ 0056  : num  15.5 15.4 15.7 15.5 15.7 ...
##  $ 006205: num  NA NA NA NA NA NA NA NA NA NA ...
##  $ 00646 : num  NA NA NA NA NA NA NA NA NA NA ...
# convert into date format using as.Date()
etf4.reorder$date<-as.Date(as.character(etf4.reorder$date), "%Y%m%d") 
head(etf4.reorder)
##         date  0050  0056 006205 00646
## 1 2010-01-04 41.91 15.49     NA    NA
## 2 2010-01-05 41.91 15.42     NA    NA
## 3 2010-01-06 42.69 15.66     NA    NA
## 4 2010-01-07 42.58 15.52     NA    NA
## 5 2010-01-08 42.84 15.69     NA    NA
## 6 2010-01-11 42.95 15.85     NA    NA
str(etf4.reorder)
## 'data.frame':    2223 obs. of  5 variables:
##  $ date  : Date, format: "2010-01-04" "2010-01-05" ...
##  $ 0050  : num  41.9 41.9 42.7 42.6 42.8 ...
##  $ 0056  : num  15.5 15.4 15.7 15.5 15.7 ...
##  $ 006205: num  NA NA NA NA NA NA NA NA NA NA ...
##  $ 00646 : num  NA NA NA NA NA NA NA NA NA NA ...
# convert character into numeric 
# convert to xts
#install.packages("xts")
library(xts)
etf4.xts<-xts(etf4.reorder[,-1], order.by = etf4.reorder$date)
head(etf4.xts)
##             0050  0056 006205 00646
## 2010-01-04 41.91 15.49     NA    NA
## 2010-01-05 41.91 15.42     NA    NA
## 2010-01-06 42.69 15.66     NA    NA
## 2010-01-07 42.58 15.52     NA    NA
## 2010-01-08 42.84 15.69     NA    NA
## 2010-01-11 42.95 15.85     NA    NA
tail(etf4.xts)
##             0050  0056 006205 00646
## 2018-12-22 72.60 24.17  25.10 23.15
## 2018-12-24 72.45 24.18  25.30 22.74
## 2018-12-25 71.53 23.97  24.96 22.47
## 2018-12-26 71.34 23.72  25.03 22.19
## 2018-12-27 72.79 23.91  25.17 23.06
## 2018-12-28 73.23 23.94  25.23 23.23
str(etf4.xts)
## An 'xts' object on 2010-01-04/2018-12-28 containing:
##   Data: num [1:2223, 1:4] 41.9 41.9 42.7 42.6 42.8 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:4] "0050" "0056" "006205" "00646"
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
##  NULL
#----------------------------------------------
# Handling missingness in your data 
#----------------------------------------------
# Last obs. carried forward
#etf4.xts$`0050`['2018-12-27']<-NA 
#tail(etf4.xts)
etf4.xts<-na.locf(etf4.xts)                
tail(etf4.xts)
##             0050  0056 006205 00646
## 2018-12-22 72.60 24.17  25.10 23.15
## 2018-12-24 72.45 24.18  25.30 22.74
## 2018-12-25 71.53 23.97  24.96 22.47
## 2018-12-26 71.34 23.72  25.03 22.19
## 2018-12-27 72.79 23.91  25.17 23.06
## 2018-12-28 73.23 23.94  25.23 23.23
# Next obs. carried backward
etf4.xts.fill<-na.locf(etf4.xts, fromLast = TRUE) 
head(etf4.xts.fill)
##             0050  0056 006205 00646
## 2010-01-04 41.91 15.49  20.45 19.61
## 2010-01-05 41.91 15.42  20.45 19.61
## 2010-01-06 42.69 15.66  20.45 19.61
## 2010-01-07 42.58 15.52  20.45 19.61
## 2010-01-08 42.84 15.69  20.45 19.61
## 2010-01-11 42.95 15.85  20.45 19.61
#-------------------------------------------------
# delete NA values
etf4.xts<-na.omit(etf4.xts)
head(etf4.xts)
##             0050  0056 006205 00646
## 2015-12-14 53.29 18.25  31.06 19.61
## 2015-12-15 53.33 18.38  31.59 19.63
## 2015-12-16 54.14 18.56  31.60 19.89
## 2015-12-17 54.77 18.81  32.23 20.05
## 2015-12-18 54.50 18.95  32.18 19.85
## 2015-12-21 54.41 19.02  33.00 19.64
# or complete cases
#install.packages("tidyr")
library(tidyr)
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:reshape2':
## 
##     smiths
## The following object is masked from 'package:magrittr':
## 
##     extract
etf4.xts1<-etf4.xts[complete.cases(etf4.xts),]
head(etf4.xts1)
##             0050  0056 006205 00646
## 2015-12-14 53.29 18.25  31.06 19.61
## 2015-12-15 53.33 18.38  31.59 19.63
## 2015-12-16 54.14 18.56  31.60 19.89
## 2015-12-17 54.77 18.81  32.23 20.05
## 2015-12-18 54.50 18.95  32.18 19.85
## 2015-12-21 54.41 19.02  33.00 19.64
#-----------------------------------------------------------
# export data
#----------------------------------------------------------
write.csv(etf4.xts, file = "myetf4.csv")
# date index disappears!!!
# you have to use write.zoo to save .xts file
write.zoo(etf4.xts, sep = ',', file = "myetf4.csv.1")
# you can save big file using saveRDS()
saveRDS(etf4.xts, file = "etf4.xts.rds")
etf4.xts2 <- readRDS("etf4.xts.rds")
head(etf4.xts2)
##             0050  0056 006205 00646
## 2015-12-14 53.29 18.25  31.06 19.61
## 2015-12-15 53.33 18.38  31.59 19.63
## 2015-12-16 54.14 18.56  31.60 19.89
## 2015-12-17 54.77 18.81  32.23 20.05
## 2015-12-18 54.50 18.95  32.18 19.85
## 2015-12-21 54.41 19.02  33.00 19.64
##
etf4.zoo <- read.zoo("myetf4.csv.1", header = TRUE, index.column =1, 
                     sep = ",", format = "%Y-%m-%d")
head(etf4.zoo)
##            X0050 X0056 X006205 X00646
## 2015-12-14 53.29 18.25   31.06  19.61
## 2015-12-15 53.33 18.38   31.59  19.63
## 2015-12-16 54.14 18.56   31.60  19.89
## 2015-12-17 54.77 18.81   32.23  20.05
## 2015-12-18 54.50 18.95   32.18  19.85
## 2015-12-21 54.41 19.02   33.00  19.64
class(etf4.zoo)
## [1] "zoo"
etf4.xts3<-as.xts(etf4.zoo)
head(etf4.xts3)
##            X0050 X0056 X006205 X00646
## 2015-12-14 53.29 18.25   31.06  19.61
## 2015-12-15 53.33 18.38   31.59  19.63
## 2015-12-16 54.14 18.56   31.60  19.89
## 2015-12-17 54.77 18.81   32.23  20.05
## 2015-12-18 54.50 18.95   32.18  19.85
## 2015-12-21 54.41 19.02   33.00  19.64
#=============================================
# Querying for data
#=============================================
etf4_2016<-etf4.xts['2016']
etf4_2016_01_06 <- etf4.xts["20160101/20160630"]
head(etf4_2016_01_06)
##             0050  0056 006205 00646
## 2016-01-04 53.42 18.54  30.48 19.79
## 2016-01-05 52.97 18.50  29.81 19.74
## 2016-01-06 52.44 18.21  30.14 19.62
## 2016-01-07 51.45 18.38  28.08 19.43
## 2016-01-08 51.54 18.15  29.32 19.32
## 2016-01-11 50.73 17.80  28.13 19.01
#-----------------------------------------
# write function to compute returns
library(magrittr)
simple_returns01<-function(x) {
      coredata(x[-1,])/coredata(x[-length(x),]) - 1 %>% 
      na.omit()
}
head(simple_returns01(AMZN.ad))
##      AMZN.Adjusted
## [1,]   0.005899985
## [2,]  -0.018115688
## [3,]  -0.017013233
## [4,]   0.027076954
## [5,]  -0.024041386
## [6,]  -0.022715064
# or to keep the index of dates
simple_returns02<-function(x) {
  na.omit(x/lag(x) - 1)
}
head(simple_returns02(AMZN.ad))
##            AMZN.Adjusted
## 2010-01-05   0.005899985
## 2010-01-06  -0.018115688
## 2010-01-07  -0.017013233
## 2010-01-08   0.027076954
## 2010-01-11  -0.024041386
## 2010-01-12  -0.022715064
#
#install.packages("PerformanceAnalytics")
library(PerformanceAnalytics)
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
head(na.omit(Return.calculate(firm3)))
##                    AMZN         GOOG          MSFG
## 2010-01-05  0.005899985 -0.004403707  0.0003231258
## 2010-01-06 -0.018115688 -0.025208746 -0.0061373255
## 2010-01-07 -0.017013233 -0.023279489 -0.0103996583
## 2010-01-08  0.027076954  0.013331111  0.0068965292
## 2010-01-11 -0.024041386 -0.001511568 -0.0127199982
## 2010-01-12 -0.022715064 -0.017684003 -0.0066073198
firm3.mon<-firm3 %>% to.monthly(indexAt = "lastof", OHLC=FALSE) %>% 
                 Return.calculate()
head(firm3.mon)
##                    AMZN         GOOG        MSFG
## 2010-01-31           NA           NA          NA
## 2010-02-28 -0.055896673 -0.005925163  0.02214583
## 2010-03-31  0.146706095  0.076537582  0.02162538
## 2010-04-30  0.009795993 -0.073035690  0.04267668
## 2010-05-31 -0.084901579 -0.076222178 -0.15139444
## 2010-06-30 -0.129124798 -0.083767454 -0.10813944
# Main problem: the return series are shown in calendar dates, 
# not in trading dates
# Therefore, we use package PMwR instead!
#install.packages("PMwR")
library(PMwR)
head(returns(firm3))
##                    AMZN         GOOG          MSFG
## 2010-01-05  0.005899985 -0.004403707  0.0003231258
## 2010-01-06 -0.018115688 -0.025208746 -0.0061373255
## 2010-01-07 -0.017013233 -0.023279489 -0.0103996583
## 2010-01-08  0.027076954  0.013331111  0.0068965292
## 2010-01-11 -0.024041386 -0.001511568 -0.0127199982
## 2010-01-12 -0.022715064 -0.017684003 -0.0066073198
firm3.mon1<-returns(firm3, period = "month")
#options(digits = 5)
head(firm3.mon1)
## [1] -0.063405455 -0.055896673  0.146706095  0.009795993 -0.084901579
## [6] -0.129124798
firm3.mon1
##             AMZN   GOOG   MSFG 
## 2010-01-29   -6.3  -15.4   -8.9
## 2010-02-26   -5.6   -0.6    2.2
## 2010-03-31   14.7    7.7    2.2
## 2010-04-30    1.0   -7.3    4.3
## 2010-05-28   -8.5   -7.6  -15.1
## 2010-06-30  -12.9   -8.4  -10.8
## 2010-07-30    7.9    9.0   12.2
## 2010-08-31    5.9   -7.2   -8.6
## 2010-09-30   25.8   16.8    4.3
## 2010-10-29    5.2   16.7    8.9
## 2010-11-30    6.2   -9.4   -4.7
## 2010-12-31    2.6    6.9   10.5
## 2011-01-31   -5.8    1.1   -0.6
## 2011-02-28    2.2    2.2   -3.6
## 2011-03-31    3.9   -4.3   -4.5
## 2011-04-29    8.7   -7.3    2.1
## 2011-05-31    0.4   -2.8   -2.9
## 2011-06-30    4.0   -4.3    4.0
## 2011-07-29    8.8   19.2    5.4
## 2011-08-31   -3.3  -10.4   -2.3
## 2011-09-30    0.5   -4.8   -6.4
## 2011-10-31   -1.3   15.1    7.0
## 2011-11-30   -9.9    1.1   -3.2
## 2011-12-30  -10.0    7.8    1.5
## 2012-01-31   12.3  -10.2   13.8
## 2012-02-29   -7.6    6.6    8.2
## 2012-03-30   12.7    3.7    1.6
## 2012-04-30   14.5   -5.7   -0.7
## 2012-05-31   -8.2   -4.0   -8.2
## 2012-06-29    7.3   -0.1    4.8
## 2012-07-31    2.2    9.1   -3.7
## 2012-08-31    6.4    8.2    5.3
## 2012-09-28    2.4   10.1   -3.4
## 2012-10-31   -8.4   -9.8   -4.1
## 2012-11-30    8.2    2.7   -6.0
## 2012-12-31   -0.5    1.3    0.3
## 2013-01-31    5.8    6.8    2.8
## 2013-02-28   -0.5    6.0    2.1
## 2013-03-28    0.8   -0.9    2.9
## 2013-04-30   -4.8    3.8   15.7
## 2013-05-31    6.1    5.7    6.2
## 2013-06-28    3.2    1.1   -1.0
## 2013-07-31    8.5    0.8   -7.8
## 2013-08-30   -6.7   -4.6    5.6
## 2013-09-30   11.3    3.4   -0.4
## 2013-10-31   16.4   17.7    6.4
## 2013-11-29    8.1    2.8    8.5
## 2013-12-31    1.3    5.8   -1.9
## 2014-01-31  -10.1    5.4    1.1
## 2014-02-28    1.0    2.9    2.0
## 2014-03-31   -7.1   -8.3    7.0
## 2014-04-30   -9.6   -5.4   -1.4
## 2014-05-30    2.8    6.3    2.1
## 2014-06-30    3.9    2.7    1.9
## 2014-07-31   -3.6   -0.6    3.5
## 2014-08-29    8.3    0.0    5.9
## 2014-09-30   -4.9    1.0    2.0
## 2014-10-31   -5.3   -3.2    1.3
## 2014-11-28   10.9   -3.1    2.5
## 2014-12-31   -8.4   -2.8   -2.8
## 2015-01-30   14.2    1.5  -13.0
## 2015-02-27    7.2    4.5    9.3
## 2015-03-31   -2.1   -1.9   -7.3
## 2015-04-30   13.4   -1.4   19.6
## 2015-05-29    1.8   -1.0   -3.0
## 2015-06-30    1.1   -2.2   -5.8
## 2015-07-31   23.5   20.2    5.8
## 2015-08-31   -4.3   -1.2   -6.2
## 2015-09-30   -0.2   -1.6    1.7
## 2015-10-30   22.3   16.8   18.9
## 2015-11-30    6.2    4.5    3.9
## 2015-12-31    1.7    2.2    2.1
## 2016-01-29  -13.2   -2.1   -0.7
## 2016-02-29   -5.9   -6.1   -7.0
## 2016-03-31    7.4    6.8    8.5
## 2016-04-29   11.1   -7.0   -9.7
## 2016-05-31    9.6    6.2    7.0
## 2016-06-30   -1.0   -5.9   -3.5
## 2016-07-29    6.0   11.1   10.8
## 2016-08-31    1.4   -0.2    2.0
## 2016-09-30    8.9    1.3    0.2
## 2016-10-31   -5.7    0.9    4.0
## 2016-11-30   -5.0   -3.4    1.2
## 2016-12-30   -0.1    1.8    3.1
## 2017-01-31    9.8    3.2    4.0
## 2017-02-28    2.6    3.3   -0.4
## 2017-03-31    4.9    0.8    2.9
## 2017-04-28    4.3    9.2    3.9
## 2017-05-31    7.5    6.5    2.6
## 2017-06-30   -2.7   -5.8   -1.3
## 2017-07-31    2.0    2.4    5.5
## 2017-08-31   -0.7    0.9    3.4
## 2017-09-29   -2.0    2.1   -0.4
## 2017-10-31   15.0    6.0   11.7
## 2017-11-30    6.5    0.5    1.7
## 2017-12-29   -0.6    2.4    1.6
## 2018-01-31   24.1   11.8   11.1
## 2018-02-28    4.2   -5.6   -0.8
## 2018-03-29   -4.3   -6.6   -2.7
## 2018-04-30    8.2   -1.4    2.5
## 2018-05-31    4.1    6.7    6.1
## 2018-06-29    4.3    2.8   -0.2
## 2018-07-31    4.6    9.1    7.6
## 2018-08-31   13.2    0.1    6.3
## 2018-09-28   -0.5   -2.0    1.8
## 2018-10-31  -20.2   -9.8   -6.6
## 2018-11-30    5.8    1.6    4.3
## 2018-12-31  -11.1   -5.4   -8.4
## 2019-01-31   14.4    7.8    2.8
## 2019-02-28   -4.6    0.3    7.7
## 2019-03-26    8.8    5.8    5.2
firm3.day<-returns(firm3)
head(firm3.day)
##                    AMZN         GOOG          MSFG
## 2010-01-05  0.005899985 -0.004403707  0.0003231258
## 2010-01-06 -0.018115688 -0.025208746 -0.0061373255
## 2010-01-07 -0.017013233 -0.023279489 -0.0103996583
## 2010-01-08  0.027076954  0.013331111  0.0068965292
## 2010-01-11 -0.024041386 -0.001511568 -0.0127199982
## 2010-01-12 -0.022715064 -0.017684003 -0.0066073198
head(merge(firm3, firm3.day))
##              AMZN     GOOG     MSFG       AMZN.1       GOOG.1
## 2010-01-04 133.90 311.3500 24.61580           NA           NA
## 2010-01-05 134.69 309.9789 24.62375  0.005899985 -0.004403707
## 2010-01-06 132.25 302.1647 24.47263 -0.018115688 -0.025208746
## 2010-01-07 130.00 295.1305 24.21812 -0.017013233 -0.023279489
## 2010-01-08 133.52 299.0649 24.38515  0.027076954  0.013331111
## 2010-01-11 130.31 298.6128 24.07497 -0.024041386 -0.001511568
##                   MSFG.1
## 2010-01-04            NA
## 2010-01-05  0.0003231258
## 2010-01-06 -0.0061373255
## 2010-01-07 -0.0103996583
## 2010-01-08  0.0068965292
## 2010-01-11 -0.0127199982
head(100* cumprod(1+firm3.day))
##                 AMZN     GOOG      MSFG
## 2010-01-05 100.59000 99.55963 100.03231
## 2010-01-06  98.76774 97.04986  99.41838
## 2010-01-07  97.08738 94.79058  98.38446
## 2010-01-08  99.71621 96.05425  99.06298
## 2010-01-11  97.31890 95.90906  97.80289
## 2010-01-12  95.10829 94.21300  97.15668
firm3.p.ret<-firm3.day %>% merge(firm3) 
head(firm3.p.ret)
##                  AMZN..       GOOG..        MSFG.. AMZN.firm3 GOOG.firm3
## 2010-01-04           NA           NA            NA     133.90   311.3500
## 2010-01-05  0.005899985 -0.004403707  0.0003231258     134.69   309.9789
## 2010-01-06 -0.018115688 -0.025208746 -0.0061373255     132.25   302.1647
## 2010-01-07 -0.017013233 -0.023279489 -0.0103996583     130.00   295.1305
## 2010-01-08  0.027076954  0.013331111  0.0068965292     133.52   299.0649
## 2010-01-11 -0.024041386 -0.001511568 -0.0127199982     130.31   298.6128
##            MSFG.firm3
## 2010-01-04   24.61580
## 2010-01-05   24.62375
## 2010-01-06   24.47263
## 2010-01-07   24.21812
## 2010-01-08   24.38515
## 2010-01-11   24.07497
# 
#============================================================
# Plot in R
#-------------------------------------------------------------
# plot zoo object
plot(firm3.day)

# plot xts object
firm3.day %>% as.xts %>% 
              plot
axis(1, index(firm3.day), format(index(firm3.day), "%Y/%m"))

# 
firm3.day.xts <-as.xts(firm3.day)
plot.xts(firm3.day.xts, auto.legend = TRUE)

#axis(side=1, at=yahoo2$date[ at ], labels=format(yahoo2$date[at], '%b-%y'))
#plot.xts(etf4_mon_ret, auto.legend = TRUE)
#-----------------------------------------------------------
#install.packages("tidyverse")
library(tidyverse)
## -- Attaching packages --------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.1.0     v purrr   0.3.2
## v tibble  2.1.1     v stringr 1.4.0
## v ggplot2 3.1.0     v forcats 0.4.0
## -- Conflicts ------------------------------------------------------------------------------------ tidyverse_conflicts() --
## x tidyr::extract()   masks magrittr::extract()
## x dplyr::filter()    masks stats::filter()
## x dplyr::first()     masks xts::first()
## x dplyr::lag()       masks stats::lag()
## x dplyr::last()      masks xts::last()
## x purrr::set_names() masks magrittr::set_names()
library(ggplot2)
# convert xts into data frame which can be used by ggplot
#etf4_returns.df<-fortify(etf4_returns_xts)
firm3_ret.df<-fortify(firm3.day, melt=TRUE)
head(firm3_ret.df)
##        Index Series        Value
## 1 2010-01-05   AMZN  0.005899985
## 2 2010-01-06   AMZN -0.018115688
## 3 2010-01-07   AMZN -0.017013233
## 4 2010-01-08   AMZN  0.027076954
## 5 2010-01-11   AMZN -0.024041386
## 6 2010-01-12   AMZN -0.022715064
#
p<-ggplot(firm3_ret.df, aes(x = Index, y = Value))+
  geom_line(aes(color = Series), size = 0.5)
p

p + scale_x_date(date_labels = "%Y/%m")

# histogram distribution
q<-firm3_ret.df %>%
  ggplot(aes(x =Value, fill = Series)) +
  geom_histogram(alpha = 0.45, binwidth = .005) +
  ggtitle("Daily Returns")
q

q + facet_wrap(~Series)+ theme_update(plot.title = element_text(hjust = 0.5))

# line distribution
firm3_ret.df %>%
  ggplot(aes(x = Value, colour = Series)) +
  geom_density(alpha = 1) +
  ggtitle("Daily Returns Density from 2010") +
  xlab("daily returns") +
  ylab("distribution") +
  theme_update(plot.title = element_text(hjust = 0.5))

# Combine line and histogram together
firm3_ret.df %>%
  ggplot(aes(x = Value)) +
  geom_density(aes(color = Series), alpha = 1) +
  geom_histogram(aes(fill = Series), alpha = 0.45, binwidth = .01) +
  guides(fill = FALSE) +
  facet_wrap(~Series) +
  ggtitle("Daily Returns from 2010") +
  xlab("daily returns") +
  ylab("distribution") +
  theme_update(plot.title = element_text(hjust = 0.5))