Quantmod enables 3 major types of column extraction functions. If you’re looking for the open, the high, or the series low - it’s available:
Op, Hi,Lo, Cl,Vo, Ad - extract the columns Open, High, Low, Close, Volume, and Adjusted (Yahoo)
is.OHLC, has.OHLC, has.Op, has.Cl, has.Hi, has.Lo, has.Ad, and has.Vo - checks the desired parameters
seriesHi and seriesLo - the highest and the lowest point in the series
Charting with Quantmod
Quantmod also provides a tool to visualize financial time series - the function chartSeries - with which we can create line charts, as well as OHLC bar and candle charts.
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
Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
Examples
Getting Apple data (AAPL)
We want to collect information about Apple. We only need to know their ticker, which is ‘AAPL’ on the ‘NASDAQ’.
We can get the required data with the getSymbols function:
AAPL[‘2024’] #returns all Apple’s 2024 OHLC
AAPL[‘2024-01’] #now just January of 2024
AAPL[’2024-06::2024-08-15 #Jun of 24through Aug 15 of 24
AAPL[‘::’] # everything in AAPL
AAPL[‘2022::’] # everything in AAPL, from 2022 onward
non.contiguous <- c(‘2007-01’,‘2007-02’,‘2007-12’)
AAPL[non.contiguous] # different dates
We can add technical analysis using tools from TTR package to the above chart. The library was developed by Josh Ulrich - details are available on CRAN
chartSeries(apple_df, subset="last 3 months")
addMACD()
addBBands()
Gold prices
We use data from Oanda - please note that Oanda only provides historical data for the past 180 days.
The dates in the data frame are not imported correctly with this method - they are turned into ascending numbers (1,2,3,…). This is because quantmod::getSymbols() returns an xts object by default.
xts objects are based on zoo objects, which are a matrix with an ‘index’ attribute. The date we see when xts/zoo objects are printed in R is the index attribute, not row names (or record names).
The easiest way to fix it is ti write xts/zoo objects to a text file is with write.zoo(). It will automatically include the index in the first column of the file.
write.zoo(sp500_df, "sp500zoo.csv", sep =",")
Descriptive statistics
The function summary() calculates the summary statistics for the financial data:
summary(apple_df)
Index AAPL.Open AAPL.High AAPL.Low
Min. :2007-01-03 Min. : 2.835 Min. : 2.929 Min. : 2.793
1st Qu.:2011-06-13 1st Qu.: 12.624 1st Qu.: 12.682 1st Qu.: 12.481
Median :2015-11-23 Median : 28.006 Median : 28.260 Median : 27.668
Mean :2015-11-25 Mean : 56.915 Mean : 57.520 Mean : 56.334
3rd Qu.:2020-05-07 3rd Qu.: 79.657 3rd Qu.: 80.438 3rd Qu.: 79.119
Max. :2024-10-18 Max. :236.480 Max. :237.490 Max. :234.010
AAPL.Close AAPL.Volume AAPL.Adjusted
Min. : 2.793 Min. :2.405e+07 Min. : 2.358
1st Qu.: 12.585 1st Qu.:9.513e+07 1st Qu.: 10.628
Median : 28.017 Median :1.878e+08 Median : 25.457
Mean : 56.953 Mean :3.441e+08 Mean : 55.060
3rd Qu.: 79.729 3rd Qu.:4.642e+08 3rd Qu.: 77.592
Max. :235.000 Max. :3.373e+09 Max. :235.000