getpoly: Download an _OHLC_ time series of prices from Polygon.

View source: R/rutils.R

getpolyR Documentation

Download an OHLC time series of prices from Polygon.

Description

Download an OHLC time series of prices from Polygon.

Usage

getpoly(
  symbol = "SPY",
  startd = as.Date("1997-01-01"),
  endd = Sys.Date(),
  tspan = "day",
  apikey
)

Arguments

symbol

The stock symbol (ticker).

startd

The start date (default is "1997-01-01").

endd

The end date (default is Sys.Date()).

tspan

The data frequency, i.e. the time span for data aggregations (default is "day" for daily data).

apikey

The API key issued by Polygon.

Details

The function getpoly() downloads historical prices from Polygon, and returns an OHLC time series of class xts.

Polygon is a provider of live and historical prices for stocks, options, foreign currencies, and cryptocurrencies.

The function getpoly() sends a request for data to the Polygon rest API, using the function read_json() from package jsonlite. The query requires an API key issued by Polygon. The API key must be passed to the argument apikey.

Polygon returns data in JSON format, which is then formatted into an OHLC time series of class xts.

The argument tspan determines the data frequency, i.e. it's the time span for data aggregations. The default is "day" for daily data. Other possible values of tspan are "minute", "hour", "week", and "month".

Value

An OHLC time series of class xts.

Examples

## Not run: 
# Polygon API key - user must obtain their own key
apikey <- "0Q2f7j8CwAbdY5M8VYt_8pwdP0V4TunxbvRVC_"
# Download SPY prices from Polygon
ohlc <- rutils::getpoly(symbol="SPY", apikey=apikey)
# Plot candlesticks of SPY OHLC prices
library(highcharter)
highcharter::highchart(type="stock") %>% hc_add_series(ohlc, type="candlestick")

## End(Not run)

algoquant/rutils documentation built on April 18, 2024, 12:05 a.m.