Module: Schwab::Accounts

Defined in:
lib/schwab/accounts.rb

Overview

Account Management API endpoints for retrieving account information, positions, transactions, and orders

Class Method Summary collapse

Class Method Details

.get_account(account_number, fields: nil, client: nil) ⇒ Hash, Resources::Account

Get a specific account by account number

Examples:

Get account with positions and orders

Schwab::Accounts.("123456", fields: ["positions", "orders"])

Parameters:

  • account_number (String)

    The account number

  • fields (String, Array<String>, nil) (defaults to: nil)

    Fields to include

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance

Returns:



38
39
40
41
42
43
44
45
# File 'lib/schwab/accounts.rb', line 38

def (, fields: nil, client: nil)
  client ||= default_client
  path = "/trader/v1/accounts/#{(, client)}"
  params = {}
  params[:fields] = normalize_fields(fields) if fields

  client.get(path, params, Resources::Account)
end

.get_account_numbers(client: nil) ⇒ Array<Hash>

Get account numbers and their encrypted hash values

Examples:

Get account numbers

Schwab::Accounts.
# => [{accountNumber: "123456789", hashValue: "ABC123XYZ"}, ...]

Parameters:

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance

Returns:

  • (Array<Hash>)

    Array of account number/hash value pairs



204
205
206
207
# File 'lib/schwab/accounts.rb', line 204

def (client: nil)
  client ||= default_client
  client.get("/trader/v1/accounts/accountNumbers")
end

.get_accounts(fields: nil, client: nil) ⇒ Array<Hash>, Array<Resources::Account> Also known as: list_accounts

Get all accounts for the authenticated user

Examples:

Get all accounts

Schwab::Accounts.get_accounts

Get accounts with positions

Schwab::Accounts.get_accounts(fields: "positions")

Parameters:

  • fields (String, Array<String>, nil) (defaults to: nil)

    Fields to include (e.g., “positions”, “orders”)

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance (uses default if not provided)

Returns:



19
20
21
22
23
24
25
26
27
# File 'lib/schwab/accounts.rb', line 19

def get_accounts(fields: nil, client: nil)
  client ||= default_client
  params = {}
  params[:fields] = normalize_fields(fields) if fields

  response = client.get("/trader/v1/accounts", params, Resources::Account)
  # API returns accounts in a wrapper, extract the array
  response.is_a?(Hash) && response[:accounts] ? response[:accounts] : response
end

.get_all_orders(from_entered_time: nil, to_entered_time: nil, status: nil, max_results: nil, client: nil) ⇒ Array<Hash>, Array<Resources::Order>

Get all orders for all accounts

Examples:

Get all orders across all accounts

Schwab::Accounts.get_all_orders

Get all filled orders today

Schwab::Accounts.get_all_orders(
  from_entered_time: Date.today,
  status: "FILLED"
)

Parameters:

  • from_entered_time (Time, DateTime, String, nil) (defaults to: nil)

    Start time for orders

  • to_entered_time (Time, DateTime, String, nil) (defaults to: nil)

    End time for orders

  • status (String, Array<String>, nil) (defaults to: nil)

    Order status filter

  • max_results (Integer, nil) (defaults to: nil)

    Maximum number of results

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance

Returns:



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/schwab/accounts.rb', line 169

def get_all_orders(from_entered_time: nil, to_entered_time: nil, status: nil, max_results: nil, client: nil)
  client ||= default_client
  path = "/trader/v1/orders"

  params = {}
  params[:fromEnteredTime] = format_datetime(from_entered_time) if from_entered_time
  params[:toEnteredTime] = format_datetime(to_entered_time) if to_entered_time
  params[:status] = normalize_order_status(status) if status
  params[:maxResults] = max_results if max_results

  client.get(path, params, Resources::Order)
end

.get_order(account_number, order_id, client: nil) ⇒ Hash, Resources::Order

Get a specific order

Examples:

Get order details

Schwab::Accounts.get_order("123456", "order789")

Parameters:

  • account_number (String)

    The account number

  • order_id (String)

    The order ID

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance

Returns:



190
191
192
193
194
195
# File 'lib/schwab/accounts.rb', line 190

def get_order(, order_id, client: nil)
  client ||= default_client
  path = "/trader/v1/accounts/#{(, client)}/orders/#{order_id}"

  client.get(path, {}, Resources::Order)
end

.get_orders(account_number, from_entered_time: nil, to_entered_time: nil, status: nil, max_results: nil, client: nil) ⇒ Array<Hash>, Array<Resources::Order>

Get orders for a specific account

Examples:

Get all orders with time range

Schwab::Accounts.get_orders("123456",
  from_entered_time: "2024-03-29T00:00:00.000Z",
  to_entered_time: "2024-03-30T00:00:00.000Z"
)

Get working orders

Schwab::Accounts.get_orders("123456", status: "WORKING")

Parameters:

  • account_number (String)

    The account number

  • from_entered_time (Time, DateTime, String, nil) (defaults to: nil)

    Start time for orders (ISO-8601 format required)

  • to_entered_time (Time, DateTime, String, nil) (defaults to: nil)

    End time for orders (ISO-8601 format required)

  • status (String, Array<String>, nil) (defaults to: nil)

    Order status filter. Valid values: AWAITING_PARENT_ORDER, AWAITING_CONDITION, AWAITING_STOP_CONDITION, AWAITING_MANUAL_REVIEW, ACCEPTED, AWAITING_UR_OUT, PENDING_ACTIVATION, QUEUED, WORKING, REJECTED, PENDING_CANCEL, CANCELED, PENDING_REPLACE, REPLACED, FILLED, EXPIRED, NEW, AWAITING_RELEASE_TIME, PENDING_ACKNOWLEDGEMENT, PENDING_RECALL, UNKNOWN

  • max_results (Integer, nil) (defaults to: nil)

    Maximum number of results (defaults to 3000)

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance

Returns:



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/schwab/accounts.rb', line 141

def get_orders(, from_entered_time: nil, to_entered_time: nil, status: nil, max_results: nil, client: nil)
  client ||= default_client
  path = "/trader/v1/accounts/#{(, client)}/orders"

  params = {}
  params[:fromEnteredTime] = format_datetime(from_entered_time) if from_entered_time
  params[:toEnteredTime] = format_datetime(to_entered_time) if to_entered_time
  params[:status] = normalize_order_status(status) if status
  params[:maxResults] = max_results if max_results

  client.get(path, params, Resources::Order)
end

.get_positions(account_number, client: nil) ⇒ Array<Hash>, Array<Resources::Position>

Get positions for a specific account

Examples:

Get all positions

Schwab::Accounts.get_positions("123456")

Parameters:

  • account_number (String)

    The account number

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/schwab/accounts.rb', line 54

def get_positions(, client: nil)
   = (, fields: "positions", client: client)

  if .is_a?(Hash)
    # Positions are nested under securitiesAccount
     = ["securitiesAccount"] || [:securitiesAccount]
    positions =  ? (["positions"] || [:positions]) : nil
  elsif .respond_to?(:positions)
    positions = .positions
  end

  positions || []
end

.get_transaction(account_number, transaction_id, client: nil) ⇒ Hash, Resources::Transaction

Get a specific transaction

Examples:

Get transaction details

Schwab::Accounts.get_transaction("123456", "trans789")

Parameters:

  • account_number (String)

    The account number

  • transaction_id (String)

    The transaction ID

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance

Returns:



114
115
116
117
118
119
# File 'lib/schwab/accounts.rb', line 114

def get_transaction(, transaction_id, client: nil)
  client ||= default_client
  path = "/trader/v1/accounts/#{(, client)}/transactions/#{transaction_id}"

  client.get(path, {}, Resources::Transaction)
end

.get_transactions(account_number, types: nil, start_date: nil, end_date: nil, symbol: nil, client: nil) ⇒ Array<Hash>, Array<Resources::Transaction>

Get transactions for a specific account

Examples:

Get all trade transactions

Schwab::Accounts.get_transactions("123456",
  types: "TRADE",
  start_date: "2024-01-01",
  end_date: "2024-01-31"
)

Get trades for AAPL in date range

Schwab::Accounts.get_transactions("123456",
  types: "TRADE",
  symbol: "AAPL",
  start_date: "2024-01-01",
  end_date: "2024-01-31"
)

Parameters:

  • account_number (String)

    The account number

  • types (String, Array<String>) (defaults to: nil)

    Transaction types to filter (REQUIRED). Valid values: TRADE, RECEIVE_AND_DELIVER, DIVIDEND_OR_INTEREST, ACH_RECEIPT, ACH_DISBURSEMENT, CASH_RECEIPT, CASH_DISBURSEMENT, ELECTRONIC_FUND, WIRE_OUT, WIRE_IN, JOURNAL, MEMORANDUM, MARGIN_CALL, MONEY_MARKET, SMA_ADJUSTMENT

  • start_date (Date, Time, String) (defaults to: nil)

    Start date for transactions (ISO-8601 format, REQUIRED)

  • end_date (Date, Time, String) (defaults to: nil)

    End date for transactions (ISO-8601 format, REQUIRED)

  • symbol (String, nil) (defaults to: nil)

    Filter by symbol

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/schwab/accounts.rb', line 93

def get_transactions(, types: nil, start_date: nil, end_date: nil, symbol: nil, client: nil)
  client ||= default_client
  path = "/trader/v1/accounts/#{(, client)}/transactions"

  params = {}
  params[:types] = normalize_transaction_types(types) if types
  params[:startDate] = format_date(start_date) if start_date
  params[:endDate] = format_date(end_date) if end_date
  params[:symbol] = symbol.upcase if symbol

  client.get(path, params, Resources::Transaction)
end

.get_user_preferences(client: nil) ⇒ Hash

Get user preferences (across all accounts)

Examples:

Get user preferences

Schwab::Accounts.get_user_preferences

Parameters:

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance

Returns:

  • (Hash)

    User preferences



215
216
217
218
# File 'lib/schwab/accounts.rb', line 215

def get_user_preferences(client: nil)
  client ||= default_client
  client.get("/trader/v1/userPreference")
end

.preview_order(account_number, order_data, client: nil) ⇒ Hash

Preview an order before placing it

Examples:

Preview a buy order

Schwab::Accounts.preview_order("123456", {
  orderType: "MARKET",
  session: "NORMAL",
  duration: "DAY",
  orderStrategyType: "SINGLE",
  orderLegCollection: [{
    instruction: "BUY",
    quantity: 10,
    instrument: {
      symbol: "AAPL",
      assetType: "EQUITY"
    }
  }]
})

Parameters:

  • account_number (String)

    The account number

  • order_data (Hash)

    Order details to preview

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance

Returns:

  • (Hash)

    Order preview with estimated costs, commissions, and margin requirements



241
242
243
244
245
246
# File 'lib/schwab/accounts.rb', line 241

def preview_order(, order_data, client: nil)
  client ||= default_client
  path = "/trader/v1/accounts/#{(, client)}/previewOrder"

  client.post(path, order_data)
end