trade_clients

Classes

TradeAccount

Represents trading account information for an Account.

TradingClient

Helper class that provides a standard way to create an ABC using

LocalTradingClient

Helper class that provides a standard way to create an ABC using

RemoteTradingClient

Helper class that provides a standard way to create an ABC using

AlpacaClient

Helper class that provides a standard way to create an ABC using

Functions

_json_default(obj)

JSON serializer for objects not serializable by default json code.

Module Contents

class trade_clients.TradeAccount(/, **data)

Bases: alpaca.trading.models.TradeAccount

Represents trading account information for an Account.

Parameters:

data (Any)

id

The account ID

Type:

UUID

account_number

The account number

Type:

str

status

The current status of the account

Type:

AccountStatus

crypto_status

The status of the account in regards to trading crypto. Only present if crypto trading is enabled for your brokerage account.

Type:

Optional[AccountStatus]

currency

Currently will always be the value “USD”.

Type:

Optional[str]

buying_power

Current available cash buying power. If multiplier = 2 then buying_power = max(equity-initial_margin(0) * 2). If multiplier = 1 then buying_power = cash.

Type:

Optional[str]

regt_buying_power

User’s buying power under Regulation T (excess equity - (equity - margin value) - * margin multiplier)

Type:

Optional[str]

daytrading_buying_power

The buying power for day trades for the account

Type:

Optional[str]

non_marginable_buying_power

The non marginable buying power for the account

Type:

Optional[str]

cash

Cash balance in the account

Type:

Optional[str]

accrued_fees

Fees accrued in this account

Type:

Optional[str]

pending_transfer_out

Cash pending transfer out of this account

Type:

Optional[str]

pending_transfer_in

Cash pending transfer into this account

Type:

Optional[str]

portfolio_value

Total value of cash + holding positions. (This field is deprecated. It is equivalent to the equity field.)

Type:

str

pattern_day_trader

Whether the account is flagged as pattern day trader or not.

Type:

Optional[bool]

trading_blocked

If true, the account is not allowed to place orders.

Type:

Optional[bool]

transfers_blocked

If true, the account is not allowed to request money transfers.

Type:

Optional[bool]

account_blocked

If true, the account activity by user is prohibited.

Type:

Optional[bool]

created_at

Timestamp this account was created at

Type:

Optional[datetime]

trade_suspended_by_user

If true, the account is not allowed to place orders.

Type:

Optional[bool]

multiplier

Multiplier value for this account.

Type:

Optional[str]

shorting_enabled

Flag to denote whether or not the account is permitted to short

Type:

Optional[bool]

equity

This value is cash + long_market_value + short_market_value. This value isn’t calculated in the SDK it is computed on the server and we return the raw value here.

Type:

Optional[str]

last_equity

Equity as of previous trading day at 16:00:00 ET

Type:

Optional[str]

long_market_value

Real-time MtM value of all long positions held in the account

Type:

Optional[str]

short_market_value

Real-time MtM value of all short positions held in the account

Type:

Optional[str]

initial_margin

Reg T initial margin requirement

Type:

Optional[str]

maintenance_margin

Maintenance margin requirement

Type:

Optional[str]

last_maintenance_margin

Maintenance margin requirement on the previous trading day

Type:

Optional[str]

sma

Value of Special Memorandum Account (will be used at a later date to provide additional buying_power)

Type:

Optional[str]

daytrade_count

The current number of daytrades that have been made in the last 5 trading days (inclusive of today)

Type:

Optional[int]

options_buying_power

Your buying power for options trading

Type:

Optional[str]

options_approved_level

The options trading level that was approved for this account. 0=disabled, 1=Covered Call/Cash-Secured Put, 2=Long Call/Put, 3=Spreads/Straddles.

Type:

Optional[int]

options_trading_level

The effective options trading level of the account. This is the minimum between account options_approved_level and account configurations max_options_trading_level. 0=disabled, 1=Covered Call/Cash-Secured Put, 2=Long, 3=Spreads/Straddles.

Type:

Optional[int]

initial_cash: float | None = None
trade_clients._json_default(obj)

JSON serializer for objects not serializable by default json code.

Parameters:

obj (object)

class trade_clients.TradingClient(live, config, alpaca_account_client)

Bases: abc.ABC

Helper class that provides a standard way to create an ABC using inheritance.

Parameters:
classmethod from_config(config, alpaca_account_client, live=False)
Parameters:
Return type:

TradingClient

live
config: trading.cli.trading.trade_config.RRTradeConfig
alpaca_account_client
defer_trade_execution
_account
close(closed_positions, open_positions, pf_history, cash)
Parameters:
  • closed_positions (list[trading.src.portfolio.position.Position])

  • open_positions (dict[str, collections.deque[trading.src.portfolio.position.Position]])

  • pf_history (list[trading.src.portfolio.position.PortfolioStats])

  • cash (float)

Return type:

None

get_clock()
get_calendar()
property account: TradeAccount
Return type:

TradeAccount

property positions: dict[str, collections.deque[trading.src.portfolio.position.Position]]
Return type:

dict[str, collections.deque[trading.src.portfolio.position.Position]]

abstractmethod _load_positions()
Return type:

dict[str, collections.deque[trading.src.portfolio.position.Position]]

abstractmethod _load_account()
Return type:

TradeAccount

_load_pf_stats()

This is not implemented for Alpaca cause the api manages these and it’s just for historical tracking

Parameters:

self – Description

Returns:

Description

Return type:

list[Position]

_load_closed_positions()

This is not implemented for Alpaca cause the api manages these and it’s just for historical tracking

Parameters:

self – Description

Returns:

Description

Return type:

list[Position]

_write_open_positions(open_positions)
Parameters:

open_positions (dict[str, collections.deque[trading.src.portfolio.position.Position]])

Return type:

None

_write_account(cash)
Parameters:

cash (float)

Return type:

None

_write_pf_stats(stats)
Parameters:

stats (list[trading.src.portfolio.position.PortfolioStats])

Return type:

None

_write_closed_positions(closed_positions)
Parameters:

closed_positions (list[trading.src.portfolio.position.Position])

Return type:

None

write_meta_data(meta_data)
Parameters:

meta_data (dict[str, Any])

Return type:

None

execute_trades(actions)
Parameters:

actions (pandas.DataFrame)

Return type:

tuple[pandas.DataFrame, float, list[alpaca.trading.requests.MarketOrderRequest]]

class trade_clients.LocalTradingClient(config, alpaca_account_client, live)

Bases: TradingClient

Helper class that provides a standard way to create an ABC using inheritance.

Parameters:
positions_path
account_path
closed_positions_path
account_value_series_path
meta_data_path
static _resolve_path(path_field, default_name)
Parameters:

default_name (str)

Return type:

pathlib.Path

write_meta_data(meta_data)
Parameters:

meta_data (dict[str, Any])

Return type:

None

_write_open_positions(open_positions)
Parameters:

open_positions (dict[str, collections.deque[trading.src.portfolio.position.Position]])

Return type:

None

_write_account(cash)
Parameters:

cash (float)

Return type:

None

_write_pf_stats(stats)
Parameters:

stats (list[trading.src.portfolio.position.PortfolioStats])

Return type:

None

_write_closed_positions(closed_positions)
Parameters:

closed_positions (list[trading.src.portfolio.position.Position])

Return type:

None

_load_positions()
Return type:

dict[str, collections.deque[trading.src.portfolio.position.Position]]

_load_account()
Return type:

TradeAccount

_load_pf_stats()

This is not implemented for Alpaca cause the api manages these and it’s just for historical tracking

Parameters:

self – Description

Returns:

Description

Return type:

list[Position]

_load_closed_positions()

This is not implemented for Alpaca cause the api manages these and it’s just for historical tracking

Parameters:

self – Description

Returns:

Description

Return type:

list[Position]

class trade_clients.RemoteTradingClient(config, alpaca_account_client, live)

Bases: TradingClient

Helper class that provides a standard way to create an ABC using inheritance.

Parameters:
_client
open_positions_key = ''
account_key = ''
closed_positions_key = ''
account_stats_key = ''
backtest_key = ''
_load_positions()
Return type:

dict[str, collections.deque[trading.src.portfolio.position.Position]]

_load_account()
Return type:

TradeAccount

_load_pf_stats()

This is not implemented for Alpaca cause the api manages these and it’s just for historical tracking

Parameters:

self – Description

Returns:

Description

Return type:

list[Position]

_load_closed_positions()

This is not implemented for Alpaca cause the api manages these and it’s just for historical tracking

Parameters:

self – Description

Returns:

Description

Return type:

list[Position]

write_meta_data(meta_data)
Parameters:

meta_data (dict[str, Any])

Return type:

None

_write_closed_positions(closed_positions)
Parameters:

closed_positions (list[trading.src.portfolio.position.Position])

Return type:

None

_write_account(cash)
Parameters:

cash (float)

Return type:

None

_write_open_positions(open_positions)
Parameters:

open_positions (dict[str, collections.deque[trading.src.portfolio.position.Position]])

Return type:

None

_write_pf_stats(stats)
Parameters:

stats (list[trading.src.portfolio.position.PortfolioStats])

Return type:

None

write_backtest_results(backtest_dir)
Return type:

None

class trade_clients.AlpacaClient(config, alpaca_account_client=None, live=False)

Bases: TradingClient

Helper class that provides a standard way to create an ABC using inheritance.

Parameters:
_load_positions()
Return type:

dict[str, collections.deque[trading.src.portfolio.position.Position]]

_load_account()
Return type:

TradeAccount

execute_trades(actions)
Parameters:

actions (pandas.DataFrame)

Return type:

tuple[pandas.DataFrame, float, list[alpaca.trading.requests.MarketOrderRequest]]