schwab 0.3.1

Unofficial Rust client library for the Schwab API, unaffiliated with Schwab brokerage or thinkorswim
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Equity order builder using [`schwab::OrderBuilder`].
//!
//! Builds single-leg equity orders for four actions (buy, sell, sell-short,
//! buy-to-cover). Order type is inferred from the presence of `price` and
//! `stop` arguments rather than an explicit enum.

use crate::auth;
use crate::cli::EquityOrderArgs;
use crate::error::AppError;
use crate::order::workflow;
use crate::shared::{DurationChoice, SessionChoice, to_number};

// ---------------------------------------------------------------------------
// Action enum (local, independent of CLI layer)
// ---------------------------------------------------------------------------

/// Equity order action.
///
/// Each variant hardcodes the Schwab [`schwab::Instruction`] to prevent
/// accidental trade reversal.
#[derive(Debug, Clone, Copy)]
pub enum EquityActionKind {
    /// Buy shares (long entry).
    Buy,
    /// Sell shares (long exit).
    Sell,
    /// Sell short (short entry).
    SellShort,
    /// Buy to cover (short exit).
    BuyToCover,
}

// ---------------------------------------------------------------------------
// Order builder
// ---------------------------------------------------------------------------

/// Builds a [`schwab::OrderBuilder`] for an equity order.
///
/// Order type is inferred from flag presence:
/// - Both `None` : market
/// - `price` only : limit
/// - `stop` only  : stop
/// - Both `Some`  : stop-limit
///
/// Buy and Sell use high-level `OrderBuilder` helpers. SellShort and
/// BuyToCover use lower-level methods with an explicit instruction.
///
/// # Errors
///
/// Returns [`AppError::OrderValidation`] when quantity or prices are
/// non-positive, or when number conversion fails.
#[must_use = "returns the built order, does not place it"]
pub fn build_equity_order(
    action: &EquityActionKind,
    symbol: &str,
    qty: f64,
    price: Option<f64>,
    stop: Option<f64>,
    session: SessionChoice,
    duration: DurationChoice,
) -> Result<schwab::OrderBuilder, AppError> {
    // --- validation ---
    if qty <= 0.0 {
        return Err(AppError::OrderValidation(
            "quantity must be positive".to_string(),
        ));
    }
    if price.is_some_and(|p| p <= 0.0) {
        return Err(AppError::OrderValidation(
            "price must be positive".to_string(),
        ));
    }
    if stop.is_some_and(|s| s <= 0.0) {
        return Err(AppError::OrderValidation(
            "stop price must be positive".to_string(),
        ));
    }

    // --- number conversion ---
    let qty_num = to_number(qty)?;
    let price_num = price.map(to_number).transpose()?;
    let stop_num = stop.map(to_number).transpose()?;

    // --- build order ---
    let base = match action {
        EquityActionKind::Buy => match (price_num, stop_num) {
            (None, None) => schwab::OrderBuilder::market_buy(symbol, qty_num),
            (Some(p), None) => schwab::OrderBuilder::limit_buy(symbol, qty_num, p),
            (None, Some(s)) => schwab::OrderBuilder::stop_buy(symbol, qty_num, s),
            (Some(p), Some(s)) => schwab::OrderBuilder::stop_limit_buy(symbol, qty_num, p, s),
        },
        EquityActionKind::Sell => match (price_num, stop_num) {
            (None, None) => schwab::OrderBuilder::market_sell(symbol, qty_num),
            (Some(p), None) => schwab::OrderBuilder::limit_sell(symbol, qty_num, p),
            (None, Some(s)) => schwab::OrderBuilder::stop_sell(symbol, qty_num, s),
            (Some(p), Some(s)) => schwab::OrderBuilder::stop_limit_sell(symbol, qty_num, p, s),
        },
        EquityActionKind::SellShort => {
            let inst = schwab::Instruction::SellShort;
            match (price_num, stop_num) {
                (None, None) => schwab::OrderBuilder::equity_market(symbol, inst, qty_num),
                (Some(p), None) => schwab::OrderBuilder::equity_limit(symbol, inst, qty_num, p),
                (None, Some(s)) => schwab::OrderBuilder::equity_stop(symbol, inst, qty_num, s),
                (Some(p), Some(s)) => {
                    schwab::OrderBuilder::equity_stop_limit(symbol, inst, qty_num, p, s)
                }
            }
        }
        EquityActionKind::BuyToCover => {
            let inst = schwab::Instruction::BuyToCover;
            match (price_num, stop_num) {
                (None, None) => schwab::OrderBuilder::equity_market(symbol, inst, qty_num),
                (Some(p), None) => schwab::OrderBuilder::equity_limit(symbol, inst, qty_num, p),
                (None, Some(s)) => schwab::OrderBuilder::equity_stop(symbol, inst, qty_num, s),
                (Some(p), Some(s)) => {
                    schwab::OrderBuilder::equity_stop_limit(symbol, inst, qty_num, p, s)
                }
            }
        }
    };

    Ok(base.session(session.into()).duration(duration.into()))
}

// ---------------------------------------------------------------------------
// Execute
// ---------------------------------------------------------------------------

/// Executes an equity order workflow.
///
/// Builds the order, determines the workflow mode from shared order flags, and
/// dispatches dry-run, preview, saved preview, or placement handling.
///
/// # Errors
///
pub async fn execute_equity(
    action: EquityActionKind,
    args: EquityOrderArgs,
) -> Result<serde_json::Value, AppError> {
    let order = build_equity_order(
        &action,
        &args.symbol,
        args.quantity,
        args.price,
        args.stop,
        args.common.session,
        args.common.duration,
    )?;
    let mode = workflow::determine_mode(
        args.common.account,
        args.common.save_preview,
        args.common.preview_first,
    )?;
    if let workflow::OrderMode::DryRun = &mode {
        return Ok(serde_json::to_value(&order)?);
    }

    let client = auth::provider()?.client().await?;
    workflow::execute_order(&client, &order, mode, "order equity").await
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::shared::{DurationChoice, SessionChoice};

    /// Helper: build and serialize to JSON for assertion.
    fn build_json(
        action: &EquityActionKind,
        symbol: &str,
        qty: f64,
        price: Option<f64>,
        stop: Option<f64>,
    ) -> serde_json::Value {
        let order = build_equity_order(
            action,
            symbol,
            qty,
            price,
            stop,
            SessionChoice::Normal,
            DurationChoice::Day,
        )
        .expect("build should succeed");
        serde_json::to_value(&order).expect("serialize should succeed")
    }

    #[test]
    fn buy_market_produces_correct_order_type() {
        let json = build_json(&EquityActionKind::Buy, "AAPL", 10.0, None, None);
        assert_eq!(json["orderType"], "MARKET");
        assert_eq!(json["orderLegCollection"][0]["instruction"], "BUY");
        assert_eq!(
            json["orderLegCollection"][0]["instrument"]["symbol"],
            "AAPL"
        );
    }

    #[test]
    fn buy_limit_sets_price() {
        let json = build_json(&EquityActionKind::Buy, "MSFT", 5.0, Some(150.0), None);
        assert_eq!(json["orderType"], "LIMIT");
        assert_eq!(json["orderLegCollection"][0]["instruction"], "BUY");
        assert!(json["price"].as_f64().is_some() || json["price"].as_str().is_some());
    }

    #[test]
    fn sell_stop_limit_sets_both_prices() {
        let json = build_json(
            &EquityActionKind::Sell,
            "GOOG",
            3.0,
            Some(100.0),
            Some(95.0),
        );
        assert_eq!(json["orderType"], "STOP_LIMIT");
        assert_eq!(json["orderLegCollection"][0]["instruction"], "SELL");
    }

    #[test]
    fn sell_short_uses_sell_short_instruction() {
        let json = build_json(&EquityActionKind::SellShort, "AAPL", 5.0, Some(200.0), None);
        assert_eq!(json["orderLegCollection"][0]["instruction"], "SELL_SHORT");
        assert_eq!(json["orderType"], "LIMIT");
    }

    #[test]
    fn buy_to_cover_uses_correct_instruction() {
        let json = build_json(
            &EquityActionKind::BuyToCover,
            "TSLA",
            2.0,
            None,
            Some(180.0),
        );
        assert_eq!(json["orderLegCollection"][0]["instruction"], "BUY_TO_COVER");
        assert_eq!(json["orderType"], "STOP");
    }

    #[test]
    fn zero_quantity_rejected() {
        let err = build_equity_order(
            &EquityActionKind::Buy,
            "AAPL",
            0.0,
            None,
            None,
            SessionChoice::Normal,
            DurationChoice::Day,
        )
        .unwrap_err();
        assert!(err.to_string().contains("quantity must be positive"));
    }

    #[test]
    fn negative_price_rejected() {
        let err = build_equity_order(
            &EquityActionKind::Sell,
            "AAPL",
            10.0,
            Some(-5.0),
            None,
            SessionChoice::Normal,
            DurationChoice::Day,
        )
        .unwrap_err();
        assert!(err.to_string().contains("price must be positive"));
    }

    #[test]
    fn negative_stop_rejected() {
        let err = build_equity_order(
            &EquityActionKind::Buy,
            "SPY",
            1.0,
            None,
            Some(-10.0),
            SessionChoice::Normal,
            DurationChoice::Day,
        )
        .unwrap_err();
        assert!(err.to_string().contains("stop price must be positive"));
    }

    #[test]
    fn session_and_duration_applied() {
        let order = build_equity_order(
            &EquityActionKind::Buy,
            "AAPL",
            10.0,
            Some(150.0),
            None,
            SessionChoice::Am,
            DurationChoice::GoodTillCancel,
        )
        .expect("build should succeed");
        let json = serde_json::to_value(&order).expect("serialize");
        assert_eq!(json["session"], "AM");
        assert_eq!(json["duration"], "GOOD_TILL_CANCEL");
    }

    #[test]
    fn buy_stop_produces_stop_order() {
        let json = build_json(&EquityActionKind::Buy, "SPY", 10.0, None, Some(400.0));
        assert_eq!(json["orderType"], "STOP");
        assert_eq!(json["orderLegCollection"][0]["instruction"], "BUY");
    }

    #[test]
    fn buy_stop_limit_produces_stop_limit_order() {
        let json = build_json(
            &EquityActionKind::Buy,
            "SPY",
            10.0,
            Some(405.0),
            Some(400.0),
        );
        assert_eq!(json["orderType"], "STOP_LIMIT");
        assert_eq!(json["orderLegCollection"][0]["instruction"], "BUY");
    }

    #[test]
    fn sell_market_produces_market_order() {
        let json = build_json(&EquityActionKind::Sell, "AAPL", 5.0, None, None);
        assert_eq!(json["orderType"], "MARKET");
        assert_eq!(json["orderLegCollection"][0]["instruction"], "SELL");
    }

    #[test]
    fn sell_limit_sets_price() {
        let json = build_json(&EquityActionKind::Sell, "MSFT", 3.0, Some(100.0), None);
        assert_eq!(json["orderType"], "LIMIT");
        assert_eq!(json["orderLegCollection"][0]["instruction"], "SELL");
    }

    #[test]
    fn sell_stop_sets_stop_price() {
        let json = build_json(&EquityActionKind::Sell, "GOOG", 2.0, None, Some(150.0));
        assert_eq!(json["orderType"], "STOP");
        assert_eq!(json["orderLegCollection"][0]["instruction"], "SELL");
    }

    #[test]
    fn sell_short_market_produces_market_order() {
        let json = build_json(&EquityActionKind::SellShort, "TSLA", 10.0, None, None);
        assert_eq!(json["orderType"], "MARKET");
        assert_eq!(json["orderLegCollection"][0]["instruction"], "SELL_SHORT");
    }

    #[test]
    fn sell_short_stop_sets_stop_price() {
        let json = build_json(&EquityActionKind::SellShort, "SPY", 5.0, None, Some(380.0));
        assert_eq!(json["orderType"], "STOP");
        assert_eq!(json["orderLegCollection"][0]["instruction"], "SELL_SHORT");
    }

    #[test]
    fn sell_short_stop_limit_sets_both_prices() {
        let json = build_json(
            &EquityActionKind::SellShort,
            "QQQ",
            3.0,
            Some(350.0),
            Some(345.0),
        );
        assert_eq!(json["orderType"], "STOP_LIMIT");
        assert_eq!(json["orderLegCollection"][0]["instruction"], "SELL_SHORT");
    }

    #[test]
    fn buy_to_cover_market_produces_market_order() {
        let json = build_json(&EquityActionKind::BuyToCover, "AAPL", 8.0, None, None);
        assert_eq!(json["orderType"], "MARKET");
        assert_eq!(json["orderLegCollection"][0]["instruction"], "BUY_TO_COVER");
    }

    #[test]
    fn buy_to_cover_limit_sets_price() {
        let json = build_json(
            &EquityActionKind::BuyToCover,
            "GOOG",
            4.0,
            Some(120.0),
            None,
        );
        assert_eq!(json["orderType"], "LIMIT");
        assert_eq!(json["orderLegCollection"][0]["instruction"], "BUY_TO_COVER");
    }

    #[test]
    fn buy_to_cover_stop_limit_sets_both_prices() {
        let json = build_json(
            &EquityActionKind::BuyToCover,
            "MSFT",
            6.0,
            Some(200.0),
            Some(195.0),
        );
        assert_eq!(json["orderType"], "STOP_LIMIT");
        assert_eq!(json["orderLegCollection"][0]["instruction"], "BUY_TO_COVER");
    }

    #[tokio::test]
    async fn execute_equity_dry_run_returns_json() {
        use crate::cli::{CommonOrderArgs, EquityOrderArgs};

        let result = execute_equity(
            EquityActionKind::Buy,
            EquityOrderArgs {
                symbol: "AAPL".to_string(),
                quantity: 10.0,
                price: Some(150.0),
                stop: None,
                common: CommonOrderArgs {
                    account: None,
                    session: SessionChoice::Normal,
                    duration: DurationChoice::Day,
                    save_preview: false,
                    preview_first: false,
                },
            },
        )
        .await;
        assert!(result.is_ok());
        let json = result.unwrap();
        assert_eq!(json["orderType"], "LIMIT");
        assert_eq!(json["orderLegCollection"][0]["instruction"], "BUY");
    }
}