market_data: Get Market Data

View source: R/Market_Data.R

market_dataR Documentation

Get Market Data

Description

The bars API provides time-aggregated price and volume data for a single stock or multiple. The v2 (Polygon) API is only available for live accounts and accepts the from, to, timeframe, multiplier, and unadjusted arguments.

Usage

market_data(
  symbol,
  v = 2,
  timeframe = "day",
  multiplier = 1,
  from = NULL,
  to = NULL,
  after = NULL,
  until = NULL,
  limit = NULL,
  full = FALSE,
  unadjusted = FALSE
)

Arguments

symbol

(character) The stock or stocks (in vector format) for which data will be retrieved. Non case-sensitive.

v

(integer) The API version number.

timeframe

(character) For the v1 API, one of

  • 'tr'/'lt'/'trade'/'last_trade' For the last trade price. See Last Trade

  • 'qu'/'lq'/'quote'/'last_quote' For the last quote price. See Last Quote

  • 'm'/'min'`/`'minute' (multiplier can be 1/5/15)

  • 'd'/'day' (multiplier will be 1)

For the v2 IEX/SIP API the following timeframes are supported:

For the "p"/"polygon" API, multiplier can be any positive integer for any one of the following timeframe's:

  • 'm'/'min'/'minute'

  • 'h'/'hour'

  • 'd'/'day'

  • 'w'/'week'

  • 'M'/'mo'/'month' (Note capitalize M for month)

  • 'q'/'quarter'

  • 'y'/'year'

multiplier

For the v1 API, with 'minute' timeframe one of 1/5/15. Otherwise, defaults to 1. For the v2 API, multiplier can only be 1. For the "polygon" API, this can be any positive integer. Default 1.

from

(equivalent to start in v1) (Date/POSIXlt/Datetime(POSIXct)/character) See Details for formatting guidelines. Return data equal to or after this time. Default is 7 days ago.

to

(equivalent to end in v1) (Date/POSIXlt/Datetime(POSIXct)/character) See Details for formatting guidelines. Return data equal to or before this time. Default is today's date.

after

v1 only (Date/POSIXlt/Datetime(POSIXct)/character) See Details for formatting guidelines. Return data after this time. Default is 7 days ago. Cannot be used with to

until

v1 only (Date/POSIXlt/Datetime(POSIXct)/character) See Details for formatting guidelines. Return data before this time. Default is today's date. Cannot be used with from

limit

v1 & v2 (integer) The amount of bars to return per symbol. This can range from 1 to 1000 for v1 and 1 to 10000 for v2. Default 1000 (v1), 10000 (v2), 50000 (p).

full

(logical) If TRUE, the function will attempt to return the entire expected dataset based on the range of dates provided and perform a data completeness check. If the requested from, to dates/times exceed that which can be returned in a single call, the API will be called repeatedly to return the full dataset. If FALSE, the request will be submitted to the API as is. Note on rate limits: The v1 API has a call limit of 1000 bars and a rate limit of 200 requests per minute. If the rate limit is reached, queries will pause for 1 minute. The polygon API free tier has a call limit of 5 requests per minute, if the rate limit is reached, queries will pause for 1 minute. Default FALSE.

unadjusted

polygon only (logical) Set to TRUE if the results should NOT be adjusted for splits. Default FALSE.

adjustment

Deprecated (character) Specifies the corporate action adjustment for the stocks. Possible values: 'raw', 'split', 'dividend' or 'all'. Default 'raw'.

Details

All values to from/after/to/until will parse correctly if a numeric year, in YYYY-MM-DD RFC 3339 format or (Datetime/POSIXct), ⁠YYYY-MM-DD HH:MM⁠ ISO8601 format. Other formats will often work, but are not guaranteed to parse correctly. All Dates/Datetimes are forced to America/New York timezone (See force_tz) in which the NYSE operates. This means that if now is used to specify 3PM in the local timezone, it will be forced 3PM in the "America/New_York timezone. This eliminates needing to consistently account for timezone conversions when providing inputs. The polygon API only accepts Dates in YYYY-MM-DD format, any arguments passed to start or end will be coerced to Date automatically if using polygon. For the polygon API, queries with timeframe: 'year' use 12/31 as an aggregate date for each year. Arguments passed to from & to will be coerced to their yearly round_date and ceiling_date respectively. For a full overview of how the Polygon.io Aggregates endpoint behaves, check out https://polygon.io/blog/aggs-api-updates/.

Value

tsymble object if just one symbol is requested, otherwise a ⁠named list⁠ of tsymble objects for each ticker symbol. A tsymble is a tsibble object with the following columns:

  • time the time of the bar as POSIXct in yyyy-mm-dd for timeframe = day, and yyyy-mm-dd hh:mm:ss for timeframes < day

  • open (numeric) open price

  • high (numeric) high price

  • low (numeric) low price

  • close (numeric) close price

  • volume (numeric) volume (in millions)

  • vw *polygon only* (numeric) the volume weighted average price

  • n *polygon only* (numeric) Number of items in aggregate window

Each tsymble has a "query" attribute with the "query" data as a list for each of the calls required to return it and a "symbol" attribute with the ticker symbol for the data. These attributes are easily accessed via get_query and get_sym respectively.

Examples

## Not run: 
# Getting data for the last week from the v1 API:
market_data(symbol = c("INTC","MSFT"), v = 1, from = Sys.Date() - lubridate::weeks(1))
# Getting price data with specific date ranges and timeframes with limit on the number of bars returned
market_data(symbol = c("INTC","MSFT"), from = "2019-03-20", to = "2019-04-01", multiplier = 15, timeframe = "min", limit = 175, v = 1)
# Get the last trade for a symbol (or multiple):
market_data("TWTR", timeframe = "lt")
# or the last quote for multiple symbols:
market_data(c("TWTR","AAPL","BYND"), timeframe = "lq")

## End(Not run)

jagg19/AlpacaforR documentation built on July 3, 2023, 12:14 p.m.