schwab-api-cli 0.1.0

schwab-api-cli — agent-first CLI for Charles Schwab Trader API (experimental — use at your own risk)
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
use std::path::PathBuf;

use clap::{Parser, Subcommand};

use crate::disclaimer::HELP_DISCLAIMER;
use crate::mode::CliMode;
use crate::output::OutputFormat;

#[derive(Debug, Parser)]
#[command(
    name = "schwab",
    version,
    about = "Agent-first CLI for Charles Schwab Trader API (experimental — use at your own risk)",
    long_about = "Schwab Trader API CLI (Accounts and Trading Production).\n\n\
        ⚠️  EXPERIMENTAL — USE AT YOUR OWN RISK. Can submit real trades.\n\
        Run `schwab disclaimer show` and `schwab disclaimer accept --yes` before live trading.\n\n\
        AGENTS: Prefer --mode agent (default). Discover commands with:\n\
          schwab --help --json\n\
          schwab capabilities --json\n\
          schwab env schema --json\n\
          schwab instructions --json\n\n\
        HUMANS: Use --mode human for guided prompts when arguments are omitted.",
    after_help = HELP_DISCLAIMER
)]
pub struct Cli {
    /// Operating mode: agent (structured, default) or human (interactive prompts)
    #[arg(long, env = "SCHWAB_MODE", default_value = "agent")]
    pub mode: CliMode,

    /// Output format
    #[arg(long, env = "SCHWAB_OUTPUT", default_value = "pretty")]
    pub output: OutputFormat,

    /// Shorthand for --output json
    #[arg(long, short = 'j', global = true)]
    pub json: bool,

    /// Shorthand for --output md
    #[arg(long, global = true)]
    pub md: bool,

    /// Auto-confirm mutations (required in non-interactive agent mode)
    #[arg(long, global = true)]
    pub yes: bool,

    /// Validate mutation without executing
    #[arg(long, global = true)]
    pub dry_run: bool,

    /// Trusted agent mode: allow autonomous trading with --trust --yes (safety.json limits still enforced)
    #[arg(long, global = true)]
    pub trust: bool,

    /// Emit full command tree as JSON (agent discovery)
    #[arg(long, global = true)]
    pub help_json: bool,

    #[command(subcommand)]
    pub command: Option<Commands>,
}

#[derive(Debug, Subcommand)]
pub enum Commands {
    /// Machine-readable command catalog for agents
    Capabilities,

    /// Environment variable schema and precedence
    Env {
        #[command(subcommand)]
        command: EnvCommands,
    },

    /// Agent system prompt / tool-use instructions
    Instructions,

    /// Trading risk disclaimer (required before live trades)
    Disclaimer {
        #[command(subcommand)]
        command: DisclaimerCommands,
    },

    /// OAuth authentication and token management
    Auth {
        #[command(subcommand)]
        command: AuthCommands,
    },

    /// Account numbers, balances, and positions
    Accounts {
        #[command(subcommand)]
        command: AccountsCommands,
    },

    /// Order entry, preview, cancel, replace
    Orders {
        #[command(subcommand)]
        command: OrdersCommands,
    },

    /// Transaction history
    Transactions {
        #[command(subcommand)]
        command: TransactionsCommands,
    },

    /// User preferences and streamer metadata
    User {
        #[command(subcommand)]
        command: UserCommands,
    },

    /// Portfolio summary across linked accounts
    Portfolio {
        #[command(subcommand)]
        command: PortfolioCommands,
    },

    /// Buy or sell equities with safety guardrails
    Trade {
        #[command(subcommand)]
        command: TradeCommands,
    },

    /// Safety limits config (safety.json) for agent trading guardrails
    Safety {
        #[command(subcommand)]
        command: SafetyCommands,
    },

    /// Multi-step trade plans (YAML/JSON) for LLM-generated rebalances
    Plan {
        #[command(subcommand)]
        command: PlanCommands,
    },

    /// Market Data API — quotes, history, instruments, hours
    Market {
        #[command(subcommand)]
        command: MarketCommands,
    },

    /// Options chain, positions, and strategy orders (vertical, iron condor)
    Options {
        #[command(subcommand)]
        command: OptionsCommands,
    },

    /// Long-running options agent driven by rules.yaml
    Agent {
        #[command(subcommand)]
        command: AgentCommands,
    },
}

#[derive(Debug, Subcommand)]
pub enum EnvCommands {
    /// JSON schema of supported environment variables
    Schema,
}

#[derive(Debug, Subcommand)]
pub enum AuthCommands {
    /// Start OAuth login (opens browser, captures redirect code)
    Login {
        /// Authorization code if already obtained (skips browser)
        #[arg(long)]
        code: Option<String>,
    },
    /// Show token status
    Status,
    /// Refresh access token using refresh token
    Refresh,
    /// Remove stored tokens
    Logout,
}

#[derive(Debug, Subcommand)]
pub enum AccountsCommands {
    /// GET /accounts/accountNumbers
    Numbers,
    /// GET /accounts
    List {
        #[arg(long)]
        fields: Option<String>,
    },
    /// GET /accounts/{accountNumber}
    Get {
        account_number: String,
        #[arg(long)]
        fields: Option<String>,
    },
}

#[derive(Debug, Subcommand)]
pub enum OrdersCommands {
    /// JSON Schema + Schwab order examples for agents
    Schema,
    /// Validate order JSON (shape + safety.json limits)
    Validate {
        /// Path to order JSON file or inline JSON string
        #[arg(long)]
        order: String,
        /// Account hash for equity % checks (optional)
        #[arg(long)]
        account_number: Option<String>,
    },
    /// GET /accounts/{accountNumber}/orders
    List {
        account_number: String,
        #[arg(long)]
        from_entered_time: Option<String>,
        #[arg(long)]
        to_entered_time: Option<String>,
        #[arg(long)]
        status: Option<String>,
        #[arg(long)]
        max_results: Option<String>,
    },
    /// GET /orders (all linked accounts)
    All {
        #[arg(long)]
        from_entered_time: Option<String>,
        #[arg(long)]
        to_entered_time: Option<String>,
        #[arg(long)]
        status: Option<String>,
        #[arg(long)]
        max_results: Option<String>,
    },
    /// GET /accounts/{accountNumber}/orders/{orderId}
    Get {
        account_number: String,
        order_id: String,
    },
    /// Poll order status until filled, terminal, or timeout
    Wait {
        account_number: String,
        order_id: String,
        /// Wait condition: accepted | filled | terminal
        #[arg(long, default_value = "filled")]
        until: String,
        /// Max seconds to poll before giving up
        #[arg(long, default_value = "3600")]
        timeout_seconds: u64,
        /// Seconds between status polls
        #[arg(long, default_value = "5")]
        interval_seconds: u64,
        /// Treat partial fill as success when waiting for filled
        #[arg(long, default_value = "false")]
        proceed_on_partial_fill: bool,
    },
    /// POST /accounts/{accountNumber}/orders
    Place {
        account_number: String,
        /// Path to order JSON file or inline JSON string
        #[arg(long)]
        order: String,
    },
    /// POST /accounts/{accountNumber}/previewOrder
    Preview {
        account_number: String,
        #[arg(long)]
        order: String,
    },
    /// DELETE /accounts/{accountNumber}/orders/{orderId}
    Cancel {
        account_number: String,
        order_id: String,
    },
    /// PUT /accounts/{accountNumber}/orders/{orderId}
    Replace {
        account_number: String,
        order_id: String,
        #[arg(long)]
        order: String,
    },
}

#[derive(Debug, Subcommand)]
pub enum TransactionsCommands {
    /// GET /accounts/{accountNumber}/transactions
    List {
        account_number: String,
        #[arg(long)]
        start_date: Option<String>,
        #[arg(long)]
        end_date: Option<String>,
        #[arg(long)]
        types: Option<String>,
        #[arg(long)]
        symbol: Option<String>,
    },
    /// GET /accounts/{accountNumber}/transactions/{transactionId}
    Get {
        account_number: String,
        transaction_id: String,
    },
}

#[derive(Debug, Subcommand)]
pub enum UserCommands {
    /// GET /userPreference
    Preference,
}

#[derive(Debug, Subcommand)]
pub enum PortfolioCommands {
    /// Aggregate positions and equity across all linked accounts
    Summary,
    /// Cash available for trading on one account (required before buys)
    BuyingPower {
        /// Account hash from `schwab accounts numbers`
        #[arg(long)]
        account_number: String,
    },
}

#[derive(Debug, Subcommand)]
pub enum TradeCommands {
    /// Buy shares (equity, single-leg)
    Buy {
        /// Account hash from `schwab accounts numbers`
        #[arg(long)]
        account_number: String,
        /// Ticker symbol
        #[arg(long)]
        symbol: String,
        /// Share quantity
        #[arg(long)]
        quantity: f64,
        /// Order type: market | limit
        #[arg(long, default_value = "market")]
        order_type: String,
        /// Limit price (required for limit orders)
        #[arg(long)]
        price: Option<f64>,
        /// Duration: day | gtc | fok
        #[arg(long)]
        duration: Option<String>,
        /// Session: normal | am | pm | seamless
        #[arg(long)]
        session: Option<String>,
    },
    /// Sell shares (equity, single-leg)
    Sell {
        #[arg(long)]
        account_number: String,
        #[arg(long)]
        symbol: String,
        #[arg(long)]
        quantity: f64,
        #[arg(long, default_value = "market")]
        order_type: String,
        #[arg(long)]
        price: Option<f64>,
        #[arg(long)]
        duration: Option<String>,
        #[arg(long)]
        session: Option<String>,
    },
}

#[derive(Debug, Subcommand)]
pub enum SafetyCommands {
    /// Show active safety.json limits and config path
    Show,
    /// Write default safety.json to the config directory
    Init,
    /// Print safety.json path only
    Path,
}

#[derive(Debug, Subcommand)]
pub enum DisclaimerCommands {
    /// Print the full trading risk disclaimer
    Show,
    /// Record acceptance (required before live trading; use --yes in agent mode)
    Accept,
    /// Show whether disclaimer has been accepted on this machine
    Status,
}

#[derive(Debug, Subcommand)]
pub enum PlanCommands {
    /// JSON Schema for trade plan files
    Schema,
    /// LLM prompt and workflow for generating trade plans
    Prompt,
    /// Validate plan structure and safety limits
    Validate {
        /// Path to .yaml, .yml, or .json trade plan
        file: PathBuf,
    },
    /// Show parsed plan contents
    Show {
        file: PathBuf,
    },
    /// Execute plan steps (requires --trust --yes in agent mode, or --dry-run)
    Run {
        file: PathBuf,
        /// Run only this step id
        #[arg(long)]
        step: Option<String>,
        /// Run from this step id through the end
        #[arg(long)]
        from_step: Option<String>,
    },
}

#[derive(Debug, Subcommand)]
pub enum MarketCommands {
    /// Agent dossier — quote + fundamentals + price context + research hints
    Info {
        /// One symbol or comma-separated list (e.g. SGOV or SGOV,JPST,AAPL)
        symbol: String,
        /// Skip price history fetch
        #[arg(long)]
        no_history: bool,
        #[arg(long, default_value = "month")]
        history_period_type: String,
        #[arg(long, default_value_t = 1)]
        history_period: u32,
        #[arg(long, default_value = "daily")]
        history_frequency_type: String,
    },
    /// GET /quotes — quotes for multiple symbols (comma-separated)
    Quotes {
        /// Comma-separated tickers (e.g. SGOV,JPST,AAPL)
        #[arg(long)]
        symbols: String,
        /// Quote fields: all, quote, fundamental, reference, extended, regular
        #[arg(long)]
        fields: Option<String>,
        #[arg(long)]
        indicative: Option<bool>,
    },
    /// GET /{symbol}/quotes — single symbol quote
    Quote {
        symbol: String,
        #[arg(long)]
        fields: Option<String>,
        #[arg(long)]
        indicative: Option<bool>,
    },
    /// GET /pricehistory — OHLCV candles
    History {
        symbol: String,
        #[arg(long)]
        period_type: Option<String>,
        #[arg(long)]
        period: Option<u32>,
        #[arg(long)]
        frequency_type: Option<String>,
        #[arg(long)]
        frequency: Option<u32>,
        /// Epoch milliseconds
        #[arg(long)]
        start_date: Option<i64>,
        #[arg(long)]
        end_date: Option<i64>,
        #[arg(long)]
        need_extended_hours_data: Option<bool>,
        #[arg(long)]
        need_previous_close: Option<bool>,
    },
    /// GET /instruments — symbol search / fundamentals (company info)
    Instrument {
        /// Symbol or search text
        #[arg(long)]
        symbol: String,
        /// Projection: symbol-search, fundamental, search, etc.
        #[arg(long, default_value = "fundamental")]
        projection: String,
    },
    /// GET /instruments/{cusip}
    InstrumentByCusip {
        cusip: String,
    },
    /// GET /markets — hours for multiple markets (comma-separated)
    Hours {
        /// equity, option, bond, future, forex (comma-separated)
        #[arg(long, default_value = "equity")]
        markets: String,
        /// YYYY-MM-DD (defaults to today)
        #[arg(long)]
        date: Option<String>,
    },
    /// GET /markets/{market_id} — hours for one market
    HoursFor {
        /// equity | option | bond | future | forex
        market: String,
        #[arg(long)]
        date: Option<String>,
    },
}

#[derive(Debug, Subcommand)]
pub enum OptionsCommands {
    /// GET /chains — option chain for an underlying
    Chain {
        #[arg(long)]
        symbol: String,
        /// CALL | PUT | ALL
        #[arg(long, name = "type")]
        contract_type: Option<String>,
        #[arg(long)]
        strike_count: Option<u32>,
        #[arg(long)]
        from_date: Option<String>,
        #[arg(long)]
        to_date: Option<String>,
    },
    /// List option positions (grouped into spreads where possible)
    Positions {
        #[arg(long)]
        account_number: Option<String>,
    },
    /// Strategy templates and symbology for agents
    Schema,
    /// Validate strategy params + safety.json
    Validate {
        #[arg(long)]
        strategy: String,
        /// JSON string or path to .json/.yaml params file
        #[arg(long)]
        params: String,
        #[arg(long)]
        account_number: Option<String>,
        #[arg(long, default_value = "margin")]
        account_type: Option<String>,
    },
    /// Preview strategy order at Schwab
    Preview {
        #[arg(long)]
        account_number: String,
        #[arg(long)]
        strategy: String,
        #[arg(long)]
        params: String,
    },
    /// Open a new options position (vertical or iron_condor)
    Open {
        #[arg(long)]
        account_number: String,
        #[arg(long)]
        strategy: String,
        #[arg(long)]
        params: String,
    },
    /// Close an open spread by position group id
    Close {
        #[arg(long)]
        account_number: String,
        #[arg(long)]
        position_id: String,
    },
}

#[derive(Debug, Subcommand)]
pub enum AgentCommands {
    /// JSON Schema for rules.yaml
    Schema,
    /// Validate rules.yaml structure
    Validate {
        file: PathBuf,
    },
    /// Show persisted agent state
    Status {
        #[arg(long)]
        rules_file: Option<PathBuf>,
    },
    /// Run the options agent loop (requires --trust --yes for live trades)
    Run {
        file: PathBuf,
        /// Execute a single tick then exit
        #[arg(long)]
        once: bool,
        /// Detach as a background daemon (writes agent.pid and agent.log next to rules)
        #[arg(long)]
        background: bool,
    },
    /// Stop a background agent started with `agent run --background`
    Stop {
        file: PathBuf,
    },
}

/// Resolved output path for clap parse result.
impl Cli {
    pub fn effective_output(&self) -> OutputFormat {
        if self.json {
            OutputFormat::Json
        } else if self.md {
            OutputFormat::Md
        } else {
            self.output
        }
    }
}