#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.
##
## WARNING: There have been significant changes to Yahoo Finance data.
## Please see the Warning section of '?getSymbols.yahoo' for details.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.yahoo.warning"=FALSE).
## [1] "GOOG"
head(GOOG)
## GOOG.Open GOOG.High GOOG.Low GOOG.Close GOOG.Volume
## 2007-01-03 231.4944 236.7899 229.0652 232.2842 15513200
## 2007-01-04 232.9847 240.4114 232.6618 240.0686 15877700
## 2007-01-05 239.6910 242.1749 237.5102 242.0209 13833500
## 2007-01-08 242.2693 243.3522 239.5420 240.2276 9570600
## 2007-01-09 241.1565 242.5475 239.0452 241.1814 10832700
## 2007-01-10 240.6498 245.1803 239.4625 243.1486 12014600
## GOOG.Adjusted
## 2007-01-03 232.2842
## 2007-01-04 240.0686
## 2007-01-05 242.0209
## 2007-01-08 240.2276
## 2007-01-09 241.1814
## 2007-01-10 243.1486
tickers = c("GOOG", "MSFT", "AMZN")
getSymbols(tickers, from = "2010-01-01", auto.assign = TRUE)
## [1] "GOOG" "MSFT" "AMZN"
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-19 containing:
## Data: num [1:2317, 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-20 19:23:16"
class(AMZN)
## [1] "xts" "zoo"
tail(AMZN,3)
## AMZN.Open AMZN.High AMZN.Low AMZN.Close AMZN.Volume
## 2019-03-15 1703.00 1718.80 1693.13 1712.36 7550900
## 2019-03-18 1712.70 1750.00 1712.63 1742.15 5429100
## 2019-03-19 1753.51 1784.16 1753.51 1761.85 6344700
## AMZN.Adjusted
## 2019-03-15 1712.36
## 2019-03-18 1742.15
## 2019-03-19 1761.85
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']
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"
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
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 ...
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
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
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