The quantmod package is capable of downloading data from a variety of sources. The current supported sources are: yahoo, google, MySQL, FRED, csv, RData, and oanda. For example, FRED (Federal Reserve Economic Data), is a database of 20,070 U.S. economic time series (see http://research.stlouisfed.org/fred2/).
Example: USD/EUR exchange rates from Oanda
For example, we can run the following command to get the data of the USD/EUR exchange rates from Oanda.
1getSymbols(Symbols = 'USD/EUR', src = "oanda")
2
3# On success it will display the following and create USDEUR object
4[1] "USD/EUR"
5
Here we have loaded the data for USD/EUR from the Oanda API which provides free currency data. The getSymbols() method doesn’t return any output. Instead, it creates an internal object in the Global Environment which in this case is the USDEUR object. The data object is an “extensible time series” (xts) object.
To see the starting point of the data, type the following command. It fetches and displays the first 15 rows of the data.
1head(USDEUR,15) # You should see the following result.
2
3 USD.EUR
42019-02-11 0.884834
52019-02-12 0.885164
62019-02-13 0.884514
72019-02-14 0.886430
82019-02-15 0.886331
92019-02-16 0.885238
102019-02-17 0.885267
112019-02-18 0.883885
122019-02-19 0.883780
132019-02-20 0.881384
142019-02-21 0.881816
152019-02-22 0.881970
162019-02-23 0.882340
172019-02-24 0.882317
182019-02-25 0.880924
19
Downloading Multiple Symbols
We can also make a request for multiple symbols. Suppose we want to request data for multiple ETFs, such as SPY, IVV, QQQ and IWF. We will first create a vector containing symbols of these ETFs. Let’s call this vector ‘etfs’. Once we have the vector, we will create two more variables defining the start date and end date for the period for which we want the data. Then we will use the getSymbols() command to actually request the data.
1
2#Lists of ETFs to load
3
4etfs <- c('SPY' # SPDR S&P 500 ETF TRUST
5 ,'IVV',# iShares Core S&P 500 ETF
6 'QQQ', # PowerShares QQQ Trust, Series 1
7 'IWF' ) #iShares Russell 1000 Growth ETF
8
9start_date <- '2014-02-01'
10
11end_date <- '2019-08-06'
12
13getSymbols(Symbols = etfs, src = "yahoo", index.class = "POSIXct", from =start_date, to = end_date)
14
15[1] "SPY" "IVV" "QQQ" "IWF"
16
17
In this example the getSymbols function returns 4 objects that are “SPY”, “IVV”, “QQQ” and “IWF”. These objects are loaded in the Global Environment. Each object should be called separately and returns only its own information.
1head(SPY)
2
3 SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
42014-02-03 177.97 178.37 173.83 174.17 254837100 156.2863
52014-02-04 174.95 175.84 174.11 175.39 165012400 157.3810
62014-02-05 174.78 175.56 173.71 175.17 164230500 157.1836
72014-02-06 175.58 177.48 175.22 177.48 132877600 159.2564
82014-02-07 178.31 179.87 177.73 179.68 170787200 161.2306
92014-02-10 179.70 180.07 179.21 180.01 92218800 161.5267
10
11head(IWF)
12
13 IWF.Open IWF.High IWF.Low IWF.Close IWF.Volume IWF.Adjusted
142014-02-03 83.33 83.57 81.30 81.42 2564900 75.79685
152014-02-04 81.78 82.33 81.53 82.17 2604700 76.49506
162014-02-05 81.86 82.18 81.15 81.92 2871200 76.26232
172014-02-06 82.26 83.07 82.21 83.05 2421900 77.31428
182014-02-07 83.53 84.31 83.28 84.25 2575800 78.43140
192014-02-10 84.39 84.53 84.08 84.48 1538900 78.64553
20
Quantmod provides built-in functions to retrieve individual columns from the above data. In order to take separate columns for one of the above objects, we can use the following commands:
1Open <- Op(IVV) # Get only the Open Price column of IVV ETF
2High <- Hi(IVV) # Get only the High price column of IVV ETF
3Low <- Lo(IVV) # Get only the Low price column of IVV ETF
4Close<- Cl(IVV) # Get only the Close Price column of IVV ETF
5Volume <- Vo(IVV) # Get only the Volume column of IVV ETF
6AdjClose <- Ad(IVV) # Get only the Adjusted close column of IVV ETF
7
Load Data from SQL Database
The getSymbols() function also allows loading data from a SQL database such as MySQL or Sqlite. To load data through MySQL, this function needs additional parameters such as database name, user, password and host. An example of this is described below:
1getSymbols(Symbols = etfs, src = "MySQL", dbname = db, user = user, password = password, host = host, index.class = "POSIXct", from = start_date, to = end_date)
2
Load Data from FRED Database
With getSymbols() we can get data from the FRED database which has thousands of datasets that cover financial, economic and production indexes, interest rates, macroeconomic indexes, monetary and international trade transactions.
In the below example, we are downloading the Fed Fund Rate (shortest interest rate term settled by the Federal Reserve of United States) data:
1getSymbols(Symbols = 'FEDFUNDS', src = "FRED", adjust=TRUE)
2
3tail(FEDFUNDS,15) # tail() gets us the last rows of the dataset
4
5 FEDFUNDS
62018-07-01 1.91
72018-08-01 1.91
82018-09-01 1.95
92018-10-01 2.19
102018-11-01 2.20
112018-12-01 2.27
122019-01-01 2.40
132019-02-01 2.40
142019-03-01 2.41
152019-04-01 2.42
162019-05-01 2.39
172019-06-01 2.38
182019-07-01 2.40
19