From 024f188ea89ba467b7bffa4d7d4f795349efe646 Mon Sep 17 00:00:00 2001 From: ttt733 Date: Fri, 12 Jul 2019 16:18:30 -0500 Subject: [PATCH 1/3] Add ability to override key used to authenticate with Polygon --- alpaca_trade_api/common.py | 7 +++++++ alpaca_trade_api/polygon/rest.py | 2 ++ alpaca_trade_api/polygon/stream2.py | 3 ++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/alpaca_trade_api/common.py b/alpaca_trade_api/common.py index 6b5b8200..bf9d6cc7 100644 --- a/alpaca_trade_api/common.py +++ b/alpaca_trade_api/common.py @@ -23,6 +23,13 @@ def get_credentials(key_id=None, secret_key=None): return key_id, secret_key +def get_polygon_credentials(alpaca_key=None): + 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' From 88bc106534dcd68c12da4d790a4d2ae8bc6967c2 Mon Sep 17 00:00:00 2001 From: ttt733 Date: Fri, 12 Jul 2019 16:22:42 -0500 Subject: [PATCH 2/3] Add new environment variable to README --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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. From b254c85674f8afcf8453f207a408e5b7e130741f Mon Sep 17 00:00:00 2001 From: ttt733 Date: Mon, 15 Jul 2019 07:08:53 -0500 Subject: [PATCH 3/3] Check for Alpaca environment variable when getting Polygon key --- alpaca_trade_api/common.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/alpaca_trade_api/common.py b/alpaca_trade_api/common.py index bf9d6cc7..e5b3ca2c 100644 --- a/alpaca_trade_api/common.py +++ b/alpaca_trade_api/common.py @@ -24,6 +24,10 @@ def get_credentials(key_id=None, secret_key=None): 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')