py_alpaca_api.trading.account

Exceptions

APIRequestError

Raised when an API request fails.

Classes

Requests

AccountActivityModel

AccountConfigModel

Model for account configuration settings.

AccountModel

Account

Functions

account_activity_class_from_dict(→ AccountActivityModel)

Converts a dictionary into an instance of the AccountActivityModel.

account_config_class_from_dict(→ AccountConfigModel)

Create AccountConfigModel from API response dictionary.

account_class_from_dict(→ AccountModel)

Converts a dictionary into an instance of the AccountModel.

Module Contents

exception py_alpaca_api.trading.account.APIRequestError(status_code: int | None = None, message: str = '')[source]

Bases: PyAlpacaAPIError

Raised when an API request fails.

class py_alpaca_api.trading.account.Requests[source]
request(method: str, url: str, headers: dict[str, str] | None = None, params: dict[str, str | bool | float | int] | None = None, json: dict[str, Any] | None = None, raw_response: bool = False)[source]

Execute HTTP request with retry logic.

Parameters:
  • method – A string representing the HTTP method to be used in the request.

  • url – A string representing the URL to send the request to.

  • headers – An optional dictionary containing the headers for the request.

  • params – An optional dictionary containing the query parameters for the request.

  • json – An optional dictionary containing the JSON payload for the request.

  • raw_response – If True, return the raw response object without status checks. Defaults to False.

Returns:

The response object returned by the server.

Raises:

APIRequestError – If the response status code is not one of the acceptable statuses (200, 204, 207) and raw_response is False.

class py_alpaca_api.trading.account.AccountActivityModel[source]
activity_type: str
id: str
cum_qty: float
leaves_qty: float
price: float
qty: float
side: str
symbol: str
transaction_time: datetime.datetime
order_id: str
type: str
order_status: str
date: datetime.datetime
net_amount: float
per_share_amount: float
py_alpaca_api.trading.account.account_activity_class_from_dict(data_dict: dict) AccountActivityModel[source]

Converts a dictionary into an instance of the AccountActivityModel.

Parameters:

data_dict – A dictionary containing the data for creating an instance of AccountActivityModel.

Returns:

An instance of the AccountActivityModel class.

Raises:

None

class py_alpaca_api.trading.account.AccountConfigModel[source]

Model for account configuration settings.

dtbp_check

Day trade buying power check setting (“entry”, “exit”, “both”)

fractional_trading

Whether fractional trading is enabled

max_margin_multiplier

Maximum margin multiplier allowed (“1”, “2”, “4”)

no_shorting

Whether short selling is disabled

pdt_check

Pattern day trader check setting (“entry”, “exit”, “both”)

ptp_no_exception_entry

Whether PTP no exception entry is enabled

suspend_trade

Whether trading is suspended

trade_confirm_email

Trade confirmation email setting (“all”, “none”)

dtbp_check: str
fractional_trading: bool
max_margin_multiplier: str
no_shorting: bool
pdt_check: str
ptp_no_exception_entry: bool
suspend_trade: bool
trade_confirm_email: str
py_alpaca_api.trading.account.account_config_class_from_dict(data: dict) AccountConfigModel[source]

Create AccountConfigModel from API response dictionary.

Parameters:

data – Dictionary containing account configuration data from API

Returns:

AccountConfigModel instance

class py_alpaca_api.trading.account.AccountModel[source]
id: str
account_number: str
status: str
crypto_status: str
options_approved_level: int
options_trading_level: int
currency: str
buying_power: float
regt_buying_power: float
daytrading_buying_power: float
effective_buying_power: float
non_marginable_buying_power: float
options_buying_power: float
bod_dtbp: float
cash: float
accrued_fees: float
pending_transfer_in: float
portfolio_value: float
pattern_day_trader: bool
trading_blocked: bool
transfers_blocked: bool
account_blocked: bool
created_at: datetime.datetime
trade_suspended_by_user: bool
multiplier: int
shorting_enabled: bool
equity: float
last_equity: float
long_market_value: float
short_market_value: float
position_market_value: float
initial_margin: float
maintenance_margin: float
last_maintenance_margin: float
sma: float
daytrade_count: int
balance_asof: str
crypto_tier: int
intraday_adjustments: int
pending_reg_taf_fees: float
py_alpaca_api.trading.account.account_class_from_dict(data_dict: dict) AccountModel[source]

Converts a dictionary into an instance of the AccountModel.

Parameters:

data_dict (dict) – A dictionary containing the data for the AccountModel instance.

Returns:

An instance of the AccountModel created from the provided dictionary.

Return type:

AccountModel

class py_alpaca_api.trading.account.Account(headers: dict[str, str], base_url: str)[source]
get() py_alpaca_api.models.account_model.AccountModel[source]

Retrieves the user’s account information.

Returns:

The user’s account model.

Return type:

AccountModel

activities(activity_type: str, date: str | None = None, until_date: str | None = None) list[py_alpaca_api.models.account_activity_model.AccountActivityModel][source]

Retrieves the account activities for the specified activity type.

Optionally filtered by date or until date.

Parameters:
  • activity_type (str) – The type of account activity to retrieve.

  • date (str, optional) – The date to filter the activities by. If provided, only activities on this date will be returned.

  • until_date (str, optional) – The date to filter the activities up to. If provided, only activities up to and including this date will be returned.

Returns:

A list of account activity models

representing the retrieved activities.

Return type:

List[AccountActivityModel]

Raises:

ValueError – If the activity type is not provided, or if both date and until_date are provided.

portfolio_history(period: str = '1W', timeframe: str = '1D', intraday_reporting: str = 'market_hours') pandas.DataFrame[source]

Retrieves portfolio history data.

Parameters:
  • period (str) – The period of time for which the portfolio history is requested. Defaults to “1W” (1 week).

  • timeframe (str) – The timeframe for the intervals of the portfolio history. Defaults to “1D” (1 day).

  • intraday_reporting (str) – The type of intraday reporting to be used. Defaults to “market_hours”.

Returns:

A pandas DataFrame containing the portfolio history data.

Return type:

pd.DataFrame

Raises:

Exception – If the request to the Alpaca API fails.

get_configuration() py_alpaca_api.models.account_config_model.AccountConfigModel[source]

Retrieves the current account configuration settings.

Returns:

The current account configuration.

Return type:

AccountConfigModel

Raises:

APIRequestError – If the request to retrieve configuration fails.

update_configuration(dtbp_check: str | None = None, fractional_trading: bool | None = None, max_margin_multiplier: str | None = None, no_shorting: bool | None = None, pdt_check: str | None = None, ptp_no_exception_entry: bool | None = None, suspend_trade: bool | None = None, trade_confirm_email: str | None = None) py_alpaca_api.models.account_config_model.AccountConfigModel[source]

Updates the account configuration settings.

Parameters:
  • dtbp_check – Day trade buying power check (“entry”, “exit”, “both”)

  • fractional_trading – Whether to enable fractional trading

  • max_margin_multiplier – Maximum margin multiplier (“1”, “2”, “4”)

  • no_shorting – Whether to disable short selling

  • pdt_check – Pattern day trader check (“entry”, “exit”, “both”)

  • ptp_no_exception_entry – Whether to enable PTP no exception entry

  • suspend_trade – Whether to suspend trading

  • trade_confirm_email – Trade confirmation emails (“all”, “none”)

Returns:

The updated account configuration.

Return type:

AccountConfigModel

Raises: