Skip to content

Commit

Permalink
feat: trading client account configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
alessiocastrica committed Oct 21, 2022
1 parent 3996a9e commit c76a3b9
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 103 deletions.
2 changes: 1 addition & 1 deletion alpaca/broker/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
Bank,
CIPInfo,
TradeAccount,
TradeAccountConfiguration,
TradeDocument,
Transfer,
Order,
Expand Down Expand Up @@ -57,6 +56,7 @@
Calendar,
Clock,
CorporateActionAnnouncement,
AccountConfiguration as TradeAccountConfiguration,
)
from alpaca.trading.models import (
BaseActivity,
Expand Down
42 changes: 0 additions & 42 deletions alpaca/broker/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,48 +274,6 @@ class UploadDocumentMimeType(str, Enum):
JSON = "application/json"


class DTBPCheck(str, Enum):
"""
Specifies when to run a DTBP check for an account.
NOTE: These values are currently the same as PDTCheck however they are not guaranteed to be in sync the future
please see https://alpaca.markets/docs/api-references/broker-api/trading/trading-configurations/#attributes
for more info.
"""

BOTH = "both"
ENTRY = "entry"
EXIT = "exit"


class PDTCheck(str, Enum):
"""
Specifies when to run a PDT check for an account.
NOTE: These values are currently the same as DTBPCheck however they are not guaranteed to be in sync the future
please see https://alpaca.markets/docs/api-references/broker-api/trading/trading-configurations/#attributes
for more info.
"""

BOTH = "both"
ENTRY = "entry"
EXIT = "exit"


class TradeConfirmationEmail(str, Enum):
"""
Used for controlling when an Account will receive a trade confirmation email.
please see https://alpaca.markets/docs/api-references/broker-api/trading/trading-configurations/#attributes
for more info.
"""

ALL = "all"
NONE = "none"


class ACHRelationshipStatus(str, Enum):
"""
Represents the state that an ACHRelationship is in.
Expand Down
28 changes: 1 addition & 27 deletions alpaca/broker/models/accounts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import Any, List, Optional
from typing import List, Optional
from uuid import UUID

from pydantic import parse_obj_as, root_validator, validator
Expand All @@ -8,12 +8,9 @@
from ..enums import (
AgreementType,
ClearingBroker,
DTBPCheck,
EmploymentStatus,
FundingSource,
PDTCheck,
TaxIdType,
TradeConfirmationEmail,
VisaType,
)
from alpaca.trading.enums import AccountStatus
Expand Down Expand Up @@ -320,26 +317,3 @@ class TradeAccount(BaseTradeAccount):
last_daytrade_count: Optional[int]
last_buying_power: Optional[str]
clearing_broker: Optional[ClearingBroker]


class TradeAccountConfiguration(BaseModel):
"""
Represents configuration options for a TradeAccount.
Attributes:
dtbp_check (DTBPCheck): Day Trade Buying Power Check. Controls Day Trading Margin Call (DTMC) checks.
fractional_trading (bool): If true, account is able to participate in fractional trading
max_margin_multiplier (str): A number between 1-4 that represents your max margin multiplier
no_shorting (bool): If true then Account becomes long-only mode.
pdt_check (PDTCheck): Controls Pattern Day Trader (PDT) checks.
suspend_trade (bool): If true Account becomes unable to submit new orders
trade_confirm_email (TradeConfirmationEmail): Controls whether Trade confirmation emails are sent.
"""

dtbp_check: DTBPCheck
fractional_trading: bool
max_margin_multiplier: str
no_shorting: bool
pdt_check: PDTCheck
suspend_trade: bool
trade_confirm_email: TradeConfirmationEmail
36 changes: 36 additions & 0 deletions alpaca/trading/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from uuid import UUID
from pydantic import parse_obj_as
import json

from alpaca.common import RawData
from alpaca.common.utils import validate_uuid_id_param, validate_symbol_or_asset_id
Expand Down Expand Up @@ -31,6 +32,7 @@
Calendar,
TradeAccount,
CorporateActionAnnouncement,
AccountConfiguration,
)


Expand Down Expand Up @@ -417,6 +419,40 @@ def get_account(self) -> Union[TradeAccount, RawData]:

return TradeAccount(**response)

def get_account_configurations(self) -> Union[AccountConfiguration, RawData]:
"""
Returns account configuration details. Contains information like shorting, margin multiplier
trader confirmation emails, and Pattern Day Trading (PDT) checks.
Returns:
alpaca.broker.models.AccountConfiguration: The account configuration details
"""
response = self.get("/account/configurations")

if self._use_raw_data:
return response

return AccountConfiguration(**response)

def set_account_configurations(
self, account_configurations: AccountConfiguration
) -> Union[AccountConfiguration, RawData]:
"""
Returns account configuration details. Contains information like shorting, margin multiplier
trader confirmation emails, and Pattern Day Trading (PDT) checks.
Returns:
alpaca.broker.models.TradeAccountConfiguration: The account configuration details
"""
response = self.patch(
"/account/configurations", data=account_configurations.dict()
)

if self._use_raw_data:
return response

return AccountConfiguration(**json.loads(response))

# ############################## WATCHLIST ################################# #

def get_watchlists(
Expand Down
42 changes: 42 additions & 0 deletions alpaca/trading/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,45 @@ class QueryOrderStatus(str, Enum):
OPEN = "open"
CLOSED = "closed"
ALL = "all"


class DTBPCheck(str, Enum):
"""
Specifies when to run a DTBP check for an account.
NOTE: These values are currently the same as PDTCheck however they are not guaranteed to be in sync the future
please see https://alpaca.markets/docs/api-references/broker-api/trading/trading-configurations/#attributes
for more info.
"""

BOTH = "both"
ENTRY = "entry"
EXIT = "exit"


class PDTCheck(str, Enum):
"""
Specifies when to run a PDT check for an account.
NOTE: These values are currently the same as DTBPCheck however they are not guaranteed to be in sync the future
please see https://alpaca.markets/docs/api-references/broker-api/trading/trading-configurations/#attributes
for more info.
"""

BOTH = "both"
ENTRY = "entry"
EXIT = "exit"


class TradeConfirmationEmail(str, Enum):
"""
Used for controlling when an Account will receive a trade confirmation email.
please see https://alpaca.markets/docs/api-references/broker-api/trading/trading-configurations/#attributes
for more info.
"""

ALL = "all"
NONE = "none"
26 changes: 26 additions & 0 deletions alpaca/trading/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
AssetClass,
AssetStatus,
AssetExchange,
DTBPCheck,
OrderStatus,
OrderType,
OrderClass,
PDTCheck,
TimeInForce,
OrderSide,
PositionSide,
Expand All @@ -18,6 +20,7 @@
ActivityType,
CorporateActionType,
CorporateActionSubType,
TradeConfirmationEmail,
TradeEvent,
)
from pydantic import Field
Expand Down Expand Up @@ -470,6 +473,29 @@ class TradeAccount(BaseModel):
daytrade_count: int


class AccountConfiguration(BaseModel):
"""
Represents configuration options for a TradeAccount.
Attributes:
dtbp_check (DTBPCheck): Day Trade Buying Power Check. Controls Day Trading Margin Call (DTMC) checks.
fractional_trading (bool): If true, account is able to participate in fractional trading
max_margin_multiplier (str): A number between 1-4 that represents your max margin multiplier
no_shorting (bool): If true then Account becomes long-only mode.
pdt_check (PDTCheck): Controls Pattern Day Trader (PDT) checks.
suspend_trade (bool): If true Account becomes unable to submit new orders
trade_confirm_email (TradeConfirmationEmail): Controls whether Trade confirmation emails are sent.
"""

dtbp_check: DTBPCheck
fractional_trading: bool
max_margin_multiplier: str
no_shorting: bool
pdt_check: PDTCheck
suspend_trade: bool
trade_confirm_email: TradeConfirmationEmail


class CorporateActionAnnouncement(BaseModel):
"""
An announcement of a corporate action. Corporate actions are events like dividend payouts, mergers and stock splits.
Expand Down
16 changes: 0 additions & 16 deletions docs/api_reference/broker/enums.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,6 @@ UploadDocumentMimeType

.. autoenum:: alpaca.broker.enums.UploadDocumentMimeType

DTBPCheck
---------

.. autoenum:: alpaca.broker.enums.DTBPCheck

PDTCheck
---------

.. autoenum:: alpaca.broker.enums.PDTCheck

TradeConfirmationEmail
----------------------

.. autoenum:: alpaca.broker.enums.TradeConfirmationEmail

ACHRelationshipStatus
---------------------

Expand Down Expand Up @@ -151,4 +136,3 @@ JournalStatus
-------------

.. autoenum:: alpaca.broker.enums.JournalStatus

7 changes: 0 additions & 7 deletions docs/api_reference/broker/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ TradeAccount
.. autoclass:: alpaca.broker.models.accounts.TradeAccount


TradeAccountConfiguration
^^^^^^^^^^^^^^^^^^^^^^^^^

.. autoclass:: alpaca.broker.models.accounts.TradeAccountConfiguration


TradeDocument
^^^^^^^^^^^^^
Expand Down Expand Up @@ -135,5 +130,3 @@ BatchJournalResponse
--------------------

.. autoclass:: alpaca.broker.models.journals.BatchJournalResponse


11 changes: 11 additions & 0 deletions docs/api_reference/trading/enums.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ CorporateActionDateType
.. autoenum:: alpaca.trading.enums.CorporateActionDateType


DTBPCheck
---------

.. autoenum:: alpaca.trading.enums.DTBPCheck

PDTCheck
---------

.. autoenum:: alpaca.trading.enums.PDTCheck

TradeConfirmationEmail
----------------------

.. autoenum:: alpaca.trading.enums.TradeConfirmationEmail
7 changes: 5 additions & 2 deletions docs/api_reference/trading/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ TradeAccount

.. autoclass:: alpaca.trading.models.TradeAccount

AccountConfiguration
--------------------

.. autoclass:: alpaca.trading.models.AccountConfiguration


Watchlist
---------
Expand Down Expand Up @@ -79,5 +84,3 @@ CorporateActionAnnouncement
---------------------------

.. autoclass:: alpaca.trading.models.CorporateActionAnnouncement


5 changes: 2 additions & 3 deletions tests/broker/broker_client/test_accounts_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

import pytest

from alpaca.broker.models import TradeAccountConfiguration
from alpaca.trading.models import AccountConfiguration as TradeAccountConfiguration
from alpaca.trading.enums import DTBPCheck, PDTCheck
from alpaca.broker.client import BrokerClient
from alpaca.broker.enums import (
AccountEntities,
DTBPCheck,
PDTCheck,
)
from alpaca.broker.models import (
Account,
Expand Down
5 changes: 2 additions & 3 deletions tests/broker/factories/accounts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import List
from alpaca.trading.enums import DTBPCheck, PDTCheck
from alpaca.trading.models import AccountConfiguration as TradeAccountConfiguration

from alpaca.broker.models import (
Identity,
Expand All @@ -7,12 +9,9 @@
Agreement,
AccountDocument,
TrustedContact,
TradeAccountConfiguration,
)
from alpaca.broker.enums import (
DTBPCheck,
FundingSource,
PDTCheck,
TaxIdType,
AgreementType,
)
Expand Down
Loading

0 comments on commit c76a3b9

Please sign in to comment.