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
-
.get_account(account_number, fields: nil, client: nil) ⇒ Hash, Resources::Account
Get a specific account by account number.
-
.get_account_numbers(client: nil) ⇒ Array<Hash>
Get account numbers and their encrypted hash values.
-
.get_accounts(fields: nil, client: nil) ⇒ Array<Hash>, Array<Resources::Account>
(also: list_accounts)
Get all accounts for the authenticated user.
-
.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.
-
.get_order(account_number, order_id, client: nil) ⇒ Hash, Resources::Order
Get a specific order.
-
.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.
-
.get_positions(account_number, client: nil) ⇒ Array<Hash>, Array<Resources::Position>
Get positions for a specific account.
-
.get_transaction(account_number, transaction_id, client: nil) ⇒ Hash, Resources::Transaction
Get a specific transaction.
-
.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.
-
.get_user_preferences(client: nil) ⇒ Hash
Get user preferences (across all accounts).
-
.preview_order(account_number, order_data, client: nil) ⇒ Hash
Preview an order before placing it.
Class Method Details
.get_account(account_number, fields: nil, client: nil) ⇒ Hash, Resources::Account
Get a specific account by account number
38 39 40 41 42 43 44 45 |
# File 'lib/schwab/accounts.rb', line 38 def get_account(account_number, fields: nil, client: nil) client ||= default_client path = "/trader/v1/accounts/#{encode_account_number(account_number, 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
204 205 206 207 |
# File 'lib/schwab/accounts.rb', line 204 def get_account_numbers(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
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
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
190 191 192 193 194 195 |
# File 'lib/schwab/accounts.rb', line 190 def get_order(account_number, order_id, client: nil) client ||= default_client path = "/trader/v1/accounts/#{encode_account_number(account_number, 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
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/schwab/accounts.rb', line 141 def get_orders(account_number, from_entered_time: nil, to_entered_time: nil, status: nil, max_results: nil, client: nil) client ||= default_client path = "/trader/v1/accounts/#{encode_account_number(account_number, 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
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/schwab/accounts.rb', line 54 def get_positions(account_number, client: nil) account_data = get_account(account_number, fields: "positions", client: client) if account_data.is_a?(Hash) # Positions are nested under securitiesAccount securities_account = account_data["securitiesAccount"] || account_data[:securitiesAccount] positions = securities_account ? (securities_account["positions"] || securities_account[:positions]) : nil elsif account_data.respond_to?(:positions) positions = account_data.positions end positions || [] end |
.get_transaction(account_number, transaction_id, client: nil) ⇒ Hash, Resources::Transaction
Get a specific transaction
114 115 116 117 118 119 |
# File 'lib/schwab/accounts.rb', line 114 def get_transaction(account_number, transaction_id, client: nil) client ||= default_client path = "/trader/v1/accounts/#{encode_account_number(account_number, 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
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/schwab/accounts.rb', line 93 def get_transactions(account_number, types: nil, start_date: nil, end_date: nil, symbol: nil, client: nil) client ||= default_client path = "/trader/v1/accounts/#{encode_account_number(account_number, 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)
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
241 242 243 244 245 246 |
# File 'lib/schwab/accounts.rb', line 241 def preview_order(account_number, order_data, client: nil) client ||= default_client path = "/trader/v1/accounts/#{encode_account_number(account_number, client)}/previewOrder" client.post(path, order_data) end |