Skip to content

Commit

Permalink
feat: support asset attributes filter for get_all_assets()
Browse files Browse the repository at this point in the history
  • Loading branch information
hiohiohio committed Dec 27, 2023
1 parent 3bcf5f1 commit 84c1a74
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
4 changes: 2 additions & 2 deletions alpaca/trading/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Asset(ModelWithID):
shortable (bool): Whether the asset can be shorted.
easy_to_borrow (bool): When shorting, whether the asset is easy to borrow
fractionable (bool): Whether fractional shares are available
attributes (bool): One of ptp_no_exception or ptp_with_exception. It will include unique characteristics of the asset here.
attributes (Optional[list[str]]): One of ptp_no_exception or ptp_with_exception. It will include unique characteristics of the asset here.
"""

asset_class: AssetClass = Field(
Expand All @@ -64,7 +64,7 @@ class Asset(ModelWithID):
min_trade_increment: Optional[float] = None
price_increment: Optional[float] = None
maintenance_margin_requirement: Optional[float] = None
attributes: Optional[list] = None
attributes: Optional[list[str]] = None


class USDPositionValues(BaseModel):
Expand Down
3 changes: 2 additions & 1 deletion alpaca/trading/requests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import date, datetime, timedelta
from typing import Optional, Any, List
from typing import Optional, Any, List, Union

import pandas as pd
from pydantic import model_validator
Expand Down Expand Up @@ -125,6 +125,7 @@ class GetAssetsRequest(NonEmptyRequest):
status: Optional[AssetStatus] = None
asset_class: Optional[AssetClass] = None
exchange: Optional[AssetExchange] = None
attributes: Optional[str] = None


class TakeProfitRequest(NonEmptyRequest):
Expand Down
70 changes: 66 additions & 4 deletions tests/trading/trading_client/test_asset_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

from typing import List
from alpaca.common.enums import BaseURL
from alpaca.trading.enums import AssetStatus
from alpaca.data.enums import Exchange
from alpaca.trading.enums import AssetClass, AssetExchange, AssetStatus
from alpaca.trading.requests import GetAssetsRequest
from alpaca.trading.client import TradingClient
from alpaca.trading.models import Asset


def test_get_all_assets(reqmock, trading_client: TradingClient):
reqmock.get(
f"{BaseURL.TRADING_PAPER.value}/v2/assets",
f"{BaseURL.TRADING_PAPER.value}/v2/assets?status=active",
text="""
[
{
Expand All @@ -28,7 +29,11 @@ def test_get_all_assets(reqmock, trading_client: TradingClient):
"easy_to_borrow": true,
"fractionable": true,
"last_close_pct_change": "string",
"last_price": "string"
"last_price": "string",
"attributes": [
"attribute1",
"attribute2"
]
}
]
""",
Expand All @@ -40,6 +45,57 @@ def test_get_all_assets(reqmock, trading_client: TradingClient):
assert isinstance(assets, List)
assert len(assets) == 1
assert isinstance(assets[0], Asset)
asset = assets[0]
assert asset.status == AssetStatus.ACTIVE
assert asset.attributes == ["attribute1", "attribute2"]


def test_get_all_assets_params(reqmock, trading_client: TradingClient):
reqmock.get(
f"{BaseURL.TRADING_PAPER.value}/v2/assets?status=active&asset_class=us_equity&exchange=NASDAQ&attributes=attribute1%2Cattribute2",
text="""
[
{
"id": "904837e3-3b76-47ec-b432-046db621571b",
"class": "us_equity",
"exchange": "NASDAQ",
"symbol": "AAPL",
"name": "Apple Inc. Common Stock",
"status": "active",
"tradable": true,
"marginable": true,
"shortable": true,
"easy_to_borrow": true,
"fractionable": true,
"last_close_pct_change": "string",
"last_price": "string",
"attributes": [
"attribute1",
"attribute2"
]
}
]
""",
)

assets = trading_client.get_all_assets(
GetAssetsRequest(
status=AssetStatus.ACTIVE,
asset_class=AssetClass.US_EQUITY,
exchange=AssetExchange.NASDAQ,
attributes=",".join(["attribute1", "attribute2"]),
)
)

assert reqmock.called_once
assert isinstance(assets, List)
assert len(assets) == 1
assert isinstance(assets[0], Asset)
asset = assets[0]
assert asset.status == AssetStatus.ACTIVE
assert asset.asset_class == AssetClass.US_EQUITY
assert asset.exchange == AssetExchange.NASDAQ
assert asset.attributes == ["attribute1", "attribute2"]


def test_get_asset(reqmock, trading_client: TradingClient):
Expand All @@ -61,7 +117,11 @@ def test_get_asset(reqmock, trading_client: TradingClient):
"easy_to_borrow": true,
"fractionable": true,
"last_close_pct_change": "string",
"last_price": "string"
"last_price": "string",
"attributes": [
"attribute1",
"attribute2"
]
}
""",
)
Expand All @@ -71,3 +131,5 @@ def test_get_asset(reqmock, trading_client: TradingClient):
assert reqmock.called_once
assert isinstance(asset, Asset)
assert asset.symbol == symbol
assert asset.status == AssetStatus.ACTIVE
assert asset.attributes == ["attribute1", "attribute2"]

0 comments on commit 84c1a74

Please sign in to comment.