rustrade-data 0.12.0

High performance & normalised WebSocket intergration for leading cryptocurrency exchanges - batteries included.
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
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
use super::SubscriptionKind;
use chrono::{DateTime, Duration, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
use std::str::FromStr;
use thiserror::Error;

/// Barter [`Subscription`](super::Subscription) [`SubscriptionKind`] that yields [`Candle`]
/// [`MarketEvent<T>`](crate::event::MarketEvent) events.
///
/// The [`interval`](Self::interval) is intrinsic to a candle subscription — it is
/// the resolution being streamed, so there is no meaningful default (a phantom
/// "1m" default is a silent-bug footgun); the field is always explicit.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
pub struct Candles {
    /// The candle resolution to subscribe to. See [`CandleInterval`].
    pub interval: CandleInterval,
}

impl SubscriptionKind for Candles {
    type Event = Candle;

    /// Returns the fixed kind tag `"candles"`, independent of [`interval`](Self::interval).
    /// The tag identifies the subscription *kind* for routing and stays stable across
    /// resolutions; it is **not** the interval. For the resolution string use
    /// [`CandleInterval::as_str`] on the [`interval`](Self::interval) field — note that
    /// [`Display`](std::fmt::Display) for `Candles` also yields only `"candles"`.
    fn as_str(&self) -> &'static str {
        "candles"
    }
}

impl std::fmt::Display for Candles {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)] // Test code: panics on bad input are acceptable
mod tests {
    use super::*;
    use chrono::Duration;
    use rust_decimal_macros::dec;

    /// Parse an RFC3339 UTC instant in tests.
    fn dt(s: &str) -> DateTime<Utc> {
        DateTime::parse_from_rfc3339(s).unwrap().with_timezone(&Utc)
    }

    /// Build a test [`Candle`] closing at the given RFC3339 instant.
    fn bar(
        close_time: &str,
        open: Decimal,
        high: Decimal,
        low: Decimal,
        close: Decimal,
        volume: Decimal,
        trade_count: u64,
    ) -> Candle {
        Candle {
            close_time: dt(close_time),
            open,
            high,
            low,
            close,
            volume: Some(volume),
            trade_count: Some(trade_count),
        }
    }

    #[test]
    fn fixed_step_adds_duration_exactly() {
        let open = dt("2024-01-15T12:00:00Z");
        assert_eq!(
            close_time_from_open(open, IntervalStep::Fixed(Duration::minutes(1))),
            Some(dt("2024-01-15T12:01:00Z"))
        );
        assert_eq!(
            close_time_from_open(open, IntervalStep::Fixed(Duration::hours(1))),
            Some(dt("2024-01-15T13:00:00Z"))
        );
        // Daily/weekly are exact fixed durations in UTC (no DST).
        assert_eq!(
            close_time_from_open(
                dt("2024-01-15T00:00:00Z"),
                IntervalStep::Fixed(Duration::days(1))
            ),
            Some(dt("2024-01-16T00:00:00Z"))
        );
        assert_eq!(
            close_time_from_open(
                dt("2024-01-15T00:00:00Z"),
                IntervalStep::Fixed(Duration::weeks(1))
            ),
            Some(dt("2024-01-22T00:00:00Z"))
        );
    }

    #[test]
    fn fixed_daily_step_crosses_month_boundary() {
        // A Jan 31 daily bar closes at Feb 1 00:00 UTC via Fixed(1 day).
        assert_eq!(
            close_time_from_open(
                dt("2024-01-31T00:00:00Z"),
                IntervalStep::Fixed(Duration::days(1))
            ),
            Some(dt("2024-02-01T00:00:00Z"))
        );
    }

    #[test]
    fn months_step_uses_calendar_arithmetic() {
        // Jan -> Feb (not +30 days = Jan 31).
        assert_eq!(
            close_time_from_open(dt("2024-01-01T00:00:00Z"), IntervalStep::Months(1)),
            Some(dt("2024-02-01T00:00:00Z"))
        );
        // Leap-year Feb -> Mar (Feb has 29 days in 2024).
        assert_eq!(
            close_time_from_open(dt("2024-02-01T00:00:00Z"), IntervalStep::Months(1)),
            Some(dt("2024-03-01T00:00:00Z"))
        );
        // Quarter = 3 months.
        assert_eq!(
            close_time_from_open(dt("2024-01-01T00:00:00Z"), IntervalStep::Months(3)),
            Some(dt("2024-04-01T00:00:00Z"))
        );
        // Year = 12 months.
        assert_eq!(
            close_time_from_open(dt("2024-01-01T00:00:00Z"), IntervalStep::Months(12)),
            Some(dt("2025-01-01T00:00:00Z"))
        );
    }

    #[test]
    fn months_step_clamps_jan_31_anchor() {
        // Monthly bar opens always land on the 1st from all known producers, so
        // this clamping is unreachable in practice; the test pins chrono's
        // documented behaviour for the variable-length-month edge case.
        // chrono clamps to the last valid day: Jan 31 + 1 month -> Feb 29 (leap year).
        assert_eq!(
            close_time_from_open(dt("2024-01-31T00:00:00Z"), IntervalStep::Months(1)),
            Some(dt("2024-02-29T00:00:00Z"))
        );
    }

    #[test]
    fn overflow_returns_none_not_panic() {
        let max = DateTime::<Utc>::MAX_UTC;
        assert_eq!(close_time_from_open(max, IntervalStep::Months(1)), None);
        assert_eq!(
            close_time_from_open(max, IntervalStep::Fixed(Duration::days(1))),
            None
        );
    }

    #[test]
    fn open_time_from_close_is_inverse() {
        // open = close − interval, for both Fixed and Months steps.
        assert_eq!(
            open_time_from_close(
                dt("2024-01-15T13:00:00Z"),
                IntervalStep::Fixed(Duration::hours(1))
            ),
            Some(dt("2024-01-15T12:00:00Z"))
        );
        // Feb 1 close of a January monthly bar → Jan 1 open.
        assert_eq!(
            open_time_from_close(dt("2024-02-01T00:00:00Z"), IntervalStep::Months(1)),
            Some(dt("2024-01-01T00:00:00Z"))
        );
        // Round-trip identity for the inputs this library actually produces:
        // monthly/quarterly closes always land on a calendar 1st, where chrono's
        // month arithmetic round-trips exactly. (It is NOT a universal identity —
        // `Months` day-clamping is asymmetric for non-1st anchors, e.g.
        // Feb 29 −1mo → Jan 29, +1mo → Feb 29; see `months_step_clamps_jan_31_anchor`.)
        let close = dt("2024-04-01T00:00:00Z");
        let open = open_time_from_close(close, IntervalStep::Months(3)).unwrap();
        assert_eq!(
            close_time_from_open(open, IntervalStep::Months(3)),
            Some(close)
        );
    }

    #[test]
    fn open_time_from_close_underflow_returns_none() {
        let min = DateTime::<Utc>::MIN_UTC;
        assert_eq!(open_time_from_close(min, IntervalStep::Months(1)), None);
        assert_eq!(
            open_time_from_close(min, IntervalStep::Fixed(Duration::days(1))),
            None
        );
    }

    #[test]
    fn candle_interval_all_covers_every_variant_in_ascending_order() {
        // `ALL`'s length is pinned to the variant count by both the
        // `[CandleInterval; 19]` type and this assertion. Full variant *coverage*
        // is not compile-enforced (Rust has no stable variant_count), so keep
        // `ALL` in sync when adding a variant.
        assert_eq!(CandleInterval::ALL.len(), 19);

        // Verify the documented ascending-duration ordering directly via `to_step`.
        // Comparing against the derived `Ord` would be tautological — that order is
        // declaration order, identical to `ALL`'s. Mapping through durations instead
        // actually fails if a variant is listed out of order.
        fn approx_secs(interval: CandleInterval) -> i64 {
            match interval.to_step() {
                IntervalStep::Fixed(d) => d.num_seconds(),
                // Only `Month1` is calendar-based; ~30d keeps it above `Week1` (7d).
                IntervalStep::Months(n) => i64::from(n) * 30 * 24 * 60 * 60,
            }
        }
        for pair in CandleInterval::ALL.windows(2) {
            assert!(
                approx_secs(pair[0]) < approx_secs(pair[1]),
                "ALL must be in strictly ascending duration order: {:?} !< {:?}",
                pair[0],
                pair[1]
            );
        }
    }

    #[test]
    fn candle_interval_display_equals_as_str_for_every_variant() {
        for interval in CandleInterval::ALL {
            assert_eq!(interval.to_string(), interval.as_str());
        }
    }

    #[test]
    fn candle_interval_as_str_matches_binance_exactly() {
        // Case-sensitive: `1M` (month) is the only uppercase form.
        assert_eq!(CandleInterval::Sec1.as_str(), "1s");
        assert_eq!(CandleInterval::Min1.as_str(), "1m");
        assert_eq!(CandleInterval::Hour6.as_str(), "6h");
        assert_eq!(CandleInterval::Month1.as_str(), "1M");
    }

    #[test]
    fn candle_interval_from_str_is_inverse_of_as_str() {
        for interval in CandleInterval::ALL {
            assert_eq!(interval.as_str().parse::<CandleInterval>(), Ok(interval));
        }
    }

    #[test]
    fn candle_interval_from_str_rejects_garbage() {
        assert!("".parse::<CandleInterval>().is_err());
        assert!("7m".parse::<CandleInterval>().is_err());
        // Case-sensitive: `1m` (minute) must not parse as `1M` (month) or vice versa.
        assert_eq!("1m".parse::<CandleInterval>(), Ok(CandleInterval::Min1));
        assert_eq!("1M".parse::<CandleInterval>(), Ok(CandleInterval::Month1));
    }

    #[test]
    fn candle_interval_serde_round_trips_every_variant() {
        for interval in CandleInterval::ALL {
            let json = serde_json::to_string(&interval).unwrap();
            // Serialises as the bare `as_str()` string.
            assert_eq!(json, format!("\"{}\"", interval.as_str()));
            let back: CandleInterval = serde_json::from_str(&json).unwrap();
            assert_eq!(back, interval);
        }
    }

    #[test]
    fn candles_kind_carries_interval_and_serde_round_trips() {
        let kind = Candles {
            interval: CandleInterval::Hour6,
        };
        let json = serde_json::to_string(&kind).unwrap();
        assert_eq!(json, r#"{"interval":"6h"}"#);
        let back: Candles = serde_json::from_str(&json).unwrap();
        assert_eq!(back, kind);
        // `SubscriptionKind::as_str` stays the kind tag, independent of interval.
        assert_eq!(kind.as_str(), "candles");
        assert_eq!(kind.to_string(), "candles");
    }

    #[test]
    fn aggregate_rolls_three_1s_bars_into_one_3s_bar() {
        let bars = [
            bar(
                "2024-01-15T12:00:01Z",
                dec!(10),
                dec!(12),
                dec!(9),
                dec!(11),
                dec!(1),
                5,
            ),
            bar(
                "2024-01-15T12:00:02Z",
                dec!(11),
                dec!(15),
                dec!(10),
                dec!(14),
                dec!(2),
                3,
            ),
            bar(
                "2024-01-15T12:00:03Z",
                dec!(14),
                dec!(16),
                dec!(13),
                dec!(15),
                dec!(3),
                2,
            ),
        ];
        let out = aggregate_candles(&bars, Duration::seconds(1), Duration::seconds(3)).unwrap();
        assert_eq!(
            out,
            vec![bar(
                "2024-01-15T12:00:03Z",
                dec!(10),
                dec!(16),
                dec!(9),
                dec!(15),
                dec!(6),
                10
            )]
        );
    }

    #[test]
    fn aggregate_splits_six_1s_bars_into_two_3s_bars() {
        let bars = [
            bar(
                "2024-01-15T12:00:01Z",
                dec!(10),
                dec!(12),
                dec!(9),
                dec!(11),
                dec!(1),
                1,
            ),
            bar(
                "2024-01-15T12:00:02Z",
                dec!(11),
                dec!(15),
                dec!(10),
                dec!(14),
                dec!(2),
                2,
            ),
            bar(
                "2024-01-15T12:00:03Z",
                dec!(14),
                dec!(16),
                dec!(13),
                dec!(15),
                dec!(3),
                3,
            ),
            bar(
                "2024-01-15T12:00:04Z",
                dec!(15),
                dec!(17),
                dec!(14),
                dec!(16),
                dec!(4),
                4,
            ),
            bar(
                "2024-01-15T12:00:05Z",
                dec!(16),
                dec!(18),
                dec!(15),
                dec!(17),
                dec!(5),
                5,
            ),
            bar(
                "2024-01-15T12:00:06Z",
                dec!(17),
                dec!(19),
                dec!(12),
                dec!(13),
                dec!(6),
                6,
            ),
        ];
        let out = aggregate_candles(&bars, Duration::seconds(1), Duration::seconds(3)).unwrap();
        assert_eq!(
            out,
            vec![
                bar(
                    "2024-01-15T12:00:03Z",
                    dec!(10),
                    dec!(16),
                    dec!(9),
                    dec!(15),
                    dec!(6),
                    6
                ),
                bar(
                    "2024-01-15T12:00:06Z",
                    dec!(15),
                    dec!(19),
                    dec!(12),
                    dec!(13),
                    dec!(15),
                    15
                ),
            ]
        );
    }

    #[test]
    fn aggregate_buckets_snap_to_the_epoch_grid_not_the_first_sample() {
        // Opens land at :01, :02, :03. First-sample anchoring would merge all
        // three into one [:01, :04) bucket; the epoch grid splits them into
        // [:00, :03) and [:03, :06).
        let bars = [
            bar(
                "2024-01-15T12:00:02Z",
                dec!(10),
                dec!(12),
                dec!(9),
                dec!(11),
                dec!(1),
                1,
            ),
            bar(
                "2024-01-15T12:00:03Z",
                dec!(11),
                dec!(15),
                dec!(10),
                dec!(14),
                dec!(2),
                2,
            ),
            bar(
                "2024-01-15T12:00:04Z",
                dec!(14),
                dec!(16),
                dec!(13),
                dec!(15),
                dec!(4),
                3,
            ),
        ];
        let target = Duration::seconds(3);
        let out = aggregate_candles(&bars, Duration::seconds(1), target).unwrap();
        assert_eq!(out.len(), 2);
        assert_eq!(out[0].close_time, dt("2024-01-15T12:00:03Z"));
        assert_eq!(out[0].volume, Some(dec!(3)));
        assert_eq!(out[1].close_time, dt("2024-01-15T12:00:06Z"));
        assert_eq!(out[1].volume, Some(dec!(4)));
        // Every output boundary sits on the epoch-anchored target grid.
        for candle in &out {
            assert_eq!(
                candle
                    .close_time
                    .timestamp_millis()
                    .rem_euclid(target.num_milliseconds()),
                0
            );
        }
    }

    #[test]
    fn aggregate_partial_bucket_does_not_scale_volume_to_bucket_width() {
        let bars = [bar(
            "2024-01-15T12:00:02Z",
            dec!(10),
            dec!(12),
            dec!(9),
            dec!(11),
            dec!(5),
            4,
        )];
        let out = aggregate_candles(&bars, Duration::seconds(1), Duration::seconds(3)).unwrap();
        assert_eq!(
            out,
            vec![bar(
                "2024-01-15T12:00:03Z",
                dec!(10),
                dec!(12),
                dec!(9),
                dec!(11),
                dec!(5),
                4
            )]
        );
    }

    #[test]
    fn aggregate_omits_fully_empty_buckets() {
        // Bars in the [:00, :03) and [:09, :12) buckets; [:03, :09) is a gap.
        let bars = [
            bar(
                "2024-01-15T12:00:01Z",
                dec!(10),
                dec!(12),
                dec!(9),
                dec!(11),
                dec!(1),
                1,
            ),
            bar(
                "2024-01-15T12:00:11Z",
                dec!(20),
                dec!(22),
                dec!(19),
                dec!(21),
                dec!(2),
                2,
            ),
        ];
        let out = aggregate_candles(&bars, Duration::seconds(1), Duration::seconds(3)).unwrap();
        // No synthetic bars for the empty buckets: gap policy is the caller's.
        assert_eq!(
            out,
            vec![
                bar(
                    "2024-01-15T12:00:03Z",
                    dec!(10),
                    dec!(12),
                    dec!(9),
                    dec!(11),
                    dec!(1),
                    1
                ),
                bar(
                    "2024-01-15T12:00:12Z",
                    dec!(20),
                    dec!(22),
                    dec!(19),
                    dec!(21),
                    dec!(2),
                    2
                ),
            ]
        );
    }

    #[test]
    fn aggregate_rejects_invalid_interval_arguments() {
        let base = Duration::seconds(2);
        assert_eq!(
            aggregate_candles(&[], base, Duration::seconds(5)).unwrap_err(),
            AggregateCandlesError::TargetNotMultipleOfBase {
                base,
                target: Duration::seconds(5)
            }
        );
        assert_eq!(
            aggregate_candles(&[], base, Duration::seconds(1)).unwrap_err(),
            AggregateCandlesError::TargetSmallerThanBase {
                base,
                target: Duration::seconds(1)
            }
        );
        assert_eq!(
            aggregate_candles(&[], Duration::zero(), Duration::seconds(3)).unwrap_err(),
            AggregateCandlesError::NonPositiveInterval {
                base: Duration::zero(),
                target: Duration::seconds(3)
            }
        );
        assert_eq!(
            aggregate_candles(&[], base, Duration::seconds(-3)).unwrap_err(),
            AggregateCandlesError::NonPositiveInterval {
                base,
                target: Duration::seconds(-3)
            }
        );
    }

    #[test]
    fn aggregate_rejects_sub_millisecond_intervals() {
        // 1.5ms floors to 1ms at the bucketing granularity, which would
        // silently mis-bucket — rejected at entry instead.
        let base = Duration::nanoseconds(1_500_000);
        let target = Duration::milliseconds(3);
        assert_eq!(
            aggregate_candles(&[], base, target).unwrap_err(),
            AggregateCandlesError::SubMillisecondInterval { base, target }
        );
        let base = Duration::milliseconds(1);
        let target = Duration::nanoseconds(2_500_000);
        assert_eq!(
            aggregate_candles(&[], base, target).unwrap_err(),
            AggregateCandlesError::SubMillisecondInterval { base, target }
        );
    }

    #[test]
    fn aggregate_of_empty_input_is_empty() {
        let out = aggregate_candles(&[], Duration::seconds(1), Duration::seconds(3)).unwrap();
        assert!(out.is_empty());
    }

    #[test]
    fn aggregate_close_time_round_trips_through_the_shared_boundary_helper() {
        let bars = [bar(
            "2024-01-15T12:00:01Z",
            dec!(10),
            dec!(12),
            dec!(9),
            dec!(11),
            dec!(1),
            1,
        )];
        let target = Duration::seconds(3);
        let out = aggregate_candles(&bars, Duration::seconds(1), target).unwrap();
        // close_time == bucket_open + target, via the single-sourced helper.
        assert_eq!(
            out[0].close_time,
            close_time_from_open(dt("2024-01-15T12:00:00Z"), IntervalStep::Fixed(target)).unwrap()
        );
    }

    #[test]
    fn aggregate_surfaces_bucket_close_overflow_as_an_error() {
        let target = Duration::seconds(3);
        // The final epoch-grid bucket below the maximum representable instant:
        // a 1ms sub-bar at its very start is itself representable, but the
        // bucket's exclusive close boundary is not.
        let bucket_open_ms = DateTime::<Utc>::MAX_UTC
            .timestamp_millis()
            .div_euclid(target.num_milliseconds())
            * target.num_milliseconds();
        let close_time = DateTime::from_timestamp_millis(bucket_open_ms + 1).unwrap();
        let bars = [Candle {
            close_time,
            open: dec!(1),
            high: dec!(1),
            low: dec!(1),
            close: dec!(1),
            volume: Some(dec!(1)),
            trade_count: Some(1),
        }];
        assert_eq!(
            aggregate_candles(&bars, Duration::milliseconds(1), target).unwrap_err(),
            AggregateCandlesError::TimestampOutOfRange { index: 0 }
        );
    }

    #[test]
    fn aggregate_sixty_1s_bars_matches_the_fields_of_a_native_1m_bar() {
        let session_open = dt("2024-01-15T12:00:00Z");
        let bars = (0..60i64)
            .map(|i| Candle {
                close_time: session_open + Duration::seconds(i + 1),
                open: Decimal::from(100 + i),
                high: Decimal::from(105 + i),
                low: Decimal::from(95 + i),
                close: Decimal::from(101 + i),
                volume: Some(Decimal::from(2)),
                trade_count: Some(3),
            })
            .collect::<Vec<_>>();
        let out = aggregate_candles(&bars, Duration::seconds(1), Duration::minutes(1)).unwrap();
        // Exactly the fields a native 1m candle over the same trades carries.
        assert_eq!(
            out,
            vec![Candle {
                close_time: dt("2024-01-15T12:01:00Z"),
                open: Decimal::from(100),
                high: Decimal::from(164),
                low: Decimal::from(95),
                close: Decimal::from(160),
                volume: Some(Decimal::from(120)),
                trade_count: Some(180),
            }]
        );
    }

    #[test]
    fn aggregate_any_none_constituent_poisons_the_bucket() {
        // Any sub-bar with unknown volume (or trade count) makes the whole
        // aggregated bucket unknown — never a silent under-count of the known
        // parts. This is the load-bearing invariant behind `Candle`'s optional
        // volume: an unknown component makes the sum unknown.
        let cell = |close: &str, v: Option<Decimal>, n: Option<u64>| Candle {
            close_time: dt(close),
            open: dec!(1),
            high: dec!(1),
            low: dec!(1),
            close: dec!(1),
            volume: v,
            trade_count: n,
        };
        let bars = [
            cell("2024-01-15T12:00:01Z", Some(dec!(1)), Some(1)),
            cell("2024-01-15T12:00:02Z", None, Some(2)), // unknown volume
            cell("2024-01-15T12:00:03Z", Some(dec!(3)), None), // unknown trade count
        ];
        let out = aggregate_candles(&bars, Duration::seconds(1), Duration::seconds(3)).unwrap();
        assert_eq!(out.len(), 1);
        assert_eq!(
            out[0].volume, None,
            "one None-volume constituent poisons the summed volume"
        );
        assert_eq!(
            out[0].trade_count, None,
            "one None-trade_count constituent poisons the summed trade_count"
        );
    }

    #[test]
    fn aggregate_all_known_constituents_sum_normally() {
        // Control for the poisoning test: when every constituent is known the
        // bucket carries the plain totals.
        let bars = [
            bar(
                "2024-01-15T12:00:01Z",
                dec!(1),
                dec!(1),
                dec!(1),
                dec!(1),
                dec!(2),
                5,
            ),
            bar(
                "2024-01-15T12:00:02Z",
                dec!(1),
                dec!(1),
                dec!(1),
                dec!(1),
                dec!(3),
                7,
            ),
        ];
        let out = aggregate_candles(&bars, Duration::seconds(1), Duration::seconds(3)).unwrap();
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].volume, Some(dec!(5)));
        assert_eq!(out[0].trade_count, Some(12));
    }

    #[test]
    fn aggregate_gap_filled_input_carries_fills_without_double_counting() {
        // Pre-gap-filled contiguous 1s series: flat v=0 fill bars around one
        // real bar. Fills carry into open/low; zero volumes cannot
        // double-count, so fill-before-aggregate equals fill-after.
        let bars = [
            bar(
                "2024-01-15T12:00:01Z",
                dec!(90),
                dec!(90),
                dec!(90),
                dec!(90),
                dec!(0),
                0,
            ),
            bar(
                "2024-01-15T12:00:02Z",
                dec!(100),
                dec!(101),
                dec!(99),
                dec!(100.5),
                dec!(10),
                7,
            ),
            bar(
                "2024-01-15T12:00:03Z",
                dec!(100.5),
                dec!(100.5),
                dec!(100.5),
                dec!(100.5),
                dec!(0),
                0,
            ),
        ];
        let out = aggregate_candles(&bars, Duration::seconds(1), Duration::seconds(3)).unwrap();
        assert_eq!(
            out,
            vec![bar(
                "2024-01-15T12:00:03Z",
                dec!(90),
                dec!(101),
                dec!(90),
                dec!(100.5),
                dec!(10),
                7
            )]
        );
    }

    #[test]
    fn aggregate_bucket_of_only_zero_volume_bars_emits_one_flat_bar() {
        // "Non-empty" means has-candles, not has-volume.
        let bars = [
            bar(
                "2024-01-15T12:00:01Z",
                dec!(50),
                dec!(50),
                dec!(50),
                dec!(50),
                dec!(0),
                0,
            ),
            bar(
                "2024-01-15T12:00:02Z",
                dec!(50),
                dec!(50),
                dec!(50),
                dec!(50),
                dec!(0),
                0,
            ),
            bar(
                "2024-01-15T12:00:03Z",
                dec!(50),
                dec!(50),
                dec!(50),
                dec!(50),
                dec!(0),
                0,
            ),
        ];
        let out = aggregate_candles(&bars, Duration::seconds(1), Duration::seconds(3)).unwrap();
        assert_eq!(
            out,
            vec![bar(
                "2024-01-15T12:00:03Z",
                dec!(50),
                dec!(50),
                dec!(50),
                dec!(50),
                dec!(0),
                0
            )]
        );
    }

    #[test]
    fn aggregate_rejects_unsorted_and_duplicate_input_observably() {
        let first = bar(
            "2024-01-15T12:00:02Z",
            dec!(10),
            dec!(12),
            dec!(9),
            dec!(11),
            dec!(1),
            1,
        );
        let second = bar(
            "2024-01-15T12:00:01Z",
            dec!(11),
            dec!(15),
            dec!(10),
            dec!(14),
            dec!(2),
            2,
        );
        assert_eq!(
            aggregate_candles(&[first, second], Duration::seconds(1), Duration::seconds(3))
                .unwrap_err(),
            AggregateCandlesError::NonMonotonicInput { index: 1 }
        );
        // Strict ordering also rejects duplicate close_times (double-fetch /
        // overlapping-page data bugs) rather than double-counting their volume.
        assert_eq!(
            aggregate_candles(&[first, first], Duration::seconds(1), Duration::seconds(3))
                .unwrap_err(),
            AggregateCandlesError::NonMonotonicInput { index: 1 }
        );
    }

    #[test]
    fn aggregate_surfaces_sub_bar_open_underflow_as_an_error() {
        // close_time at the minimum representable instant: the sub-bar's open
        // (close − base) underflows and must surface, not panic.
        let bars = [Candle {
            close_time: DateTime::<Utc>::MIN_UTC,
            open: dec!(1),
            high: dec!(1),
            low: dec!(1),
            close: dec!(1),
            volume: Some(dec!(1)),
            trade_count: Some(1),
        }];
        assert_eq!(
            aggregate_candles(&bars, Duration::seconds(1), Duration::seconds(3)).unwrap_err(),
            AggregateCandlesError::TimestampOutOfRange { index: 0 }
        );
    }

    #[test]
    fn aggregate_surfaces_bucket_open_underflow_from_grid_flooring_as_an_error() {
        // The sub-bar's own open is exactly the minimum representable instant,
        // so the per-candle underflow check passes — but flooring that open
        // onto a coarse 3-day grid lands below it, a distinct underflow that
        // only the bucket-level check catches.
        let base = Duration::milliseconds(1);
        let bars = [Candle {
            close_time: DateTime::<Utc>::MIN_UTC + base,
            open: dec!(1),
            high: dec!(1),
            low: dec!(1),
            close: dec!(1),
            volume: Some(dec!(1)),
            trade_count: Some(1),
        }];
        assert_eq!(
            aggregate_candles(&bars, base, Duration::days(3)).unwrap_err(),
            AggregateCandlesError::TimestampOutOfRange { index: 0 }
        );
    }

    #[test]
    fn aggregate_floors_pre_epoch_opens_toward_negative_infinity() {
        // Sub-bar open is 1s *before* the epoch. Euclidean flooring snaps it to
        // -3s, so the bucket closes exactly on the epoch. Plain `/` truncates
        // toward zero, snapping to 0s and mis-bucketing the bar one bucket late.
        let bars = [bar(
            "1970-01-01T00:00:00Z",
            dec!(10),
            dec!(12),
            dec!(9),
            dec!(11),
            dec!(1),
            1,
        )];
        let out = aggregate_candles(&bars, Duration::seconds(1), Duration::seconds(3)).unwrap();
        assert_eq!(
            out,
            vec![bar(
                "1970-01-01T00:00:00Z",
                dec!(10),
                dec!(12),
                dec!(9),
                dec!(11),
                dec!(1),
                1
            )]
        );
    }

    #[test]
    fn aggregate_with_target_equal_to_base_is_an_identity_regrid() {
        // k = 1 passes validation deliberately: on-grid input passes through.
        let bars = [
            bar(
                "2024-01-15T12:00:01Z",
                dec!(10),
                dec!(12),
                dec!(9),
                dec!(11),
                dec!(1),
                1,
            ),
            bar(
                "2024-01-15T12:00:02Z",
                dec!(11),
                dec!(15),
                dec!(10),
                dec!(14),
                dec!(2),
                2,
            ),
        ];
        let out = aggregate_candles(&bars, Duration::seconds(1), Duration::seconds(1)).unwrap();
        assert_eq!(out, bars.to_vec());
    }

    /// Pins the serde contract documented on [`Candle::volume`].
    ///
    /// The absent-key case is the one worth a test: making the field optional turned a `missing
    /// field` rejection into a silent `None`, and nothing else in the codebase would notice if a
    /// `#[serde(default)]`-style change or a rename reintroduced the old behaviour.
    #[test]
    fn an_absent_volume_or_trade_count_deserialises_as_unknown_and_a_zero_stays_a_zero() {
        let without = serde_json::json!({
            "close_time": "2024-01-15T12:00:00Z",
            "open": "10", "high": "12", "low": "9", "close": "11",
        });
        let candle: Candle = serde_json::from_value(without).unwrap();
        assert_eq!(candle.volume, None);
        assert_eq!(candle.trade_count, None);

        // A record written before the migration: the fabricated zero survives verbatim, so this
        // type cannot recover the distinction it now expresses.
        let pre_migration = serde_json::json!({
            "close_time": "2024-01-15T12:00:00Z",
            "open": "10", "high": "12", "low": "9", "close": "11",
            "volume": "0", "trade_count": 0,
        });
        let candle: Candle = serde_json::from_value(pre_migration).unwrap();
        assert_eq!(candle.volume, Some(Decimal::ZERO));
        assert_eq!(candle.trade_count, Some(0));

        // `None` round-trips as an explicit null rather than degrading to the absent-key case.
        let unknown = Candle {
            close_time: dt("2024-01-15T12:00:00Z"),
            open: dec!(10),
            high: dec!(12),
            low: dec!(9),
            close: dec!(11),
            volume: None,
            trade_count: None,
        };
        let encoded = serde_json::to_value(unknown).unwrap();
        assert_eq!(encoded["volume"], serde_json::Value::Null);
        assert_eq!(encoded["trade_count"], serde_json::Value::Null);
        assert_eq!(serde_json::from_value::<Candle>(encoded).unwrap(), unknown);
    }
}

/// Normalised Barter OHLCV [`Candle`] model.
///
/// # `close_time` contract
///
/// `close_time` is the **exclusive end-of-period boundary** of the candle:
///
/// ```text
/// close_time == open_time + interval
/// ```
///
/// A candle aggregates the trades that fall in the **half-open interval**
/// `[close_time − interval, close_time)` — i.e. trades with
/// `open_time ≤ ts < close_time`. A trade landing exactly on `close_time`
/// belongs to the **next** candle, so `close_time` equals the next candle's
/// open instant.
///
/// Two distinct caveats apply to the boundary — do not conflate them:
///
/// - **Not session-aligned** (daily/weekly/monthly): the boundary is the UTC
///   period grid (`day → next 00:00 UTC`, etc.), **not** an exchange session
///   close. The library has no session calendar.
/// - **Variable-length calendar arithmetic** (month/quarter/year only): these
///   are nominal boundaries computed with calendar months (chrono [`Months`]),
///   not fixed [`Duration`]s. Daily and weekly are exact fixed durations in UTC
///   (no DST), exact to the millisecond. The `open_time + N months` equality
///   holds only when `open_time` is calendar-grid-aligned (e.g. a `1M` candle
///   opens on the 1st at 00:00 UTC); a non-aligned open day-clamps
///   (Jan 31 + 1 month → Feb 28/29). Venue-supplied open times are always
///   aligned, so clamping never arises in practice, but it is part of the
///   contract for callers computing boundaries from arbitrary instants.
///
/// `Candle` deliberately carries **neither `open_time` nor `interval`** — recover
/// them from the originating fetch request / subscription resolution
/// (`open_time ≡ close_time − interval`). Range-computing producers derive
/// `close_time` through [`close_time_from_open`] so the boundary is defined in
/// exactly one place (the Massive WS path uses the venue-supplied boundary
/// directly — see [`close_time_from_open`] for the full producer list).
///
/// # Using a `Candle` with the engine
///
/// When wrapping a `Candle` into a [`MarketEvent`](crate::event::MarketEvent) for
/// a consuming engine (live or backtest), set
/// [`time_exchange`](crate::event::MarketEvent::time_exchange) to this
/// `close_time` — it is the period-END instant, the only choice that avoids
/// lookahead (see that field's contract). The library's own candle producers
/// already do this.
///
/// [`Months`]: chrono::Months
/// [`Duration`]: chrono::Duration
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Hash, Debug, Deserialize, Serialize)]
pub struct Candle {
    pub close_time: DateTime<Utc>,
    pub open: Decimal,
    pub high: Decimal,
    pub low: Decimal,
    pub close: Decimal,
    /// Consolidated trade volume over the candle period, or `None` when the
    /// producer carries no volume for this instrument class.
    ///
    /// `None` is a first-class "volume is unknown" fact, **not** zero: some feeds
    /// (e.g. spot FX candles) publish no consolidated volume at all. Encoding
    /// that absence as `Some(0.0)` would be a silent lie — a volume-derived
    /// feature would read a real, quiet zero rather than an explicit unknown.
    /// [`aggregate_candles`] propagates the absence: any `None` constituent makes
    /// the whole aggregated bucket `None`.
    ///
    /// # Serde contract
    ///
    /// Plain `Option` semantics, with two consequences worth stating because they
    /// changed when this field became optional:
    ///
    /// - An **absent** `volume` key now deserialises to `None`. It was previously a
    ///   hard `missing field` error, so a truncated or hand-written record that used
    ///   to be rejected is now accepted as "volume unknown". Reject it explicitly if
    ///   a producer of yours must always report one.
    /// - A record persisted **before** the migration carries the fabricated
    ///   `"volume": 0` verbatim, and reads back as `Some(0)` — indistinguishable
    ///   from a genuine zero-volume bar. This type cannot recover the distinction;
    ///   re-fetch, or track the affected range out-of-band.
    ///
    /// `None` serialises as `"volume": null` (the key is written, not skipped), so a
    /// round trip through JSON preserves absence rather than degrading it into the
    /// missing-key case above.
    pub volume: Option<Decimal>,
    /// Number of trades in the candle period, or `None` when the producer does
    /// not report a trade count.
    ///
    /// Same contract as [`volume`](Self::volume) throughout, including serde:
    /// `None` means "unknown", never zero; any `None` constituent makes an
    /// aggregated bucket `None`; an absent key deserialises to `None`; and a
    /// pre-migration `"trade_count": 0` still reads back as `Some(0)`.
    pub trade_count: Option<u64>,
}

/// One step from a candle's open instant to its exclusive close boundary.
///
/// Keyed on a primitive step type (not on any per-exchange interval enum) so
/// every producer — regardless of how it names its native intervals — maps to
/// the same two cases and routes through [`close_time_from_open`]. This is the
/// mechanism that makes the [`Candle::close_time`] contract *enforced by
/// construction* rather than merely documented.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum IntervalStep {
    /// A fixed-length step (seconds through weeks — exact in UTC, no DST).
    Fixed(chrono::Duration),
    /// A variable-length calendar step in whole months. Covers calendar
    /// `month` (1), `quarter` (3) and `year` (12) — leap-year-correct via
    /// chrono's [`checked_add_months`](DateTime::checked_add_months).
    Months(u32),
}

/// Compute a candle's exclusive `close_time` boundary from its `open` instant.
///
/// This is the single shared boundary helper that every range-computing
/// [`Candle`] producer routes through (Massive REST, Hyperliquid, IBKR), so the
/// `close_time == open + interval` contract is computed in exactly one place. The
/// Massive WS path is the lone exception: it trusts the venue-supplied boundary
/// directly (see `WsAggregateMsg::into_candle` for the rationale).
///
/// - [`IntervalStep::Fixed`] adds a [`chrono::Duration`].
/// - [`IntervalStep::Months`] uses calendar-correct month arithmetic
///   ([`checked_add_months`](DateTime::checked_add_months)), so a Jan monthly
///   bar yields `Feb 1 00:00 UTC` and a leap-year Feb monthly bar yields
///   `Mar 1 00:00 UTC`.
///
/// # Returns
///
/// `None` on overflow — when the computed boundary falls outside the
/// representable [`DateTime<Utc>`] range. Callers **must** surface this as their
/// producer error type (an observable failure), **never** a silent fallback to a
/// plausible-but-wrong timestamp such as `UNIX_EPOCH`.
#[must_use]
pub fn close_time_from_open(open: DateTime<Utc>, step: IntervalStep) -> Option<DateTime<Utc>> {
    match step {
        IntervalStep::Fixed(duration) => open.checked_add_signed(duration),
        IntervalStep::Months(n) => open.checked_add_months(chrono::Months::new(n)),
    }
}

/// Inverse of [`close_time_from_open`]: recover a candle's `open` instant from its
/// exclusive `close_time` boundary (`open == close − interval`).
///
/// Used by range-bounded historical fetches to widen the venue request window:
/// the candle whose `close_time == start` has `open == start − interval`, so a
/// fetch that wants `close_time ∈ [start, end]` must ask the venue for opens down
/// to `start − interval` (then trim the result by `close_time`). See
/// [`Candle::close_time`].
///
/// # Returns
///
/// `None` on underflow (the computed open falls below the representable
/// [`DateTime<Utc>`] range).
///
/// For the range-widening use-case this `None` is **not** an error: it means the
/// candle whose `close_time == start` would have an unrepresentable open
/// (`start − interval` below [`DateTime<Utc>`] minimum) and therefore cannot
/// exist. Callers should fall back to the original lower bound — the un-widened
/// fetch already yields the complete, correct result set, so this is the right
/// outcome rather than a silent failure. (Contrast [`close_time_from_open`],
/// whose `None` *does* signal data loss for a real candle and must be surfaced
/// as an error.)
#[must_use]
pub fn open_time_from_close(close: DateTime<Utc>, step: IntervalStep) -> Option<DateTime<Utc>> {
    match step {
        IntervalStep::Fixed(duration) => close.checked_sub_signed(duration),
        IntervalStep::Months(n) => close.checked_sub_months(chrono::Months::new(n)),
    }
}

/// Error returned by [`aggregate_candles`] for invalid arguments or input.
///
/// Every variant is an observable failure: the aggregation never silently
/// repairs, reorders or drops input.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum AggregateCandlesError {
    /// `base_interval` or `target` is zero or negative.
    #[error("candle intervals must be positive (base: {base}, target: {target})")]
    NonPositiveInterval {
        /// The `base_interval` argument as supplied.
        base: Duration,
        /// The `target` argument as supplied.
        target: Duration,
    },
    /// `base_interval` or `target` has a sub-millisecond component. Bucketing
    /// operates at millisecond granularity (venue candle timestamps are
    /// millisecond-resolution), so a sub-millisecond interval would silently
    /// mis-bucket — it is rejected instead.
    #[error("candle intervals must be whole milliseconds (base: {base}, target: {target})")]
    SubMillisecondInterval {
        /// The `base_interval` argument as supplied.
        base: Duration,
        /// The `target` argument as supplied.
        target: Duration,
    },
    /// `target` is shorter than `base_interval` — aggregation only coarsens.
    #[error(
        "target interval must not be smaller than the base interval (base: {base}, target: {target})"
    )]
    TargetSmallerThanBase {
        /// The `base_interval` argument as supplied.
        base: Duration,
        /// The `target` argument as supplied.
        target: Duration,
    },
    /// `target` is not an integer multiple of `base_interval`, so base candles
    /// cannot tile target buckets exactly.
    #[error(
        "target interval must be an integer multiple of the base interval (base: {base}, target: {target})"
    )]
    TargetNotMultipleOfBase {
        /// The `base_interval` argument as supplied.
        base: Duration,
        /// The `target` argument as supplied.
        target: Duration,
    },
    /// The input candle at `index` is not strictly later (by `close_time`) than
    /// its predecessor. Strictness also rejects duplicate timestamps: no two
    /// distinct fixed-interval candles can legitimately share a `close_time`,
    /// so a duplicate is a data bug (e.g. an overlapping-page double fetch)
    /// whose volume must not be double-counted.
    #[error("input candles must be strictly ascending by close_time (violation at index {index})")]
    NonMonotonicInput {
        /// Index into the input slice of the offending candle.
        index: usize,
    },
    /// A boundary computed for the candle at `index` falls outside the
    /// representable [`DateTime<Utc>`] range. Three computations can fail this
    /// way: the sub-bar `open` underflowed (input near the minimum instant);
    /// the bucket's epoch-grid-floored open underflowed (flooring a
    /// *representable* open onto a coarse `target` grid can still land below
    /// the minimum instant); or the bucket `close_time` overflowed (input near
    /// the maximum instant).
    #[error(
        "candle at index {index} yields a boundary outside the representable DateTime<Utc> range"
    )]
    TimestampOutOfRange {
        /// Index into the input slice attributing the failure: the offending
        /// candle itself for a sub-bar `open` underflow, or the affected
        /// bucket's first sub-bar for a bucket-level boundary failure (whose
        /// own sub-bar boundary need not be invalid).
        index: usize,
    },
}

/// Aggregate fixed-interval OHLCV [`Candle`]s into a coarser fixed interval.
///
/// A pure, venue-agnostic batch primitive: `candles` at resolution
/// `base_interval` are bucketed onto the epoch-anchored `target` grid, and one
/// aggregated [`Candle`] is emitted per **non-empty** bucket, ascending by
/// `close_time`. All price/volume arithmetic is [`Decimal`]-exact — no
/// floating point.
///
/// # Aggregation rule
///
/// Each output candle carries, over its bucket's sub-bars:
///
/// - `open`: the earliest sub-bar's `open`
/// - `close`: the latest sub-bar's `close`
/// - `high` / `low`: the maximum / minimum across the bucket
/// - `volume` / `trade_count`: sums across the bucket
/// - `close_time`: the bucket's exclusive close boundary
///   (`bucket_open + target`), computed via [`close_time_from_open`] so the
///   [`Candle::close_time`] contract stays single-sourced
///
/// A **partial** bucket (fewer than `target / base_interval` sub-bars present)
/// aggregates only the sub-bars actually present — `volume` is never scaled up
/// to the bucket width.
///
/// # Bucketing: epoch anchoring
///
/// Each sub-bar's open (recovered as `close_time − base_interval` via
/// [`open_time_from_close`]) is floored onto the grid of integer multiples of
/// `target` since the Unix epoch. This is the natural anchoring for every
/// target that divides a day (seconds through hours). **Caveat**: the Unix
/// epoch was a *Thursday*, so for multi-day targets — e.g. 3-day or weekly
/// buckets — the epoch grid diverges from venue-native anchoring (venues serve
/// Monday-anchored weekly candles). Callers wanting venue-native weekly bars
/// should fetch them from the venue rather than aggregate.
///
/// # Caller obligations
///
/// - **Strictly ascending**: input must be strictly ascending by `close_time`.
///   Violations (including duplicate timestamps) surface as
///   [`AggregateCandlesError::NonMonotonicInput`] — input is never silently
///   reordered or deduplicated.
/// - **Uniform resolution**: every input candle must span exactly
///   `base_interval` (its open is `close_time − base_interval`). This cannot
///   be validated from a [`Candle`] alone (candles carry no interval field)
///   and is trusted.
/// - **Grid alignment is _not_ validated**: sub-bar opens are floored onto the
///   epoch grid wherever they fall, so legitimately non-epoch-anchored input
///   (e.g. venue-native Monday-anchored weekly bars) is accepted — but its
///   buckets are epoch-anchored regardless (see above).
/// - **Millisecond timestamps**: bucketing operates at millisecond
///   granularity (venue candle timestamps are millisecond-resolution). A
///   `close_time` with a sub-millisecond component has that component ignored
///   during bucketing. Sub-millisecond *intervals* are rejected as
///   [`AggregateCandlesError::SubMillisecondInterval`].
///
/// # Gap policy: none (deliberately)
///
/// A fully-empty bucket emits **no** output candle — synthesising flat bars
/// (forward-fill) is consumer policy, not a library concern. The primitive is
/// neutral about which side of the call that policy runs on:
///
/// - feed raw, sparse candles and gap-fill the *output*, or
/// - feed a pre-gap-filled contiguous series — zero-volume fill bars aggregate
///   consistently (they carry into `open`/`high`/`low`/`close`; volume sums
///   are unchanged, so fills cannot double-count).
///
/// "Non-empty" means *has input candles*, not *has volume*: a bucket
/// containing only zero-volume fill bars emits one flat zero-volume candle.
///
/// # Trailing bucket
///
/// Batch semantics: the final bucket is emitted even if the input merely
/// *ends* mid-bucket — indistinguishable from a trailing gap. Live/streaming
/// callers must withhold input until a bucket completes (or drop the tail)
/// themselves — and should feed bounded windows, not re-aggregate a growing
/// history buffer per event: every call re-validates and re-scans its entire
/// input.
///
/// # Limitations
///
/// Fixed durations only: calendar-month aggregation (variable-length buckets,
/// [`IntervalStep::Months`]) is out of scope. `target == base_interval` is
/// accepted and acts as an identity/re-grid pass.
///
/// # Panics
///
/// Panics if a bucket's accumulated `volume` overflows [`Decimal`] or its
/// accumulated `trade_count` overflows `u64`. Both require magnitudes no
/// venue feed produces (~7.9 × 10²⁸ summed volume, ~1.8 × 10¹⁹ summed
/// trades), so either indicates corrupt input; the panic keeps that failure
/// loud on every build profile rather than wrapping silently.
///
/// # Errors
///
/// See [`AggregateCandlesError`]. Boundary computations that leave the
/// representable [`DateTime<Utc>`] range — sub-bar open underflow near the
/// minimum instant, epoch-grid flooring pushing a bucket's open below the
/// minimum instant, or bucket `close_time` overflow near the maximum — surface
/// as [`AggregateCandlesError::TimestampOutOfRange`] — never a silent
/// plausible-but-wrong timestamp.
pub fn aggregate_candles(
    candles: &[Candle],
    base_interval: Duration,
    target: Duration,
) -> Result<Vec<Candle>, AggregateCandlesError> {
    if base_interval <= Duration::zero() || target <= Duration::zero() {
        return Err(AggregateCandlesError::NonPositiveInterval {
            base: base_interval,
            target,
        });
    }
    // Bucketing operates at millisecond granularity (venue candle timestamps
    // are ms); a sub-ms interval would floor and silently mis-bucket.
    if Duration::milliseconds(base_interval.num_milliseconds()) != base_interval
        || Duration::milliseconds(target.num_milliseconds()) != target
    {
        return Err(AggregateCandlesError::SubMillisecondInterval {
            base: base_interval,
            target,
        });
    }
    if target < base_interval {
        return Err(AggregateCandlesError::TargetSmallerThanBase {
            base: base_interval,
            target,
        });
    }
    let target_ms = target.num_milliseconds();
    if target_ms % base_interval.num_milliseconds() != 0 {
        return Err(AggregateCandlesError::TargetNotMultipleOfBase {
            base: base_interval,
            target,
        });
    }

    /// An in-progress epoch-grid bucket accumulator.
    struct Bucket {
        /// The bucket's open instant in epoch milliseconds (on the `target` grid).
        open_ms: i64,
        /// Index of the bucket's first sub-bar, for error attribution.
        first_index: usize,
        open: Decimal,
        high: Decimal,
        low: Decimal,
        close: Decimal,
        /// `None` if *any* sub-bar folded into this bucket had unknown volume —
        /// an unknown component makes the sum unknown, never a silent under-count.
        volume: Option<Decimal>,
        /// `None` under the same any-`None`-poisons rule as [`volume`](Self::volume).
        trade_count: Option<u64>,
    }

    /// Emit a completed bucket as an aggregated [`Candle`], deriving its
    /// exclusive close boundary through the shared [`close_time_from_open`]
    /// helper.
    fn flush(bucket: Bucket, target: Duration) -> Result<Candle, AggregateCandlesError> {
        let out_of_range = AggregateCandlesError::TimestampOutOfRange {
            index: bucket.first_index,
        };
        // `open_ms` is grid-floored, so it can sit *below* a representable
        // sub-bar open that already passed the per-candle range check —
        // a distinct underflow that must surface here too.
        let bucket_open = DateTime::from_timestamp_millis(bucket.open_ms).ok_or(out_of_range)?;
        let close_time =
            close_time_from_open(bucket_open, IntervalStep::Fixed(target)).ok_or(out_of_range)?;
        Ok(Candle {
            close_time,
            open: bucket.open,
            high: bucket.high,
            low: bucket.low,
            close: bucket.close,
            volume: bucket.volume,
            trade_count: bucket.trade_count,
        })
    }

    // Empty input is valid (arguments were still checked above) and needs no
    // allocation at all — `Vec::new()` rather than the 1-slot hint below.
    if candles.is_empty() {
        return Ok(Vec::new());
    }

    // Dense-case capacity estimate (one output per `target / base_interval`
    // sub-bars), not an upper bound: sparse input can emit up to one bucket
    // per candle, in which case the `Vec` grows normally past the hint. The
    // `usize::MAX` fallback (quotient overflowing `usize` on 32-bit platforms,
    // where `usize::MAX < i64::MAX`) degrades to a capacity of 1.
    let sub_bars_per_bucket =
        usize::try_from(target_ms / base_interval.num_milliseconds()).unwrap_or(usize::MAX);
    let mut output = Vec::with_capacity(candles.len() / sub_bars_per_bucket + 1);
    let mut bucket: Option<Bucket> = None;
    let mut prev_close_time: Option<DateTime<Utc>> = None;

    for (index, candle) in candles.iter().enumerate() {
        if prev_close_time.is_some_and(|prev| candle.close_time <= prev) {
            return Err(AggregateCandlesError::NonMonotonicInput { index });
        }
        prev_close_time = Some(candle.close_time);

        // Recover the sub-bar's open through the shared boundary helper, then
        // snap it onto the epoch grid. `div_euclid` (not `/`) floors toward
        // negative infinity, which keeps pre-1970 instants correctly bucketed.
        // The re-multiply cannot overflow: floor-then-remultiply stays within
        // one `target_ms` of the input, and a representable `DateTime<Utc>` is
        // ~±8.3 × 10¹⁵ ms — three orders of magnitude inside `i64`.
        let sub_open = open_time_from_close(candle.close_time, IntervalStep::Fixed(base_interval))
            .ok_or(AggregateCandlesError::TimestampOutOfRange { index })?;
        let bucket_open_ms = sub_open.timestamp_millis().div_euclid(target_ms) * target_ms;

        match &mut bucket {
            Some(current) if current.open_ms == bucket_open_ms => {
                current.high = current.high.max(candle.high);
                current.low = current.low.min(candle.low);
                current.close = candle.close;
                // Any-`None`-poisons: a bucket with one unknown component sums to
                // an unknown total, never a silent under-count of the known parts.
                current.volume = match (current.volume, candle.volume) {
                    (Some(acc), Some(v)) => Some(acc + v),
                    _ => None,
                };
                // A raw u64 `+=` would wrap silently in release builds (no
                // overflow-checks profile); a checked add keeps overflow loud
                // on every profile. The `expect` is the deliberate documented
                // panic of `# Panics`: overflow means corrupt input, not a
                // recoverable state. `None` on either side propagates unknown.
                #[allow(clippy::expect_used)]
                {
                    current.trade_count = match (current.trade_count, candle.trade_count) {
                        (Some(acc), Some(n)) => Some(
                            acc.checked_add(n)
                                .expect("bucket trade_count overflows u64"),
                        ),
                        _ => None,
                    };
                }
            }
            _ => {
                if let Some(completed) = bucket.take() {
                    output.push(flush(completed, target)?);
                }
                bucket = Some(Bucket {
                    open_ms: bucket_open_ms,
                    first_index: index,
                    open: candle.open,
                    high: candle.high,
                    low: candle.low,
                    close: candle.close,
                    volume: candle.volume,
                    trade_count: candle.trade_count,
                });
            }
        }
    }

    if let Some(last) = bucket.take() {
        output.push(flush(last, target)?);
    }

    Ok(output)
}

/// Candle interval/resolution, shared across every venue that produces candles.
///
/// This is the **venue-agnostic union** of all supported candle resolutions — it
/// is deliberately *not* gated to any one exchange's capabilities. Per-venue
/// support rules (e.g. Hyperliquid rejecting `Sec1`/`Hour6`) live in the exchange
/// layer, not on this enum (separation of concerns). Because the enum is a union,
/// **each venue's interval guard must be re-reviewed whenever a variant is added.**
///
/// # String form
///
/// [`as_str`](Self::as_str) is the **single source of truth** for every string
/// representation: [`Display`](std::fmt::Display), [`Serialize`], [`FromStr`] and [`Deserialize`] all
/// delegate to it (or its inverse), so there is exactly one place mapping
/// variant↔string. The strings follow Binance's kline `interval` convention and are
/// **case-sensitive** — note `Month1 → "1M"` (uppercase) vs `Min1 → "1m"`. The union
/// is a superset of any one venue's menu: `Sec5`/`Sec15`/`Sec30` have no Binance
/// kline equivalent, so a string round-trip is *not* proof a venue serves it.
///
/// # Ordering
///
/// Variants are declared in **ascending duration** order. `Ord`/`PartialOrd` exist
/// only as a compile requirement (the type embeds in
/// [`Candles`], which must stay `Ord` to
/// preserve the derived `Ord` on `Subscription`); the chronological declaration
/// order makes the derived order at least sensible. Nothing currently sorts
/// intervals semantically.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum CandleInterval {
    /// 1 second
    Sec1,
    /// 5 seconds
    Sec5,
    /// 15 seconds
    Sec15,
    /// 30 seconds
    Sec30,
    /// 1 minute
    Min1,
    /// 3 minutes
    Min3,
    /// 5 minutes
    Min5,
    /// 15 minutes
    Min15,
    /// 30 minutes
    Min30,
    /// 1 hour
    Hour1,
    /// 2 hours
    Hour2,
    /// 4 hours
    Hour4,
    /// 6 hours
    Hour6,
    /// 8 hours
    Hour8,
    /// 12 hours
    Hour12,
    /// 1 day
    Day1,
    /// 3 days
    Day3,
    /// 1 week
    Week1,
    /// 1 month
    Month1,
}

impl CandleInterval {
    /// Every [`CandleInterval`] variant, in ascending-duration declaration order.
    ///
    /// Lets variant-exhaustive tests (round-trip, channel-suffix drift guards)
    /// iterate without hand-listing the variants.
    ///
    /// When adding a [`CandleInterval`] variant, add it here too: the length
    /// literal and the `candle_interval_all_covers_every_variant_in_ascending_order`
    /// test pin `ALL`'s length to the variant count, but full coverage is not
    /// compile-enforced — the exhaustive `match`es elsewhere are the compile gate.
    pub const ALL: [CandleInterval; 19] = [
        Self::Sec1,
        Self::Sec5,
        Self::Sec15,
        Self::Sec30,
        Self::Min1,
        Self::Min3,
        Self::Min5,
        Self::Min15,
        Self::Min30,
        Self::Hour1,
        Self::Hour2,
        Self::Hour4,
        Self::Hour6,
        Self::Hour8,
        Self::Hour12,
        Self::Day1,
        Self::Day3,
        Self::Week1,
        Self::Month1,
    ];

    /// The exchange string form of this interval (e.g. `"1m"`, `"6h"`, `"1M"`).
    ///
    /// The **single source of truth** for all string representations — `Display`,
    /// `Serialize`, `FromStr` and `Deserialize` all key off this. Case-sensitive:
    /// `Month1 → "1M"`, every other variant lowercase.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Sec1 => "1s",
            Self::Sec5 => "5s",
            Self::Sec15 => "15s",
            Self::Sec30 => "30s",
            Self::Min1 => "1m",
            Self::Min3 => "3m",
            Self::Min5 => "5m",
            Self::Min15 => "15m",
            Self::Min30 => "30m",
            Self::Hour1 => "1h",
            Self::Hour2 => "2h",
            Self::Hour4 => "4h",
            Self::Hour6 => "6h",
            Self::Hour8 => "8h",
            Self::Hour12 => "12h",
            Self::Day1 => "1d",
            Self::Day3 => "3d",
            Self::Week1 => "1w",
            Self::Month1 => "1M",
        }
    }

    /// Map this interval to the shared [`IntervalStep`] used to compute a candle's
    /// exclusive `close_time` boundary via [`close_time_from_open`]. All intervals
    /// are fixed-length except `1M`, which is a calendar month.
    #[must_use]
    pub fn to_step(self) -> IntervalStep {
        match self {
            Self::Sec1 => IntervalStep::Fixed(Duration::seconds(1)),
            Self::Sec5 => IntervalStep::Fixed(Duration::seconds(5)),
            Self::Sec15 => IntervalStep::Fixed(Duration::seconds(15)),
            Self::Sec30 => IntervalStep::Fixed(Duration::seconds(30)),
            Self::Min1 => IntervalStep::Fixed(Duration::minutes(1)),
            Self::Min3 => IntervalStep::Fixed(Duration::minutes(3)),
            Self::Min5 => IntervalStep::Fixed(Duration::minutes(5)),
            Self::Min15 => IntervalStep::Fixed(Duration::minutes(15)),
            Self::Min30 => IntervalStep::Fixed(Duration::minutes(30)),
            Self::Hour1 => IntervalStep::Fixed(Duration::hours(1)),
            Self::Hour2 => IntervalStep::Fixed(Duration::hours(2)),
            Self::Hour4 => IntervalStep::Fixed(Duration::hours(4)),
            Self::Hour6 => IntervalStep::Fixed(Duration::hours(6)),
            Self::Hour8 => IntervalStep::Fixed(Duration::hours(8)),
            Self::Hour12 => IntervalStep::Fixed(Duration::hours(12)),
            Self::Day1 => IntervalStep::Fixed(Duration::days(1)),
            Self::Day3 => IntervalStep::Fixed(Duration::days(3)),
            Self::Week1 => IntervalStep::Fixed(Duration::weeks(1)),
            Self::Month1 => IntervalStep::Months(1),
        }
    }
}

impl std::fmt::Display for CandleInterval {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Error returned by [`CandleInterval::from_str`] for an unrecognised string.
///
/// The offending input is kept private and exposed via [`input`](Self::input) so
/// the error's representation can evolve (e.g. gaining context) without a
/// breaking change — mirroring `std`'s opaque parse-error types.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseCandleIntervalError {
    invalid: String,
}

impl ParseCandleIntervalError {
    /// The input string that failed to parse.
    #[must_use]
    pub fn input(&self) -> &str {
        &self.invalid
    }
}

impl std::fmt::Display for ParseCandleIntervalError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "invalid candle interval: {:?}", self.invalid)
    }
}

impl std::error::Error for ParseCandleIntervalError {}

impl FromStr for CandleInterval {
    type Err = ParseCandleIntervalError;

    /// The inverse of [`CandleInterval::as_str`] — case-sensitive (`"1M"` is the
    /// only uppercase form). Keeps variant↔string mapping in exactly one place.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "1s" => Ok(Self::Sec1),
            "5s" => Ok(Self::Sec5),
            "15s" => Ok(Self::Sec15),
            "30s" => Ok(Self::Sec30),
            "1m" => Ok(Self::Min1),
            "3m" => Ok(Self::Min3),
            "5m" => Ok(Self::Min5),
            "15m" => Ok(Self::Min15),
            "30m" => Ok(Self::Min30),
            "1h" => Ok(Self::Hour1),
            "2h" => Ok(Self::Hour2),
            "4h" => Ok(Self::Hour4),
            "6h" => Ok(Self::Hour6),
            "8h" => Ok(Self::Hour8),
            "12h" => Ok(Self::Hour12),
            "1d" => Ok(Self::Day1),
            "3d" => Ok(Self::Day3),
            "1w" => Ok(Self::Week1),
            "1M" => Ok(Self::Month1),
            other => Err(ParseCandleIntervalError {
                invalid: other.to_owned(),
            }),
        }
    }
}

impl Serialize for CandleInterval {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for CandleInterval {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let raw = <std::borrow::Cow<'de, str>>::deserialize(deserializer)?;
        raw.parse().map_err(de::Error::custom)
    }
}