Skip to content

Commit

Permalink
feat: Add sandbox data support (#464)
Browse files Browse the repository at this point in the history
* Add sandbox data url
Adds the sandbox data endpoint url to the BaseURL enum

* Add sandbox argument to clients
Adds the sandbox parameter to the historical clients and sets the base_url to the
sandbox url. Also adds the use_basic_auth argument to the CryptoHistoricalDataClient
as it's required for broker api sandbox authentication
  • Loading branch information
DeXtreme authored Jun 28, 2024
1 parent 6e3f3a9 commit e1a9097
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions alpaca/common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class BaseURL(str, Enum):
TRADING_PAPER = "https://paper-api.alpaca.markets"
TRADING_LIVE = "https://api.alpaca.markets"
DATA = "https://data.alpaca.markets"
DATA_SANDBOX = "https://data.sandbox.alpaca.markets"
MARKET_DATA_STREAM = "wss://stream.data.alpaca.markets"
OPTION_DATA_STREAM = "wss://stream.data.alpaca.markets" # Deprecated: use MARKET_DATA_STREAM instead!
TRADING_STREAM_PAPER = "wss://paper-api.alpaca.markets/stream"
Expand Down
17 changes: 15 additions & 2 deletions alpaca/data/historical/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def __init__(
oauth_token: Optional[str] = None,
raw_data: bool = False,
url_override: Optional[str] = None,
use_basic_auth: bool = False,
sandbox: bool = False,
) -> None:
"""
Instantiates a Historical Data Client for Crypto Data.
Expand All @@ -58,15 +60,26 @@ def __init__(
methods. Defaults to False. This has not been implemented yet.
url_override (Optional[str], optional): If specified allows you to override the base url the client points
to for proxy/testing.
use_basic_auth (bool, optional): If true, API requests will use basic authorization headers. Set to true if using
broker api sandbox credentials
sandbox (bool): True if using sandbox mode. Defaults to False.
"""

base_url = (
url_override
if url_override is not None
else BaseURL.DATA_SANDBOX if sandbox else BaseURL.DATA
)

super().__init__(
api_key=api_key,
secret_key=secret_key,
oauth_token=oauth_token,
api_version="v1beta3",
base_url=url_override if url_override is not None else BaseURL.DATA,
sandbox=False,
base_url=base_url,
sandbox=sandbox,
raw_data=raw_data,
use_basic_auth=use_basic_auth,
)

def get_crypto_bars(
Expand Down
16 changes: 13 additions & 3 deletions alpaca/data/historical/option.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(
use_basic_auth: bool = False,
raw_data: bool = False,
url_override: Optional[str] = None,
sandbox: bool = False,
) -> None:
"""
Instantiates a Historical Data Client.
Expand All @@ -56,20 +57,29 @@ def __init__(
api_key (Optional[str], optional): Alpaca API key. Defaults to None.
secret_key (Optional[str], optional): Alpaca API secret key. Defaults to None.
oauth_token (Optional[str]): The oauth token if authenticating via OAuth. Defaults to None.
use_basic_auth (bool, optional): If true, API requests will use basic authorization headers.
use_basic_auth (bool, optional): If true, API requests will use basic authorization headers. Set to true if using
broker api sandbox credentials
raw_data (bool, optional): If true, API responses will not be wrapped and raw responses will be returned from
methods. Defaults to False. This has not been implemented yet.
url_override (Optional[str], optional): If specified allows you to override the base url the client points
to for proxy/testing.
sandbox (bool): True if using sandbox mode. Defaults to False.
"""

base_url = (
url_override
if url_override is not None
else BaseURL.DATA_SANDBOX if sandbox else BaseURL.DATA
)

super().__init__(
api_key=api_key,
secret_key=secret_key,
oauth_token=oauth_token,
use_basic_auth=use_basic_auth,
api_version="v1beta1",
base_url=url_override if url_override is not None else BaseURL.DATA,
sandbox=False,
base_url=base_url,
sandbox=sandbox,
raw_data=raw_data,
)

Expand Down
16 changes: 13 additions & 3 deletions alpaca/data/historical/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(
use_basic_auth: bool = False,
raw_data: bool = False,
url_override: Optional[str] = None,
sandbox: bool = False,
) -> None:
"""
Instantiates a Historical Data Client.
Expand All @@ -56,20 +57,29 @@ def __init__(
api_key (Optional[str], optional): Alpaca API key. Defaults to None.
secret_key (Optional[str], optional): Alpaca API secret key. Defaults to None.
oauth_token (Optional[str]): The oauth token if authenticating via OAuth. Defaults to None.
use_basic_auth (bool, optional): If true, API requests will use basic authorization headers.
use_basic_auth (bool, optional): If true, API requests will use basic authorization headers. Set to true if using
broker api sandbox credentials
raw_data (bool, optional): If true, API responses will not be wrapped and raw responses will be returned from
methods. Defaults to False. This has not been implemented yet.
url_override (Optional[str], optional): If specified allows you to override the base url the client points
to for proxy/testing.
sandbox (bool): True if using sandbox mode. Defaults to False.
"""

base_url = (
url_override
if url_override is not None
else BaseURL.DATA_SANDBOX if sandbox else BaseURL.DATA
)

super().__init__(
api_key=api_key,
secret_key=secret_key,
oauth_token=oauth_token,
use_basic_auth=use_basic_auth,
api_version="v2",
base_url=url_override if url_override is not None else BaseURL.DATA,
sandbox=False,
base_url=base_url,
sandbox=sandbox,
raw_data=raw_data,
)

Expand Down

0 comments on commit e1a9097

Please sign in to comment.