Downloading Data Using Quantmod Package in R
Once the quantmod package is installed and library is loaded, we can start using the library. We will start by showing some examples of how to download data from the web and load the data into the environment.
Quantmod provides a very powerful function for downloading financial data from the web. This function is called getSymbols. The getSymbols() method sends a request to download and manage data from public sources or local data. It is necessary to pass some parameters within this method to make the desired request. The first argument of this function is a character vector specifying the names of the symbols to be downloaded. Then you can specify the source from which you want to get the data.
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.
getSymbols(Symbols = 'USD/EUR', src = "oanda") # On success it will display the following and create USDEUR object  "USD/EUR"
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.
head(USDEUR,15) # You should see the following result. USD.EUR 2019-02-11 0.884834 2019-02-12 0.885164 2019-02-13 0.884514 2019-02-14 0.886430 2019-02-15 0.886331 2019-02-16 0.885238 2019-02-17 0.885267 2019-02-18 0.883885 2019-02-19 0.883780 2019-02-20 0.881384 2019-02-21 0.881816 2019-02-22 0.881970 2019-02-23 0.882340 2019-02-24 0.882317 2019-02-25 0.880924
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.
#Lists of ETFs to load etfs <- c('SPY' # SPDR S&P 500 ETF TRUST ,'IVV',# iShares Core S&P 500 ETF 'QQQ', # PowerShares QQQ Trust, Series 1 'IWF' ) #iShares Russell 1000 Growth ETF start_date <- '2014-02-01' end_date <- '2019-08-06' getSymbols(Symbols = etfs, src = "yahoo", index.class = "POSIXct", from =start_date, to = end_date)  "SPY" "IVV" "QQQ" "IWF"
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.
head(SPY) SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted 2014-02-03 177.97 178.37 173.83 174.17 254837100 156.2863 2014-02-04 174.95 175.84 174.11 175.39 165012400 157.3810 2014-02-05 174.78 175.56 173.71 175.17 164230500 157.1836 2014-02-06 175.58 177.48 175.22 177.48 132877600 159.2564 2014-02-07 178.31 179.87 177.73 179.68 170787200 161.2306 2014-02-10 179.70 180.07 179.21 180.01 92218800 161.5267 head(IWF) IWF.Open IWF.High IWF.Low IWF.Close IWF.Volume IWF.Adjusted 2014-02-03 83.33 83.57 81.30 81.42 2564900 75.79685 2014-02-04 81.78 82.33 81.53 82.17 2604700 76.49506 2014-02-05 81.86 82.18 81.15 81.92 2871200 76.26232 2014-02-06 82.26 83.07 82.21 83.05 2421900 77.31428 2014-02-07 83.53 84.31 83.28 84.25 2575800 78.43140 2014-02-10 84.39 84.53 84.08 84.48 1538900 78.64553
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:
Open <- Op(IVV) # Get only the Open Price column of IVV ETF High <- Hi(IVV) # Get only the High price column of IVV ETF Low <- Lo(IVV) # Get only the Low price column of IVV ETF Close<- Cl(IVV) # Get only the Close Price column of IVV ETF Volume <- Vo(IVV) # Get only the Volume column of IVV ETF AdjClose <- Ad(IVV) # Get only the Adjusted close column of IVV ETF
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:
getSymbols(Symbols = etfs, src = "MySQL", dbname = db, user = user, password = password, host = host, index.class = "POSIXct", from = start_date, to = end_date)
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:
getSymbols(Symbols = 'FEDFUNDS', src = "FRED", adjust=TRUE) tail(FEDFUNDS,15) # tail() gets us the last rows of the dataset FEDFUNDS 2018-07-01 1.91 2018-08-01 1.91 2018-09-01 1.95 2018-10-01 2.19 2018-11-01 2.20 2018-12-01 2.27 2019-01-01 2.40 2019-02-01 2.40 2019-03-01 2.41 2019-04-01 2.42 2019-05-01 2.39 2019-06-01 2.38 2019-07-01 2.40
- Introduction to Quantitative Trading
- Quantitative Trading - Advantages and Disadvantages
- Types of Quantitative Trading Strategies
- Momentum Strategies
- Mean Reversion Strategies
- Market Making Strategies and Day Trading Strategies
- How to Generate Trading Ideas
- Designing A Trading Strategy For Profit
- Backtesting a Trading Strategy - Considerations
- Risk Management of a Trading Strategy
- Risk Indicators - VIX Index and TED Spread
- Plotting the VIX Index and TED Spread in R
- Introduction to Quantmod in R
- Downloading Data Using Quantmod Package in R
- Creating Charts with Quantmod
- Data Analysis with Quantmod in R
- Measuring Overall ETFs Performance
- Quantstrat Example in R - EMA Crossover Strategy
- Quantstrat - EMA Crossover Strategy - Performance and Risk Metrics
- Quantstrat Example in R - RSI Strategy
- Quantstrat Case Study - Multiple Symbol Portfolio