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"
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-20 containing:
## Data: num [1:2318, 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-21 18:42:17"
class(AMZN)
## [1] "xts" "zoo"
tail(AMZN,3)
## AMZN.Open AMZN.High AMZN.Low AMZN.Close AMZN.Volume
## 2019-03-18 1712.70 1750.00 1712.63 1742.15 5429100
## 2019-03-19 1753.51 1784.16 1753.51 1761.85 6364200
## 2019-03-20 1769.94 1799.50 1767.03 1797.27 6248300
## AMZN.Adjusted
## 2019-03-18 1742.15
## 2019-03-19 1761.85
## 2019-03-20 1797.27
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