diff --git a/README.md b/README.md index 1601d3b5..13eeafbb 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ The Alpaca SDK will check the environment for a number of variables which can be | APCA_RETRY_WAIT=3 | 3 | seconds to wait between each retry attempt | | APCA_RETRY_CODES=429,504 | 429,504 | comma-separated HTTP status code for which retry is attempted | | POLYGON_WS_URL | wss://alpaca.socket.polygon.io/stocks | Endpoint for streaming polygon data. You likely don't need to change this unless you want to proxy it for example | +| POLYGON_KEY_ID | | Your Polygon key, if it's not the same as your Alpaca API key. Most users will not need to set this to access Polygon.| ## REST @@ -238,9 +239,10 @@ Deregisters the event handler function that was previously registered via `on` o --- # Polygon API Service -Alpaca's API key ID can be used to access Polygon API whose document is found [here](https://polygon.io/docs/). -This python SDK wraps their API service and seamlessly integrates with Alpaca API. -`alpaca_trade_api.REST.polygon` will be the `REST` object for Polygon. +Alpaca's API key ID can be used to access Polygon API, the documentation for +which is found [here](https://polygon.io/docs/). +This python SDK wraps their API service and seamlessly integrates it with the Alpaca +API. `alpaca_trade_api.REST.polygon` will be the `REST` object for Polygon. The example below gives AAPL daily OHLCV data in a DataFrame format. diff --git a/alpaca_trade_api/common.py b/alpaca_trade_api/common.py index 6b5b8200..e5b3ca2c 100644 --- a/alpaca_trade_api/common.py +++ b/alpaca_trade_api/common.py @@ -23,6 +23,17 @@ def get_credentials(key_id=None, secret_key=None): return key_id, secret_key +def get_polygon_credentials(alpaca_key=None): + try: + alpaca_key, _ = get_credentials(alpaca_key) + except ValueError: + pass + key_id = os.environ.get('POLYGON_KEY_ID') or alpaca_key + if key_id is None: + raise ValueError('Key ID must be given to access Polygon API') + return key_id + + def get_api_version(api_version): api_version = api_version or os.environ.get('APCA_API_VERSION') if api_version is None: diff --git a/alpaca_trade_api/polygon/rest.py b/alpaca_trade_api/polygon/rest.py index 73576f84..46606e8f 100644 --- a/alpaca_trade_api/polygon/rest.py +++ b/alpaca_trade_api/polygon/rest.py @@ -6,6 +6,7 @@ Exchange, SymbolTypeMap, ConditionMap, Company, Dividends, Splits, Earnings, Financials, NewsList, Ticker ) +from alpaca_trade_api.common import get_polygon_credentials def _is_list_like(o): @@ -15,6 +16,7 @@ def _is_list_like(o): class REST(object): def __init__(self, api_key, staging=False): + self._api_key = get_polygon_credentials(api_key) self._api_key = api_key self._staging = staging self._session = requests.Session() diff --git a/alpaca_trade_api/polygon/stream2.py b/alpaca_trade_api/polygon/stream2.py index c68f058f..2bbca98b 100644 --- a/alpaca_trade_api/polygon/stream2.py +++ b/alpaca_trade_api/polygon/stream2.py @@ -7,12 +7,13 @@ from .entity import ( Quote, Trade, Agg, Entity, ) +from alpaca_trade_api.common import get_polygon_credentials import logging class StreamConn(object): def __init__(self, key_id=None): - self._key_id = key_id or os.environ.get('APCA_API_KEY_ID') + self._key_id = get_polygon_credentials(key_id) self._endpoint = os.environ.get( 'POLYGON_WS_URL', 'wss://alpaca.socket.polygon.io/stocks'