sub-.stock: Using '[' on Objects of Class "stock"

[.stockR Documentation

Using [ on Objects of Class "stock"

Description

You can quickly retrieve price, dividend, and split data useing the [ operator on an object of class stock, much like subsetting a matrix or list in base R.

Usage

## S3 method for class 'stock'
x[i, j = NULL, silent = FALSE]

Arguments

x

an object of class "stock".

i

an xts subsetting string following the same rules as for an xts object: "2015" will return data for all of 2015, "2018-04" will return data for March of 2018 only, "2016-09-19/2016-12-31" will return data from 19 Sep 2016 to the end of the year, etc. If NULL, then the full time range of available data for the selected stock will be returned.

j

Character vector specifying column names to be included in the output. Available column names are:

"DividendAmount", "RecordDate", "DeclaredDate", "Type", "PaymentFrequency", "Open", "High", "Low", "Close", "TWAP", "VWAP", "Volume", "PreviousCloseDate", "PreviousClose", "Denominator", "Numerator", "ExDate", "company", "acquired_by", "multiple", "type", "acquired"

silent

Boolean. If FALSE (Default), messages will be displayed for special cases that might produce unexpected behavior; e.g., if no data is available for the date range specified. If TRUE, these messages will not be displayed.

Details

The rules are:

  1. If a dividend took place during the specified date range, then the column "DividendAmount" will always be included in the output.

  2. If a split took place during the specified date range, then the columns "Denominator" and "Numerator" will always be included in the output. "Denominator" refers to the number of shares AFTER the split and "Numerator" number of shares BEFORE the split; for example, a 2-to-1 split would have "Denominator" = 2 and "Numerator" = 1.

Value

an xts object, or NULL if no data is available for the specified asset within the specified date range.

Examples


# If no columns are specified, all available columns are returned.
# Single date:
stock_data$IVV["2019-06-11"]

# Dividends are correctly applied, even when issued on a non-trading day. In
# 2012, for example, Texas Instruments (TXN) had scheduled a dividend for the
# 29th of October but the exchange was closed due to Hurricane Sandy. The
# dividend, then, should be applied to the next trading day (31 October). Here,
# we'll select just the closing price for a date range around that time.
# Remember, any dividends that occur within the date range are always included.
stock_data$TXN["2012-10-25/2012-11-05", "Close"]

# Splits are handled as follows for Apple's 7-to-1 split of 2014:
stock_data$AAPL["2014-06-05/2014-06-11", c("Open", "Close")]

# Not specifying which columns you want can quickly lead to a lot of data, as in
# the below subset of IVV for the month of June 2019:
stock_data$IVV["2019-06"]

###### M&A Events: Mergers, Acquisitions, Spinoffs, Breakups, etc.
# Consider the M&A event that took place on 31 Oct 2018 in which Praxair (PX)
# merged with Linde AG (not included in dataset) to form Linde PLC, a new
# company traded under the symbol LIN.

gothic-hedge-society/FinancieR documentation built on June 18, 2022, 4:55 a.m.