Skip to content

Commit

Permalink
Merge pull request #93 from alpacahq/feature/polygon-key-config
Browse files Browse the repository at this point in the history
Add ability to override key used to authenticate with Polygon
  • Loading branch information
ttt733 authored Jul 15, 2019
2 parents fec9392 + b254c85 commit cdb9319
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down
11 changes: 11 additions & 0 deletions alpaca_trade_api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions alpaca_trade_api/polygon/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion alpaca_trade_api/polygon/stream2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit cdb9319

Please sign in to comment.