bot-engine 0.1.0

Trading bot engine: order manager, inventory, event routing
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
//! Paper Exchange: Simulates order execution using real quotes.
//!
//! Wraps a real exchange for quote data but simulates fills locally.
//! Orders fill when the quote price crosses the order price.
//! Uses FillSimulator for shared fill matching logic.
//! Uses MarginLedger for accurate perp margin accounting.

use super::fill_simulator::{FillSimulator, PendingOrder};
use crate::simulation::MarginLedger;
use async_lock::RwLock;
use bot_core::{
    AccountState, AssetId, ClientOrderId, Exchange, ExchangeError, ExchangeId, ExchangeOrderId,
    Fill, InstrumentId, InstrumentMeta, MarketIndex, OrderInput, PlaceOrderResult, Qty, Quote,
    TimeInForce,
};
use rust_decimal::Decimal;
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;

/// Newtype wrapper around `Arc<dyn Exchange>` to implement Exchange trait.
/// This allows PaperExchange to wrap any `Arc<dyn Exchange>` while satisfying orphan rules.
pub struct ArcExchange(pub Arc<dyn Exchange>);

impl ArcExchange {
    /// Wrap an exchange trait object.
    pub fn new(exchange: Arc<dyn Exchange>) -> Self {
        Self(exchange)
    }
}

#[async_trait::async_trait]
impl Exchange for ArcExchange {
    fn exchange_id(&self) -> &ExchangeId {
        self.0.exchange_id()
    }

    fn environment(&self) -> bot_core::Environment {
        self.0.environment()
    }

    async fn place_orders(
        &self,
        orders: &[OrderInput],
    ) -> Result<Vec<PlaceOrderResult>, ExchangeError> {
        self.0.place_orders(orders).await
    }

    async fn cancel_order(
        &self,
        instrument: &InstrumentId,
        market_index: &MarketIndex,
        client_id: &ClientOrderId,
        exchange_order_id: Option<&ExchangeOrderId>,
    ) -> Result<(), ExchangeError> {
        self.0
            .cancel_order(instrument, market_index, client_id, exchange_order_id)
            .await
    }

    async fn cancel_all_orders(
        &self,
        instrument: &InstrumentId,
        market_index: &MarketIndex,
    ) -> Result<u32, ExchangeError> {
        self.0.cancel_all_orders(instrument, market_index).await
    }

    async fn poll_user_fills(&self, cursor: Option<&str>) -> Result<Vec<Fill>, ExchangeError> {
        self.0.poll_user_fills(cursor).await
    }

    async fn poll_quotes(&self, instruments: &[InstrumentId]) -> Result<Vec<Quote>, ExchangeError> {
        self.0.poll_quotes(instruments).await
    }

    async fn poll_account_state(&self) -> Result<AccountState, ExchangeError> {
        self.0.poll_account_state().await
    }
}

// ============================================================================
// NoOpExchange - Minimal placeholder for standalone PaperExchange
// ============================================================================

/// Minimal exchange that does nothing - used internally by PaperExchange::new_standalone()
/// All quotes come from queue_quotes/inject_quote, not from this exchange.
pub struct NoOpExchange {
    exchange_id: ExchangeId,
    environment: bot_core::Environment,
}

impl NoOpExchange {
    /// Create a no-op testnet paper exchange identity.
    pub fn new() -> Self {
        Self::with_config("paper-exchange", bot_core::Environment::Testnet)
    }

    /// Create with a custom exchange ID (for backtest mode matching production exchange)
    pub fn with_exchange_id(id: &str) -> Self {
        Self::with_config(id, bot_core::Environment::Testnet)
    }

    /// Create with custom exchange ID and environment (for full compatibility)
    pub fn with_config(id: &str, environment: bot_core::Environment) -> Self {
        Self {
            exchange_id: ExchangeId::new(id),
            environment,
        }
    }
}

impl Default for NoOpExchange {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait::async_trait]
impl Exchange for NoOpExchange {
    fn exchange_id(&self) -> &ExchangeId {
        &self.exchange_id
    }

    fn environment(&self) -> bot_core::Environment {
        self.environment
    }

    async fn place_orders(
        &self,
        _orders: &[OrderInput],
    ) -> Result<Vec<PlaceOrderResult>, ExchangeError> {
        // PaperExchange handles orders itself, this is never called
        Ok(vec![])
    }

    async fn cancel_order(
        &self,
        _instrument: &InstrumentId,
        _market_index: &MarketIndex,
        _client_id: &ClientOrderId,
        _exchange_order_id: Option<&ExchangeOrderId>,
    ) -> Result<(), ExchangeError> {
        Ok(())
    }

    async fn cancel_all_orders(
        &self,
        _instrument: &InstrumentId,
        _market_index: &MarketIndex,
    ) -> Result<u32, ExchangeError> {
        Ok(0)
    }

    async fn poll_user_fills(&self, _cursor: Option<&str>) -> Result<Vec<Fill>, ExchangeError> {
        Ok(vec![])
    }

    async fn poll_quotes(
        &self,
        _instruments: &[InstrumentId],
    ) -> Result<Vec<Quote>, ExchangeError> {
        // PaperExchange uses queue_quotes/inject_quote, not this
        Ok(vec![])
    }

    async fn poll_account_state(&self) -> Result<AccountState, ExchangeError> {
        Ok(AccountState {
            positions: vec![],
            account_value: None,
            unrealized_pnl: None,
        })
    }
}

/// Internal state for the paper exchange
struct PaperState {
    /// Fill simulator handles pending orders, balances, and fill matching
    simulator: FillSimulator,
    /// Margin ledger for accurate perp accounting (positions, margin, equity)
    margin_ledger: MarginLedger,
    /// Simulated fills waiting to be returned
    simulated_fills: Vec<Fill>,
    /// Current time (for fill timestamps)
    time_ms: i64,
    /// Last known quotes (for fill matching)
    last_quotes: HashMap<InstrumentId, Quote>,
    /// Injected quotes for simulation testing (bypasses real exchange)
    injected_quotes: HashMap<InstrumentId, Quote>,
    /// Whether to use injected quotes instead of real ones
    use_injected_quotes: bool,
    /// Queued quotes for batch backtesting (FIFO)
    quote_queue: VecDeque<Quote>,
}

/// Type alias for standalone PaperExchange (no inner exchange dependency)
pub type StandalonePaperExchange = PaperExchange<NoOpExchange>;

/// Create a standalone paper exchange for backtesting without any inner exchange.
/// Uses `queue_quotes()` or `inject_quote()` for price data.
///
/// # Example
/// ```ignore
/// let exchange = create_standalone_paper_exchange(balances);
/// exchange.queue_quotes(historical_quotes).await;
/// // Use exchange for backtesting
/// ```
pub fn create_standalone_paper_exchange(
    initial_balances: HashMap<AssetId, Decimal>,
) -> PaperExchange<NoOpExchange> {
    create_standalone_paper_exchange_with_id(
        initial_balances,
        "paper-exchange",
        bot_core::Environment::Testnet,
    )
}

/// Create a standalone paper exchange with a custom exchange ID and environment.
/// Use exchange_id = "hyperliquid" and environment = Mainnet for backtest mode compatibility.
pub fn create_standalone_paper_exchange_with_id(
    initial_balances: HashMap<AssetId, Decimal>,
    exchange_id: &str,
    environment: bot_core::Environment,
) -> PaperExchange<NoOpExchange> {
    PaperExchange::new(
        NoOpExchange::with_config(exchange_id, environment),
        initial_balances,
    )
}

/// Paper Exchange - wraps a real exchange for quotes, simulates fills locally.
///
/// Uses `FillSimulator` for shared fill matching logic.
///
/// # Usage
/// ```ignore
/// let real_exchange = HyperliquidExchange::new(config);
/// let paper = PaperExchange::new(real_exchange, initial_balances);
/// // Use paper as the exchange - strategies see no difference
/// ```
pub struct PaperExchange<E: Exchange> {
    /// The real exchange for quote data
    quote_source: E,
    /// Internal state
    state: Arc<RwLock<PaperState>>,
}

impl<E: Exchange> PaperExchange<E> {
    /// Create a new paper exchange wrapping a real quote source
    pub fn new(quote_source: E, initial_balances: HashMap<AssetId, Decimal>) -> Self {
        // Extract starting USDC for the margin ledger
        let starting_usdc = initial_balances
            .get(&AssetId::new("USDC"))
            .copied()
            .unwrap_or(Decimal::ZERO);

        Self {
            quote_source,
            state: Arc::new(RwLock::new(PaperState {
                simulator: FillSimulator::new(initial_balances),
                margin_ledger: MarginLedger::new(starting_usdc, Decimal::ZERO),
                simulated_fills: Vec::new(),
                time_ms: bot_core::now_ms(),
                last_quotes: HashMap::new(),
                injected_quotes: HashMap::new(),
                use_injected_quotes: false,
                quote_queue: VecDeque::new(),
            })),
        }
    }

    /// Set a balance directly (for testing)
    pub async fn set_balance(&self, asset: AssetId, amount: Decimal) {
        self.state
            .write()
            .await
            .simulator
            .set_balance(asset, amount);
    }

    /// Register instrument metadata for quote-aware paper/backtest accounting.
    pub async fn register_instrument_meta(&self, meta: &InstrumentMeta) {
        self.state
            .write()
            .await
            .simulator
            .register_instrument_meta(meta);
    }

    /// Register several instrument metadata records.
    pub async fn register_instrument_metas(&self, metas: &[InstrumentMeta]) {
        let mut state = self.state.write().await;
        for meta in metas {
            state.simulator.register_instrument_meta(meta);
        }
    }

    /// Get current balance
    pub async fn balance(&self, asset: &AssetId) -> Decimal {
        self.state.read().await.simulator.balance(asset)
    }

    /// Get pending orders count
    pub async fn pending_orders_count(&self) -> usize {
        self.state.read().await.simulator.pending_orders_count()
    }

    /// Set fee rate for fill simulation (e.g., 0.0004 = 0.04%)
    /// For spot BUY orders, fee is deducted from base asset
    pub async fn set_fee_rate(&self, fee_rate: Decimal) {
        let mut state = self.state.write().await;
        state.simulator.set_fee_rate(fee_rate);
        state.margin_ledger.set_fee_rate(fee_rate);
    }

    /// Get current balances (for testing)
    pub async fn get_balances(&self) -> HashMap<AssetId, Decimal> {
        let state = self.state.read().await;
        let mut balances = HashMap::new();
        // Get USDC balance
        balances.insert(
            AssetId::new("USDC"),
            state.simulator.balance(&AssetId::new("USDC")),
        );
        balances.insert(
            AssetId::new("BTC"),
            state.simulator.balance(&AssetId::new("BTC")),
        );
        balances.insert(
            AssetId::new("ETH"),
            state.simulator.balance(&AssetId::new("ETH")),
        );
        balances
    }

    /// Get position for an instrument (for testing)
    /// Returns the net position qty (positive = long, negative = short)
    pub async fn get_position(&self, instrument: &InstrumentId) -> Decimal {
        let instrument_str = instrument.to_string();
        let state = self.state.read().await;

        // For PERP instruments, use margin ledger
        if instrument_str.ends_with("-PERP") {
            return state.margin_ledger.position_qty(instrument);
        }

        // For SPOT, position is the base asset balance
        let base_asset = if let Some(pos) = instrument_str.rfind('-') {
            AssetId::new(&instrument_str[..pos])
        } else {
            AssetId::new(&instrument_str)
        };
        state.simulator.balance(&base_asset)
    }

    /// Set leverage for an instrument in the margin ledger
    pub async fn set_instrument_leverage(
        &self,
        instrument: &InstrumentId,
        leverage: Decimal,
        max_leverage: Decimal,
    ) {
        self.state
            .write()
            .await
            .margin_ledger
            .set_leverage(instrument, leverage, max_leverage);
    }

    /// Get the margin ledger's free USDC (for testing)
    pub async fn free_usdc(&self) -> Decimal {
        self.state.read().await.margin_ledger.free_usdc()
    }

    // =========================================================================
    // Simulation Testing Controls
    // =========================================================================

    /// Enable simulation mode - quotes will come from injected_quotes instead of real exchange.
    /// This allows deterministic testing where you control the price feed.
    pub async fn enable_simulation_mode(&self) {
        self.state.write().await.use_injected_quotes = true;
    }

    /// Disable simulation mode - return to using real exchange quotes.
    pub async fn disable_simulation_mode(&self) {
        self.state.write().await.use_injected_quotes = false;
    }

    /// Check if simulation mode is enabled.
    pub async fn is_simulation_mode(&self) -> bool {
        self.state.read().await.use_injected_quotes
    }

    /// Inject a quote for simulation testing.
    /// When simulation mode is enabled, poll_quotes will return these instead of real quotes.
    pub async fn inject_quote(&self, instrument: InstrumentId, bid: Decimal, ask: Decimal) {
        let mut state = self.state.write().await;
        let quote = Quote {
            instrument: instrument.clone(),
            bid: bot_core::Price::new(bid),
            ask: bot_core::Price::new(ask),
            bid_size: Qty::new(Decimal::new(1000, 0)),
            ask_size: Qty::new(Decimal::new(1000, 0)),
            ts: state.time_ms,
        };
        state.injected_quotes.insert(instrument, quote);
    }

    // =========================================================================
    // Batch Backtesting Controls
    // =========================================================================

    /// Queue multiple quotes for batch backtesting (FIFO).
    /// When quotes are queued, poll_quotes will pop from this queue first.
    /// This enables fast-forward backtesting with realistic price-crossing fills.
    pub async fn queue_quotes(&self, quotes: Vec<Quote>) {
        let mut state = self.state.write().await;
        state.quote_queue.extend(quotes);
    }

    /// Check if there are still queued quotes remaining.
    /// Use this to detect when backtesting is complete.
    pub async fn has_queued_quotes(&self) -> bool {
        !self.state.read().await.quote_queue.is_empty()
    }

    /// Get the number of queued quotes remaining.
    pub async fn queue_len(&self) -> usize {
        self.state.read().await.quote_queue.len()
    }

    /// Clear all queued quotes.
    pub async fn clear_quote_queue(&self) {
        self.state.write().await.quote_queue.clear();
    }

    /// Set simulated time (for deterministic testing).
    pub async fn set_time(&self, time_ms: i64) {
        self.state.write().await.time_ms = time_ms;
    }

    /// Advance simulated time by delta milliseconds.
    pub async fn advance_time(&self, delta_ms: i64) {
        self.state.write().await.time_ms += delta_ms;
    }

    /// Get current simulated time.
    pub async fn current_time(&self) -> i64 {
        self.state.read().await.time_ms
    }

    /// Get all simulated fills (for testing).
    pub async fn fills(&self) -> Vec<Fill> {
        self.state.read().await.simulated_fills.clone()
    }

    /// Check if any pending orders can be filled based on current quotes
    async fn check_fills(&self) {
        let mut state = self.state.write().await;
        // Clone to avoid borrow conflict
        let quotes = state.last_quotes.clone();
        let time_ms = state.time_ms;
        let simulated = state.simulator.check_fills(&quotes, time_ms);

        for sim_fill in simulated {
            // Apply margin accounting for PERP fills
            if state
                .simulator
                .instrument_is_perp(&sim_fill.fill.instrument)
            {
                state.margin_ledger.apply_perp_fill(
                    &sim_fill.fill.instrument,
                    sim_fill.fill.side,
                    sim_fill.fill.price.0,
                    sim_fill.fill.qty.0,
                    sim_fill.fill.fee.amount,
                );
            }
            state.simulated_fills.push(sim_fill.fill);
        }
    }

    /// Check if balance is sufficient for order.
    /// For PERP instruments: uses margin-aware check (notional / leverage).
    /// For spot-like instruments: selling requires base, buying requires quote.
    fn check_balance_for_order(
        simulator: &FillSimulator,
        margin_ledger: &MarginLedger,
        order: &OrderInput,
    ) -> Result<(), String> {
        if simulator.instrument_is_perp(&order.instrument) {
            // PERP: delegate to margin-aware check
            margin_ledger.check_margin_for_perp_order(
                &order.instrument,
                order.side,
                order.price.0,
                order.qty.0,
                order.reduce_only,
            )
        } else {
            simulator.check_balance(&order.instrument, order.side, order.price.0, order.qty.0)
        }
    }
}

#[async_trait::async_trait]
impl<E: Exchange + Send + Sync> Exchange for PaperExchange<E> {
    fn exchange_id(&self) -> &ExchangeId {
        self.quote_source.exchange_id()
    }

    fn environment(&self) -> bot_core::Environment {
        self.quote_source.environment()
    }

    async fn place_orders(
        &self,
        orders: &[OrderInput],
    ) -> Result<Vec<PlaceOrderResult>, ExchangeError> {
        #[cfg(feature = "wasm")]
        web_sys::console::log_1(
            &format!(
                "[WASM Runner] place_orders called with {} orders",
                orders.len()
            )
            .into(),
        );
        tracing::debug!(
            "[WASM Runner] place_orders called with {} orders",
            orders.len()
        );
        let mut state = self.state.write().await;
        let mut results = Vec::new();

        for order in orders {
            #[cfg(feature = "wasm")]
            web_sys::console::log_1(
                &format!(
                    "[WASM Runner] Processing order: {:?} {} {} @ {} (instrument={})",
                    order.client_id, order.side, order.qty, order.price, order.instrument
                )
                .into(),
            );
            tracing::debug!(
                "[WASM Runner] Processing order: {:?} {} {} @ {}",
                order.client_id,
                order.side,
                order.qty,
                order.price
            );

            // Check balance using margin-aware logic
            if let Err(reason) =
                Self::check_balance_for_order(&state.simulator, &state.margin_ledger, order)
            {
                #[cfg(feature = "wasm")]
                web_sys::console::log_1(
                    &format!("[WASM Runner] Order REJECTED: {}", reason).into(),
                );
                tracing::debug!("[WASM Runner] Order rejected: {}", reason);
                results.push(PlaceOrderResult::Rejected { reason });
                continue;
            }

            let exchange_order_id = state.simulator.next_exchange_order_id("paper");

            // For IOC orders, check if we can fill immediately
            if order.tif == TimeInForce::Ioc {
                if let Some(quote) = state.last_quotes.get(&order.instrument) {
                    let can_fill = match order.side {
                        bot_core::OrderSide::Buy => quote.ask.0 <= order.price.0,
                        bot_core::OrderSide::Sell => quote.bid.0 >= order.price.0,
                    };

                    if can_fill {
                        let fill_price = match order.side {
                            bot_core::OrderSide::Buy => quote.ask,
                            bot_core::OrderSide::Sell => quote.bid,
                        };

                        let fill = Fill {
                            trade_id: bot_core::TradeId::new(format!(
                                "paper_{}",
                                exchange_order_id.0
                            )),
                            client_id: Some(order.client_id.clone()),
                            exchange_order_id: Some(exchange_order_id.clone()),
                            instrument: order.instrument.clone(),
                            side: order.side,
                            price: fill_price,
                            qty: order.qty.clone(),
                            fee: bot_core::Fee::new(Decimal::ZERO, AssetId::new("USDC")),
                            ts: state.time_ms,
                        };

                        if state.simulator.instrument_is_perp(&fill.instrument) {
                            // PERP: use margin ledger for proper accounting
                            state.margin_ledger.apply_perp_fill(
                                &fill.instrument,
                                fill.side,
                                fill.price.0,
                                fill.qty.0,
                                fill.fee.amount,
                            );
                        }
                        state.simulator.apply_fill(&fill);
                        state.simulated_fills.push(fill.clone());

                        results.push(PlaceOrderResult::Accepted {
                            exchange_order_id: Some(exchange_order_id),
                            filled_qty: Some(fill.qty),
                            avg_fill_px: Some(fill.price),
                        });
                    } else {
                        // IOC cannot fill immediately
                        results.push(PlaceOrderResult::Rejected {
                            reason: "IOC order cannot fill at current price".into(),
                        });
                    }
                } else {
                    // No quote available
                    results.push(PlaceOrderResult::Rejected {
                        reason: "No quote available for instrument".into(),
                    });
                }
            } else {
                // Non-IOC orders go to pending via FillSimulator
                let created_at = state.time_ms; // Copy before mutable borrow
                #[cfg(feature = "wasm")]
                web_sys::console::log_1(
                    &format!(
                        "[WASM Runner] Adding to simulator pending: {} {} @ {} (TIF={:?})",
                        order.side, order.qty, order.price, order.tif
                    )
                    .into(),
                );
                state.simulator.add_pending_order(PendingOrder {
                    client_id: order.client_id.clone(),
                    exchange_order_id: exchange_order_id.clone(),
                    instrument: order.instrument.clone(),
                    side: order.side,
                    price: order.price.clone(),
                    qty: order.qty.clone(),
                    remaining_qty: order.qty.clone(),
                    created_at,
                });
                #[cfg(feature = "wasm")]
                web_sys::console::log_1(
                    &format!(
                        "[WASM Runner] After add: pending_count={}",
                        state.simulator.pending_orders_count()
                    )
                    .into(),
                );

                results.push(PlaceOrderResult::Accepted {
                    exchange_order_id: Some(exchange_order_id),
                    filled_qty: None,
                    avg_fill_px: None,
                });
            }
        }

        Ok(results)
    }

    async fn cancel_order(
        &self,
        _instrument: &InstrumentId,
        _market_index: &MarketIndex,
        client_id: &ClientOrderId,
        _exchange_order_id: Option<&ExchangeOrderId>,
    ) -> Result<(), ExchangeError> {
        #[cfg(feature = "wasm")]
        web_sys::console::log_1(
            &format!("[WASM Runner] cancel_order called: {:?}", client_id).into(),
        );
        let mut state = self.state.write().await;
        state.simulator.remove_order(client_id);
        Ok(())
    }

    async fn cancel_all_orders(
        &self,
        instrument: &InstrumentId,
        _market_index: &MarketIndex,
    ) -> Result<u32, ExchangeError> {
        #[cfg(feature = "wasm")]
        web_sys::console::log_1(
            &format!(
                "[WASM Runner] cancel_all_orders called: instrument={}",
                instrument
            )
            .into(),
        );
        let mut state = self.state.write().await;
        let removed = state.simulator.remove_orders_for_instrument(instrument);
        #[cfg(feature = "wasm")]
        web_sys::console::log_1(
            &format!(
                "[WASM Runner] cancel_all_orders removed {} orders",
                removed.len()
            )
            .into(),
        );
        Ok(removed.len() as u32)
    }

    async fn poll_user_fills(&self, _cursor: Option<&str>) -> Result<Vec<Fill>, ExchangeError> {
        // Check if any pending orders can now fill
        self.check_fills().await;

        // Drain simulated fills
        let mut state = self.state.write().await;
        let fills = std::mem::take(&mut state.simulated_fills);
        Ok(fills)
    }

    async fn poll_quotes(&self, instruments: &[InstrumentId]) -> Result<Vec<Quote>, ExchangeError> {
        let mut state = self.state.write().await;

        // Quote source priority:
        // 1. quote_queue (batch backtest mode) - pop next quote
        // 2. injected_quotes (simulation mode) - use latest injected
        // 3. real exchange (live/paper trading) - fetch from underlying
        let quotes = if !state.quote_queue.is_empty() {
            // Batch backtest mode: pop next quote from queue
            if let Some(quote) = state.quote_queue.pop_front() {
                // Update time to match the quote timestamp
                state.time_ms = quote.ts;
                vec![quote]
            } else {
                vec![]
            }
        } else if state.use_injected_quotes {
            // Simulation mode: use injected quotes
            instruments
                .iter()
                .filter_map(|inst| state.injected_quotes.get(inst).cloned())
                .collect()
        } else {
            // Normal mode: get real quotes from the underlying exchange
            // Note: we need to drop the lock before the await
            drop(state);
            let quotes = self.quote_source.poll_quotes(instruments).await?;
            state = self.state.write().await;
            state.time_ms = bot_core::now_ms();
            quotes
        };

        // Store for fill matching
        for quote in &quotes {
            state
                .last_quotes
                .insert(quote.instrument.clone(), quote.clone());
        }

        // Check fills immediately after updating quotes
        // This is important because runner calls poll_user_fills BEFORE poll_quotes
        let quotes_copy = state.last_quotes.clone();
        let time_ms = state.time_ms;
        let simulated = state.simulator.check_fills(&quotes_copy, time_ms);
        for sim_fill in simulated {
            // Apply margin accounting for PERP fills
            if state
                .simulator
                .instrument_is_perp(&sim_fill.fill.instrument)
            {
                state.margin_ledger.apply_perp_fill(
                    &sim_fill.fill.instrument,
                    sim_fill.fill.side,
                    sim_fill.fill.price.0,
                    sim_fill.fill.qty.0,
                    sim_fill.fill.fee.amount,
                );
            }
            state.simulated_fills.push(sim_fill.fill);
        }

        // Check for liquidations at current mark prices
        let marks: HashMap<InstrumentId, Decimal> = state
            .last_quotes
            .iter()
            .map(|(id, q)| (id.clone(), q.mid().0))
            .collect();
        let liquidated = state.margin_ledger.check_liquidations(&marks);
        for instrument in liquidated {
            if let Some(mark) = marks.get(&instrument) {
                state.margin_ledger.liquidate(&instrument, *mark);
            }
        }

        Ok(quotes)
    }

    async fn poll_account_state(&self) -> Result<AccountState, ExchangeError> {
        let state = self.state.read().await;

        // Build mark prices from last known quotes
        let marks: HashMap<InstrumentId, Decimal> = state
            .last_quotes
            .iter()
            .map(|(id, q)| (id.clone(), q.mid().0))
            .collect();

        // Return real equity including margin positions and unrealized PnL
        let equity = state.margin_ledger.equity(&marks);
        let unrealized = state.margin_ledger.total_unrealized_pnl(&marks);
        let positions = state.margin_ledger.position_snapshots(&marks);

        Ok(AccountState {
            positions,
            account_value: Some(equity),
            unrealized_pnl: Some(unrealized),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bot_core::{OrderSide, Price};

    // Create a minimal mock for testing PaperExchange
    struct MinimalMockExchange {
        exchange_id: ExchangeId,
        quotes: Arc<RwLock<Vec<Quote>>>,
    }

    impl MinimalMockExchange {
        fn new() -> Self {
            Self {
                exchange_id: ExchangeId::new("minimal-mock"),
                quotes: Arc::new(RwLock::new(Vec::new())),
            }
        }

        async fn set_quote(&self, quote: Quote) {
            self.quotes.write().await.push(quote);
        }
    }

    #[async_trait::async_trait]
    impl Exchange for MinimalMockExchange {
        fn exchange_id(&self) -> &ExchangeId {
            &self.exchange_id
        }

        fn environment(&self) -> bot_core::Environment {
            bot_core::Environment::Testnet
        }

        async fn place_orders(
            &self,
            _orders: &[OrderInput],
        ) -> Result<Vec<PlaceOrderResult>, ExchangeError> {
            unimplemented!("MinimalMockExchange doesn't support orders")
        }

        async fn cancel_order(
            &self,
            _instrument: &InstrumentId,
            _market_index: &MarketIndex,
            _client_id: &ClientOrderId,
            _exchange_order_id: Option<&ExchangeOrderId>,
        ) -> Result<(), ExchangeError> {
            Ok(())
        }

        async fn cancel_all_orders(
            &self,
            _instrument: &InstrumentId,
            _market_index: &MarketIndex,
        ) -> Result<u32, ExchangeError> {
            Ok(0)
        }

        async fn poll_user_fills(&self, _cursor: Option<&str>) -> Result<Vec<Fill>, ExchangeError> {
            Ok(vec![])
        }

        async fn poll_quotes(
            &self,
            _instruments: &[InstrumentId],
        ) -> Result<Vec<Quote>, ExchangeError> {
            let quotes = self.quotes.read().await;
            Ok(quotes.clone())
        }

        async fn poll_account_state(&self) -> Result<AccountState, ExchangeError> {
            Ok(AccountState {
                positions: vec![],
                account_value: None,
                unrealized_pnl: None,
            })
        }
    }

    #[tokio::test]
    async fn test_paper_exchange_uses_fill_simulator() {
        let mut balances = HashMap::new();
        balances.insert(AssetId::new("USDC"), Decimal::new(10000, 0));

        let mock = MinimalMockExchange::new();
        let paper = PaperExchange::new(mock, balances);

        // Check USDC balance
        let balance = paper.balance(&AssetId::new("USDC")).await;
        assert_eq!(balance, Decimal::new(10000, 0));
    }

    #[tokio::test]
    async fn test_paper_exchange_accepts_perp_short() {
        let mut balances = HashMap::new();
        balances.insert(AssetId::new("USDC"), Decimal::new(100000, 0)); // 100k margin

        let mock = MinimalMockExchange::new();

        // Set a quote first
        mock.set_quote(Quote {
            instrument: InstrumentId::new("BTC-PERP"),
            bid: Price::new(Decimal::new(50000, 0)),
            ask: Price::new(Decimal::new(50001, 0)),
            bid_size: Qty::new(Decimal::new(10, 0)),
            ask_size: Qty::new(Decimal::new(10, 0)),
            ts: 0,
        })
        .await;

        let paper = PaperExchange::new(mock, balances);

        // Poll quotes to populate last_quotes
        paper
            .poll_quotes(&[InstrumentId::new("BTC-PERP")])
            .await
            .unwrap();

        // Place a SELL (short) order on PERP - should work with only margin
        let orders = vec![OrderInput {
            client_id: ClientOrderId::new("0xtest12345678901234567890123456"),
            instrument: InstrumentId::new("BTC-PERP"),
            market_index: MarketIndex::new(0),
            side: OrderSide::Sell,
            price: Price::new(Decimal::new(51000, 0)),
            qty: Qty::new(Decimal::new(1, 0)),
            tif: bot_core::TimeInForce::Gtc,
            post_only: false,
            reduce_only: false,
        }];

        let results = paper.place_orders(&orders).await.unwrap();

        match &results[0] {
            PlaceOrderResult::Accepted { .. } => { /* expected */ }
            PlaceOrderResult::Rejected { reason } => {
                panic!(
                    "PERP short should be accepted with margin, got rejection: {}",
                    reason
                );
            }
        }
    }
}