Extracting columns from financial time series

Importing and Managing Financial Data in R

Joshua Ulrich

Quantitative Analyst & quantmod Co-Author and Maintainer

OHLC

  • Stands for "Open High Low Close"
  • Open and Close: first and last observed prices
  • High and Low: largest and smallest observed prices
  • Often Volume: sum of all contracts traded
Importing and Managing Financial Data in R

OHLC data

head(DC)
                    DC.Open DC.High DC.Low DC.Close DC.Volume
2016-01-16 01:00:00  20.845  20.850 20.835   20.845       157
2016-01-16 02:00:00  20.845  20.850 20.835   20.845       214
2016-01-16 03:00:00  20.845  20.850 20.835   20.845       103
2016-01-16 04:00:00  20.845  20.855 20.835   20.845       180
2016-01-16 05:00:00  20.845  20.845 20.845   20.845       211
2016-01-16 06:00:00  20.845  20.845 20.840   20.845        35
Importing and Managing Financial Data in R

Single-column extractor functions

  • Op() - opening price
  • Hi() - high price
  • Lo() - low price
  • Cl() - close price
  • Vo() - traded volume
  • Ad() - adjusted close price
Importing and Managing Financial Data in R

Single-column extractor functions

# Open price
dc_open <- Op(DC)
head(dc_open, 4)
                     DC.Open
 2016-01-16 01:00:00   20.84
 2016-01-16 02:00:00   20.85
 2016-01-16 03:00:00   20.85
 2016-01-16 04:00:00   20.85
# High price
dc_high <- Hi(DC)
head(dc_high, 4)
                    DC.High
2016-01-16 01:00:00   20.85
2016-01-16 02:00:00   20.85
2016-01-16 03:00:00   20.85
2016-01-16 04:00:00   20.85
Importing and Managing Financial Data in R

Multi-column extractor functions

# Extract multiple columns
dc_ohlc <- OHLC(DC)
head(dc_ohlc)
                    DC.Open DC.High DC.Low DC.Close
2016-01-16 01:00:00   20.84   20.85  20.83    20.84
2016-01-16 02:00:00   20.85   20.85  20.83    20.85
2016-01-16 03:00:00   20.85   20.85  20.84    20.85
2016-01-16 04:00:00   20.85   20.85  20.84    20.85
2016-01-16 05:00:00   20.85   20.85  20.84    20.85
2016-01-16 06:00:00   20.84   20.85  20.84    20.85
Importing and Managing Financial Data in R

getPrice()

  • 3 arguments
    • x: object that contains data
    • symbol: optional symbol if x contains multiple symbols
    • prefer: optional preferred price
  • If prefer not specified:
    • "price", then "trade", then "close"
Importing and Managing Financial Data in R
head(DC)
                     Price Volume Bid.Price Bid.Size Ask.Price Ask.Size
 2016-01-16 00:00:07    NA     NA     20.84      198     20.85      684
 2016-01-16 00:00:08    NA     NA     20.84      198     20.85      683
 2016-01-16 00:00:08    NA     NA     20.84      198     20.85      682
 2016-01-16 00:00:11    NA     NA     20.84      198     20.85      683
 2016-01-16 00:00:25    NA     NA     20.84      198     20.85      684
 2016-01-16 00:00:44 20.84      1     20.84      198     20.85      684
dc_bid <- getPrice(DC, prefer = "bid")
head(dc_bid)
                     Bid.Price
 2016-01-16 00:00:07     20.84
 2016-01-16 00:00:08     20.84
 2016-01-16 00:00:08     20.84
 2016-01-16 00:00:11     20.84
 2016-01-16 00:00:25     20.84
 2016-01-16 00:00:44     20.84
Importing and Managing Financial Data in R

Let's practice!

Importing and Managing Financial Data in R

Preparing Video For Download...