currency
library(Quandl)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(quantmod)
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
# Create a currency_pair object
currency_pair <- "USD/EUR"
# Load British Pound to Canadian Dollar exchange rate data
getSymbols(currency_pair, src = "oanda")
## '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 in as.POSIXlt.POSIXct(Sys.time()): unknown timezone 'zone/tz/2018e.
## 1.0/zoneinfo/America/New_York'
## [1] "USDEUR"
# Examine object using str()
str(USDEUR)
## An 'xts' object on 2018-04-10/2018-10-05 containing:
## Data: num [1:179, 1] 0.811 0.808 0.81 0.811 0.811 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr "USD.EUR"
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## List of 2
## $ src : chr "oanda"
## $ updated: POSIXct[1:1], format: "2018-10-06 02:35:47"
# Try to load data from 190 days ago
getSymbols(currency_pair, from = Sys.Date() - 30, to = Sys.Date(), src = "oanda")
## [1] "USDEUR"
Stocks
# Create an object containing the Pfizer ticker symbol
symbol <- "PYPL"
# Use getSymbols to import the data
getSymbols(symbol, src = "yahoo")
##
## 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] "PYPL"
# Look at the first few rows of data
tail(PYPL)
## PYPL.Open PYPL.High PYPL.Low PYPL.Close PYPL.Volume
## 2018-09-28 88.79 89.045 87.60 87.84 7920600
## 2018-10-01 88.13 88.800 87.40 87.55 9260600
## 2018-10-02 87.27 87.350 85.83 86.68 6488200
## 2018-10-03 87.40 87.950 86.37 87.15 6625000
## 2018-10-04 86.39 86.500 83.54 84.34 9077900
## 2018-10-05 84.21 84.840 82.32 83.18 10734800
## PYPL.Adjusted
## 2018-09-28 87.84
## 2018-10-01 87.55
## 2018-10-02 86.68
## 2018-10-03 87.15
## 2018-10-04 84.34
## 2018-10-05 83.18
Extract certain column
# Look at the head of PYPL
head(PYPL)
## PYPL.Open PYPL.High PYPL.Low PYPL.Close PYPL.Volume
## 2015-07-06 38.00 39.75 36.00 36.71 5866600
## 2015-07-07 37.72 37.81 36.00 36.62 7359000
## 2015-07-08 36.34 36.36 34.53 34.70 5387700
## 2015-07-09 35.10 35.52 33.99 34.50 3760100
## 2015-07-10 34.66 35.19 33.98 34.69 4472800
## 2015-07-13 35.59 37.50 35.50 36.78 7626000
## PYPL.Adjusted
## 2015-07-06 36.71
## 2015-07-07 36.62
## 2015-07-08 34.70
## 2015-07-09 34.50
## 2015-07-10 34.69
## 2015-07-13 36.78
# Extract the close column
py_close <- Cl(PYPL)
# Look at the head of dc_close
head(py_close)
## PYPL.Close
## 2015-07-06 36.71
## 2015-07-07 36.62
## 2015-07-08 34.70
## 2015-07-09 34.50
## 2015-07-10 34.69
## 2015-07-13 36.78
# Extract the volume column
py_volume <- Vo(PYPL)
# Look at the head of dc_volume
head(py_volume)
## PYPL.Volume
## 2015-07-06 5866600
## 2015-07-07 7359000
## 2015-07-08 5387700
## 2015-07-09 3760100
## 2015-07-10 4472800
## 2015-07-13 7626000
Use quantmod to download multiple instruments and extract the close column
# Set the default to pull data from Google Finance
setDefaults(getSymbols, src = "yahoo")
# Get GOOG data
getSymbols("GOOG")
## [1] "GOOG"
# Verify the data was actually pulled from Google Finance
str(GOOG)
## An 'xts' object on 2007-01-03/2018-10-05 containing:
## Data: num [1:2962, 1:6] 231 233 240 242 241 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:6] "GOOG.Open" "GOOG.High" "GOOG.Low" "GOOG.Close" ...
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## List of 2
## $ src : chr "yahoo"
## $ updated: POSIXct[1:1], format: "2018-10-06 02:35:49"
# Symbols
symbols <- c("PYPL", "ETSY", "INTU","SPHD", "HACK" )
# Create new environment
data_env <- new.env()
# Load symbols into data_env
getSymbols(symbols, env = data_env)
## [1] "PYPL" "ETSY" "INTU" "SPHD" "HACK"
# Extract the close column from each object and combine into one xts object
close_data <- do.call(merge, eapply(data_env, Cl))
# View the head of close_data
head(close_data)
## ETSY.Close SPHD.Close INTU.Close HACK.Close PYPL.Close
## 2007-01-03 NA NA 29.49 NA NA
## 2007-01-04 NA NA 29.90 NA NA
## 2007-01-05 NA NA 29.83 NA NA
## 2007-01-08 NA NA 28.83 NA NA
## 2007-01-09 NA NA 28.93 NA NA
## 2007-01-10 NA NA 29.39 NA NA