Skip to content

Securities Account

The securities account is the account that holds the securities that the user or company.

SecuritiesAccount

Bases: BaseModel

The SecuritiesAccount model represents a securities account in the trading system.

Attributes:

Name Type Description
clearing_account_id str

The ID of the clearing account associated with this securities account.

id str

The unique ID of the securities account.

private_account bool

A flag indicating whether the securities account is private.

version int

The version of the securities account.

client Client

The client associated with the securities account, used for API interactions.

Source code in alpha_trader/securities_account/__init__.py
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
class SecuritiesAccount(BaseModel):
    """
    The SecuritiesAccount model represents a securities account in the trading system.

    Attributes:
        clearing_account_id (str): The ID of the clearing account associated with this securities account.
        id (str): The unique ID of the securities account.
        private_account (bool): A flag indicating whether the securities account is private.
        version (int): The version of the securities account.
        client (Client): The client associated with the securities account, used for API interactions.
    """

    clearing_account_id: str
    id: str
    private_account: bool
    version: int
    client: Client

    @staticmethod
    def initialize_from_api_response(api_response: Dict, client: Client):
        return SecuritiesAccount(
            clearing_account_id=api_response["clearingAccountId"],
            id=api_response["id"],
            private_account=api_response["privateAccount"],
            version=api_response["version"],
            client=client,
        )

    def __str__(self):
        return f"SecuritiesAccount(id={self.id})"

    def __repr__(self):
        return self.__str__()

    @property
    def portfolio(self) -> Portfolio:
        """
        Retrieve the portfolio of this securities account
        Returns:
            Portfolio: The portfolio associated with this securities account
        """
        response = self.client.request("GET", f"api/portfolios/{self.id}")

        return Portfolio.initialize_from_api_response(response.json(), self.client)

    @property
    def orders(self) -> List[Order]:
        """
            Orders for this securities account
        Returns:
            List of orders
        """
        response = self.client.request(
            "GET", f"api/securityorders/securitiesaccount/{self.id}"
        )

        return [
            Order.initialize_from_api_response(res, self.client)
            for res in response.json()
        ]

    def delete_all_orders(self):
        response = self.client.request("DELETE", f"api/securityorders", params={"owner": self.id})

        if response.status_code > 205:
            print(response.text)

        return response.status_code

    def order(
        self,
        action: str,
        order_type: str,
        quantity: int,
        security_identifier: str,
        price: float = None,
        counter_party: str | None = None
    ) -> Order:
        """Create an order for this securities account

        Args:
            action: action of the order "BUY" or "SELL"
            order_type: order type "LIMIT" or "MARKET"
            price: price of the order
            quantity: number of shares
            security_identifier: security identifier of the order
            counter_party: security account id of the counterparty

        Returns:
            Order
        """
        return Order.create(
            action=action,
            order_type=order_type,
            price=price,
            quantity=quantity,
            security_identifier=security_identifier,
            client=self.client,
            owner_securities_account_id=self.id,
            counter_party=counter_party
        )

orders: List[Order] property

Orders for this securities account

Returns: List of orders

portfolio: Portfolio property

Retrieve the portfolio of this securities account Returns: Portfolio: The portfolio associated with this securities account

order(action, order_type, quantity, security_identifier, price=None, counter_party=None)

Create an order for this securities account

Parameters:

Name Type Description Default
action str

action of the order "BUY" or "SELL"

required
order_type str

order type "LIMIT" or "MARKET"

required
price float

price of the order

None
quantity int

number of shares

required
security_identifier str

security identifier of the order

required
counter_party str | None

security account id of the counterparty

None

Returns:

Type Description
Order

Order

Source code in alpha_trader/securities_account/__init__.py
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def order(
    self,
    action: str,
    order_type: str,
    quantity: int,
    security_identifier: str,
    price: float = None,
    counter_party: str | None = None
) -> Order:
    """Create an order for this securities account

    Args:
        action: action of the order "BUY" or "SELL"
        order_type: order type "LIMIT" or "MARKET"
        price: price of the order
        quantity: number of shares
        security_identifier: security identifier of the order
        counter_party: security account id of the counterparty

    Returns:
        Order
    """
    return Order.create(
        action=action,
        order_type=order_type,
        price=price,
        quantity=quantity,
        security_identifier=security_identifier,
        client=self.client,
        owner_securities_account_id=self.id,
        counter_party=counter_party
    )