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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
use console::Style;
use serde_json::Value;

/// Human-readable body for agent tick / run-once envelopes (pretty output mode).
pub fn format_tick_data(data: &Value) -> String {
    let mut out = String::new();

    let agent_id = str_field(data, "agent_id").unwrap_or("agent");
    let dry_run = data.get("dry_run").and_then(|v| v.as_bool()).unwrap_or(false);

    let header = Style::new().cyan().bold();
    let _label = Style::new().dim();
    let accent = Style::new().yellow();

    out.push_str(&format!("{}", header.apply_to(agent_id)));
    if dry_run {
        out.push_str(&format!("  {}", accent.apply_to("[dry-run]")));
    }
    if let Some(session) = str_field(data, "session") {
        let session_style = match session {
            "regular" => Style::new().green(),
            "overnight" => Style::new().magenta(),
            "idle" => Style::new().dim(),
            _ => Style::new().dim(),
        };
        out.push_str(&format!("  {}", session_style.apply_to(session)));
    }
    if data.get("at_open").and_then(|v| v.as_bool()).unwrap_or(false) {
        out.push_str(&format!("  {}", accent.apply_to("[at open]")));
    }
    out.push('\n');

    let signals = data.get("signals").and_then(|v| v.as_array());
    let actions = data.get("actions").and_then(|v| v.as_array());
    let skipped = data.get("skipped").and_then(|v| v.as_array());
    let monitored = data.get("monitored_positions").and_then(|v| v.as_array());

    out.push_str(&format_monitored_positions(monitored));

    out.push_str(&format_section("Signals", signals, format_signal));
    out.push_str(&format_section("Actions", actions, format_action));
    out.push_str(&format_skipped(skipped));

    if let Some(llm) = data.get("llm_review") {
        if !llm.is_null() {
            out.push('\n');
            out.push_str(&format_llm_review(llm));
        }
    }

    out.trim_end().to_string()
}

fn format_monitored_positions(monitored: Option<&Vec<Value>>) -> String {
    let Some(arr) = monitored else {
        return String::new();
    };
    if arr.is_empty() {
        return String::new();
    }

    let header = Style::new().bold();
    let hold = Style::new().green();
    let exit = Style::new().yellow();
    let mut out = format!(
        "\n{}\n",
        header.apply_to(format!(
            "Monitoring {} open position{}",
            arr.len(),
            if arr.len() == 1 { "" } else { "s" }
        ))
    );

    for pos in arr {
        out.push_str(&format!("{}\n", format_monitored_position(pos, &hold, &exit)));
    }
    out
}

fn format_monitored_position(pos: &Value, hold: &Style, exit: &Style) -> String {
    let underlying = str_field(pos, "underlying").unwrap_or("?");
    let strategy = str_field(pos, "strategy").unwrap_or("spread");
    let expiry = str_field(pos, "expiry").unwrap_or("?");
    let status = str_field(pos, "status").unwrap_or("holding");

    let credit = pos.get("entry_credit").and_then(|v| v.as_f64());
    let credit_s = credit
        .map(|c| format!("  cr ${c:.2}"))
        .unwrap_or_default();

    let profit = pos.get("profit_pct").and_then(|v| v.as_f64());
    let profit_s = profit
        .map(|p| format!("  P/L {p:+.0}%"))
        .unwrap_or_default();

    let dte = pos
        .get("dte")
        .and_then(|v| v.as_i64().or_else(|| v.as_u64().map(|u| u as i64)));
    let dte_s = dte.map(|d| format!("  {d} DTE")).unwrap_or_default();

    let greeks_s = pos
        .get("market_context")
        .map(format_monitor_greeks_suffix)
        .unwrap_or_default();

    let status_s = if status.starts_with("exit:") {
        exit.apply_to(status).to_string()
    } else {
        hold.apply_to(status).to_string()
    };

    format!(
        "{underlying} {strategy}  exp {expiry}{credit_s}{profit_s}{dte_s}{greeks_s}{status_s}"
    )
}

fn format_monitor_greeks_suffix(ctx: &Value) -> String {
    let mut parts = Vec::new();
    if let Some(px) = ctx.get("underlying_price").and_then(|v| v.as_f64()) {
        parts.push(format!("@${px:.2}"));
    }
    if let (Some(short), Some(long)) = (
        ctx.get("short_strike").and_then(|v| v.as_f64()),
        ctx.get("long_strike").and_then(|v| v.as_f64()),
    ) {
        parts.push(format!("${short:.0}/${long:.0}"));
    }
    if let Some(delta) = ctx.get("short_delta").and_then(|v| v.as_f64()) {
        parts.push(format!("δ {delta:.2}"));
    }
    if let Some(otm) = ctx.get("short_otm_pct").and_then(|v| v.as_f64()) {
        parts.push(format!("{otm:.1}% OTM"));
    }
    if let Some(pop) = ctx
        .get("approx_short_otm_probability_pct")
        .and_then(|v| v.as_f64())
    {
        parts.push(format!("~{pop:.0}% OTM prob"));
    }
    if parts.is_empty() {
        String::new()
    } else {
        format!("  ({})", parts.join(", "))
    }
}

pub fn format_status_data(data: &Value) -> String {
    let mut out = String::new();
    let label = Style::new().dim();

    if let Some(path) = str_field(data, "state_path") {
        out.push_str(&format!("  {} {}\n", label.apply_to("state file:"), path));
    }

    if let Some(state) = data.get("state") {
        for (key, fmt) in [
            ("agent_id", "agent"),
            ("tick_count", "ticks"),
            ("trades_today", "trades today"),
            ("open_positions", "open positions"),
            ("pending_orders", "pending orders"),
            ("last_tick", "last tick"),
            ("last_llm_summary", "last LLM"),
        ] {
            if let Some(v) = state.get(key) {
                if key == "last_llm_summary" {
                    if let Some(summary) = format_llm_summary_line(v) {
                        out.push_str(&format!("  {} {}\n", label.apply_to(fmt), summary));
                    }
                } else {
                    out.push_str(&format!(
                        "  {} {}\n",
                        label.apply_to(fmt),
                        json_scalar(v)
                    ));
                }
            }
        }
    }

    out.trim_end().to_string()
}

pub fn format_validate_data(data: &Value) -> String {
    let label = Style::new().dim();
    let ok = Style::new().green();
    let mut out = String::new();

    out.push_str(&format!(
        "  {} {}\n",
        label.apply_to("valid:"),
        ok.apply_to("yes")
    ));
    for (key, label_text) in [
        ("agent_id", "agent id"),
        ("accounts", "accounts"),
        ("watchlist", "watchlist"),
        ("llm_enabled", "LLM"),
        ("telegram_enabled", "telegram"),
    ] {
        if let Some(v) = data.get(key) {
            out.push_str(&format!(
                "  {} {}\n",
                label.apply_to(label_text),
                json_scalar(v)
            ));
        }
    }
    out.trim_end().to_string()
}

pub fn format_background_data(data: &Value) -> String {
    let label = Style::new().dim();
    let mut out = String::new();
    for (key, label_text) in [
        ("pid", "pid"),
        ("rules", "rules"),
        ("pid_file", "pid file"),
        ("log_file", "log file"),
    ] {
        if let Some(v) = data.get(key) {
            out.push_str(&format!(
                "  {} {}\n",
                label.apply_to(label_text),
                json_scalar(v)
            ));
        }
    }
    out.trim_end().to_string()
}

fn format_section<F>(title: &str, items: Option<&Vec<Value>>, formatter: F) -> String
where
    F: Fn(&Value) -> String,
{
    let Some(arr) = items else {
        return String::new();
    };
    if arr.is_empty() {
        return String::new();
    }
    let header = Style::new().bold();
    let mut out = format!("\n{}\n", header.apply_to(title));
    for item in arr {
        out.push_str(&format!("{}\n", formatter(item)));
    }
    out
}

fn format_skipped(skipped: Option<&Vec<Value>>) -> String {
    let Some(arr) = skipped else {
        return String::new();
    };
    if arr.is_empty() {
        return String::new();
    }
    let header = Style::new().bold();
    let mut out = format!("\n{}\n", header.apply_to("Skipped"));
    for item in arr {
        if let Some(s) = item.as_str() {
            out.push_str(&format!("{s}\n"));
        }
    }
    out
}

fn format_signal(signal: &Value) -> String {
    let kind = str_field(signal, "type").unwrap_or("signal");
    match kind {
        "entry" => format_entry_signal(signal),
        "exit" => {
            let underlying = str_field(signal, "underlying").unwrap_or("?");
            let reason = str_field(signal, "reason").unwrap_or("exit");
            let pos = str_field(signal, "position_id").unwrap_or("");
            let mark = signal.get("mark").and_then(|v| v.as_f64());
            let mark_s = mark
                .map(|m| format!("  mark ${m:.2}"))
                .unwrap_or_default();
            format!("EXIT  {underlying}  {reason}  {pos}{mark_s}")
        }
        _ => compact_json(signal),
    }
}

fn format_entry_signal(signal: &Value) -> String {
    let strategy = str_field(signal, "strategy").unwrap_or("spread");
    let params = signal.get("params");
    let underlying = params
        .and_then(|p| str_field(p, "underlying"))
        .or_else(|| str_field(signal, "underlying"))
        .unwrap_or("?");
    let expiry = params
        .and_then(|p| str_field(p, "expiry"))
        .unwrap_or("");

    let strikes = match strategy {
        "vertical" => {
            let spread_type = params
                .and_then(|p| str_field(p, "type"))
                .unwrap_or("spread");
            let short = params.and_then(|p| p.get("short_strike")).and_then(|v| v.as_f64());
            let long = params.and_then(|p| p.get("long_strike")).and_then(|v| v.as_f64());
            match (short, long) {
                (Some(s), Some(l)) => format!("{spread_type}  ${s:.0}/${l:.0}"),
                _ => spread_type.to_string(),
            }
        }
        "iron_condor" => {
            let ps = params.and_then(|p| p.get("put_short")).and_then(|v| v.as_f64());
            let pl = params.and_then(|p| p.get("put_long")).and_then(|v| v.as_f64());
            let cs = params.and_then(|p| p.get("call_short")).and_then(|v| v.as_f64());
            let cl = params.and_then(|p| p.get("call_long")).and_then(|v| v.as_f64());
            match (ps, pl, cs, cl) {
                (Some(ps), Some(pl), Some(cs), Some(cl)) => {
                    format!("iron condor  puts ${ps:.0}/${pl:.0}  calls ${cs:.0}/${cl:.0}")
                }
                _ => "iron condor".to_string(),
            }
        }
        _ => strategy.to_string(),
    };

    let credit = signal
        .get("estimated_credit")
        .or_else(|| params.and_then(|p| p.get("limit_credit")))
        .and_then(|v| v.as_f64());
    let credit_s = credit
        .map(|c| format!("  ~${c:.2} cr"))
        .unwrap_or_default();

    let ctx = signal.get("market_context");
    let ctx_s = ctx.map(format_market_context_suffix).unwrap_or_default();

    format!(
        "ENTRY  {underlying}  {strikes}  exp {expiry}{credit_s}{ctx_s}"
    )
}

fn format_market_context_suffix(ctx: &Value) -> String {
    let mut parts = Vec::new();
    if let Some(dte) = ctx.get("dte").and_then(|v| v.as_u64()) {
        parts.push(format!("{dte} DTE"));
    }
    if let Some(delta) = ctx.get("short_delta").and_then(|v| v.as_f64()) {
        parts.push(format!("δ {delta:.2}"));
    }
    if let Some(pct) = ctx.get("credit_to_width_pct").and_then(|v| v.as_f64()) {
        parts.push(format!("{pct:.0}% cr/width"));
    }
    if let Some(loss) = ctx.get("max_loss_total_usd").and_then(|v| v.as_f64()) {
        parts.push(format!("max loss ${loss:.0}"));
    }
    if parts.is_empty() {
        String::new()
    } else {
        format!("  ({})", parts.join(", "))
    }
}

fn format_action(action: &Value) -> String {
    if let Some(fill) = str_field(action, "fill_status") {
        return format_order_action(action, &fill);
    }
    if action.get("exit").is_some() {
        let underlying = action
            .pointer("/signal/underlying")
            .and_then(|v| v.as_str())
            .unwrap_or("?");
        let reason = action
            .pointer("/signal/reason")
            .and_then(|v| v.as_str())
            .unwrap_or("exit");
        return format!("EXIT filled  {underlying}  ({reason})");
    }
    if let Some(kind) = str_field(action, "kind") {
        return format!("{kind}  {}", compact_json(action));
    }
    compact_json(action)
}

fn format_order_action(action: &Value, fill_status: &str) -> String {
    let underlying = action
        .pointer("/signal/params/underlying")
        .or_else(|| action.pointer("/signal/underlying"))
        .and_then(|v| v.as_str())
        .unwrap_or("?");
    let strategy = action
        .pointer("/signal/strategy")
        .and_then(|v| v.as_str())
        .unwrap_or("spread");

    let order_id = action
        .pointer("/entry/orderId")
        .or_else(|| action.pointer("/place/orderId"))
        .and_then(|v| v.as_u64())
        .map(|id| format!("  order #{id}"))
        .unwrap_or_default();

    let status = match fill_status {
        "FILLED" => Style::new().green().apply_to("FILLED").to_string(),
        "REJECTED" | "CANCELED" | "EXPIRED" => {
            Style::new().red().apply_to(fill_status).to_string()
        }
        _ => Style::new().yellow().apply_to(fill_status).to_string(),
    };

    let note = action
        .get("note")
        .and_then(|v| v.as_str())
        .map(|n| format!("{n}"))
        .unwrap_or_default();

    format!("ENTRY {status}  {underlying} {strategy}{order_id}{note}")
}

fn format_llm_review(llm: &Value) -> String {
    let header = Style::new().bold();
    let label = Style::new().dim();
    let phase = str_field(llm, "phase").unwrap_or("review");
    let phase_label = match phase {
        "overnight_digest" => "overnight digest",
        other => other,
    };
    let model = str_field(llm, "model").unwrap_or("model");
    let web = llm.get("used_web").and_then(|v| v.as_bool()).unwrap_or(false);
    let web_tag = if web { " + web" } else { "" };

    let mut out = format!(
        "{}\n",
        header.apply_to(format!("LLM {phase_label} ({model}{web_tag})"))
    );

    if let Some(rec) = llm.pointer("/new_entries/recommendation").and_then(|v| v.as_str()) {
        let rec_style = match rec {
            "proceed" => Style::new().green(),
            "defer" | "skip" | "hold" => Style::new().yellow(),
            _ => Style::new().cyan(),
        };
        out.push_str(&format!(
            "  {} {}\n",
            label.apply_to("entries:"),
            rec_style.apply_to(rec)
        ));
    }

    if let Some(reason) = llm
        .pointer("/new_entries/reasoning")
        .and_then(|v| v.as_str())
    {
        if !reason.is_empty() {
            out.push_str(&format!("  {}\n", wrap_text(reason, 76, "  ")));
        }
    }

    if let Some(commentary) = llm.get("market_commentary").and_then(|v| v.as_str()) {
        if !commentary.is_empty() {
            out.push_str(&format!(
                "  {}\n",
                label.apply_to("market:")
            ));
            out.push_str(&format!("  {}\n", wrap_text(commentary, 76, "  ")));
        }
    }

    if let Some(alerts) = llm.get("risk_alerts").and_then(|v| v.as_array()) {
        if !alerts.is_empty() {
            out.push_str(&format!("  {}\n", label.apply_to("alerts:")));
            for alert in alerts {
                if let Some(s) = alert.as_str() {
                    out.push_str(&format!("{s}\n"));
                }
            }
        }
    }

    if let Some(positions) = llm.get("positions").and_then(|v| v.as_array()) {
        if !positions.is_empty() {
            out.push_str(&format!("  {}\n", label.apply_to("positions:")));
            for pos in positions {
                let id = pos.get("position_id").and_then(|v| v.as_str()).unwrap_or("?");
                let rec = pos
                    .get("recommendation")
                    .and_then(|v| v.as_str())
                    .unwrap_or("hold");
                out.push_str(&format!("{id}: {rec}\n"));
            }
        }
    }

    out.trim_end().to_string()
}

fn format_llm_summary_line(v: &Value) -> Option<String> {
    if v.is_null() {
        return None;
    }
    let phase = str_field(v, "phase").unwrap_or("?");
    let rec = v
        .pointer("/new_entries/recommendation")
        .and_then(|r| r.as_str())
        .unwrap_or("");
    Some(format!("{phase} → entries {rec}"))
}

fn wrap_text(text: &str, width: usize, indent: &str) -> String {
    let mut lines = Vec::new();
    let mut current = String::new();
    for word in text.split_whitespace() {
        if current.is_empty() {
            current = word.to_string();
        } else if current.len() + 1 + word.len() <= width {
            current.push(' ');
            current.push_str(word);
        } else {
            lines.push(current);
            current = word.to_string();
        }
    }
    if !current.is_empty() {
        lines.push(current);
    }
    lines
        .into_iter()
        .map(|l| format!("{indent}{l}"))
        .collect::<Vec<_>>()
        .join("\n")
}

fn str_field<'a>(value: &'a Value, key: &str) -> Option<&'a str> {
    value.get(key).and_then(|v| v.as_str())
}

fn json_scalar(v: &Value) -> String {
    match v {
        Value::String(s) => s.clone(),
        Value::Number(n) => n.to_string(),
        Value::Bool(b) => b.to_string(),
        Value::Array(a) => a
            .iter()
            .filter_map(|i| i.as_str())
            .collect::<Vec<_>>()
            .join(", "),
        _ => v.to_string(),
    }
}

fn compact_json(v: &Value) -> String {
    serde_json::to_string(v).unwrap_or_else(|_| "{}".into())
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn format_skipped_only_tick() {
        let data = json!({
            "agent_id": "options-pilot-8709",
            "dry_run": true,
            "signals": [],
            "actions": [],
            "monitored_positions": [{
                "underlying": "IWM",
                "strategy": "vertical",
                "expiry": "2026-07-31",
                "entry_credit": 0.25,
                "profit_pct": 8.0,
                "dte": 36,
                "status": "holding"
            }],
            "skipped": ["new entries paused — max_trades_per_day (1) reached"],
            "llm_review": null
        });
        let out = format_tick_data(&data);
        assert!(out.contains("options-pilot-8709"));
        assert!(out.contains("Monitoring 1 open position"));
        assert!(out.contains("IWM"));
        assert!(out.contains("max_trades_per_day"));
        assert!(!out.contains("\"signals\""));
    }

    #[test]
    fn format_entry_signal_with_context() {
        let data = json!({
            "agent_id": "options-pilot-8709",
            "dry_run": false,
            "monitored_positions": [],
            "signals": [{
                "type": "entry",
                "strategy": "vertical",
                "params": {
                    "underlying": "IWM",
                    "expiry": "2025-07-18",
                    "type": "put_credit",
                    "short_strike": 282.0,
                    "long_strike": 280.0
                },
                "estimated_credit": 0.26,
                "market_context": {
                    "dte": 36,
                    "short_delta": -0.18,
                    "credit_to_width_pct": 13.0,
                    "max_loss_total_usd": 174.0
                }
            }],
            "actions": [],
            "skipped": [],
            "llm_review": {
                "phase": "selection",
                "model": "anthropic/claude-sonnet-4",
                "used_web": false,
                "new_entries": { "recommendation": "proceed", "reasoning": "Premium acceptable." },
                "market_commentary": "",
                "risk_alerts": []
            }
        });
        let out = format_tick_data(&data);
        assert!(out.contains("ENTRY  IWM"));
        assert!(out.contains("put_credit"));
        assert!(out.contains("LLM selection"));
        assert!(out.contains("proceed"));
    }
}