nautilus-bybit 0.55.0

Bybit exchange integration adapter for the Nautilus trading engine
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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! WebSocket message dispatch for the Bybit execution client.
//!
//! Routes incoming [`BybitWsMessage`] variants to the appropriate parsing and
//! event emission paths. Tracked orders (submitted through this client) produce
//! proper order events; untracked orders fall back to execution reports for
//! downstream reconciliation.

use std::sync::atomic::{AtomicBool, Ordering};

use ahash::AHashMap;
use anyhow::Context;
use dashmap::{DashMap, DashSet};
use nautilus_core::{UUID4, UnixNanos, time::AtomicTime};
use nautilus_live::ExecutionEventEmitter;
use nautilus_model::{
    enums::{LiquiditySide, OrderSide, OrderType},
    events::{
        OrderAccepted, OrderCanceled, OrderEventAny, OrderFilled, OrderTriggered, OrderUpdated,
    },
    identifiers::{AccountId, ClientOrderId, InstrumentId, StrategyId, TradeId, VenueOrderId},
    instruments::{Instrument, InstrumentAny},
    types::{Money, Price, Quantity},
};
use rust_decimal::Decimal;
use ustr::Ustr;

use super::{
    messages::{BybitWsAccountExecution, BybitWsAccountOrder, BybitWsMessage},
    parse::{parse_millis_i64, parse_ws_account_state, parse_ws_position_status_report},
};
use crate::common::{
    enums::BybitOrderStatus,
    parse::{
        make_bybit_symbol, parse_millis_timestamp, parse_price_with_precision,
        parse_quantity_with_precision,
    },
};

const DEDUP_CAPACITY: usize = 10_000;

const BYBIT_OP_ORDER_CREATE: &str = "order.create";
const BYBIT_OP_ORDER_AMEND: &str = "order.amend";
const BYBIT_OP_ORDER_CANCEL: &str = "order.cancel";

/// Order identity context stored at submission time, used by the WS dispatch
/// task to produce proper order events without Cache access.
#[derive(Debug, Clone)]
pub struct OrderIdentity {
    pub instrument_id: InstrumentId,
    pub strategy_id: StrategyId,
    pub order_side: OrderSide,
    pub order_type: OrderType,
}

/// Tracks which type of WS request is pending for a given req_id.
#[derive(Debug, Clone, Copy)]
pub enum PendingOperation {
    Place,
    Cancel,
    Amend,
}

/// Shared state for cross-stream event deduplication between the private
/// and trade WebSocket dispatch loops.
pub type PendingRequestData = (
    Vec<ClientOrderId>,
    Vec<Option<VenueOrderId>>,
    PendingOperation,
);

/// Snapshot of an order's price, quantity, and trigger price at last dispatch.
/// Used to detect modifications when Bybit sends back an order with the same
/// status but changed fields.
#[derive(Debug, Clone)]
pub struct OrderStateSnapshot {
    pub quantity: Quantity,
    pub price: Option<Price>,
    pub trigger_price: Option<Price>,
}

#[derive(Debug)]
pub struct WsDispatchState {
    pub order_identities: DashMap<ClientOrderId, OrderIdentity>,
    pub pending_requests: DashMap<String, PendingRequestData>,
    pub order_snapshots: DashMap<ClientOrderId, OrderStateSnapshot>,
    pub emitted_accepted: DashSet<ClientOrderId>,
    pub triggered_orders: DashSet<ClientOrderId>,
    pub filled_orders: DashSet<ClientOrderId>,
    clearing: AtomicBool,
}

impl Default for WsDispatchState {
    fn default() -> Self {
        Self {
            order_identities: DashMap::new(),
            pending_requests: DashMap::new(),
            order_snapshots: DashMap::new(),
            emitted_accepted: DashSet::default(),
            triggered_orders: DashSet::default(),
            filled_orders: DashSet::default(),
            clearing: AtomicBool::new(false),
        }
    }
}

impl WsDispatchState {
    fn evict_if_full(&self, set: &DashSet<ClientOrderId>) {
        if set.len() >= DEDUP_CAPACITY
            && self
                .clearing
                .compare_exchange(false, true, Ordering::AcqRel, Ordering::Relaxed)
                .is_ok()
        {
            set.clear();
            self.clearing.store(false, Ordering::Release);
        }
    }

    fn insert_accepted(&self, cid: ClientOrderId) {
        self.evict_if_full(&self.emitted_accepted);
        self.emitted_accepted.insert(cid);
    }

    fn insert_filled(&self, cid: ClientOrderId) {
        self.evict_if_full(&self.filled_orders);
        self.filled_orders.insert(cid);
    }

    fn insert_triggered(&self, cid: ClientOrderId) {
        self.evict_if_full(&self.triggered_orders);
        self.triggered_orders.insert(cid);
    }
}

/// Dispatches a WebSocket message with cross-stream deduplication.
///
/// For orders with a tracked identity (submitted through this client), produces
/// proper order events (OrderAccepted, OrderCanceled, OrderFilled, etc.).
/// For untracked orders (external or pre-existing), falls back to execution
/// reports for downstream reconciliation.
#[allow(clippy::too_many_arguments)]
pub fn dispatch_ws_message(
    message: &BybitWsMessage,
    emitter: &ExecutionEventEmitter,
    state: &WsDispatchState,
    account_id: AccountId,
    instruments: &AHashMap<Ustr, InstrumentAny>,
    clock: &AtomicTime,
) {
    match message {
        BybitWsMessage::AccountOrder(msg) => {
            let ts_init = clock.get_time_ns();
            for order in &msg.data {
                let symbol = make_bybit_symbol(order.symbol, order.category);
                let Some(instrument) = instruments.get(&symbol) else {
                    log::warn!("No instrument for order update: {symbol}");
                    continue;
                };
                dispatch_order_update(order, instrument, emitter, state, account_id, ts_init);
            }
        }
        BybitWsMessage::AccountExecution(msg) => {
            let ts_init = clock.get_time_ns();
            for exec in &msg.data {
                let symbol = make_bybit_symbol(exec.symbol, exec.category);
                let Some(instrument) = instruments.get(&symbol) else {
                    log::warn!("No instrument for execution update: {symbol}");
                    continue;
                };
                dispatch_execution_fill(exec, instrument, emitter, state, account_id, ts_init);
            }
        }
        BybitWsMessage::AccountWallet(msg) => {
            let ts_init = clock.get_time_ns();
            let ts_event = parse_millis_i64(msg.creation_time, "wallet.creation_time")
                .unwrap_or_else(|e| {
                    log::warn!("Failed to parse wallet creation_time, using ts_init: {e}");
                    ts_init
                });
            for wallet in &msg.data {
                match parse_ws_account_state(wallet, account_id, ts_event, ts_init) {
                    Ok(state) => emitter.send_account_state(state),
                    Err(e) => log::error!("Failed to parse account state: {e}"),
                }
            }
        }
        BybitWsMessage::AccountPosition(msg) => {
            let ts_init = clock.get_time_ns();
            for position in &msg.data {
                let symbol = make_bybit_symbol(position.symbol, position.category);
                let Some(instrument) = instruments.get(&symbol) else {
                    log::warn!("No instrument for position update: {symbol}");
                    continue;
                };
                match parse_ws_position_status_report(position, account_id, instrument, ts_init) {
                    Ok(report) => emitter.send_position_report(report),
                    Err(e) => log::error!("Failed to parse position status report: {e}"),
                }
            }
        }
        BybitWsMessage::OrderResponse(resp) => {
            let ts_init = clock.get_time_ns();
            dispatch_order_response(resp, emitter, state, ts_init);
        }
        BybitWsMessage::Error(e) => {
            log::warn!("WebSocket error: code={} message={}", e.code, e.message);
        }
        BybitWsMessage::Reconnected => {
            log::info!("WebSocket reconnected");
        }
        BybitWsMessage::Auth(_)
        | BybitWsMessage::Orderbook(_)
        | BybitWsMessage::Trade(_)
        | BybitWsMessage::Kline(_)
        | BybitWsMessage::TickerLinear(_)
        | BybitWsMessage::TickerOption(_) => {}
    }
}

/// Dispatches a single order status update.
///
/// Tracked orders produce lifecycle events (OrderAccepted, OrderTriggered,
/// OrderCanceled, OrderRejected). Untracked orders fall back to
/// `OrderStatusReport` for reconciliation.
fn dispatch_order_update(
    order: &BybitWsAccountOrder,
    instrument: &InstrumentAny,
    emitter: &ExecutionEventEmitter,
    state: &WsDispatchState,
    account_id: AccountId,
    ts_init: UnixNanos,
) {
    let client_order_id = if order.order_link_id.is_empty() {
        None
    } else {
        Some(ClientOrderId::new(order.order_link_id.as_str()))
    };

    let identity = client_order_id
        .as_ref()
        .and_then(|cid| state.order_identities.get(cid).map(|r| r.clone()));

    if let (Some(client_order_id), Some(identity)) = (client_order_id, identity) {
        let venue_order_id = VenueOrderId::new(order.order_id.as_str());

        match order.order_status {
            BybitOrderStatus::Created | BybitOrderStatus::New | BybitOrderStatus::Untriggered => {
                let snapshot = parse_order_snapshot(order, instrument);

                if state.emitted_accepted.contains(&client_order_id)
                    || state.filled_orders.contains(&client_order_id)
                    || state.triggered_orders.contains(&client_order_id)
                {
                    if let Some(snapshot) = snapshot
                        && is_snapshot_updated(&snapshot, &client_order_id, state)
                    {
                        let updated = OrderUpdated::new(
                            emitter.trader_id(),
                            identity.strategy_id,
                            identity.instrument_id,
                            client_order_id,
                            snapshot.quantity,
                            UUID4::new(),
                            ts_init,
                            ts_init,
                            false,
                            Some(venue_order_id),
                            Some(account_id),
                            snapshot.price,
                            snapshot.trigger_price,
                            None,
                            false,
                        );
                        state.order_snapshots.insert(client_order_id, snapshot);
                        emitter.send_order_event(OrderEventAny::Updated(updated));
                        return;
                    }
                    log::debug!("Skipping duplicate Accepted for {client_order_id}");
                    return;
                }

                state.insert_accepted(client_order_id);

                if let Some(snapshot) = snapshot {
                    state.order_snapshots.insert(client_order_id, snapshot);
                }

                let accepted = OrderAccepted::new(
                    emitter.trader_id(),
                    identity.strategy_id,
                    identity.instrument_id,
                    client_order_id,
                    venue_order_id,
                    account_id,
                    UUID4::new(),
                    ts_init,
                    ts_init,
                    false,
                );
                emitter.send_order_event(OrderEventAny::Accepted(accepted));
            }
            BybitOrderStatus::Triggered => {
                if state.filled_orders.contains(&client_order_id) {
                    log::debug!("Skipping stale Triggered for {client_order_id} (already filled)");
                    return;
                }
                ensure_accepted_emitted(
                    client_order_id,
                    account_id,
                    venue_order_id,
                    &identity,
                    emitter,
                    state,
                    ts_init,
                );
                state.insert_triggered(client_order_id);
                let triggered = OrderTriggered::new(
                    emitter.trader_id(),
                    identity.strategy_id,
                    identity.instrument_id,
                    client_order_id,
                    UUID4::new(),
                    ts_init,
                    ts_init,
                    false,
                    Some(venue_order_id),
                    Some(account_id),
                );
                emitter.send_order_event(OrderEventAny::Triggered(triggered));
            }
            BybitOrderStatus::Rejected => {
                let filled_qty = parse_quantity_with_precision(
                    &order.cum_exec_qty,
                    instrument.size_precision(),
                    "order.cumExecQty",
                )
                .unwrap_or_default();

                if filled_qty.is_positive() {
                    // Partially filled then rejected - treat as canceled
                    ensure_accepted_emitted(
                        client_order_id,
                        account_id,
                        venue_order_id,
                        &identity,
                        emitter,
                        state,
                        ts_init,
                    );
                    let canceled = OrderCanceled::new(
                        emitter.trader_id(),
                        identity.strategy_id,
                        identity.instrument_id,
                        client_order_id,
                        UUID4::new(),
                        ts_init,
                        ts_init,
                        false,
                        Some(venue_order_id),
                        Some(account_id),
                    );
                    cleanup_terminal(client_order_id, state);
                    emitter.send_order_event(OrderEventAny::Canceled(canceled));
                } else {
                    let reason = if order.reject_reason.is_empty() {
                        Ustr::from("Order rejected by venue")
                    } else {
                        order.reject_reason
                    };
                    state.order_identities.remove(&client_order_id);
                    state.order_snapshots.remove(&client_order_id);
                    emitter.emit_order_rejected_event(
                        identity.strategy_id,
                        identity.instrument_id,
                        client_order_id,
                        reason.as_str(),
                        ts_init,
                        false,
                    );
                }
            }
            BybitOrderStatus::PartiallyFilled => {
                // Fills arrive on the execution channel; no event needed here.
                // Ensure accepted was emitted so the fill has a valid prior state.
                ensure_accepted_emitted(
                    client_order_id,
                    account_id,
                    venue_order_id,
                    &identity,
                    emitter,
                    state,
                    ts_init,
                );

                // A successful amend on a partially filled order keeps the
                // PartiallyFilled status. Detect price/qty/trigger changes and
                // emit OrderUpdated so PendingUpdate resolves.
                if let Some(snapshot) = parse_order_snapshot(order, instrument)
                    && is_snapshot_updated(&snapshot, &client_order_id, state)
                {
                    let updated = OrderUpdated::new(
                        emitter.trader_id(),
                        identity.strategy_id,
                        identity.instrument_id,
                        client_order_id,
                        snapshot.quantity,
                        UUID4::new(),
                        ts_init,
                        ts_init,
                        false,
                        Some(venue_order_id),
                        Some(account_id),
                        snapshot.price,
                        snapshot.trigger_price,
                        None,
                        false,
                    );
                    state.order_snapshots.insert(client_order_id, snapshot);
                    emitter.send_order_event(OrderEventAny::Updated(updated));
                }
            }
            BybitOrderStatus::Filled => {
                // Fills arrive on the execution channel; no event needed here.
                // Ensure accepted was emitted so the fill has a valid prior state.
                ensure_accepted_emitted(
                    client_order_id,
                    account_id,
                    venue_order_id,
                    &identity,
                    emitter,
                    state,
                    ts_init,
                );
                // Identity cleaned up in dispatch_execution_fill when leaves_qty
                // reaches zero, since there is no guaranteed ordering between
                // the order and execution topics.
            }
            BybitOrderStatus::Canceled
            | BybitOrderStatus::PartiallyFilledCanceled
            | BybitOrderStatus::Deactivated => {
                ensure_accepted_emitted(
                    client_order_id,
                    account_id,
                    venue_order_id,
                    &identity,
                    emitter,
                    state,
                    ts_init,
                );
                let canceled = OrderCanceled::new(
                    emitter.trader_id(),
                    identity.strategy_id,
                    identity.instrument_id,
                    client_order_id,
                    UUID4::new(),
                    ts_init,
                    ts_init,
                    false,
                    Some(venue_order_id),
                    Some(account_id),
                );
                cleanup_terminal(client_order_id, state);
                emitter.send_order_event(OrderEventAny::Canceled(canceled));
            }
        }
    } else {
        // Untracked order: fall back to report for reconciliation
        match super::parse::parse_ws_order_status_report(order, instrument, account_id, ts_init) {
            Ok(report) => emitter.send_order_status_report(report),
            Err(e) => log::error!("Failed to parse order status report: {e}"),
        }
    }
}

/// Dispatches a single execution (fill) message.
///
/// Tracked orders are parsed directly to [`OrderFilled`]. Untracked orders
/// fall back to [`FillReport`] for reconciliation.
fn dispatch_execution_fill(
    exec: &BybitWsAccountExecution,
    instrument: &InstrumentAny,
    emitter: &ExecutionEventEmitter,
    state: &WsDispatchState,
    account_id: AccountId,
    ts_init: UnixNanos,
) {
    let client_order_id = if exec.order_link_id.is_empty() {
        None
    } else {
        Some(ClientOrderId::new(exec.order_link_id.as_str()))
    };

    let identity = client_order_id
        .as_ref()
        .and_then(|cid| state.order_identities.get(cid).map(|r| r.clone()));

    if let (Some(client_order_id), Some(identity)) = (client_order_id, identity) {
        let venue_order_id = VenueOrderId::new(exec.order_id.as_str());

        ensure_accepted_emitted(
            client_order_id,
            account_id,
            venue_order_id,
            &identity,
            emitter,
            state,
            ts_init,
        );

        match parse_order_filled(exec, instrument, &identity, emitter, account_id, ts_init) {
            Ok(filled) => {
                state.insert_filled(client_order_id);
                state.triggered_orders.remove(&client_order_id);
                emitter.send_order_event(OrderEventAny::Filled(filled));

                if exec.leaves_qty == "0" {
                    cleanup_terminal(client_order_id, state);
                }
            }
            Err(e) => log::error!("Failed to parse OrderFilled for {client_order_id}: {e}"),
        }
    } else {
        // Untracked: fall back to FillReport for reconciliation
        match super::parse::parse_ws_fill_report(exec, account_id, instrument, ts_init) {
            Ok(report) => emitter.send_fill_report(report),
            Err(e) => log::error!("Failed to parse fill report: {e}"),
        }
    }
}

/// Parses a Bybit execution message directly into an [`OrderFilled`] event.
fn parse_order_filled(
    exec: &BybitWsAccountExecution,
    instrument: &InstrumentAny,
    identity: &OrderIdentity,
    emitter: &ExecutionEventEmitter,
    account_id: AccountId,
    ts_init: UnixNanos,
) -> anyhow::Result<OrderFilled> {
    let client_order_id = ClientOrderId::new(exec.order_link_id.as_str());
    let venue_order_id = VenueOrderId::new(exec.order_id.as_str());
    let trade_id =
        TradeId::new_checked(exec.exec_id.as_str()).context("invalid execId in Bybit execution")?;

    let last_qty = parse_quantity_with_precision(
        &exec.exec_qty,
        instrument.size_precision(),
        "execution.execQty",
    )?;
    let last_px = parse_price_with_precision(
        &exec.exec_price,
        instrument.price_precision(),
        "execution.execPrice",
    )?;

    let liquidity_side = if exec.is_maker {
        LiquiditySide::Maker
    } else {
        LiquiditySide::Taker
    };

    let fee_decimal: Decimal = exec
        .exec_fee
        .parse()
        .with_context(|| format!("failed to parse execFee='{}'", exec.exec_fee))?;
    let commission_currency = instrument.quote_currency();
    let commission = Money::from_decimal(fee_decimal, commission_currency).with_context(|| {
        format!(
            "failed to create commission from execFee='{}'",
            exec.exec_fee
        )
    })?;

    let ts_event = parse_millis_timestamp(&exec.exec_time, "execution.execTime")?;

    Ok(OrderFilled::new(
        emitter.trader_id(),
        identity.strategy_id,
        identity.instrument_id,
        client_order_id,
        venue_order_id,
        account_id,
        trade_id,
        identity.order_side,
        identity.order_type,
        last_qty,
        last_px,
        commission_currency,
        liquidity_side,
        UUID4::new(),
        ts_event,
        ts_init,
        false,
        None, // venue_position_id
        Some(commission),
    ))
}

/// Handles a Bybit WS order response, emitting rejection events for failures.
fn dispatch_order_response(
    resp: &super::messages::BybitWsOrderResponse,
    emitter: &ExecutionEventEmitter,
    state: &WsDispatchState,
    ts_init: UnixNanos,
) {
    if resp.ret_code == 0 {
        // Check for per-order failures in batch retExtInfo even on success
        let pending = resp
            .req_id
            .as_ref()
            .and_then(|rid| state.pending_requests.remove(rid))
            .map(|(_, v)| v);

        if let Some((cids, voids, pending_op)) = pending {
            let batch_errors = resp.extract_batch_errors();
            let data_array = resp.data.as_array();

            for (idx, error) in batch_errors.iter().enumerate() {
                if error.code == 0 {
                    continue;
                }

                // Extract orderLinkId from the corresponding data entry
                let cid = data_array
                    .and_then(|arr| arr.get(idx))
                    .and_then(extract_order_link_id_from_data)
                    .or_else(|| cids.get(idx).copied());

                let Some(cid) = cid else {
                    log::warn!(
                        "Batch error at index {idx} without correlation: code={}, msg={}",
                        error.code,
                        error.msg,
                    );
                    continue;
                };

                let Some(identity) = state.order_identities.get(&cid).map(|r| r.clone()) else {
                    log::warn!(
                        "Batch error for untracked order: client_order_id={cid}, msg={}",
                        error.msg,
                    );
                    continue;
                };

                let stored_void = voids.get(idx).and_then(|v| *v);

                emit_rejection_for_op(
                    &pending_op,
                    cid,
                    &identity,
                    stored_void,
                    &error.msg,
                    emitter,
                    state,
                    ts_init,
                );
            }
        }
        return;
    }

    // Remove the pending request entry (if any) to get client_order_ids and op
    let pending = resp
        .req_id
        .as_ref()
        .and_then(|rid| state.pending_requests.remove(rid))
        .map(|(_, v)| v);

    let effective_op = pending
        .as_ref()
        .map(|(_, _, op)| *op)
        .or_else(|| pending_op_from_str(resp.op.as_str()))
        .unwrap_or_else(|| {
            log::warn!("Unknown order operation '{}', defaulting to Place", resp.op);
            PendingOperation::Place
        });

    // For batch rejections (ret_code != 0), emit rejections for ALL orders
    if let Some((cids, voids, _)) = &pending
        && cids.len() > 1
    {
        for (idx, cid) in cids.iter().enumerate() {
            let Some(identity) = state.order_identities.get(cid).map(|r| r.clone()) else {
                log::warn!(
                    "Batch reject for untracked order: client_order_id={cid}, ret_msg={}",
                    resp.ret_msg,
                );
                continue;
            };
            let void = voids.get(idx).and_then(|v| *v);
            emit_rejection_for_op(
                &effective_op,
                *cid,
                &identity,
                void,
                &resp.ret_msg,
                emitter,
                state,
                ts_init,
            );
        }
        return;
    }

    // Single-order rejection path
    let client_order_id = extract_order_link_id_from_data(&resp.data).or_else(|| {
        pending
            .as_ref()
            .and_then(|(cids, _, _)| cids.first().copied())
    });

    let stored_venue_order_id = pending
        .as_ref()
        .and_then(|(_, voids, _)| voids.first().and_then(|v| *v));

    let Some(client_order_id) = client_order_id else {
        log::warn!(
            "Order response error without correlation: op={}, ret_code={}, ret_msg={}, req_id={:?}",
            resp.op,
            resp.ret_code,
            resp.ret_msg,
            resp.req_id,
        );
        return;
    };

    let Some(identity) = state
        .order_identities
        .get(&client_order_id)
        .map(|r| r.clone())
    else {
        log::warn!(
            "Order response error for untracked order: op={}, client_order_id={client_order_id}, ret_msg={}",
            resp.op,
            resp.ret_msg,
        );
        return;
    };

    let venue_order_id = extract_venue_order_id_from_data(&resp.data).or(stored_venue_order_id);

    emit_rejection_for_op(
        &effective_op,
        client_order_id,
        &identity,
        venue_order_id,
        &resp.ret_msg,
        emitter,
        state,
        ts_init,
    );
}

/// Emits the appropriate rejection event based on the pending operation type.
#[allow(clippy::too_many_arguments)]
fn emit_rejection_for_op(
    pending_op: &PendingOperation,
    client_order_id: ClientOrderId,
    identity: &OrderIdentity,
    venue_order_id: Option<VenueOrderId>,
    reason: &str,
    emitter: &ExecutionEventEmitter,
    state: &WsDispatchState,
    ts_init: UnixNanos,
) {
    match pending_op {
        PendingOperation::Place => {
            state.order_identities.remove(&client_order_id);
            emitter.emit_order_rejected_event(
                identity.strategy_id,
                identity.instrument_id,
                client_order_id,
                reason,
                ts_init,
                false,
            );
        }
        PendingOperation::Cancel => {
            emitter.emit_order_cancel_rejected_event(
                identity.strategy_id,
                identity.instrument_id,
                client_order_id,
                venue_order_id,
                reason,
                ts_init,
            );
        }
        PendingOperation::Amend => {
            emitter.emit_order_modify_rejected_event(
                identity.strategy_id,
                identity.instrument_id,
                client_order_id,
                venue_order_id,
                reason,
                ts_init,
            );
        }
    }
}

/// Maps an operation string to a `PendingOperation`.
fn pending_op_from_str(op: &str) -> Option<PendingOperation> {
    match op {
        BYBIT_OP_ORDER_CREATE => Some(PendingOperation::Place),
        BYBIT_OP_ORDER_CANCEL => Some(PendingOperation::Cancel),
        BYBIT_OP_ORDER_AMEND => Some(PendingOperation::Amend),
        _ => None,
    }
}

/// Parses an order snapshot from a WS order message for modification detection.
fn parse_order_snapshot(
    order: &BybitWsAccountOrder,
    instrument: &InstrumentAny,
) -> Option<OrderStateSnapshot> {
    let quantity =
        parse_quantity_with_precision(&order.qty, instrument.size_precision(), "order.qty").ok()?;

    let price = if !order.price.is_empty() && order.price != "0" {
        parse_price_with_precision(&order.price, instrument.price_precision(), "order.price").ok()
    } else {
        None
    };

    let trigger_price = if !order.trigger_price.is_empty() && order.trigger_price != "0" {
        parse_price_with_precision(
            &order.trigger_price,
            instrument.price_precision(),
            "order.triggerPrice",
        )
        .ok()
    } else {
        None
    };

    Some(OrderStateSnapshot {
        quantity,
        price,
        trigger_price,
    })
}

/// Returns whether the incoming snapshot differs from the stored snapshot.
fn is_snapshot_updated(
    snapshot: &OrderStateSnapshot,
    client_order_id: &ClientOrderId,
    state: &WsDispatchState,
) -> bool {
    let Some(previous) = state.order_snapshots.get(client_order_id) else {
        return false;
    };

    if let (Some(prev_price), Some(new_price)) = (previous.price, snapshot.price)
        && prev_price != new_price
    {
        return true;
    }

    if let (Some(prev_trigger), Some(new_trigger)) =
        (previous.trigger_price, snapshot.trigger_price)
        && prev_trigger != new_trigger
    {
        return true;
    }

    previous.quantity != snapshot.quantity
}

/// Synthesizes and emits `OrderAccepted` if one has not yet been emitted for
/// this order. Handles fast-filling orders that skip the `New` state on Bybit.
fn ensure_accepted_emitted(
    client_order_id: ClientOrderId,
    account_id: AccountId,
    venue_order_id: VenueOrderId,
    identity: &OrderIdentity,
    emitter: &ExecutionEventEmitter,
    state: &WsDispatchState,
    ts_init: UnixNanos,
) {
    if state.emitted_accepted.contains(&client_order_id) {
        return;
    }
    state.insert_accepted(client_order_id);
    let accepted = OrderAccepted::new(
        emitter.trader_id(),
        identity.strategy_id,
        identity.instrument_id,
        client_order_id,
        venue_order_id,
        account_id,
        UUID4::new(),
        ts_init,
        ts_init,
        false,
    );
    emitter.send_order_event(OrderEventAny::Accepted(accepted));
}

/// Removes a terminal order from all tracking sets.
fn cleanup_terminal(client_order_id: ClientOrderId, state: &WsDispatchState) {
    state.order_identities.remove(&client_order_id);
    state.order_snapshots.remove(&client_order_id);
    state.emitted_accepted.remove(&client_order_id);
    state.triggered_orders.remove(&client_order_id);
    state.filled_orders.remove(&client_order_id);
}

/// Tries to extract `orderLinkId` from the response data Value.
fn extract_order_link_id_from_data(data: &serde_json::Value) -> Option<ClientOrderId> {
    data.get("orderLinkId")
        .and_then(|v| v.as_str())
        .filter(|s| !s.is_empty())
        .map(ClientOrderId::new)
}

/// Tries to extract `orderId` from the response data Value.
fn extract_venue_order_id_from_data(data: &serde_json::Value) -> Option<VenueOrderId> {
    data.get("orderId")
        .and_then(|v| v.as_str())
        .filter(|s| !s.is_empty())
        .map(VenueOrderId::new)
}

#[cfg(test)]
mod tests {
    use ahash::AHashMap;
    use nautilus_common::messages::{ExecutionEvent, execution::ExecutionReport};
    use nautilus_core::{
        UnixNanos,
        time::{AtomicTime, get_atomic_clock_realtime},
    };
    use nautilus_live::emitter::ExecutionEventEmitter;
    use nautilus_model::{
        enums::{AccountType, OrderSide, OrderType},
        events::OrderEventAny,
        identifiers::{AccountId, ClientOrderId, InstrumentId, StrategyId, TraderId},
        instruments::{Instrument, InstrumentAny},
    };
    use rstest::rstest;
    use ustr::Ustr;

    use super::*;
    use crate::{
        common::{parse::parse_linear_instrument, testing::load_test_json},
        http::models::{BybitFeeRate, BybitInstrumentLinearResponse},
        websocket::messages::BybitWsMessage,
    };

    fn sample_fee_rate(
        symbol: &str,
        taker: &str,
        maker: &str,
        base_coin: Option<&str>,
    ) -> BybitFeeRate {
        BybitFeeRate {
            symbol: Ustr::from(symbol),
            taker_fee_rate: taker.to_string(),
            maker_fee_rate: maker.to_string(),
            base_coin: base_coin.map(Ustr::from),
        }
    }

    fn linear_instrument() -> InstrumentAny {
        let json = load_test_json("http_get_instruments_linear.json");
        let response: BybitInstrumentLinearResponse = serde_json::from_str(&json).unwrap();
        let instrument = &response.result.list[0];
        let fee_rate = sample_fee_rate("BTCUSDT", "0.00055", "0.0001", Some("BTC"));
        let ts = UnixNanos::new(1_700_000_000_000_000_000);
        parse_linear_instrument(instrument, &fee_rate, ts, ts).unwrap()
    }

    fn build_instruments(instruments: &[InstrumentAny]) -> AHashMap<Ustr, InstrumentAny> {
        let mut map = AHashMap::new();
        for inst in instruments {
            map.insert(inst.id().symbol.inner(), inst.clone());
        }
        map
    }

    fn test_account_id() -> AccountId {
        AccountId::from("BYBIT-001")
    }

    fn create_emitter() -> (
        ExecutionEventEmitter,
        tokio::sync::mpsc::UnboundedReceiver<ExecutionEvent>,
    ) {
        let clock = get_atomic_clock_realtime();
        let trader_id = TraderId::from("TESTER-001");
        let account_id = test_account_id();
        let mut emitter =
            ExecutionEventEmitter::new(clock, trader_id, account_id, AccountType::Margin, None);
        let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
        emitter.set_sender(tx);
        (emitter, rx)
    }

    fn default_identity() -> OrderIdentity {
        OrderIdentity {
            instrument_id: InstrumentId::from("BTCUSDT-LINEAR.BYBIT"),
            strategy_id: StrategyId::from("S-001"),
            order_side: OrderSide::Buy,
            order_type: OrderType::Limit,
        }
    }

    #[rstest]
    fn test_dispatch_tracked_canceled_order_emits_accepted_then_canceled() {
        let instrument = linear_instrument();
        let instruments = build_instruments(std::slice::from_ref(&instrument));
        let (emitter, mut rx) = create_emitter();
        let clock = get_atomic_clock_realtime();
        let state = WsDispatchState::default();

        // Fixture has orderStatus=Cancelled
        let json = load_test_json("ws_account_order.json");
        let msg: crate::websocket::messages::BybitWsAccountOrderMsg =
            serde_json::from_str(&json).unwrap();

        if let Some(order) = msg.data.first()
            && !order.order_link_id.is_empty()
        {
            let cid = ClientOrderId::new(order.order_link_id.as_str());
            state.order_identities.insert(cid, default_identity());
        }

        let ws_msg = BybitWsMessage::AccountOrder(msg);
        dispatch_ws_message(
            &ws_msg,
            &emitter,
            &state,
            test_account_id(),
            &instruments,
            clock,
        );

        // First: synthesized Accepted
        let event1 = rx.try_recv().unwrap();
        assert!(
            matches!(event1, ExecutionEvent::Order(OrderEventAny::Accepted(ref a)) if a.strategy_id == StrategyId::from("S-001")),
            "Expected Accepted, found {event1:?}"
        );

        // Second: Canceled (from Cancelled status)
        let event2 = rx.try_recv().unwrap();
        assert!(
            matches!(event2, ExecutionEvent::Order(OrderEventAny::Canceled(_))),
            "Expected Canceled, found {event2:?}"
        );
    }

    #[rstest]
    fn test_dispatch_untracked_order_emits_report() {
        let instrument = linear_instrument();
        let instruments = build_instruments(std::slice::from_ref(&instrument));
        let (emitter, mut rx) = create_emitter();
        let clock = get_atomic_clock_realtime();
        let state = WsDispatchState::default();

        let json = load_test_json("ws_account_order.json");
        let msg: crate::websocket::messages::BybitWsAccountOrderMsg =
            serde_json::from_str(&json).unwrap();

        // No identity registered → untracked
        let ws_msg = BybitWsMessage::AccountOrder(msg);
        dispatch_ws_message(
            &ws_msg,
            &emitter,
            &state,
            test_account_id(),
            &instruments,
            clock,
        );

        let event = rx.try_recv().unwrap();
        assert!(matches!(
            event,
            ExecutionEvent::Report(ExecutionReport::Order(_))
        ));
    }

    #[rstest]
    fn test_dispatch_tracked_execution_emits_order_filled() {
        let instrument = linear_instrument();
        let instruments = build_instruments(std::slice::from_ref(&instrument));
        let (emitter, mut rx) = create_emitter();
        let clock = get_atomic_clock_realtime();
        let state = WsDispatchState::default();

        let json = load_test_json("ws_account_execution.json");
        let msg: crate::websocket::messages::BybitWsAccountExecutionMsg =
            serde_json::from_str(&json).unwrap();

        // Register identity for the execution's orderLinkId
        if let Some(exec) = msg.data.first()
            && !exec.order_link_id.is_empty()
        {
            let cid = ClientOrderId::new(exec.order_link_id.as_str());
            state.order_identities.insert(cid, default_identity());
        }

        let ws_msg = BybitWsMessage::AccountExecution(msg);
        dispatch_ws_message(
            &ws_msg,
            &emitter,
            &state,
            test_account_id(),
            &instruments,
            clock,
        );

        // First event should be synthesized Accepted
        let event1 = rx.try_recv().unwrap();
        assert!(
            matches!(event1, ExecutionEvent::Order(OrderEventAny::Accepted(_))),
            "Expected Accepted, found {event1:?}"
        );

        // Second event should be OrderFilled
        let event2 = rx.try_recv().unwrap();
        match event2 {
            ExecutionEvent::Order(OrderEventAny::Filled(filled)) => {
                assert_eq!(filled.strategy_id, StrategyId::from("S-001"));
                assert_eq!(filled.order_side, OrderSide::Buy);
                assert_eq!(filled.order_type, OrderType::Limit);
            }
            other => panic!("Expected Filled event, found {other:?}"),
        }
    }

    #[rstest]
    fn test_dispatch_untracked_execution_emits_fill_report() {
        let instrument = linear_instrument();
        let instruments = build_instruments(std::slice::from_ref(&instrument));
        let (emitter, mut rx) = create_emitter();
        let clock = get_atomic_clock_realtime();
        let state = WsDispatchState::default();

        let json = load_test_json("ws_account_execution.json");
        let msg: crate::websocket::messages::BybitWsAccountExecutionMsg =
            serde_json::from_str(&json).unwrap();

        // No identity registered → untracked
        let ws_msg = BybitWsMessage::AccountExecution(msg);
        dispatch_ws_message(
            &ws_msg,
            &emitter,
            &state,
            test_account_id(),
            &instruments,
            clock,
        );

        let event = rx.try_recv().unwrap();
        assert!(matches!(
            event,
            ExecutionEvent::Report(ExecutionReport::Fill(_))
        ));
    }

    #[rstest]
    fn test_dispatch_wallet_emits_account_state() {
        let instruments = AHashMap::new();
        let (emitter, mut rx) = create_emitter();
        let clock = get_atomic_clock_realtime();
        let state = WsDispatchState::default();

        let json = load_test_json("ws_account_wallet.json");
        let msg: crate::websocket::messages::BybitWsAccountWalletMsg =
            serde_json::from_str(&json).unwrap();
        let ws_msg = BybitWsMessage::AccountWallet(msg);

        dispatch_ws_message(
            &ws_msg,
            &emitter,
            &state,
            test_account_id(),
            &instruments,
            clock,
        );

        let event = rx.try_recv().unwrap();
        assert!(matches!(event, ExecutionEvent::Account(_)));
    }

    #[rstest]
    fn test_dispatch_data_message_ignored() {
        let instruments = AHashMap::new();
        let (emitter, mut rx) = create_emitter();
        let clock = get_atomic_clock_realtime();
        let state = WsDispatchState::default();

        let json = load_test_json("ws_public_trade.json");
        let msg: crate::websocket::messages::BybitWsTradeMsg = serde_json::from_str(&json).unwrap();
        let ws_msg = BybitWsMessage::Trade(msg);

        dispatch_ws_message(
            &ws_msg,
            &emitter,
            &state,
            test_account_id(),
            &instruments,
            clock,
        );

        assert!(rx.try_recv().is_err());
    }

    #[rstest]
    fn test_accepted_dedup_prevents_duplicate() {
        let instrument = linear_instrument();
        let instruments = build_instruments(std::slice::from_ref(&instrument));
        let (emitter, mut rx) = create_emitter();
        let clock = get_atomic_clock_realtime();
        let state = WsDispatchState::default();

        // Fixture has orderStatus=Cancelled. Patch to New for this dedup test.
        let json = load_test_json("ws_account_order.json");
        let mut value: serde_json::Value = serde_json::from_str(&json).unwrap();
        value["data"][0]["orderStatus"] = serde_json::Value::String("New".to_string());
        let msg: crate::websocket::messages::BybitWsAccountOrderMsg =
            serde_json::from_value(value).unwrap();

        if let Some(order) = msg.data.first()
            && !order.order_link_id.is_empty()
        {
            let cid = ClientOrderId::new(order.order_link_id.as_str());
            state.order_identities.insert(cid, default_identity());
        }

        let ws_msg = BybitWsMessage::AccountOrder(msg.clone());
        dispatch_ws_message(
            &ws_msg,
            &emitter,
            &state,
            test_account_id(),
            &instruments,
            clock,
        );

        let event = rx.try_recv().unwrap();
        assert!(matches!(
            event,
            ExecutionEvent::Order(OrderEventAny::Accepted(_))
        ));

        // Dispatch the same message again: dedup should suppress the duplicate
        let ws_msg2 = BybitWsMessage::AccountOrder(msg);
        dispatch_ws_message(
            &ws_msg2,
            &emitter,
            &state,
            test_account_id(),
            &instruments,
            clock,
        );

        assert!(rx.try_recv().is_err());
    }

    fn new_order_value() -> serde_json::Value {
        let json = load_test_json("ws_account_order.json");
        let mut value: serde_json::Value = serde_json::from_str(&json).unwrap();
        value["data"][0]["orderStatus"] = serde_json::Value::String("New".to_string());
        value
    }

    struct DispatchTestContext {
        instruments: AHashMap<Ustr, InstrumentAny>,
        emitter: ExecutionEventEmitter,
        rx: tokio::sync::mpsc::UnboundedReceiver<ExecutionEvent>,
        clock: &'static AtomicTime,
        state: WsDispatchState,
    }

    impl DispatchTestContext {
        fn new() -> Self {
            let instrument = linear_instrument();
            let instruments = build_instruments(std::slice::from_ref(&instrument));
            let (emitter, rx) = create_emitter();
            let clock = get_atomic_clock_realtime();
            let state = WsDispatchState::default();
            Self {
                instruments,
                emitter,
                rx,
                clock,
                state,
            }
        }

        fn accept_order(&mut self, value: &serde_json::Value) {
            let msg: crate::websocket::messages::BybitWsAccountOrderMsg =
                serde_json::from_value(value.clone()).unwrap();

            if let Some(order) = msg.data.first()
                && !order.order_link_id.is_empty()
                && !self
                    .state
                    .order_identities
                    .contains_key(&ClientOrderId::new(order.order_link_id.as_str()))
            {
                let cid = ClientOrderId::new(order.order_link_id.as_str());
                self.state.order_identities.insert(cid, default_identity());
            }

            self.dispatch_value(value);

            let event = self.rx.try_recv().unwrap();
            assert!(
                matches!(event, ExecutionEvent::Order(OrderEventAny::Accepted(_))),
                "Expected Accepted, found {event:?}"
            );
        }

        fn dispatch_value(&self, value: &serde_json::Value) {
            let msg: crate::websocket::messages::BybitWsAccountOrderMsg =
                serde_json::from_value(value.clone()).unwrap();
            let ws_msg = BybitWsMessage::AccountOrder(msg);
            dispatch_ws_message(
                &ws_msg,
                &self.emitter,
                &self.state,
                test_account_id(),
                &self.instruments,
                self.clock,
            );
        }

        fn recv_updated(&mut self) -> OrderUpdated {
            let event = self.rx.try_recv().unwrap();
            match event {
                ExecutionEvent::Order(OrderEventAny::Updated(updated)) => updated,
                other => panic!("Expected Updated event, found {other:?}"),
            }
        }
    }

    #[rstest]
    fn test_dispatch_order_updated_on_price_change() {
        let mut ctx = DispatchTestContext::new();
        let value = new_order_value();
        ctx.accept_order(&value);

        let mut amended = value;
        amended["data"][0]["price"] = serde_json::Value::String("31000".to_string());
        ctx.dispatch_value(&amended);

        let updated = ctx.recv_updated();
        assert_eq!(updated.client_order_id, ClientOrderId::from("client-1"));
        assert_eq!(updated.price, Some(Price::from("31000.00")));
        assert_eq!(updated.quantity, Quantity::from("0.010"));
        assert_eq!(updated.trigger_price, None);
        assert!(updated.venue_order_id.is_some());
    }

    #[rstest]
    fn test_dispatch_order_updated_on_quantity_change() {
        let mut ctx = DispatchTestContext::new();
        let value = new_order_value();
        ctx.accept_order(&value);

        let mut amended = value;
        amended["data"][0]["qty"] = serde_json::Value::String("0.020".to_string());
        ctx.dispatch_value(&amended);

        let updated = ctx.recv_updated();
        assert_eq!(updated.quantity, Quantity::from("0.020"));
        assert_eq!(updated.price, Some(Price::from("30000.00")));
    }

    #[rstest]
    fn test_dispatch_order_updated_on_trigger_price_change() {
        let mut ctx = DispatchTestContext::new();
        let mut value = new_order_value();
        value["data"][0]["triggerPrice"] = serde_json::Value::String("29000".to_string());
        ctx.accept_order(&value);

        let mut amended = value;
        amended["data"][0]["triggerPrice"] = serde_json::Value::String("28000".to_string());
        ctx.dispatch_value(&amended);

        let updated = ctx.recv_updated();
        assert_eq!(updated.trigger_price, Some(Price::from("28000.00")));
        assert_eq!(updated.price, Some(Price::from("30000.00")));
    }

    #[rstest]
    fn test_dispatch_dedup_suppresses_identical_after_snapshot() {
        let mut ctx = DispatchTestContext::new();
        let value = new_order_value();
        ctx.accept_order(&value);

        ctx.dispatch_value(&value);

        assert!(
            ctx.rx.try_recv().is_err(),
            "Expected no event for identical redelivery"
        );
    }

    #[rstest]
    fn test_dispatch_order_updated_stores_snapshot_for_subsequent_change() {
        let mut ctx = DispatchTestContext::new();
        let value = new_order_value();
        ctx.accept_order(&value);

        let mut amended1 = value.clone();
        amended1["data"][0]["price"] = serde_json::Value::String("31000".to_string());
        ctx.dispatch_value(&amended1);
        let _ = ctx.recv_updated();

        let mut amended2 = value;
        amended2["data"][0]["price"] = serde_json::Value::String("32000".to_string());
        ctx.dispatch_value(&amended2);

        let updated = ctx.recv_updated();
        assert_eq!(updated.price, Some(Price::from("32000.00")));
    }

    #[rstest]
    #[case::price_changed(
        Some(Price::from("100.00")),
        None,
        Quantity::from("1.000"),
        Some(Price::from("200.00")),
        None,
        Quantity::from("1.000"),
        true
    )]
    #[case::trigger_changed(
        None,
        Some(Price::from("100.00")),
        Quantity::from("1.000"),
        None,
        Some(Price::from("90.00")),
        Quantity::from("1.000"),
        true
    )]
    #[case::qty_changed(
        Some(Price::from("100.00")),
        None,
        Quantity::from("1.000"),
        Some(Price::from("100.00")),
        None,
        Quantity::from("2.000"),
        true
    )]
    #[case::no_change(
        Some(Price::from("100.00")),
        None,
        Quantity::from("1.000"),
        Some(Price::from("100.00")),
        None,
        Quantity::from("1.000"),
        false
    )]
    fn test_is_snapshot_updated(
        #[case] prev_price: Option<Price>,
        #[case] prev_trigger: Option<Price>,
        #[case] prev_qty: Quantity,
        #[case] new_price: Option<Price>,
        #[case] new_trigger: Option<Price>,
        #[case] new_qty: Quantity,
        #[case] expected: bool,
    ) {
        let state = WsDispatchState::default();
        let cid = ClientOrderId::from("test-1");
        state.order_snapshots.insert(
            cid,
            OrderStateSnapshot {
                quantity: prev_qty,
                price: prev_price,
                trigger_price: prev_trigger,
            },
        );

        let new_snapshot = OrderStateSnapshot {
            quantity: new_qty,
            price: new_price,
            trigger_price: new_trigger,
        };
        assert_eq!(is_snapshot_updated(&new_snapshot, &cid, &state), expected);
    }

    #[rstest]
    fn test_is_snapshot_updated_no_previous() {
        let state = WsDispatchState::default();
        let cid = ClientOrderId::from("test-1");

        let new_snapshot = OrderStateSnapshot {
            quantity: Quantity::from("1.000"),
            price: Some(Price::from("100.00")),
            trigger_price: None,
        };
        assert!(!is_snapshot_updated(&new_snapshot, &cid, &state));
    }

    #[rstest]
    #[case::limit_order("30000", "0", Some(Price::from("30000.00")), None)]
    #[case::conditional("0", "29000", None, Some(Price::from("29000.00")))]
    #[case::both(
        "30000",
        "29000",
        Some(Price::from("30000.00")),
        Some(Price::from("29000.00"))
    )]
    fn test_parse_order_snapshot(
        #[case] price: &str,
        #[case] trigger: &str,
        #[case] expected_price: Option<Price>,
        #[case] expected_trigger: Option<Price>,
    ) {
        let instrument = linear_instrument();
        let json = load_test_json("ws_account_order.json");
        let mut value: serde_json::Value = serde_json::from_str(&json).unwrap();
        value["data"][0]["price"] = serde_json::Value::String(price.to_string());
        value["data"][0]["triggerPrice"] = serde_json::Value::String(trigger.to_string());
        let msg: crate::websocket::messages::BybitWsAccountOrderMsg =
            serde_json::from_value(value).unwrap();

        let snapshot = parse_order_snapshot(&msg.data[0], &instrument).unwrap();
        assert_eq!(snapshot.price, expected_price);
        assert_eq!(snapshot.trigger_price, expected_trigger);
        assert_eq!(snapshot.quantity, Quantity::from("0.010"));
    }

    #[rstest]
    fn test_parse_order_snapshot_invalid_qty_returns_none() {
        let instrument = linear_instrument();
        let json = load_test_json("ws_account_order.json");
        let mut value: serde_json::Value = serde_json::from_str(&json).unwrap();
        value["data"][0]["qty"] = serde_json::Value::String(String::new());
        let msg: crate::websocket::messages::BybitWsAccountOrderMsg =
            serde_json::from_value(value).unwrap();

        assert!(parse_order_snapshot(&msg.data[0], &instrument).is_none());
    }

    #[rstest]
    fn test_dispatch_order_updated_on_partially_filled_price_change() {
        let mut ctx = DispatchTestContext::new();
        let value = new_order_value();
        ctx.accept_order(&value);

        let mut amended = value;
        amended["data"][0]["orderStatus"] =
            serde_json::Value::String("PartiallyFilled".to_string());
        amended["data"][0]["cumExecQty"] = serde_json::Value::String("0.005".to_string());
        amended["data"][0]["price"] = serde_json::Value::String("31000".to_string());
        ctx.dispatch_value(&amended);

        let updated = ctx.recv_updated();
        assert_eq!(updated.client_order_id, ClientOrderId::from("client-1"));
        assert_eq!(updated.price, Some(Price::from("31000.00")));
    }
}