diff --git a/polygon/rest/__init__.py b/polygon/rest/__init__.py index a101edf1..31f100f1 100644 --- a/polygon/rest/__init__.py +++ b/polygon/rest/__init__.py @@ -1,7 +1,14 @@ from .aggs import AggsClient from .trades import TradesClient from .quotes import QuotesClient -from .reference import MarketsClient, TickersClient, SplitsClient, DividendsClient +from .reference import ( + MarketsClient, + TickersClient, + SplitsClient, + DividendsClient, + ConditionsClient, + ExchangesClient, +) class RESTClient( @@ -12,5 +19,7 @@ class RESTClient( TickersClient, SplitsClient, DividendsClient, + ConditionsClient, + ExchangesClient, ): pass diff --git a/polygon/rest/models/__init__.py b/polygon/rest/models/__init__.py index a6a9c916..85139477 100644 --- a/polygon/rest/models/__init__.py +++ b/polygon/rest/models/__init__.py @@ -5,15 +5,6 @@ from .tickers import * from .splits import * from .dividends import * - -from enum import Enum - - -class Sort(Enum): - ASC = "asc" - DESC = "desc" - - -class Order(Enum): - ASC = "asc" - DESC = "desc" +from .conditions import * +from .exchanges import * +from .shared import * diff --git a/polygon/rest/models/conditions.py b/polygon/rest/models/conditions.py new file mode 100644 index 00000000..52b1e6aa --- /dev/null +++ b/polygon/rest/models/conditions.py @@ -0,0 +1,52 @@ +from typing import Optional +from .shared import AssetClass, DataType +from dataclasses import dataclass + + +@dataclass +class SipMapping: + CTA: Optional[str] = None + OPRA: Optional[str] = None + UTP: Optional[str] = None + + +@dataclass +class Consolidated: + updates_high_low: Optional[bool] = None + updates_open_close: Optional[bool] = None + updates_volume: Optional[bool] = None + + +@dataclass +class MarketCenter: + updates_high_low: Optional[bool] = None + updates_open_close: Optional[bool] = None + updates_volume: Optional[bool] = None + + +@dataclass +class UpdateRules: + consolidated: Optional[Consolidated] = None + market_center: Optional[MarketCenter] = None + + +@dataclass +class Condition: + "Condition contains data for a condition that Polygon.io uses." + abbreviation: Optional[str] = None + asset_class: Optional[AssetClass] = None + data_types: Optional[DataType] = None + description: Optional[str] = None + exchange: Optional[int] = None + id: Optional[int] = None + legacy: Optional[bool] = None + name: Optional[str] = None + sip_mapping: Optional[SipMapping] = None + Type: Optional[ + str + ] = None # todo: 'type' is a keyword so here I capitalized. Should we capital case all dataclasses? + update_rules: Optional[UpdateRules] = None + + @staticmethod + def from_dict(d): + return Condition(**d) diff --git a/polygon/rest/models/dividends.py b/polygon/rest/models/dividends.py index b691812a..f70adf9f 100644 --- a/polygon/rest/models/dividends.py +++ b/polygon/rest/models/dividends.py @@ -1,23 +1,8 @@ from typing import Optional -from enum import Enum +from .shared import DividendType, Frequency from dataclasses import dataclass -class DividendType(Enum): - CD = "CD" - SC = "SC" - LT = "LT" - ST = "ST" - - -class Frequency(Enum): - OneTime = 0 - Anually = 1 - BiAnually = 2 - Quarterly = 4 - Monthly = 12 - - @dataclass class Dividend: "Dividend contains data for a historical cash dividend, including the ticker symbol, declaration date, ex-dividend date, record date, pay date, frequency, and amount." diff --git a/polygon/rest/models/exchanges.py b/polygon/rest/models/exchanges.py new file mode 100644 index 00000000..64527710 --- /dev/null +++ b/polygon/rest/models/exchanges.py @@ -0,0 +1,22 @@ +from typing import Optional +from .shared import AssetClass, Locale, ExchangeType +from dataclasses import dataclass + + +@dataclass +class Exchange: + "Exchange contains data for a condition that Polygon.io uses." + acronym: Optional[str] = None + asset_class: Optional[AssetClass] = None + id: Optional[int] = None + locale: Optional[Locale] = None + mic: Optional[str] = None + name: Optional[str] = None + operating_mic: Optional[str] = None + participant_id: Optional[str] = None + type: Optional[ExchangeType] = None + url: Optional[str] = None + + @staticmethod + def from_dict(d): + return Exchange(**d) diff --git a/polygon/rest/models/markets.py b/polygon/rest/models/markets.py index b2b3078f..efa5b2ff 100644 --- a/polygon/rest/models/markets.py +++ b/polygon/rest/models/markets.py @@ -1,7 +1,20 @@ -from typing import Optional, Dict +from typing import Optional from dataclasses import dataclass +@dataclass +class Currencies: + crypto: Optional[str] = None + fx: Optional[str] = None + + +@dataclass +class Exchanges: + nasdaq: Optional[str] = None + nyse: Optional[str] = None + otc: Optional[str] = None + + @dataclass class MarketHoliday: "MarketHoliday contains data for upcoming market holidays and their open/close times." @@ -21,9 +34,9 @@ def from_dict(d): class MarketStatus: "MarketStatus contains data for the current trading status of the exchanges and overall financial markets." after_hours: Optional[bool] = None - currencies: Optional[Dict[str, str]] = None + currencies: Optional[Currencies] = None early_hours: Optional[bool] = None - exchanges: Optional[Dict[str, str]] = None + exchanges: Optional[Exchanges] = None market: Optional[str] = None server_time: Optional[str] = None diff --git a/polygon/rest/models/shared.py b/polygon/rest/models/shared.py new file mode 100644 index 00000000..c8af7626 --- /dev/null +++ b/polygon/rest/models/shared.py @@ -0,0 +1,62 @@ +from enum import Enum + + +class Sort(Enum): + ASC = "asc" + DESC = "desc" + + +class Order(Enum): + ASC = "asc" + DESC = "desc" + + +class Locale(Enum): + US = "us" + GLOBAL = "global" + + +class Market(Enum): + STOCKS = "stocks" + CRYPTO = "crypto" + FX = "fx" + + +class AssetClass(Enum): + STOCKS = "stocks" + OPTIONS = "options" + CRYPTO = "crypto" + FX = "fx" + + +class DividendType(Enum): + CD = "CD" + SC = "SC" + LT = "LT" + ST = "ST" + + +class Frequency(Enum): + OneTime = 0 + Anually = 1 + BiAnually = 2 + Quarterly = 4 + Monthly = 12 + + +class DataType(Enum): + DataTrade = "trade" + DataBBO = "bbo" + DataNBBO = "nbbo" + + +class SIP(Enum): + CTA = "CTA" + UTP = "UTP" + OPRA = "OPRA" + + +class ExchangeType(Enum): + exchange = "exchange" + TRF = "TRF" + SIP = "SIP" diff --git a/polygon/rest/models/tickers.py b/polygon/rest/models/tickers.py index d5382a8a..681fd669 100644 --- a/polygon/rest/models/tickers.py +++ b/polygon/rest/models/tickers.py @@ -1,26 +1,8 @@ from typing import Optional, List -from enum import Enum +from .shared import Locale, Market, AssetClass from dataclasses import dataclass -class Locale(Enum): - US = "us" - GLOBAL = "global" - - -class Market(Enum): - STOCKS = "stocks" - CRYPTO = "crypto" - FX = "fx" - - -class AssetClass(Enum): - STOCKS = "stocks" - OPTIONS = "options" - CRYPTO = "crypto" - FX = "fx" - - @dataclass class Address: address1: Optional[str] = None diff --git a/polygon/rest/reference.py b/polygon/rest/reference.py index bcf0a8ac..bc0c7487 100644 --- a/polygon/rest/reference.py +++ b/polygon/rest/reference.py @@ -1,4 +1,3 @@ -from polygon.rest.models.dividends import DividendType from .base import BaseClient from typing import Optional, Any, Dict, List, Union, Iterator from .models import ( @@ -14,7 +13,12 @@ Locale, Split, Dividend, + DividendType, Frequency, + Condition, + DataType, + SIP, + Exchange, ) from urllib3 import HTTPResponse @@ -77,6 +81,7 @@ def list_tickers( ) -> Union[Iterator[Ticker], HTTPResponse]: """ Query all ticker symbols which are supported by Polygon.io. This API currently includes Stocks/Equities, Crypto, and Forex. + :param ticker: Specify a ticker symbol. Defaults to empty string which queries all tickers. :param ticker_lt: Ticker less than :param ticker_lte: Ticker less than or equal to @@ -115,6 +120,7 @@ def get_ticker_details( ) -> Union[TickerDetails, HTTPResponse]: """ Get a single ticker supported by Polygon.io. This response will have detailed information about the ticker and the company behind it. + :param ticker: The ticker symbol of the asset. :param date: Specify a point in time to get information about the ticker available on that date. When retrieving information from SEC filings, we compare this date with the period of report date on the SEC filing. :param params: Any additional query params @@ -127,7 +133,7 @@ def get_ticker_details( path=url, params=params, deserializer=TickerDetails.from_dict, raw=raw ) - def get_ticker_news( + def list_ticker_news( self, ticker: Optional[str] = None, ticker_lt: Optional[str] = None, @@ -141,9 +147,10 @@ def get_ticker_news( published_utc_gte: Optional[str] = None, params: Optional[Dict[str, Any]] = None, raw: bool = False, - ) -> Union[TickerDetails, HTTPResponse]: + ) -> Union[Iterator[TickerNews], HTTPResponse]: """ Get the most recent news articles relating to a stock ticker symbol, including a summary of the article and a link to the original source. + :param ticker: Return results that contain this ticker. :param published_utc: Return results published on, before, or after this date. :param limit: Limit the number of results returned, default is 10 and max is 1000. @@ -155,19 +162,23 @@ def get_ticker_news( """ url = "/v2/reference/news" - return self._get( - path=url, params=params, deserializer=TickerNews.from_dict, raw=raw + return self._paginate( + path=url, + params=self._get_params(self.list_ticker_news, locals()), + raw=raw, + deserializer=TickerNews.from_dict, ) def get_ticker_types( self, - asset_class: Optional[AssetClass] = None, - locale: Optional[Locale] = None, + asset_class: Optional[Union[str, AssetClass]] = None, + locale: Optional[Union[str, Locale]] = None, params: Optional[Dict[str, Any]] = None, raw: bool = False, ) -> Union[TickerTypes, HTTPResponse]: """ List all ticker types that Polygon.io has. + :param asset_class: Filter by asset class. :param locale: Filter by locale. :param params: Any additional query params @@ -260,13 +271,13 @@ def list_dividends( pay_date_lte: Optional[str] = None, pay_date_gt: Optional[str] = None, pay_date_gte: Optional[str] = None, - frequency: Optional[Frequency] = None, + frequency: Optional[Union[int, Frequency]] = None, cash_amount: Optional[float] = None, cash_amount_lt: Optional[float] = None, cash_amount_lte: Optional[float] = None, cash_amount_gt: Optional[float] = None, cash_amount_gte: Optional[float] = None, - dividend_type: Optional[DividendType] = None, + dividend_type: Optional[Union[str, DividendType]] = None, limit: Optional[int] = None, sort: Optional[Union[str, Sort]] = None, order: Optional[Union[str, Order]] = None, @@ -319,3 +330,64 @@ def list_dividends( raw=raw, deserializer=Dividend.from_dict, ) + + +class ConditionsClient(BaseClient): + def list_conditions( + self, + asset_class: Optional[Union[str, AssetClass]] = None, + data_type: Optional[Union[str, DataType]] = None, + id: Optional[int] = None, + sip: Optional[Union[str, SIP]] = None, + limit: Optional[int] = None, + sort: Optional[Union[str, Sort]] = None, + order: Optional[Union[str, Order]] = None, + params: Optional[Dict[str, Any]] = None, + raw: bool = False, + ) -> Union[Iterator[Condition], HTTPResponse]: + """ + List all conditions that Polygon.io uses. + + :param asset_class: Filter for conditions within a given asset class. + :param data_type: Data types that this condition applies to. + :param id: Filter for conditions with a given ID. + :param sip: Filter by SIP. If the condition contains a mapping for that SIP, the condition will be returned. + :param limit: Limit the number of results returned, default is 10 and max is 1000. + :param sort: Sort field used for ordering. + :param order: Order results based on the sort field. + :param params: Any additional query params + :param raw: Return raw object instead of results object + :return: List of conditions + """ + url = "/v3/reference/conditions" + + return self._paginate( + path=url, + params=self._get_params(self.list_conditions, locals()), + raw=raw, + deserializer=Condition.from_dict, + ) + + +class ExchangesClient(BaseClient): + def get_exchanges( + self, + asset_class: Optional[Union[str, AssetClass]] = None, + locale: Optional[Union[str, Locale]] = None, + params: Optional[Dict[str, Any]] = None, + raw: bool = False, + ) -> Union[Exchange, HTTPResponse]: + """ + List all exchanges that Polygon.io knows about. + + :param asset_class: Filter by asset class. + :param locale: Filter by locale. + :param params: Any additional query params + :param raw: Return HTTPResponse object instead of results object + :return: List of quotes + """ + url = "/v3/reference/exchanges" + + return self._get( + path=url, params=params, deserializer=Exchange.from_dict, raw=raw + )