pine-broker 0.1.0

Simulated broker for backtesting Pine Script strategies.
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
//! The accounting half of the simulator: order book, position, trades, equity.
//!
//! Venue-independent — the only pluggable part is the [`FillModel`]. Trades are
//! paired first-in-first-out: closing reduces the oldest open lots first, which
//! is what `strategy.closedtrades` reports.

use crate::{
    Broker, Commission, Direction, Exit, FillModel, OcaType, Order, OrderKind, Position, Sizing,
    Trade,
};
use pine_core::Bar;
use std::collections::HashMap;

pub struct BarBroker<F: FillModel> {
    fills: F,
    commission: Option<Commission>,
    /// How an order without an explicit `qty` is sized.
    sizing: Sizing,
    /// Maximum concurrent open entries in one direction (`pyramiding`).
    max_entries: usize,
    /// Tick size, so `strategy.exit` distances given in ticks become prices.
    mintick: f64,
    /// Starting capital, kept so `strategy.netprofit` can be derived from the
    /// equity identity.
    initial: f64,
    /// Cash balance; commission is charged here, price P&L in `realized`.
    cash: f64,
    realized: f64,

    /// Pending orders keyed by id, so a resubmission replaces rather than
    /// stacks — matching Pine's order commands.
    pending: HashMap<String, Order>,
    /// Insertion order of `pending`, so fills happen in submission order.
    order: Vec<String>,
    /// Exit brackets, evaluated each bar after the pending orders.
    exits: Vec<Exit>,

    open: Vec<Trade>,
    closed: Vec<Trade>,

    bar_index: u64,
}

impl<F: FillModel> BarBroker<F> {
    pub fn new(fills: F, initial_capital: f64) -> Self {
        Self {
            fills,
            commission: None,
            sizing: Sizing::Contracts(1.0),
            max_entries: 1,
            mintick: 0.0,
            initial: initial_capital,
            cash: initial_capital,
            realized: 0.0,
            pending: HashMap::new(),
            order: Vec::new(),
            exits: Vec::new(),
            open: Vec::new(),
            closed: Vec::new(),
            bar_index: 0,
        }
    }

    pub fn with_commission(mut self, commission: Commission) -> Self {
        self.commission = Some(commission);
        self
    }

    pub fn with_sizing(mut self, sizing: Sizing) -> Self {
        self.sizing = sizing;
        self
    }

    pub fn with_mintick(mut self, mintick: f64) -> Self {
        self.mintick = mintick;
        self
    }

    pub fn with_pyramiding(mut self, pyramiding: usize) -> Self {
        self.max_entries = pyramiding.max(1);
        self
    }

    fn open_lots_toward(&self, direction: Direction) -> usize {
        self.open
            .iter()
            .filter(|t| t.size.signum() == direction.sign())
            .count()
    }

    fn net_size(&self) -> f64 {
        self.open.iter().map(|t| t.size).sum()
    }

    /// Net signed size of the lots matching `target` (all lots if `None`).
    fn matched_size(&self, target: Option<&str>) -> f64 {
        self.open
            .iter()
            .filter(|t| target.is_none_or(|id| t.entry_id == id))
            .map(|t| t.size)
            .sum()
    }

    /// Average entry price of the lots matching `target`, weighted by size.
    fn matched_avg(&self, target: Option<&str>) -> f64 {
        let (value, qty): (f64, f64) = self
            .open
            .iter()
            .filter(|t| target.is_none_or(|id| t.entry_id == id))
            .fold((0.0, 0.0), |(v, q), t| {
                (v + t.entry_price * t.size, q + t.size)
            });
        if qty == 0.0 {
            0.0
        } else {
            value / qty
        }
    }

    fn commission_on(&self, qty: f64, price: f64) -> f64 {
        self.commission.map_or(0.0, |c| c.charge(qty, price))
    }

    /// Apply a fill of `signed_qty` contracts at `price`: close opposing lots
    /// first (FIFO), then open a lot with whatever direction remains. `target`
    /// restricts which lots may be closed to those from that entry — a reducing
    /// order leaves the remainder unopened, so it only ever shrinks them.
    fn apply_fill(&mut self, mut signed_qty: f64, price: f64, id: &str, target: Option<&str>) {
        // This fill's commission, split across the portions it closes and opens
        // by contract count, so each closed trade carries its exit commission
        // and each opened lot its entry commission.
        let order_qty_abs = signed_qty.abs();
        let order_commission = self.commission_on(signed_qty, price);
        self.cash -= order_commission;

        // Close opposing open lots, oldest first. A partial close records a
        // closed trade for the exited portion and leaves the rest open, as Pine
        // does, so `strategy.closedtrades` counts partial exits too.
        while signed_qty != 0.0 {
            let Some(index) = self.open.iter().position(|t| {
                t.size.signum() != signed_qty.signum()
                    && target.is_none_or(|want| t.entry_id == want)
            }) else {
                break;
            };

            let lot = &self.open[index];
            let closed = signed_qty.abs().min(lot.size.abs());
            let closed_signed = closed * lot.size.signum();
            let entry_share = lot.commission * closed / lot.size.abs();
            let exit_share = order_commission * closed / order_qty_abs;

            self.realized += (price - lot.entry_price) * closed_signed;
            signed_qty += closed_signed; // moves signed_qty toward zero

            self.closed.push(Trade {
                entry_id: lot.entry_id.clone(),
                size: closed_signed,
                entry_price: lot.entry_price,
                entry_bar: lot.entry_bar,
                exit_price: Some(price),
                exit_bar: Some(self.bar_index),
                commission: entry_share + exit_share,
            });

            let lot = &mut self.open[index];
            lot.size -= closed_signed;
            lot.commission -= entry_share;
            if lot.size == 0.0 {
                self.open.remove(index);
            }
        }

        // Whatever quantity is left opens a new lot — but only for an entry. A
        // targeted reduce never flips into a new position, so it stops here.
        if signed_qty != 0.0 && target.is_none() {
            self.open.push(Trade {
                entry_id: id.to_string(),
                size: signed_qty,
                entry_price: price,
                entry_bar: self.bar_index,
                exit_price: None,
                exit_bar: None,
                commission: order_commission * signed_qty.abs() / order_qty_abs,
            });
        }
    }

    /// The signed quantity an order actually trades at `price`, resolving the
    /// default quantity and, for a reducing or reversing order, the position.
    fn resolve_qty(&self, order: &Order, price: f64) -> f64 {
        if order.reduce_only {
            // Never flips: close at most the matched position. An explicit qty
            // wins; otherwise `qty_percent` closes that share, and with neither
            // `strategy.close` shuts the whole position.
            let pool = self.matched_size(order.close_target.as_deref());
            let closable = match (order.qty, order.qty_percent) {
                (Some(q), _) => pool.abs().min(q.abs()),
                (None, Some(pct)) => pool.abs() * (pct / 100.0),
                (None, None) => pool.abs(),
            };
            return -pool.signum() * closable;
        }

        let requested = match order.qty {
            Some(q) => q.abs(),
            None => {
                // Pine sizes a default-qty order from the close of the bar it
                // was generated on; fall back to the fill price if unstamped.
                let sizing_price = order.sizing_price.unwrap_or(price);
                self.sizing
                    .contracts(sizing_price, self.equity(sizing_price))
            }
        };
        let net = self.net_size();
        let want = order.direction.sign() * requested;
        if order.reverses && net != 0.0 && net.signum() != order.direction.sign() {
            // Close the opposite position and open `requested` the other way.
            want - net
        } else {
            want
        }
    }

    /// Evaluate every exit bracket against `bar`: for a matched position, fill
    /// the stop-loss, trailing stop or take-profit if the bar reaches it (a stop
    /// wins when several do, the conservative assumption), then retire it.
    fn evaluate_exits(&mut self, bar: &Bar) {
        let ids: Vec<String> = self.exits.iter().map(|e| e.id.clone()).collect();
        for id in ids {
            let Some(exit) = self.exits.iter().find(|e| e.id == id).cloned() else {
                continue;
            };
            let target = exit.from_entry.as_deref();
            let pos = self.matched_size(target);
            if pos == 0.0 {
                continue; // Nothing to protect yet (the entry has not filled).
            }
            let dir = pos.signum();
            let entry_avg = self.matched_avg(target);
            let mintick = self.mintick;
            let exit_dir = if dir > 0.0 {
                Direction::Short
            } else {
                Direction::Long
            };

            // Take-profit and stop-loss prices, from an explicit level or a tick
            // distance either side of the entry.
            let tp = exit
                .limit
                .or_else(|| exit.profit_ticks.map(|t| entry_avg + dir * t * mintick));
            let sl = exit
                .stop
                .or_else(|| exit.loss_ticks.map(|t| entry_avg - dir * t * mintick));

            // Arm and advance the trailing stop with this bar: the reference
            // trails "each time the trade's profit reaches a new high", so it
            // follows the peak within the bar and can fill the same one.
            let trail_stop = self.advance_trail(&id, dir, entry_avg, bar);

            // A stop wins over the take-profit when a bar reaches both. The
            // trailing stop fills at its level — price set the peak this bar,
            // then retraced to the stop.
            let hit = sl
                .and_then(|p| self.leg_fill(OrderKind::Stop(p), exit_dir, bar))
                .or_else(|| {
                    trail_stop.filter(|&ts| {
                        if dir > 0.0 {
                            bar.low <= ts
                        } else {
                            bar.high >= ts
                        }
                    })
                })
                .or_else(|| tp.and_then(|p| self.leg_fill(OrderKind::Limit(p), exit_dir, bar)));

            if let Some(price) = hit {
                let requested = match (exit.qty, exit.qty_percent) {
                    (Some(q), _) => pos.abs().min(q.abs()),
                    (None, Some(pct)) => pos.abs() * (pct / 100.0),
                    (None, None) => pos.abs(),
                };
                self.apply_fill(-dir * requested, price, &exit.id, target);
                self.exits.retain(|e| e.id != id);
            }
        }
    }

    /// Arm a trailing exit and advance its peak from `bar`, returning the stop
    /// price if it is active — `trail_offset` ticks behind the best price seen.
    fn advance_trail(&mut self, id: &str, dir: f64, entry_avg: f64, bar: &Bar) -> Option<f64> {
        let mintick = self.mintick;
        let exit = self.exits.iter_mut().find(|e| e.id == id)?;
        let offset = exit.trail_offset?;
        let bar_best = if dir > 0.0 { bar.high } else { bar.low };

        if !exit.activated {
            let level = exit
                .trail_price
                .or_else(|| exit.trail_points.map(|pts| entry_avg + dir * pts * mintick));
            if let Some(level) = level {
                exit.activated = if dir > 0.0 {
                    bar.high >= level
                } else {
                    bar.low <= level
                };
            }
        }
        if !exit.activated {
            return None;
        }

        exit.peak = Some(match exit.peak {
            Some(pk) if dir > 0.0 => pk.max(bar_best),
            Some(pk) => pk.min(bar_best),
            None => bar_best,
        });
        exit.peak.map(|pk| pk - dir * offset * mintick)
    }

    /// The fill price of one exit leg against `bar`, or `None` if unreached.
    fn leg_fill(&self, kind: OrderKind, direction: Direction, bar: &Bar) -> Option<f64> {
        let leg = Order {
            kind,
            ..Order::market("", direction, None)
        };
        self.fills.fill(&leg, bar)
    }

    /// Whether an entry order is blocked by the pyramiding limit: it would add a
    /// new lot to an already-full stack on its own side.
    fn pyramiding_blocks(&self, order: &Order) -> bool {
        if order.reduce_only || !order.reverses {
            return false; // Only `strategy.entry` obeys pyramiding.
        }
        let net = self.net_size();
        let same_side = net != 0.0 && net.signum() == order.direction.sign();
        same_side && self.open_lots_toward(order.direction) >= self.max_entries
    }

    /// Apply an OCA group's effect after `filled` executes: cancel the group's
    /// other unfilled orders, or reduce them by the filled size.
    fn apply_oca(&mut self, filled: &Order, filled_qty: f64) {
        let Some(group) = filled.oca_name.clone() else {
            return;
        };
        if filled.oca_type == OcaType::None {
            return;
        }
        let siblings: Vec<String> = self
            .pending
            .values()
            .filter(|o| o.id != filled.id && o.oca_name.as_deref() == Some(group.as_str()))
            .map(|o| o.id.clone())
            .collect();
        for id in siblings {
            match filled.oca_type {
                OcaType::Cancel => {
                    self.pending.remove(&id);
                    self.order.retain(|o| o != &id);
                }
                OcaType::Reduce => {
                    if let Some(o) = self.pending.get_mut(&id) {
                        // Shrink by the filled size; a non-positive remainder
                        // cancels the order outright.
                        let base = o.qty.unwrap_or(filled_qty.abs());
                        let left = base - filled_qty.abs();
                        if left > 0.0 {
                            o.qty = Some(left);
                        } else {
                            self.pending.remove(&id);
                            self.order.retain(|o| o != &id);
                        }
                    }
                }
                OcaType::None => {}
            }
        }
    }
}

impl<F: FillModel> Broker for BarBroker<F> {
    fn submit(&mut self, order: Order) {
        if !self.pending.contains_key(&order.id) {
            self.order.push(order.id.clone());
        }
        self.pending.insert(order.id.clone(), order);
    }

    fn submit_exit(&mut self, mut exit: Exit) {
        if let Some(slot) = self.exits.iter_mut().find(|e| e.id == exit.id) {
            // Re-submitting the same exit each bar must not restart a trailing
            // stop, so carry its runtime state onto the replacement.
            exit.activated = slot.activated;
            exit.peak = slot.peak;
            *slot = exit;
        } else {
            self.exits.push(exit);
        }
    }

    fn cancel(&mut self, id: &str) {
        if self.pending.remove(id).is_some() {
            self.order.retain(|o| o != id);
        }
        self.exits.retain(|e| e.id != id);
    }

    fn cancel_all(&mut self) {
        self.pending.clear();
        self.order.clear();
        self.exits.clear();
    }

    fn advance(&mut self, bar: &Bar) {
        self.bar_index = bar.index;

        // Fill in submission order; a filled order leaves the book.
        let ids: Vec<String> = self.order.clone();
        for id in ids {
            let Some(order) = self.pending.get(&id).cloned() else {
                continue;
            };
            if self.pyramiding_blocks(&order) {
                // The stack is full: drop the entry, as Pine rejects it.
                self.pending.remove(&id);
                self.order.retain(|o| o != &id);
                continue;
            }
            if let Some(price) = self.fills.fill(&order, bar) {
                let qty = self.resolve_qty(&order, price);
                if qty != 0.0 {
                    self.apply_fill(qty, price, &order.id, order.close_target.as_deref());
                    self.apply_oca(&order, qty);
                }
                self.pending.remove(&id);
                self.order.retain(|o| o != &id);
            }
        }

        // Then the protective exits, against the position those fills produced.
        self.evaluate_exits(bar);
    }

    fn position(&self) -> Position {
        let size = self.net_size();
        if size == 0.0 {
            return Position::default();
        }
        // Average price weighted over the open lots on the net side.
        let (value, qty): (f64, f64) = self
            .open
            .iter()
            .filter(|t| t.size.signum() == size.signum())
            .fold((0.0, 0.0), |(v, q), t| {
                (v + t.entry_price * t.size, q + t.size)
            });
        Position {
            size,
            avg_price: if qty == 0.0 { 0.0 } else { value / qty },
        }
    }

    fn initial_capital(&self) -> f64 {
        self.initial
    }

    fn equity(&self, price: f64) -> f64 {
        let unrealized: f64 = self
            .open
            .iter()
            .map(|t| (price - t.entry_price) * t.size)
            .sum();
        self.cash + self.realized + unrealized
    }

    fn open_trades(&self) -> Vec<&Trade> {
        self.open.iter().collect()
    }

    fn closed_trades(&self) -> &[Trade] {
        &self.closed
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Commission, Direction, OrderKind, PineFills};

    fn bar(index: u64, open: f64, high: f64, low: f64, close: f64) -> Bar {
        Bar {
            open,
            high,
            low,
            close,
            volume: 0.0,
            index,
            ..Bar::default()
        }
    }

    fn broker() -> BarBroker<PineFills> {
        BarBroker::new(PineFills::default(), 10_000.0)
    }

    #[test]
    fn a_market_entry_fills_at_the_open() {
        let mut b = broker();
        b.submit(Order::market("long", Direction::Long, Some(2.0)));
        b.advance(&bar(0, 100.0, 105.0, 99.0, 104.0));

        let pos = b.position();
        assert_eq!(pos.size, 2.0);
        assert_eq!(pos.avg_price, 100.0);
        // Marked at 104: two contracts up 4 each.
        assert_eq!(b.equity(104.0), 10_008.0);
    }

    #[test]
    fn a_closed_trade_keeps_its_size_and_profit() {
        let mut b = broker();
        b.submit(Order::market("L", Direction::Long, Some(2.0)));
        b.advance(&bar(0, 100.0, 100.0, 100.0, 100.0));
        b.submit(Order {
            reduce_only: true,
            ..Order::market("L", Direction::Short, None)
        });
        b.advance(&bar(1, 110.0, 110.0, 110.0, 110.0));

        let trade = &b.closed_trades()[0];
        assert_eq!(trade.size, 2.0);
        assert_eq!(trade.entry_price, 100.0);
        assert_eq!(trade.exit_price, Some(110.0));
        assert_eq!(trade.profit(0.0), 20.0); // (110 - 100) * 2, price ignored once closed
    }

    #[test]
    fn closing_realises_profit_and_flattens() {
        let mut b = broker();
        b.submit(Order::market("long", Direction::Long, Some(1.0)));
        b.advance(&bar(0, 100.0, 100.0, 100.0, 100.0));

        b.submit(Order {
            reduce_only: true,
            ..Order::market("exit", Direction::Short, Some(1.0))
        });
        b.advance(&bar(1, 110.0, 110.0, 110.0, 110.0));

        assert!(b.position().is_flat());
        assert_eq!(b.closed_trades().len(), 1);
        assert_eq!(b.equity(110.0), 10_010.0);
    }

    #[test]
    fn an_opposite_entry_reverses_the_position() {
        let mut b = broker();
        b.submit(Order::market("a", Direction::Long, Some(5.0)));
        b.advance(&bar(0, 100.0, 100.0, 100.0, 100.0));

        // Short 5 against long 5 sells 10: closes the long and opens short 5.
        b.submit(Order::market("b", Direction::Short, Some(5.0)));
        b.advance(&bar(1, 100.0, 100.0, 100.0, 100.0));

        assert_eq!(b.position().size, -5.0);
        assert_eq!(b.closed_trades().len(), 1);
    }

    #[test]
    fn a_buy_limit_waits_for_the_price() {
        let mut b = broker();
        b.submit(Order {
            kind: OrderKind::Limit(95.0),
            reverses: false,
            ..Order::market("buy", Direction::Long, Some(1.0))
        });

        // Bar stays above 95: no fill.
        b.advance(&bar(0, 100.0, 101.0, 96.0, 99.0));
        assert!(b.position().is_flat());

        // Next bar dips to 94: fills at the limit.
        b.advance(&bar(1, 97.0, 98.0, 94.0, 96.0));
        assert_eq!(b.position().size, 1.0);
        assert_eq!(b.position().avg_price, 95.0);
    }

    #[test]
    fn commission_reduces_equity() {
        let mut b = broker().with_commission(Commission::Percent(1.0));
        b.submit(Order::market("long", Direction::Long, Some(1.0)));
        b.advance(&bar(0, 100.0, 100.0, 100.0, 100.0));

        // 1% of 100 = 1.0 charged on entry.
        assert_eq!(b.equity(100.0), 9_999.0);
    }

    #[test]
    fn a_take_profit_exit_closes_when_price_reaches_it() {
        let mut b = broker();
        b.submit(Order::market("L", Direction::Long, Some(1.0)));
        b.submit_exit(Exit {
            limit: Some(110.0),
            ..Exit::resting("X", Some("L".into()), None, None)
        });

        // Entry fills at 100; this bar's high 105 does not reach 110.
        b.advance(&bar(0, 100.0, 105.0, 99.0, 104.0));
        assert_eq!(b.position().size, 1.0);

        // Next bar reaches 110: the take-profit sells at 110.
        b.advance(&bar(1, 106.0, 112.0, 105.0, 108.0));
        assert!(b.position().is_flat());
        assert_eq!(b.closed_trades().len(), 1);
        assert_eq!(b.equity(108.0), 10_010.0); // realised +10
    }

    #[test]
    fn a_stop_loss_in_ticks_sits_a_distance_from_the_entry() {
        // mintick 0.5, loss 4 ticks -> stop 2.0 below a long entry.
        let fills = PineFills {
            slippage: 0.0,
            mintick: 0.5,
        };
        let mut b = BarBroker::new(fills, 10_000.0).with_mintick(0.5);
        b.submit(Order::market("L", Direction::Long, Some(1.0)));
        b.submit_exit(Exit {
            loss_ticks: Some(4.0),
            ..Exit::resting("X", Some("L".into()), None, None)
        });

        // Entry fills at 100; stop is 98. This bar's low 99 stays above it.
        b.advance(&bar(0, 100.0, 105.0, 99.0, 104.0));
        assert_eq!(b.position().size, 1.0);

        // Next bar dips to 97: the stop sells at 98.
        b.advance(&bar(1, 100.0, 101.0, 97.0, 99.0));
        assert!(b.position().is_flat());
        assert_eq!(b.equity(99.0), 9_998.0); // realised -2
    }

    #[test]
    fn close_targets_only_the_named_entry() {
        let mut b = broker();
        b.submit(Order {
            reverses: false,
            ..Order::market("A", Direction::Long, Some(1.0))
        });
        b.advance(&bar(0, 100.0, 100.0, 100.0, 100.0));
        b.submit(Order {
            reverses: false,
            ..Order::market("B", Direction::Long, Some(1.0))
        });
        b.advance(&bar(1, 101.0, 101.0, 101.0, 101.0));
        assert_eq!(b.position().size, 2.0);

        // Close only A: its lot goes, B's remains.
        b.submit(Order {
            reduce_only: true,
            close_target: Some("A".into()),
            qty: None,
            ..Order::market("A", Direction::Long, None)
        });
        b.advance(&bar(2, 102.0, 102.0, 102.0, 102.0));
        assert_eq!(b.position().size, 1.0);
        assert_eq!(b.closed_trades().len(), 1);
        assert_eq!(b.position().avg_price, 101.0); // B's entry
    }

    #[test]
    fn cash_sizing_buys_contracts_worth_the_cash() {
        let mut b = broker().with_sizing(Sizing::Cash(1_000.0));
        // No explicit qty: 1000 cash / 100 price = 10 contracts.
        b.submit(Order::market("L", Direction::Long, None));
        b.advance(&bar(0, 100.0, 100.0, 100.0, 100.0));
        assert_eq!(b.position().size, 10.0);
    }

    #[test]
    fn percent_of_equity_sizing_scales_with_the_account() {
        let mut b = broker().with_sizing(Sizing::PercentOfEquity(50.0));
        // 50% of 10000 equity = 5000, at price 100 = 50 contracts.
        b.submit(Order::market("L", Direction::Long, None));
        b.advance(&bar(0, 100.0, 100.0, 100.0, 100.0));
        assert_eq!(b.position().size, 50.0);
    }

    #[test]
    fn pyramiding_caps_entries_in_one_direction() {
        let mut b = broker().with_pyramiding(2);
        for (i, id) in ["A", "B", "C"].iter().enumerate() {
            b.submit(Order {
                reverses: true,
                ..Order::market(*id, Direction::Long, Some(1.0))
            });
            b.advance(&bar(i as u64, 100.0, 100.0, 100.0, 100.0));
        }
        // Two lots allowed; the third entry is rejected.
        assert_eq!(b.position().size, 2.0);
    }

    #[test]
    fn oca_cancel_removes_the_sibling_when_one_fills() {
        let mut b = broker();
        // A buy stop at 105 and a buy limit at 95, same OCA group.
        b.submit(Order {
            kind: OrderKind::Stop(105.0),
            oca_name: Some("G".into()),
            oca_type: OcaType::Cancel,
            ..Order::market("up", Direction::Long, Some(1.0))
        });
        b.submit(Order {
            kind: OrderKind::Limit(95.0),
            oca_name: Some("G".into()),
            oca_type: OcaType::Cancel,
            ..Order::market("down", Direction::Long, Some(1.0))
        });

        // This bar reaches both 105 and 95; the first to fill cancels the other.
        b.advance(&bar(0, 100.0, 106.0, 94.0, 100.0));
        assert_eq!(b.position().size, 1.0);
    }

    #[test]
    fn close_qty_percent_reduces_the_position() {
        let mut b = broker();
        b.submit(Order::market("L", Direction::Long, Some(4.0)));
        b.advance(&bar(0, 100.0, 100.0, 100.0, 100.0));

        // Close 50% of the 4-contract position: 2 remain.
        b.submit(Order {
            reduce_only: true,
            close_target: Some("L".into()),
            qty_percent: Some(50.0),
            qty: None,
            ..Order::market("L", Direction::Long, None)
        });
        b.advance(&bar(1, 110.0, 110.0, 110.0, 110.0));
        assert_eq!(b.position().size, 2.0);

        // The exited half is a closed trade; the rest stays open.
        assert_eq!(b.closed_trades().len(), 1);
        assert_eq!(b.closed_trades()[0].size, 2.0);
        assert_eq!(b.closed_trades()[0].profit(0.0), 20.0); // (110 - 100) * 2
        assert_eq!(b.open_trades().len(), 1);
        assert_eq!(b.open_trades()[0].size, 2.0);
    }

    #[test]
    fn a_trailing_stop_follows_the_peak_and_fills_at_its_level() {
        // mintick 0.5: activation 4 ticks (2.0) above entry, trailing 2 ticks
        // (1.0) behind the peak.
        let fills = PineFills {
            slippage: 0.0,
            mintick: 0.5,
        };
        let mut b = BarBroker::new(fills, 10_000.0).with_mintick(0.5);
        b.submit(Order::market("L", Direction::Long, Some(1.0)));
        b.submit_exit(Exit {
            trail_points: Some(4.0),
            trail_offset: Some(2.0),
            ..Exit::resting("X", Some("L".into()), None, None)
        });

        // Entry at 100; high 101 has not reached the 102 activation level.
        b.advance(&bar(0, 100.0, 101.0, 99.0, 100.0));
        assert_eq!(b.position().size, 1.0);

        // Same bar arms and fills: price rallies to 105 (peak, so the stop is at
        // 104), then the low 101 retraces through it — the stop fills at its own
        // level, 104, not the earlier open, for a profit of 4.
        b.advance(&bar(1, 102.0, 105.0, 101.0, 104.0));
        assert!(b.position().is_flat());
        assert_eq!(b.closed_trades().len(), 1);
        assert_eq!(b.equity(104.0), 10_004.0);
    }
}