Skip to main content

rustyschwab_types/
accounts.rs

1//! Account types including balances, positions, and user preferences.
2//!
3//! This module contains types for account-related API responses:
4//!
5//! - Account details and balances
6//! - Position information
7//! - User preferences and streamer info
8//!
9//! Response types keep fields this crate does not model in `extra` (Schwab
10//! adds them, for example `aggregatedBalance`), except
11//! [`StreamerInfo`](crate::accounts::StreamerInfo),
12//! whose unmodelled fields could be credentials.
13
14use crate::common::AccountsInstrument;
15use serde::{Deserialize, Serialize};
16use serde_with::skip_serializing_none;
17use std::collections::HashMap;
18
19type Extra = HashMap<String, serde_json::Value>;
20
21#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
22#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
23#[serde(rename_all = "camelCase")]
24#[non_exhaustive]
25pub struct AccountNumberHash {
26    pub account_number: String,
27    pub hash_value: String,
28}
29
30impl AccountNumberHash {
31    /// An account number and the hash Schwab's account paths take in its place.
32    pub fn new(account_number: impl Into<String>, hash_value: impl Into<String>) -> Self {
33        Self {
34            account_number: account_number.into(),
35            hash_value: hash_value.into(),
36        }
37    }
38}
39
40#[skip_serializing_none]
41#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
42#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
43#[serde(rename_all = "camelCase")]
44#[non_exhaustive]
45pub struct Account {
46    pub securities_account: SecuritiesAccount,
47    #[serde(flatten)]
48    pub extra: Extra,
49}
50
51#[skip_serializing_none]
52#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
53#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
54#[serde(rename_all = "camelCase")]
55#[non_exhaustive]
56pub struct SecuritiesAccount {
57    pub account_number: String,
58    #[serde(rename = "type")]
59    pub account_type: Option<String>,
60    pub round_trips: Option<i32>,
61    pub is_day_trader: Option<bool>,
62    pub is_closing_only_restricted: Option<bool>,
63    pub positions: Option<Vec<Position>>,
64    pub initial_balances: Option<Balance>,
65    pub current_balances: Option<Balance>,
66    pub projected_balances: Option<Balance>,
67    #[serde(flatten)]
68    pub extra: Extra,
69}
70
71#[skip_serializing_none]
72#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
73#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
74#[serde(rename_all = "camelCase")]
75#[non_exhaustive]
76pub struct Position {
77    pub short_quantity: Option<f64>,
78    pub average_price: Option<f64>,
79    pub current_day_cost: Option<f64>,
80    pub current_day_profit_loss: Option<f64>,
81    pub current_day_profit_loss_percentage: Option<f64>,
82    pub long_quantity: Option<f64>,
83    pub settled_long_quantity: Option<f64>,
84    pub settled_short_quantity: Option<f64>,
85    pub instrument: Option<AccountsInstrument>,
86    pub market_value: Option<f64>,
87    pub maintenance_requirement: Option<f64>,
88    pub average_long_price: Option<f64>,
89    pub average_short_price: Option<f64>,
90    pub tax_lot_average_long_price: Option<f64>,
91    pub tax_lot_average_short_price: Option<f64>,
92    pub long_open_profit_loss: Option<f64>,
93    pub short_open_profit_loss: Option<f64>,
94    pub previous_session_long_quantity: Option<f64>,
95    pub previous_session_short_quantity: Option<f64>,
96    pub current_cost: Option<f64>,
97    #[serde(flatten)]
98    pub extra: Extra,
99}
100
101/// One of an account's three balance blocks. Margin and cash accounts
102/// report different fields, and the initial, current and projected blocks
103/// each carry a subset, so everything is optional: a cash account's
104/// spending power is `cash_available_for_trading`, a margin account's is
105/// `buying_power` and `available_funds`.
106#[skip_serializing_none]
107#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
108#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
109#[serde(rename_all = "camelCase")]
110#[non_exhaustive]
111pub struct Balance {
112    pub accrued_interest: Option<f64>,
113    pub cash_balance: Option<f64>,
114    pub cash_receipts: Option<f64>,
115    pub long_option_market_value: Option<f64>,
116    pub liquidation_value: Option<f64>,
117    pub long_market_value: Option<f64>,
118    pub money_market_fund: Option<f64>,
119    pub savings: Option<f64>,
120    pub short_market_value: Option<f64>,
121    pub pending_deposits: Option<f64>,
122    pub available_funds: Option<f64>,
123    pub available_funds_non_marginable_trade: Option<f64>,
124    pub buying_power: Option<f64>,
125    pub buying_power_non_marginable_trade: Option<f64>,
126    pub day_trading_buying_power: Option<f64>,
127    pub day_trading_buying_power_call: Option<f64>,
128    pub equity: Option<f64>,
129    pub equity_percentage: Option<f64>,
130    pub long_margin_value: Option<f64>,
131    pub maintenance_call: Option<f64>,
132    pub maintenance_requirement: Option<f64>,
133    pub margin: Option<f64>,
134    pub margin_equity: Option<f64>,
135    pub reg_t_call: Option<f64>,
136    pub short_balance: Option<f64>,
137    pub short_margin_value: Option<f64>,
138    pub short_option_market_value: Option<f64>,
139    pub sma: Option<f64>,
140    pub mutual_fund_value: Option<f64>,
141    pub bond_value: Option<f64>,
142    pub margin_balance: Option<f64>,
143    pub long_stock_value: Option<f64>,
144    pub short_stock_value: Option<f64>,
145    pub account_value: Option<f64>,
146    pub day_trading_equity_call: Option<f64>,
147    /// Projected block of a margin account.
148    pub stock_buying_power: Option<f64>,
149    /// Projected block: the account is in a margin or cash call.
150    pub is_in_call: Option<bool>,
151    /// Cash accounts (and a margin account's initial block).
152    pub cash_available_for_trading: Option<f64>,
153    /// Cash accounts.
154    pub cash_available_for_withdrawal: Option<f64>,
155    /// Cash accounts.
156    pub cash_call: Option<f64>,
157    /// Cash accounts.
158    pub cash_debit_call_value: Option<f64>,
159    /// Cash accounts.
160    pub long_non_marginable_market_value: Option<f64>,
161    /// Cash accounts.
162    pub total_cash: Option<f64>,
163    /// Cash accounts.
164    pub unsettled_cash: Option<f64>,
165    #[serde(flatten)]
166    pub extra: Extra,
167}
168
169#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
170#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
171#[serde(rename_all = "camelCase")]
172#[non_exhaustive]
173pub struct AccountsResponse {
174    pub accounts: Vec<Account>,
175}
176
177#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
178#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
179#[serde(rename_all = "camelCase")]
180#[non_exhaustive]
181pub struct UserPreferences {
182    pub accounts: Option<Vec<AccountPreferences>>,
183    pub streamer_info: Option<Vec<StreamerInfo>>,
184    pub offers: Option<Vec<Offer>>,
185    #[serde(flatten)]
186    pub extra: Extra,
187}
188
189#[skip_serializing_none]
190#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
191#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
192#[serde(rename_all = "camelCase")]
193#[non_exhaustive]
194pub struct AccountPreferences {
195    pub account_number: Option<String>,
196    pub primary_account: Option<bool>,
197    #[serde(rename = "type")]
198    pub account_type: Option<String>,
199    pub nick_name: Option<String>,
200    pub account_color: Option<String>,
201    pub display_acct_id: Option<String>,
202    pub auto_position_effect: Option<bool>,
203    #[serde(flatten)]
204    pub extra: Extra,
205}
206
207#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
208#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
209#[serde(rename_all = "camelCase")]
210#[non_exhaustive]
211pub struct StreamerInfo {
212    pub streamer_socket_url: String,
213    pub schwab_client_customer_id: String,
214    pub schwab_client_correl_id: String,
215    pub schwab_client_channel: String,
216    pub schwab_client_function_id: String,
217}
218
219#[skip_serializing_none]
220#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
221#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
222#[serde(rename_all = "camelCase")]
223#[non_exhaustive]
224pub struct Offer {
225    pub level2_permissions: Option<bool>,
226    pub mkt_data_permission: Option<String>,
227    #[serde(flatten)]
228    pub extra: Extra,
229}