rungbot-exec 0.5.7

Live order execution for rungbot: an idempotent order journal, hard caps, reconcile, and Gate.io, Revolut X and Binance spot clients. Opt-in.
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
//! Signals become orders: `dry` plans them, `live` places market orders, inside the
//! rails. A live run also does its housekeeping here, before the first new order.
//!
//! The rails, in the order a signal meets them:
//!
//! * live placement needs `trade_mode: live`, `live_trading_enabled` and no halt file;
//! * the venue price must sit within `max_slippage_pct` of the signal's price (bull
//!   policy sells are exempt);
//! * a sell must still clear entry +`target_pct` at the venue price;
//! * one order is clamped to `max_order_usd` (not a policy sell); an order under the
//!   venue minimum is skipped;
//! * per-day notional and order caps (not a policy sell), and a per-ISO-week cap on buys;
//! * the buy budget is the venue's free quote asset, less the pending onramp top-up,
//!   at most `usdc_bag_usd` when set, split evenly across the venue's coins.
//!
//! Every order is journaled and written to disk before the venue sees it, and its
//! answer written right after. A journal write that fails stops the run from placing
//! anything more: the signals left are skipped (their ladder entries roll back) and one
//! `fatal` result names the failure. Nothing here returns early past a venue call, so
//! the caller always gets every result and still saves the ladder state, the cap
//! counters and the P&L ledger.

use std::collections::{BTreeMap, BTreeSet};

use serde_json::{json, Value};

use super::config::RunConfig;
use super::funding;
use super::signals::Signal;
use super::RunResult;
use crate::housekeeping::{
    self, cost_basis, fetch_balances, log_pnl, pair_limit_sell, pnl_tail, set_cost_basis, Balances,
    Books, LadderState, Persist, Route, RunCtx, Settings,
};
use crate::ids;
use crate::journal::{client_id, Order, Side};
use crate::pyfmt::{self, g};
use crate::reconcile::VenueSource;
use crate::venue::Venue;

/// Everything execution reads and writes besides the signals.
pub struct ExecIo<'a> {
    pub venues: &'a dyn VenueSource,
    pub books: Books<'a>,
    /// Writes the journal and the P&L ledger. The journal is written after every row is
    /// recorded and before the venue sees the order, so a crash can never leave an order
    /// the journal forgot.
    pub persist: &'a mut Persist,
}

/// What this run knows about the market and the policy.
#[derive(Debug, Clone, Default)]
pub struct ExecCtx {
    pub now: f64,
    pub halted: bool,
    /// The regime's market label, when it could be read.
    pub market: Option<String>,
    /// Coins whose sell side trails.
    pub trailing: BTreeSet<String>,
    /// The bull sell policy governs sells.
    pub policy_on: bool,
}

/// The housekeeping settings for this config.
pub fn hk_settings(cfg: &RunConfig) -> Settings {
    Settings {
        target_pct: cfg.target_pct,
        fees: rungbot_core::housekeeping::Fees {
            gate: cfg.fee_pct_gate,
            revx: cfg.fee_pct_revx,
            binance: cfg.fee_pct_binance,
        },
        window_hours: cfg.window_hours,
        limit_ttl_days: cfg.limit_ttl_days,
        limit_ttl_days_bear: cfg.limit_ttl_days_bear,
        limit_ttl_days_chop: cfg.limit_ttl_days_chop,
        ttl_renag_days: cfg.ttl_renag_days,
        routing: cfg
            .routing
            .iter()
            .map(|(s, r)| Route {
                sym: s.clone(),
                exch: r.exch.clone(),
                pair: r.pair.clone(),
                quote: r.quote.clone(),
            })
            .collect(),
        entries: cfg.entries.clone(),
    }
}

fn num(v: Option<&Value>) -> f64 {
    v.and_then(Value::as_f64).unwrap_or(0.0)
}

struct Ledgers {
    day: String,
    day_notional: f64,
    day_orders: i64,
    week: String,
    week_notional: f64,
}

impl Ledgers {
    fn load(state: &LadderState, now: f64) -> Ledgers {
        let day = rungbot_core::time::utc_day(now);
        let week = rungbot_core::time::iso_week(now);
        let d = state.get("_daily").filter(|v| v.is_object());
        let w = state.get("_weekly").filter(|v| v.is_object());
        let same_day = d.and_then(|d| d.get("date")).and_then(Value::as_str) == Some(&day);
        let same_week = w.and_then(|w| w.get("week")).and_then(Value::as_str) == Some(&week);
        Ledgers {
            day_notional: if same_day {
                num(d.and_then(|d| d.get("notional")))
            } else {
                0.0
            },
            day_orders: if same_day {
                num(d.and_then(|d| d.get("orders"))) as i64
            } else {
                0
            },
            week_notional: if same_week {
                num(w.and_then(|w| w.get("notional")))
            } else {
                0.0
            },
            day,
            week,
        }
    }

    fn store(&self, state: &mut LadderState) {
        state.insert(
            "_daily".into(),
            json!({"date": self.day, "notional": self.day_notional, "orders": self.day_orders}),
        );
        state.insert(
            "_weekly".into(),
            json!({"week": self.week, "notional": self.week_notional}),
        );
    }
}

/// The error line of a run stopped by a journal write that failed.
pub fn journal_fatal_text(e: &str) -> String {
    format!(
        "{e} -- stopped placing orders for this run. The journal on disk may be missing \
         an order a venue now holds: fix the disk, then run `rungbot-exec reconcile` \
         before the next run"
    )
}

fn res(sym: &str, side: &str, mode: &str) -> RunResult {
    RunResult {
        sym: Some(sym.into()),
        side: Some(side.into()),
        mode: Some(mode.into()),
        ..Default::default()
    }
}

/// Turn signals into orders. See the module docs.
pub fn execute_trades(
    cfg: &RunConfig,
    buys: &[Signal],
    sells: &[Signal],
    ctx: &ExecCtx,
    io: &mut ExecIo,
) -> Vec<RunResult> {
    let mode = cfg.trade_mode.as_str();
    if mode != "dry" && mode != "live" {
        return Vec::new();
    }
    if mode == "dry" && buys.is_empty() && sells.is_empty() {
        return Vec::new();
    }
    let now = ctx.now;
    let mut results: Vec<RunResult> = Vec::new();

    let (bal, errs) = fetch_balances(io.venues, mode);
    results.extend(errs.iter().map(RunResult::from_outcome));
    let mut counts: BTreeMap<&str, usize> = BTreeMap::new();
    for (_, r) in &cfg.routing {
        *counts.entry(r.exch.as_str()).or_insert(0) += 1;
    }

    let live = mode == "live";
    let block: Option<String> = if live && !cfg.live_trading_enabled {
        Some("LIVE_TRADING_ENABLED is not 'yes'".into())
    } else if live && ctx.halted {
        Some(format!("halt file present ({})", cfg.halt_file.display()))
    } else {
        None
    };

    let mut led = Ledgers::load(io.books.ladder, now);
    led.store(io.books.ladder);

    let settings = hk_settings(cfg);
    let policy_set: BTreeSet<String> = if ctx.policy_on {
        cfg.routing
            .iter()
            .map(|(s, _)| s.clone())
            .chain(cfg.watchlist.iter().map(|(s, _)| s.clone()))
            .collect()
    } else {
        BTreeSet::new()
    };

    if live {
        let rctx = RunCtx {
            now,
            blocked: block.is_some(),
            market: ctx.market.as_deref(),
            trailing: &ctx.trailing,
            policy: &policy_set,
        };
        let before = io.books.journal.clone();
        let out = housekeeping::run(&settings, &rctx, &bal, &mut io.books, io.venues, io.persist);
        results.extend(out.iter().map(RunResult::from_outcome));
        if *io.books.journal != before {
            let _ = io.persist.journal(io.books.journal);
        }
    }

    // The pending top-up to the second venue stays free on the onramp venue.
    let mut onramp_held = 0.0;
    let rxb = bal.full.get("revx").filter(|b| !b.is_empty());
    let gtb = bal.full.get("gate").filter(|b| !b.is_empty());
    if let (Some(rxb), Some(gtb)) = (rxb, gtb) {
        let routing: Vec<(String, String)> = cfg
            .routing
            .iter()
            .map(|(s, r)| (s.clone(), r.exch.clone()))
            .collect();
        let reserve = funding::onramp_reserve(
            funding::revx_stable_from(rxb),
            funding::gate_stable_from(gtb),
            &cfg.deploy_alloc,
            &routing,
        );
        let staged = rxb.get("USDC").map_or(0.0, |b| b.free);
        onramp_held = (reserve - staged).max(0.0);
    }

    let slippage_ok = |venue: f64, signal: f64| {
        !(signal != 0.0
            && cfg.max_slippage_pct > 0.0
            && (venue / signal - 1.0).abs() > cfg.max_slippage_pct / 100.0)
    };
    let daily_room = |led: &Ledgers, notional: f64| -> Option<String> {
        if cfg.max_daily_orders > 0 && led.day_orders >= cfg.max_daily_orders {
            return Some(format!(
                "daily order cap reached ({})",
                cfg.max_daily_orders
            ));
        }
        if cfg.max_daily_notional > 0.0 && led.day_notional + notional > cfg.max_daily_notional {
            return Some(format!(
                "daily notional cap reached (${:.0})",
                cfg.max_daily_notional
            ));
        }
        None
    };
    let weekly_room = |led: &Ledgers, notional: f64| -> Option<String> {
        if cfg.max_weekly_notional > 0.0 && led.week_notional + notional > cfg.max_weekly_notional {
            return Some(format!(
                "weekly buy cap reached (${:.0}/wk)",
                cfg.max_weekly_notional
            ));
        }
        None
    };

    // ---- BUYS: a market buy, then its paired resting limit sell
    for r in buys {
        let Some(route) = cfg.route(&r.sym) else {
            continue;
        };
        if let Some(e) = io.persist.failed() {
            let mut x = res(&r.sym, "buy", mode);
            x.skip = Some(format!("not placed: {e}"));
            results.push(x);
            continue;
        }
        let (exch, pair, quote) = (
            route.exch.as_str(),
            route.pair.as_str(),
            route.quote.as_str(),
        );
        let client = match io.venues.venue(exch) {
            Ok(c) => c,
            Err(e) => {
                let mut x = res(&r.sym, "buy", mode);
                x.err = Some(e);
                results.push(x);
                continue;
            }
        };
        let mut stable = bal.free(exch, quote);
        if exch == "revx" && onramp_held != 0.0 {
            stable = (stable - onramp_held).max(0.0);
        }
        if cfg.usdc_bag_usd > 0.0 {
            stable = stable.min(cfg.usdc_bag_usd);
        }
        let base_bag = stable / *counts.get(exch).unwrap_or(&1) as f64;
        let mut notional = r.pct / 100.0 * base_bag;
        let (price, minq) = match client
            .price(pair)
            .and_then(|p| client.limits(pair).map(|l| (p, l.min_quote)))
        {
            Ok(v) => v,
            Err(e) => {
                let mut x = res(&r.sym, "buy", mode);
                x.err = Some(e.to_string());
                results.push(x);
                continue;
            }
        };
        if !slippage_ok(price, r.usd) {
            let mut x = res(&r.sym, "buy", mode);
            x.skip = Some(format!(
                "venue ${} diverges >{:.0}% from signal ${}",
                g(price, 6),
                cfg.max_slippage_pct,
                g(r.usd, 6)
            ));
            results.push(x);
            continue;
        }
        if cfg.max_order_usd > 0.0 && notional > cfg.max_order_usd {
            notional = cfg.max_order_usd;
        }
        if notional < minq.max(0.01) {
            let mut x = res(&r.sym, "buy", mode);
            x.skip = Some(format!("${notional:.2} {quote} < min ${minq:.2}"));
            results.push(x);
            continue;
        }
        let qty_est = if price != 0.0 { notional / price } else { 0.0 };
        let lim = price * (1.0 + cfg.target_pct / 100.0);
        if !live {
            let mut x = res(&r.sym, "buy", "dry");
            x.plan = Some(format!(
                "BUY ${notional:.2} {quote} of {pair} @ ~${}; then LIMIT SELL ~{} @ ${} (+{:.0}%)",
                pyfmt::float_repr(price),
                g(qty_est, 6),
                g(lim, 6),
                cfg.target_pct
            ));
            results.push(x);
        } else if let Some(b) = &block {
            let mut x = res(&r.sym, "buy", "live");
            x.skip = Some(format!("live blocked: {b}"));
            results.push(x);
        } else if let Some(cap) = daily_room(&led, notional).or_else(|| weekly_room(&led, notional))
        {
            let mut x = res(&r.sym, "buy", "live");
            x.skip = Some(cap);
            results.push(x);
        } else {
            let x = live_buy(
                cfg, &settings, ctx, io, client, exch, pair, r, notional, price, &bal,
            );
            if x.committed {
                led.day_orders += 1;
                led.day_notional += notional;
                led.week_notional += notional;
            }
            results.push(x);
        }
    }

    // ---- SELLS: take-profit exits
    for r in sells {
        let Some(route) = cfg.route(&r.sym) else {
            continue;
        };
        if let Some(e) = io.persist.failed() {
            let mut x = res(&r.sym, "sell", mode);
            x.skip = Some(format!("not placed: {e}"));
            results.push(x);
            continue;
        }
        let (exch, pair) = (route.exch.as_str(), route.pair.as_str());
        let client = match io.venues.venue(exch) {
            Ok(c) => c,
            Err(e) => {
                let mut x = res(&r.sym, "sell", mode);
                x.err = Some(e);
                results.push(x);
                continue;
            }
        };
        let held = bal.free(exch, &r.sym);
        let read = client.price(pair).and_then(|p| {
            let q = client.round_amount(pair, r.pct / 100.0 * held)?;
            let l = client.limits(pair)?;
            Ok((p, q, l))
        });
        let (price, mut qty, limits) = match read {
            Ok(v) => v,
            Err(e) => {
                let mut x = res(&r.sym, "sell", mode);
                x.err = Some(e.to_string());
                results.push(x);
                continue;
            }
        };
        let policy = r.policy.as_deref().is_some_and(|p| !p.is_empty());
        if !policy && !slippage_ok(price, r.usd) {
            let mut x = res(&r.sym, "sell", mode);
            x.skip = Some(format!(
                "venue ${} diverges >{:.0}% from signal ${}",
                g(price, 6),
                cfg.max_slippage_pct,
                g(r.usd, 6)
            ));
            results.push(x);
            continue;
        }
        let tgt = r
            .entry
            .filter(|e| *e != 0.0)
            .map(|e| e * (1.0 + cfg.target_pct / 100.0));
        if let Some(t) = tgt.filter(|t| *t != 0.0 && price < *t) {
            let mut x = res(&r.sym, "sell", mode);
            x.skip = Some(format!(
                "venue ${} below take-profit ${} (signal stale)",
                g(price, 6),
                g(t, 6)
            ));
            results.push(x);
            continue;
        }
        if !policy && cfg.max_order_usd > 0.0 && qty * price > cfg.max_order_usd {
            qty = match client.round_amount(pair, cfg.max_order_usd / price) {
                Ok(q) => q,
                Err(e) => {
                    let mut x = res(&r.sym, "sell", mode);
                    x.err = Some(e.to_string());
                    results.push(x);
                    continue;
                }
            };
        }
        let notional = qty * price;
        if qty < limits.min_base || notional < limits.min_quote.max(0.01) {
            let mut x = res(&r.sym, "sell", mode);
            x.skip = Some(format!(
                "{} {} (~${notional:.2}) below exchange min",
                g(qty, 6),
                r.sym
            ));
            results.push(x);
            continue;
        }
        if !live {
            let mut x = res(&r.sym, "sell", "dry");
            x.plan = Some(format!(
                "SELL {} {} (~${notional:.2}) on {pair} @ ~${}",
                g(qty, 6),
                r.sym,
                pyfmt::float_repr(price)
            ));
            results.push(x);
            continue;
        }
        if let Some(b) = &block {
            let mut x = res(&r.sym, "sell", "live");
            x.skip = Some(format!("live blocked: {b}"));
            results.push(x);
            continue;
        }
        if !policy {
            if let Some(cap) = daily_room(&led, notional) {
                let mut x = res(&r.sym, "sell", "live");
                x.skip = Some(cap);
                results.push(x);
                continue;
            }
        }
        let mut x = live_sell(cfg, ctx, io, client, exch, pair, r, qty, price);
        x.policy = policy;
        if x.committed {
            led.day_orders += 1;
            led.day_notional += notional;
        }
        results.push(x);
    }
    led.store(io.books.ladder);
    if let Some(e) = io.persist.failed() {
        results.push(RunResult {
            mode: Some(mode.into()),
            hk: true,
            fatal: true,
            err: Some(journal_fatal_text(e)),
            ..Default::default()
        });
    }
    results
}

/// A journaled market buy; on a fill, the fill-based cost basis and the paired sell.
#[allow(clippy::too_many_arguments)]
fn live_buy(
    cfg: &RunConfig,
    settings: &Settings,
    ctx: &ExecCtx,
    io: &mut ExecIo,
    client: &dyn Venue,
    exch: &str,
    pair: &str,
    r: &Signal,
    notional: f64,
    price: f64,
    bal: &Balances,
) -> RunResult {
    let now = ctx.now;
    let sym = r.sym.as_str();
    let mut out = res(sym, "buy", "live");
    let cid = match ids::safe_cid(&client_id(sym, Side::Buy, now, r.rung)) {
        Ok(c) => c,
        Err(e) => {
            out.err = Some(e.to_string());
            return out;
        }
    };
    if io.books.journal.exists(&cid) {
        out.skip = Some("already journaled this slot (idempotent)".into());
        return out;
    }
    io.books.journal.record(Order {
        client_id: cid.clone(),
        sym: sym.into(),
        exch: exch.into(),
        pair: pair.into(),
        side: "buy".into(),
        kind: "market_buy".into(),
        status: "pending".into(),
        quote: Some(notional),
        rung: Some(r.rung),
        ts: Some(now),
        ..Default::default()
    });
    if let Err(e) = io.persist.journal(io.books.journal) {
        io.books.journal.orders.shift_remove(&cid);
        out.skip = Some(format!("not placed: {e}"));
        return out;
    }
    let fill = client
        .market_buy(pair, notional, Some(&cid))
        .and_then(|raw| client.parse_order(&raw));
    let fill = match fill {
        Ok(f) => f,
        Err(e) => {
            io.books.journal.update(&cid, |o| {
                o.status = "error".into();
                o.last_error = Some(e.to_string());
            });
            let _ = io.persist.journal(io.books.journal);
            out.err = Some(format!("market buy: {e}"));
            return out;
        }
    };
    let base = fill.base_qty.filter(|b| *b != 0.0);
    if !fill.filled && base.is_none() {
        // Settles later: reconcile books the real fill and pairs the sell then.
        io.books.journal.update(&cid, |o| {
            o.status = if fill.status.is_empty() {
                "open".into()
            } else {
                fill.status.clone()
            };
            o.order_id = Some(fill.order_id.clone());
        });
        let _ = io.persist.journal(io.books.journal);
        out.committed = true;
        out.done = Some(format!(
            "BUY ~${notional:.2} placed, awaiting fill; limit sell pairs once the fill reconciles"
        ));
        return out;
    }
    let mut fb = base.unwrap_or(if price != 0.0 { notional / price } else { 0.0 });
    if fill.fee != 0.0 && fill.fee_asset.as_deref() == Some(sym) {
        fb = (fb - fill.fee).max(0.0);
    }
    let fp = fill.avg_price.filter(|p| *p != 0.0).unwrap_or(price);
    let fq = if fill.quote != 0.0 {
        fill.quote
    } else {
        fb * fp
    };
    io.books.journal.update(&cid, |o| {
        o.status = "filled".into();
        o.order_id = Some(fill.order_id.clone());
        o.filled_base = Some(fb);
        o.filled_quote = Some(fq);
        o.avg_price = Some(fp);
        o.filled_ts = Some(now);
    });
    let _ = io.persist.journal(io.books.journal);
    set_cost_basis(
        settings,
        io.books.ladder,
        sym,
        bal.free(exch, sym),
        fq,
        fb,
        fp,
    );
    out.committed = true;
    if ctx.policy_on {
        out.done = Some(format!(
            "BOUGHT ~{} {sym} (~${fq:.2}); core add under the bull sell policy (no +{:.0}% pair)",
            g(fb, 6),
            cfg.target_pct
        ));
        return out;
    }
    let paired = pair_limit_sell(
        settings,
        io.books.journal,
        io.persist,
        client,
        exch,
        pair,
        sym,
        fb,
        fp,
        now,
        r.rung,
        "",
    );
    let head = format!("BOUGHT ~{} {sym} (~${fq:.2})", g(fb, 6));
    match paired {
        Ok(p) if p.level == housekeeping::Level::Done => {
            out.done = Some(format!("{head}; {}", p.text))
        }
        Ok(p) => {
            out.err = Some(format!(
                "MANUAL ACTION: bought ~{} {sym} but {}",
                g(fb, 6),
                p.text
            ))
        }
        Err(e) => {
            out.err = Some(format!(
                "MANUAL ACTION: bought ~{} {sym} but the limit sell was not placed: {e}",
                g(fb, 6)
            ))
        }
    }
    out
}

/// A journaled market take-profit sell, logged to the P&L ledger.
#[allow(clippy::too_many_arguments)]
fn live_sell(
    cfg: &RunConfig,
    ctx: &ExecCtx,
    io: &mut ExecIo,
    client: &dyn Venue,
    exch: &str,
    pair: &str,
    r: &Signal,
    qty: f64,
    price: f64,
) -> RunResult {
    let now = ctx.now;
    let sym = r.sym.as_str();
    let mut out = res(sym, "sell", "live");
    // A leading `m` keeps market-sell ids apart from the paired limit sells.
    let cid = match ids::safe_cid(&format!("m{}", client_id(sym, Side::Sell, now, r.rung))) {
        Ok(c) => c,
        Err(e) => {
            out.err = Some(e.to_string());
            return out;
        }
    };
    if io.books.journal.exists(&cid) {
        out.skip = Some("already journaled this slot (idempotent)".into());
        return out;
    }
    io.books.journal.record(Order {
        client_id: cid.clone(),
        sym: sym.into(),
        exch: exch.into(),
        pair: pair.into(),
        side: "sell".into(),
        kind: "market_sell".into(),
        status: "pending".into(),
        base: Some(qty),
        rung: Some(r.rung),
        ts: Some(now),
        ..Default::default()
    });
    if let Err(e) = io.persist.journal(io.books.journal) {
        io.books.journal.orders.shift_remove(&cid);
        out.skip = Some(format!("not placed: {e}"));
        return out;
    }
    let fill = client
        .market_sell(pair, qty, Some(&cid))
        .and_then(|raw| client.parse_order(&raw));
    match fill {
        Ok(fill) => {
            io.books.journal.update(&cid, |o| {
                o.status = "filled".into();
                o.order_id = Some(fill.order_id.clone());
                o.filled_base = fill.base_qty;
                o.filled_quote = Some(fill.quote);
                o.avg_price = fill.avg_price;
                o.filled_ts = Some(now);
            });
            let _ = io.persist.journal(io.books.journal);
            let fq = if fill.quote != 0.0 {
                fill.quote
            } else {
                qty * price
            };
            let fqty = fill.base_qty.filter(|b| *b != 0.0).unwrap_or(qty);
            let cb = cost_basis(io.books.ladder, sym)
                .or_else(|| cfg.entries.get(sym).copied().filter(|e| *e != 0.0));
            let avg = fill.avg_price.filter(|p| *p != 0.0).unwrap_or(price);
            let realized = log_pnl(
                io.books.pnl,
                sym,
                fqty,
                fq,
                Some(avg),
                cb,
                now,
                "market_sell",
            );
            io.persist.pnl(io.books.pnl);
            out.committed = true;
            out.done = Some(format!(
                "SOLD ~{} {sym} (~${fq:.2}){}",
                g(fqty, 6),
                pnl_tail(realized, cb, Some(avg))
            ));
        }
        Err(e) => {
            io.books.journal.update(&cid, |o| {
                o.status = "error".into();
                o.last_error = Some(e.to_string());
            });
            let _ = io.persist.journal(io.books.journal);
            out.err = Some(format!("market sell: {e}"));
        }
    }
    out
}