tradefloor 0.11.0

A reproducible evaluation environment for financial AI agents.
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
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
//! The index futures (`futures_index_listed`) and their night session
//! (`night_session_steps`): the engine's half.
//!
//! A future reads the index, the dividends, the forecast and the market
//! factor's sigma, and writes only its own state ([`FuturesState`]): its
//! basis, its book, its marks and its settlements, and the generator on
//! `stream::DERIVATIVES` they draw from. So every stock price, close and
//! economy is the one the engine prints with these switches off.
//!
//! # The price
//!
//! `F = (S - PV(D)) * exp(r * tau) + b`, in index points. `S` is the index
//! the futures read: the live level within a session, the night's path
//! between sessions (or the close's level without a night session). `tau`
//! is the sessions from now to the expiry's open over 252, where a session
//! that has run `k` of its 390 ticks has used `k / 390` of its day and the
//! night uses none. `r` is the mean over the sessions to expiry of the
//! policy rate the forecast expects to be in force in each
//! (`forecast_horizon_sessions`), or the policy rate now without one. `D`
//! is each constituent's dividend for every ex-date after the last open and
//! no later than the expiry, in index points (shares times the amount over
//! the divisor), discounted at `r` from its ex-date: the declared amount
//! within 21 sessions of the ex-date, beyond it the Lintner rule's
//! declaration on today's state, iterated a quarter at a time. `b` is the
//! basis noise (an AR(1) in index basis points, `basis_sd` and
//! `basis_persistence`) plus each contract's mark of agents' own flow, both
//! scaled by `min(1, sessions to expiry / 6)` so the future converges on the
//! index as it expires.
//!
//! # The night
//!
//! Under `night_session_steps` the close runs the next open on a copy of
//! the engine. The copy takes the OVERNIGHT stream's draws the real open
//! will take, since nothing draws on that stream but the open and nothing
//! between a close and the next open sets an input to the draws, so the
//! copy's opening prints are the real open's to the bit. Its index and each
//! future's fair value on those prints are the night's targets, and
//! [`Engine::night_tick`] walks each future's log fair value to its target
//! on a Brownian bridge with the night's share of the index's one-session
//! variance, all on one normal per step, so the last step lands on the
//! open. The path starts on the close's marks and takes the close's macro
//! re-mark of the index (`macro_publication_repricing`, public from the
//! close) whole at its first step; an order on an index future before that
//! step walks it first, so none trades on the close's print once the
//! re-mark is known. The real open takes its own draws as it always did. A host's write
//! between sessions (a pin, a listing or delisting, a change of status, and
//! from Python a fundamentals write) reruns the copy and moves the night's
//! path by the change in its targets at once. A write the copy is not rerun
//! for (a column or a price written from Rust, a draw patched on the
//! OVERNIGHT stream) reaches the futures at the open.

use super::*;
use crate::agent_book::{
    remove_front, AgentFill, AgentOrder, BookState, Liquidity, RestMode, AGENT_BOOK_CAP, DEPTH_OWNER,
};
use crate::derivatives::calendar::{self, ROLL_SESSIONS, SESSIONS_PER_YEAR};
use crate::derivatives::{
    index_future_symbol, ContractKind, ContractSpec, ContractSymbol, IndexFutureSpec, Quote, Settlement,
    SymbolKind, INDEX_FUTURE,
};
use crate::market_maker::MARKET_MAKER_ID;
use crate::order_book::{OrderBook, Side, SubmitOptions};

/// The ticks in a session, the day the futures' clock divides.
const SESSION_TICKS: f64 = 390.0;

/// Indices into [`ListedFuture::taken`]: what agents took from the front of
/// the maker's ladder and of the latent depth, contracts.
const TAKEN_MAKER_BID: usize = 0;
const TAKEN_MAKER_ASK: usize = 1;
const TAKEN_DEPTH_BID: usize = 2;
const TAKEN_DEPTH_ASK: usize = 3;

/// The smallest cumulative size, as a share of daily volume, the latent
/// grid descends to, and the ratio from one level to the next: the equity
/// book's (`crate::agent_book`).
const DEPTH_GRID_FLOOR: f64 = 1e-4;
const DEPTH_LEVEL_RATIO: f64 = 1.25;

/// Where the futures stand in the day: before the first open, in a session,
/// or between a close and the next open.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FuturesPhase {
    BeforeFirstOpen,
    Session,
    Night,
}

impl FuturesPhase {
    fn code(self) -> f64 {
        match self {
            FuturesPhase::BeforeFirstOpen => 0.0,
            FuturesPhase::Session => 1.0,
            FuturesPhase::Night => 2.0,
        }
    }

    fn from_code(v: f64) -> Option<Self> {
        if v == 0.0 {
            Some(FuturesPhase::BeforeFirstOpen)
        } else if v == 1.0 {
            Some(FuturesPhase::Session)
        } else if v == 2.0 {
            Some(FuturesPhase::Night)
        } else {
            None
        }
    }
}

/// One listed index future.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ListedFuture {
    pub(crate) expiry: i64,
    /// The last close's settlement mark, and the fair value it was set on.
    /// NaN before the contract's first close.
    pub(crate) mark: f64,
    pub(crate) fair_close: f64,
    /// Agents' flow's mark on the basis, index basis points.
    pub(crate) impact: f64,
    /// Agents' net taker contracts against the house since the last step,
    /// which the next step adds to `impact`.
    pub(crate) pending: f64,
    /// See the `TAKEN_*` indices.
    pub(crate) taken: [f64; 4],
}

impl ListedFuture {
    fn new(expiry: i64) -> Self {
        ListedFuture {
            expiry,
            mark: f64::NAN,
            fair_close: f64::NAN,
            impact: 0.0,
            pending: 0.0,
            taken: [0.0; 4],
        }
    }
}

/// Numbers per listed future in [`FuturesState::to_words`].
const CONTRACT_WIDTH: usize = 9;

/// The night's path, between a close and the next open.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct NightBridge {
    /// The night's steps, and those walked.
    pub(crate) steps: u32,
    pub(crate) done: u32,
    /// The night's variance of the log index, fraction squared.
    pub(crate) variance: f64,
    /// The contracts bridged, by expiry, in listing order.
    pub(crate) expiries: Vec<i64>,
    /// The index, then each contract's fair value: where the path is, and
    /// where the open will put it. Levels, not logs.
    pub(crate) now: Vec<f64>,
    pub(crate) target: Vec<f64>,
}

impl NightBridge {
    /// The bridge as one flat list: steps, done, variance, the count `n`,
    /// the `n` expiries, then `now` and `target`, `n + 1` each.
    pub(crate) fn to_words(&self) -> Vec<f64> {
        let n = self.expiries.len();
        let mut out = Vec::with_capacity(4 + n + 2 * (n + 1));
        out.push(f64::from(self.steps));
        out.push(f64::from(self.done));
        out.push(self.variance);
        out.push(n as f64);
        out.extend(self.expiries.iter().map(|e| *e as f64));
        out.extend_from_slice(&self.now);
        out.extend_from_slice(&self.target);
        out
    }

    /// The inverse of [`NightBridge::to_words`], refusing a list that is not
    /// one.
    pub(crate) fn from_words(words: &[f64]) -> Result<Self, String> {
        let bad = |why: &str| format!("this snapshot's night_bridge does not parse: {why}");
        let whole = |v: f64, what: &str| -> Result<u64, String> {
            if v.is_finite() && v >= 0.0 && v.fract() == 0.0 && v <= 1e9 {
                Ok(v as u64)
            } else {
                Err(bad(&format!("{what} is {v}")))
            }
        };
        if words.len() < 4 {
            return Err(bad("fewer than four numbers"));
        }
        let steps = whole(words[0], "the step count")? as u32;
        let done = whole(words[1], "the steps walked")? as u32;
        let variance = words[2];
        let n = whole(words[3], "the contract count")? as usize;
        if words.len() != 4 + n + 2 * (n + 1) {
            return Err(bad(&format!("{} numbers for {n} contracts", words.len())));
        }
        if done > steps || !(variance.is_finite() && variance >= 0.0) {
            return Err(bad(&format!("{done} of {steps} steps walked at a variance of {variance}")));
        }
        let mut expiries = Vec::with_capacity(n);
        for v in &words[4..4 + n] {
            if !(v.is_finite() && v.fract() == 0.0) {
                return Err(bad(&format!("an expiry is {v}")));
            }
            expiries.push(*v as i64);
        }
        let now = words[4 + n..4 + n + n + 1].to_vec();
        let target = words[4 + n + n + 1..].to_vec();
        if now.iter().chain(target.iter()).any(|v| !(v.is_finite() && *v > 0.0)) {
            return Err(bad("a level is not finite and positive"));
        }
        Ok(NightBridge { steps, done, variance, expiries, now, target })
    }
}

/// Everything the index futures carry.
#[derive(Debug, Clone)]
pub(crate) struct FuturesState {
    pub(crate) phase: FuturesPhase,
    /// The basis noise, index basis points.
    pub(crate) basis: f64,
    /// The listed contracts, in expiry order.
    pub(crate) contracts: Vec<ListedFuture>,
    /// Every final settlement so far, in order.
    pub(crate) settlements: Vec<Settlement>,
    /// Agents' resting orders on contracts, their undelivered fills and the
    /// arrival and fill counters: the equity book's shape, its `taken`,
    /// `flow`, `impacts` and `memory` unused.
    pub(crate) book: BookState,
    pub(crate) night: Option<NightBridge>,
    /// `stream::DERIVATIVES`.
    pub(crate) rng: GameRng,
}

impl FuturesState {
    pub(crate) fn new(seed: u64) -> Self {
        FuturesState {
            phase: FuturesPhase::BeforeFirstOpen,
            basis: 0.0,
            contracts: Vec::new(),
            settlements: Vec::new(),
            book: BookState::default(),
            night: None,
            rng: GameRng::substream(seed, stream::DERIVATIVES),
        }
    }

    /// The numbers the snapshot's `futures` key and the state hash carry:
    /// the phase, the basis, the contract count and each contract's expiry,
    /// mark, closing fair value, impact, pending flow and four taken
    /// counts, then the settlement count and each settlement's session,
    /// value and reference. NaN where a contract has no mark yet.
    pub(crate) fn to_words(&self) -> Vec<f64> {
        let mut out = Vec::with_capacity(
            4 + CONTRACT_WIDTH * self.contracts.len() + 3 * self.settlements.len());
        out.push(self.phase.code());
        out.push(self.basis);
        out.push(self.contracts.len() as f64);
        for c in &self.contracts {
            out.push(c.expiry as f64);
            out.push(c.mark);
            out.push(c.fair_close);
            out.push(c.impact);
            out.push(c.pending);
            out.extend_from_slice(&c.taken);
        }
        out.push(self.settlements.len() as f64);
        for s in &self.settlements {
            out.push(s.session as f64);
            out.push(s.value);
            out.push(s.reference);
        }
        out
    }

    /// Put back what [`FuturesState::to_words`] wrote, refusing a list that
    /// is not one. The book, the night and the generator are carried apart.
    pub(crate) fn set_words(&mut self, words: &[f64]) -> Result<(), String> {
        let bad = |why: &str| format!("this snapshot's futures do not parse: {why}");
        let whole = |v: f64, what: &str| -> Result<usize, String> {
            if v.is_finite() && v >= 0.0 && v.fract() == 0.0 && v <= 1e9 {
                Ok(v as usize)
            } else {
                Err(bad(&format!("{what} is {v}")))
            }
        };
        if words.len() < 4 {
            return Err(bad("fewer than four numbers"));
        }
        let phase = FuturesPhase::from_code(words[0])
            .ok_or_else(|| bad(&format!("the phase is {}", words[0])))?;
        let basis = words[1];
        if !basis.is_finite() {
            return Err(bad(&format!("the basis is {basis}")));
        }
        let n = whole(words[2], "the contract count")?;
        let mut at = 3;
        if words.len() < at + CONTRACT_WIDTH * n + 1 {
            return Err(bad("it is shorter than its counts say"));
        }
        let mut contracts = Vec::with_capacity(n);
        for _ in 0..n {
            let w = &words[at..at + CONTRACT_WIDTH];
            if !(w[0].is_finite() && w[0].fract() == 0.0) {
                return Err(bad(&format!("an expiry is {}", w[0])));
            }
            if w[3..].iter().any(|v| !v.is_finite()) {
                return Err(bad("a contract's impact, flow or taken depth is not finite"));
            }
            contracts.push(ListedFuture {
                expiry: w[0] as i64,
                mark: w[1],
                fair_close: w[2],
                impact: w[3],
                pending: w[4],
                taken: [w[5], w[6], w[7], w[8]],
            });
            at += CONTRACT_WIDTH;
        }
        let m = whole(words[at], "the settlement count")?;
        at += 1;
        if words.len() != at + 3 * m {
            return Err(bad(&format!("{} numbers for {n} contracts and {m} settlements", words.len())));
        }
        let mut settlements = Vec::with_capacity(m);
        for k in 0..m {
            let w = &words[at + 3 * k..at + 3 * k + 3];
            if !(w[0].is_finite() && w[0].fract() == 0.0 && w[1].is_finite() && w[2].is_finite()) {
                return Err(bad("a settlement is not a session and two finite prices"));
            }
            settlements.push(settlement_of(w[0] as i64, w[2]));
        }
        self.phase = phase;
        self.basis = basis;
        self.contracts = contracts;
        self.settlements = settlements;
        Ok(())
    }
}

/// The index future expiring at `session`, settled on `reference`.
fn settlement_of(session: i64, reference: f64) -> Settlement {
    let kind = ContractKind::IndexFuture;
    Settlement::new(index_future_symbol(session), kind, kind.root().to_string(), session, reference, reference)
}

/// How far a future's basis has converged: `min(1, sessions to expiry /
/// 6)`, the six sessions before expiry being the roll's.
fn convergence(sessions_to_expiry: f64) -> f64 {
    crate::mathx::max(0.0, crate::mathx::min(1.0, sessions_to_expiry / ROLL_SESSIONS as f64))
}

/// A price on the contract's grid, rounded away from the touch: up for an
/// ask, down for a bid.
fn on_grid(price: f64, tick: f64, side: Side) -> f64 {
    let steps = price / tick;
    match side {
        Side::Sell => (steps - 1e-7).ceil() * tick,
        Side::Buy => (steps + 1e-7).floor() * tick,
    }
}

/// `futures_impact_memory`: the futures' metaorder memory law, the equity
/// book's (`crate::agent_book::memory_displacement`) with the switch's own
/// coefficient `y` in place of `impact_memory_coefficient`, so the futures
/// carry the law whether or not the equity book does. `D(m) = sign(m) y
/// sigma h(|m|)`, `h(x) = x^delta` at or above the crossover `m*` and `x
/// m*^(delta - 1)` below it; `delta` is the latent depth's exponent and
/// `m*` the equity memory's crossover.
#[derive(Debug, Clone, Copy)]
struct FuturesMemoryLaw {
    y: f64,
    delta: f64,
    star: f64,
    sigma: f64,
}

impl FuturesMemoryLaw {
    fn of(params: &crate::params::ModelParams, sigma: f64) -> Self {
        let (delta, _) = crate::agent_book::depth_shape(params);
        FuturesMemoryLaw { y: params.futures_impact_memory, delta, star: params.impact_memory_crossover, sigma }
    }

    /// The displacement of a memory at `m` (fractions of daily volume), a
    /// log price.
    fn displacement(&self, m: f64) -> f64 {
        if !(self.y > 0.0) || m == 0.0 || !(self.sigma > 0.0) {
            return 0.0;
        }
        let a = m.abs();
        let h = if self.star > 0.0 && a < self.star {
            a * crate::mathx::pow(self.star, self.delta - 1.0)
        } else {
            crate::mathx::pow(a, self.delta)
        };
        let d = self.y * self.sigma * h;
        if m > 0.0 { d } else { -d }
    }

    /// The memory whose displacement is `d`.
    fn inverse(&self, d: f64) -> f64 {
        if !(self.y > 0.0) || d == 0.0 || !(self.sigma > 0.0) {
            return 0.0;
        }
        let a = d.abs() / (self.y * self.sigma);
        let m = if self.star > 0.0 && a < crate::mathx::pow(self.star, self.delta) {
            a / crate::mathx::pow(self.star, self.delta - 1.0)
        } else {
            crate::mathx::pow(a, 1.0 / self.delta)
        };
        if d > 0.0 { m } else { -m }
    }
}

/// `futures_impact_memory`: no contract the house sells or buys is priced
/// better than the memory's own price once that contract is in it, the
/// equity book's `MemoryBound` on the contract's grid, applied on both
/// sides. `reference` is the contract's price now, which carries
/// `booked`, the displacement the last step put in it as a log price;
/// `scale` turns a displacement of the memory into one of the price (the
/// convergence on the index as the contract expires, and index over price).
struct FuturesMemoryBound {
    law: FuturesMemoryLaw,
    volume: f64,
    reference: f64,
    position: f64,
    booked: f64,
    scale: f64,
    tick: f64,
}

impl FuturesMemoryBound {
    /// The bound on the `q`-th contract (cumulative) on one side: the
    /// book's `Sell` side is what a buy meets.
    fn price(&self, side: Side, q: f64) -> f64 {
        let m = match side {
            Side::Sell => self.position + q / self.volume,
            Side::Buy => self.position - q / self.volume,
        };
        self.reference * crate::mathx::exp(self.scale * self.law.displacement(m) - self.booked)
    }

    /// Cumulative contracts up to which a level at `price` is inside the
    /// bound.
    fn reach(&self, side: Side, price: f64) -> f64 {
        let x = (crate::mathx::log(price / self.reference) + self.booked) / self.scale;
        let m = self.law.inverse(x);
        match side {
            Side::Sell => (m - self.position) * self.volume,
            Side::Buy => (self.position - m) * self.volume,
        }
    }

    /// One side's house orders, best first, with every contract past the
    /// bound re-priced onto it: `crate::agent_book::MemoryBound::apply` on
    /// the contract's grid.
    fn apply(&self, orders: Vec<crate::order_book::BookOrder>, side: Side, symbol: &str)
        -> Vec<crate::order_book::BookOrder> {
        let worse = |a: f64, b: f64| match side {
            Side::Sell => crate::mathx::max(a, b),
            Side::Buy => crate::mathx::min(a, b),
        };
        let step = self.volume * DEPTH_GRID_FLOOR;
        let mut out = Vec::with_capacity(orders.len());
        let mut placed = 0.0;
        let mut last: Option<f64> = None;
        let mut pieces: Vec<(f64, f64)> = Vec::new();
        for o in orders {
            let mut left = o.remaining;
            let within = crate::mathx::max(0.0, crate::mathx::min(left, self.reach(side, o.price) - placed));
            pieces.clear();
            if within > 0.0 {
                pieces.push((o.price, within));
                left -= within;
                placed += within;
            }
            while left > 1e-9 {
                let span = crate::mathx::max(step, 0.25 * placed);
                let take = crate::mathx::min(left, span);
                placed += take;
                left -= take;
                let bound = on_grid(self.price(side, placed), self.tick, side);
                pieces.push((worse(o.price, bound), take));
            }
            for (k, &(price, n)) in pieces.iter().enumerate() {
                let price = match last {
                    Some(prev) => worse(price, prev),
                    None => price,
                };
                last = Some(price);
                out.push(if k == 0 {
                    crate::order_book::BookOrder { price, quantity: n, remaining: n, ..o.clone() }
                } else {
                    o.piece(k as u32, price, n, symbol)
                });
            }
        }
        out
    }
}

/// What a future is worth now, and from what.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Valuation {
    pub(crate) index: f64,
    pub(crate) fair: f64,
    pub(crate) rate: f64,
    pub(crate) dividends: f64,
    pub(crate) sessions: f64,
    /// The basis now, index basis points, after the convergence.
    pub(crate) basis_bp: f64,
    pub(crate) price: f64,
}

impl Engine {
    // ── State and clock ───────────────────────────────────────────────────

    pub(super) fn futures_on(&self) -> bool {
        self.params.futures_index_listed != 0.0
    }

    /// `futures_impact_memory`: agents' flow against the house marks the
    /// index futures through the metaorder memory, not the spec's linear
    /// mark.
    fn futures_memory_on(&self) -> bool {
        self.params.futures_impact_memory != 0.0
    }

    /// The contract in `slot`'s memory row (`crate::agent_book::MEMORY_*`),
    /// zeros when it has none.
    fn futures_memory_row(&self, slot: usize) -> [f64; crate::agent_book::MEMORY_WIDTH] {
        self.futures.book.memory.get(slot).copied().unwrap_or([0.0; crate::agent_book::MEMORY_WIDTH])
    }

    /// The night's steps: 0 without a night session.
    fn night_steps(&self) -> u32 {
        if self.futures_on() {
            self.params.night_session_steps as u32
        } else {
            0
        }
    }

    /// Whether the night has walked a step since the last close. From then
    /// until the next open the futures' path shows where that open will be
    /// (`night_tick`), so stocks, which do not trade at night, refuse
    /// orders (`Engine::submit_order`).
    pub fn night_walked(&self) -> bool {
        self.night_steps() > 0
            && self.futures.phase == FuturesPhase::Night
            && self.futures.night.as_ref().is_some_and(|b| b.done > 0)
    }

    /// Now on the futures' clock, in sessions: the session of the last open
    /// plus the share of its ticks run, or the next session's open between
    /// sessions.
    fn futures_now(&self) -> f64 {
        let t = self.elapsed_days as f64;
        match self.futures.phase {
            FuturesPhase::BeforeFirstOpen => t,
            FuturesPhase::Session => {
                t + crate::mathx::min(1.0, f64::from(self.ticks_today()) / SESSION_TICKS)
            }
            FuturesPhase::Night => t + 1.0,
        }
    }

    /// Expiries strictly after this session are listed: the last open's, or
    /// the one before the next open before any.
    fn futures_listing_after(&self) -> i64 {
        match self.futures.phase {
            FuturesPhase::BeforeFirstOpen => self.elapsed_days - 1,
            _ => self.elapsed_days,
        }
    }

    /// List the front two on a fresh engine. Nothing with the switch off.
    pub(super) fn futures_list_initial(&mut self) {
        if !self.futures_on() {
            return;
        }
        let after = self.futures_listing_after();
        self.futures.contracts = calendar::index_future_expiries(after, calendar::INDEX_FUTURES_LISTED)
            .into_iter()
            .map(ListedFuture::new)
            .collect();
    }

    /// The listed contract named `symbol`, by slot.
    fn futures_slot(&self, symbol: &str) -> Option<usize> {
        let parsed = ContractSymbol::parse(symbol).ok()?;
        if parsed.root != ContractKind::IndexFuture.root() || parsed.kind != SymbolKind::Future {
            return None;
        }
        self.futures.contracts.iter().position(|c| c.expiry == parsed.expiry)
    }

    /// Whether this model lists futures of any family: one of
    /// `futures_index_listed`, `futures_vix_listed`, `futures_rates_listed`
    /// and `futures_oil_listed` set. A property of the model, not of the
    /// moment: a model that lists VIX futures says so before its first
    /// close, when [`Engine::contracts`] is still empty. False on every
    /// shipped preset.
    pub fn lists_futures(&self) -> bool {
        self.futures_on() || self.vix_futures_on() || self.rate_futures_on() || self.oil_futures_on()
    }

    /// Whether `symbol` names a listed contract: an index, VIX, rate or oil
    /// future under its family's switch.
    pub fn is_listed_contract(&self, symbol: &str) -> bool {
        (self.futures_on() && self.futures_slot(symbol).is_some())
            || (self.vix_futures_on() && self.vix_futures_slot(symbol).is_some())
            || (self.rate_futures_on() && self.rate_futures_slot(symbol).is_some())
            || (self.oil_futures_on() && self.oil_futures_slot(symbol).is_some())
            || self.is_listed_option(symbol)
            || self.is_listed_name_option(symbol)
    }

    /// Whether an order with the id `id` already waits in any book: the
    /// names' or a contract family's.
    pub(super) fn contract_order_id_taken(&self, id: &str) -> bool {
        self.book.orders.iter().any(|o| o.id == id)
            || self.futures.book.orders.iter().any(|o| o.id == id)
            || self.vix_futures_orders().iter().any(|o| o.id == id)
            || self.rate_futures_orders().iter().any(|o| o.id == id)
            || self.oil_futures_orders().iter().any(|o| o.id == id)
            || self.option_orders().iter().any(|o| o.id == id)
            || self.name_option_orders().iter().any(|o| o.id == id)
    }

    // ── Pricing ───────────────────────────────────────────────────────────

    /// The mean policy rate expected to be in force over the sessions from
    /// now to `expiry`, a fraction a year: the forecast's path where there
    /// is one, the policy rate now where there is not.
    pub(super) fn futures_rate(&self, expiry: i64, now: f64) -> f64 {
        let current = self.economy.federal_funds_rate;
        let Some(f) = self.forecast() else {
            return current / 100.0;
        };
        let first = crate::mathx::max(now.floor(), f.day as f64) as i64;
        if expiry <= first || f.policy_rate.is_empty() {
            return current / 100.0;
        }
        let last = f.policy_rate.len() as i64;
        let mut total = 0.0;
        for s in first..expiry {
            // The rate in force in session `s` is the one set at the close
            // before it: the policy rate now for the forecast's own day, the
            // forecast's `h`-session expectation for `s = day + h`.
            let h = s - f.day;
            total += if h <= 0 {
                current
            } else {
                f.policy_rate[(crate::mathx::min(h as f64, last as f64) - 1.0) as usize]
            };
        }
        total / (expiry - first) as f64 / 100.0
    }

    /// The present value, in index points at `rate`, of the constituents'
    /// dividends going ex after the last open and no later than `expiry`.
    fn futures_dividends(&self, expiry: i64, rate: f64, now: f64) -> f64 {
        self.carry_dividends(expiry, rate, now, self.futures_listing_after())
    }

    /// The present value, in index points at `rate`, of the constituents'
    /// dividends going ex after session `after` and no later than `expiry`:
    /// the declared amount within 21 sessions of an ex-date, the Lintner
    /// rule's projection beyond.
    pub(super) fn carry_dividends(&self, expiry: i64, rate: f64, now: f64, after: i64) -> f64 {
        use crate::market::dividends as dv;
        if self.params.dividend_payout_share == 0.0 || !(self.index_divisor > 0.0) {
            return 0.0;
        }
        let mut total = 0.0;
        for c in &self.companies {
            if !c.is_public || c.is_bankrupt {
                continue;
            }
            // The same lookahead the American options' pricer reads, so a
            // name's projected dividends are one number wherever they enter.
            for u in dv::lookahead(&self.params, &c.ticker, c.stock.dividend.as_ref(), after, c.stock.price, expiry) {
                let points = c.stock.shares_outstanding * u.amount / self.index_divisor;
                total += points * crate::mathx::exp(-rate * (u.ex_day as f64 - now) / SESSIONS_PER_YEAR as f64);
            }
        }
        total
    }

    /// The carry fair value of the future expiring at `expiry`, on the index
    /// `index` at `now`: `(S - PV(D)) * exp(r tau)`, with its parts.
    fn futures_carry(&self, expiry: i64, index: f64, now: f64) -> (f64, f64, f64, f64) {
        let sessions = crate::mathx::max(0.0, expiry as f64 - now);
        let rate = self.futures_rate(expiry, now);
        let dividends = self.futures_dividends(expiry, rate, now);
        let fair = (index - dividends) * crate::mathx::exp(rate * sessions / SESSIONS_PER_YEAR as f64);
        (fair, rate, dividends, sessions)
    }

    /// The index the futures read in a session: the live level.
    fn futures_live_index(&self) -> f64 {
        if self.index_divisor > 0.0 {
            self.index_level_now()
        } else {
            crate::market::index_value::INDEX_BASE
        }
    }

    /// What the contract in `slot` is worth now.
    pub(crate) fn futures_valuation(&self, slot: usize) -> Valuation {
        let c = &self.futures.contracts[slot];
        let now = self.futures_now();
        let (index, fair, rate, dividends, sessions) = match self.futures.phase {
            FuturesPhase::Night => {
                let bridged = self.futures.night.as_ref().and_then(|b| {
                    b.expiries.iter().position(|e| *e == c.expiry).map(|j| (b.now[0], b.now[1 + j]))
                });
                let index = match bridged {
                    Some((i, _)) => i,
                    None if self.index_close > 0.0 => self.index_close,
                    None => self.futures_live_index(),
                };
                let (carry, rate, dividends, sessions) = self.futures_carry(c.expiry, index, now);
                let fair = match bridged {
                    Some((_, f)) => f,
                    None if c.fair_close.is_finite() => c.fair_close,
                    None => carry,
                };
                (index, fair, rate, dividends, sessions)
            }
            _ => {
                let index = self.futures_live_index();
                let (fair, rate, dividends, sessions) = self.futures_carry(c.expiry, index, now);
                (index, fair, rate, dividends, sessions)
            }
        };
        let basis_bp = convergence(sessions) * (self.futures.basis + c.impact);
        Valuation { index, fair, rate, dividends, sessions, basis_bp, price: fair + basis_bp * index / 1e4 }
    }

    /// The mid of the book of the listed index future expiring at `expiry`,
    /// or its valuation's price while a side of the book is empty; `None`
    /// with the index futures off or no contract of that expiry listed. The
    /// index options' dealer reads it under
    /// `options_index_forward_from_future`.
    pub(super) fn index_future_mid(&self, expiry: i64) -> Option<f64> {
        if !self.futures_on() {
            return None;
        }
        let slot = self.futures.contracts.iter().position(|c| c.expiry == expiry)?;
        let book = self.futures_book(slot, None);
        Some(match (book.best_bid(), book.best_ask()) {
            (Some(b), Some(a)) => 0.5 * (a + b),
            _ => self.futures_valuation(slot).price,
        })
    }

    /// The daily volume the futures' depth is sized to, contracts: the
    /// constituents' daily dollar volume times the spec's share, over a
    /// contract's notional at `index`.
    fn futures_daily_volume(&self, index: f64) -> f64 {
        let spec = &INDEX_FUTURE;
        let mut dollars = 0.0;
        for c in &self.companies {
            if c.is_public && !c.is_bankrupt {
                dollars += crate::agent_book::daily_volume(c) * c.stock.price;
            }
        }
        let notional = spec.multiplier * index;
        if !(notional > 0.0) || !(dollars > 0.0) {
            return 1.0;
        }
        crate::mathx::max(1.0, spec.volume_share * dollars / notional)
    }

    // ── The book ──────────────────────────────────────────────────────────

    /// The latent depth on one side of a contract's book: the equity book's
    /// square-root pool (`agent_book::append_latent_depth`) at the spec's
    /// shape, on the contract's grid.
    fn futures_depth(book: &mut OrderBook, side: Side, touch: f64, sigma: f64, volume: f64, spec: &IndexFutureSpec) {
        let y = spec.depth_coefficient;
        let reach = spec.depth_reach * volume;
        if !(y > 0.0 && volume > 0.0 && touch > 0.0 && sigma > 0.0) {
            return;
        }
        let floor = volume * DEPTH_GRID_FLOOR;
        let mut bounds = vec![reach];
        while let Some(&last) = bounds.last() {
            let next = last / DEPTH_LEVEL_RATIO;
            if next < floor {
                break;
            }
            bounds.push(next);
        }
        bounds.reverse();
        let mut placed = 0.0;
        let mut pending: Option<(f64, f64)> = None;
        let mut levels: Vec<(f64, f64)> = Vec::new();
        for bound in bounds {
            let distance = y * sigma * crate::mathx::pow(bound / volume, spec.depth_exponent);
            let raw = match side {
                Side::Sell => touch * (1.0 + distance),
                Side::Buy => touch * (1.0 - distance),
            };
            let price = on_grid(raw, spec.tick, side);
            if !(price > 0.0) {
                break;
            }
            let shares = bound.floor() - placed;
            if !(shares >= 1.0) {
                continue;
            }
            placed += shares;
            pending = match pending {
                Some((p, q)) if p == price => Some((p, q + shares)),
                Some(level) => {
                    levels.push(level);
                    Some((price, shares))
                }
                None => Some((price, shares)),
            };
        }
        if let Some(level) = pending {
            levels.push(level);
        }
        for (price, shares) in levels {
            book.rest_limit(side, price, shares, DEPTH_OWNER);
        }
    }

    /// The agent-facing book of the contract in `slot`, leaving out one
    /// agent's orders: the maker's ladder around the futures price, the
    /// latent depth beside it, less what agents have taken from the front
    /// of each, and every agent's resting orders.
    fn futures_book(&self, slot: usize, exclude: Option<&str>) -> OrderBook {
        let spec = &INDEX_FUTURE;
        let c = &self.futures.contracts[slot];
        let v = self.futures_valuation(slot);
        let symbol = index_future_symbol(c.expiry);
        let volume = self.futures_daily_volume(v.index);
        let sigma = self.market_vol.sigma_daily();
        let mut book = OrderBook::new(symbol.clone(), Some(v.price)).with_cap(AGENT_BOOK_CAP);
        let size = crate::mathx::max(1.0, (volume * spec.maker_level_share).floor());
        let n = (v.price / spec.tick).floor();
        for k in 0..spec.maker_levels {
            let k = k as f64;
            book.rest_limit(Side::Buy, (n - k) * spec.tick, size, MARKET_MAKER_ID);
            book.rest_limit(Side::Sell, (n + 1.0 + k) * spec.tick, size, MARKET_MAKER_ID);
        }
        let bid = n * spec.tick;
        let ask = (n + 1.0) * spec.tick;
        Self::futures_depth(&mut book, Side::Buy, bid, sigma, volume, spec);
        Self::futures_depth(&mut book, Side::Sell, ask, sigma, volume, spec);
        remove_front(&mut book, Side::Buy, MARKET_MAKER_ID, c.taken[TAKEN_MAKER_BID]);
        remove_front(&mut book, Side::Sell, MARKET_MAKER_ID, c.taken[TAKEN_MAKER_ASK]);
        remove_front(&mut book, Side::Buy, DEPTH_OWNER, c.taken[TAKEN_DEPTH_BID]);
        remove_front(&mut book, Side::Sell, DEPTH_OWNER, c.taken[TAKEN_DEPTH_ASK]);
        // `futures_impact_memory`: no house contract on either side is
        // better than the memory's price after it, the book of Alfonsi,
        // Fruth and Schied (2010) with the maker's ladder over it: an order
        // pays the curve it books, and selling back walks it down.
        if self.futures_memory_on() {
            let row = self.futures_memory_row(slot);
            let law = FuturesMemoryLaw::of(&self.params, sigma);
            let position = crate::agent_book::memory_position(&self.params, &row)
                + if volume > 0.0 { row[crate::agent_book::MEMORY_FLOW] / volume } else { 0.0 };
            let scale = convergence(v.sessions) * v.index / v.price;
            if volume > 0.0 && v.price > 0.0 && scale > 0.0 && law.sigma > 0.0 && law.y > 0.0 {
                let bound = FuturesMemoryBound {
                    law,
                    volume,
                    reference: v.price,
                    position,
                    booked: scale * row[crate::agent_book::MEMORY_BOOKED],
                    scale,
                    tick: spec.tick,
                };
                book.bids = bound.apply(std::mem::take(&mut book.bids), Side::Buy, &symbol);
                book.asks = bound.apply(std::mem::take(&mut book.asks), Side::Sell, &symbol);
            }
        }
        for o in &self.futures.book.orders {
            if o.ticker != symbol || exclude == Some(o.agent.as_str()) {
                continue;
            }
            book.post_limit(o.side, o.limit, o.remaining, &o.agent, Some(o.id.clone()));
        }
        book
    }

    /// The agent-facing book of a listed contract: `None` for a symbol not
    /// listed.
    pub fn contract_book(&self, symbol: &str) -> Option<OrderBook> {
        if self.options_on() {
            if let Some(book) = self.option_book(symbol) {
                return Some(book);
            }
        }
        if self.names_on() {
            if let Some(book) = self.name_option_book(symbol) {
                return Some(book);
            }
        }
        if self.vix_futures_on() {
            if let Some(slot) = self.vix_futures_slot(symbol) {
                return self.vix_futures_book_at(slot);
            }
        }
        if self.rate_futures_on() {
            if let Some(slot) = self.rate_futures_slot(symbol) {
                return self.rate_futures_book_at(slot);
            }
        }
        if self.oil_futures_on() {
            if let Some(slot) = self.oil_futures_slot(symbol) {
                return self.oil_futures_book_at(slot);
            }
        }
        if !self.futures_on() {
            return None;
        }
        let slot = self.futures_slot(symbol)?;
        Some(self.futures_book(slot, None))
    }

    /// The listed contract at position `k` in listing order (the index
    /// futures, then the VIX, rate and oil futures), for
    /// [`Engine::book_for`]'s indices past the rate instruments.
    pub(super) fn contract_book_at(&self, k: usize) -> Option<OrderBook> {
        let index = if self.futures_on() { self.futures.contracts.len() } else { 0 };
        if k < index {
            return Some(self.futures_book(k, None));
        }
        let vix = if self.vix_futures_on() { self.vix_futures.contracts.len() } else { 0 };
        if k < index + vix {
            return self.vix_futures_book_at(k - index);
        }
        let rates = if self.rate_futures_on() { self.rate_futures.contracts.len() } else { 0 };
        if k < index + vix + rates {
            return self.rate_futures_book_at(k - index - vix);
        }
        self.oil_futures_book_at(k - index - vix - rates)
    }

    fn futures_push_fill(&mut self, mut fill: AgentFill) -> AgentFill {
        fill.sequence = self.futures.book.fill_sequence;
        self.futures.book.fill_sequence += 1;
        self.futures.book.fills.push(fill.clone());
        fill
    }

    fn futures_reduce_order(&mut self, order_id: &str, quantity: f64) {
        if let Some(o) = self.futures.book.orders.iter_mut().find(|o| o.id == order_id) {
            o.remaining -= quantity;
        }
        self.futures.book.orders.retain(|o| o.remaining > 1e-9);
    }

    /// An order meets the contract's book. Records what it took from the
    /// house, the other agents' resting orders it hit, a taker fill per
    /// level and its net flow against the house for the next step. Returns
    /// the taker fills and the mid the order met.
    #[allow(clippy::too_many_arguments)]
    fn futures_meet(
        &mut self,
        slot: usize,
        agent: &str,
        order_id: &str,
        side: Side,
        quantity: f64,
        limit: Option<f64>,
    ) -> (Vec<AgentFill>, f64) {
        let mut book = self.futures_book(slot, Some(agent));
        let symbol = book.company_id.clone();
        let reference = book.mid_price().unwrap_or(self.futures_valuation(slot).price);
        let r = book.submit(
            side,
            quantity,
            agent,
            SubmitOptions { limit_price: limit, post_remainder: false, order_id: None, skip_own: true, house_ids: false },
        );
        let (day, tick) = (self.current_day, self.ticks_today());
        let mut taker = Vec::with_capacity(r.fills.len());
        for f in &r.fills {
            let owner: &str = &f.maker_id;
            let house = owner == MARKET_MAKER_ID || owner == DEPTH_OWNER;
            if house {
                let k = match (side, owner == MARKET_MAKER_ID) {
                    (Side::Buy, true) => TAKEN_MAKER_ASK,
                    (Side::Sell, true) => TAKEN_MAKER_BID,
                    (Side::Buy, false) => TAKEN_DEPTH_ASK,
                    (Side::Sell, false) => TAKEN_DEPTH_BID,
                };
                if self.futures_memory_on() {
                    // The memory's flow and what it paid against the mid
                    // it met, for the next step (`futures_memory_step`).
                    let n = self.futures.contracts.len();
                    self.futures.book.add_house_flow(n, slot, side, f.quantity, f.price, reference);
                }
                let c = &mut self.futures.contracts[slot];
                c.taken[k] += f.quantity;
                c.pending += match side {
                    Side::Buy => f.quantity,
                    Side::Sell => -f.quantity,
                };
            } else {
                self.futures_reduce_order(&f.maker_order_id, f.quantity);
                self.futures_push_fill(AgentFill {
                    agent: owner.to_string(),
                    order_id: f.maker_order_id.clone(),
                    ticker: symbol.clone(),
                    side: match side {
                        Side::Buy => Side::Sell,
                        Side::Sell => Side::Buy,
                    },
                    quantity: f.quantity,
                    price: f.price,
                    liquidity: Liquidity::Maker,
                    counterparty: agent.to_string(),
                    reference,
                    day,
                    tick,
                    sequence: 0,
                });
            }
            taker.push(self.futures_push_fill(AgentFill {
                agent: agent.to_string(),
                order_id: order_id.to_string(),
                ticker: symbol.clone(),
                side,
                quantity: f.quantity,
                price: f.price,
                liquidity: Liquidity::Taker,
                counterparty: owner.to_string(),
                reference,
                day,
                tick,
                sequence: 0,
            }));
        }
        (taker, reference)
    }

    /// Match every resting order the contracts' books now cross, at the
    /// books' prices, in arrival order: the futures price moved through it.
    fn futures_cross_resting(&mut self) {
        if self.futures.book.orders.is_empty() {
            return;
        }
        let ids: Vec<String> = self.futures.book.orders.iter().map(|o| o.id.clone()).collect();
        for id in ids {
            let Some(o) = self.futures.book.orders.iter().find(|o| o.id == id).cloned() else {
                continue;
            };
            let Some(slot) = self.futures_slot(&o.ticker) else {
                continue;
            };
            let book = self.futures_book(slot, Some(&o.agent));
            let crossed = match o.side {
                Side::Buy => book.best_ask().is_some_and(|a| o.limit >= a),
                Side::Sell => book.best_bid().is_some_and(|b| o.limit <= b),
            };
            if !crossed {
                continue;
            }
            let (fills, _) = self.futures_meet(slot, &o.agent, &o.id, o.side, o.remaining, Some(o.limit));
            let filled: f64 = fills.iter().map(|f| f.quantity).sum();
            self.futures_reduce_order(&o.id, filled);
        }
    }

    /// An agent's order on a listed contract (`Engine::submit_order` with a
    /// contract symbol): a market order takes what the book holds, a limit
    /// takes what it holds at the limit or better and rests the rest in the
    /// contract's queue. Its net flow against the house moves the
    /// contract's basis from the next step.
    pub(super) fn submit_contract_order(
        &mut self,
        agent: &str,
        symbol: &str,
        side: Side,
        quantity: f64,
        limit: Option<f64>,
        order_id: Option<String>,
    ) -> Result<OrderReport, String> {
        let slot = self.futures_slot(symbol).ok_or_else(|| {
            format!("{symbol} is not a listed contract: Engine.contracts() lists the ones that trade")
        })?;
        let id = match order_id {
            Some(id) => {
                if id.is_empty() {
                    return Err("order_id cannot be empty".to_string());
                }
                id
            }
            None => format!("{agent}-{symbol}-{}", self.futures.book.sequence),
        };
        if self.contract_order_id_taken(&id) {
            return Err(format!("order id {id:?} is already waiting in the book"));
        }
        // Before the night's first step the book stands on the close's
        // print, and the close's re-mark, already public, enters at that
        // step: the order walks it first and meets the book after it.
        if self.futures.phase == FuturesPhase::Night && self.futures.night.as_ref().is_some_and(|b| b.done == 0) {
            self.night_tick(1);
        }
        let sequence = self.futures.book.sequence;
        self.futures.book.sequence += 1;
        self.futures_cross_resting();
        let (fills, reference) = self.futures_meet(slot, agent, &id, side, quantity, limit);
        let mut filled = 0.0;
        let mut notional = 0.0;
        let mut worst: Option<f64> = None;
        for f in &fills {
            filled += f.quantity;
            notional += f.price * f.quantity;
            worst = Some(f.price);
        }
        let remainder = quantity - filled;
        let (resting, mode) = match limit {
            Some(p) if remainder > 1e-9 => {
                self.futures.book.orders.push(AgentOrder {
                    id: id.clone(),
                    agent: agent.to_string(),
                    ticker: symbol.to_string(),
                    side,
                    limit: p,
                    quantity,
                    remaining: remainder,
                    sequence,
                    mode: RestMode::Queue,
                });
                (remainder, Some(RestMode::Queue))
            }
            _ => (0.0, None),
        };
        Ok(OrderReport {
            order_id: id,
            agent: agent.to_string(),
            ticker: symbol.to_string(),
            side,
            requested: quantity,
            filled,
            average_price: if filled > 0.0 { Some(notional / filled) } else { None },
            worst_price: worst,
            reference,
            resting,
            unfilled: if mode.is_some() { 0.0 } else { remainder },
            mode,
            fills,
        })
    }

    /// Cancel a resting order on a contract. Returns whether one was removed.
    pub(super) fn cancel_contract_order(&mut self, order_id: &str, agent: Option<&str>) -> bool {
        let before = self.futures.book.orders.len();
        self.futures
            .book
            .orders
            .retain(|o| !(o.id == order_id && agent.is_none_or(|a| a == o.agent)));
        self.futures.book.orders.len() != before
    }

    // ── Steps ─────────────────────────────────────────────────────────────

    /// The index a step's flow is scaled on: the night's path, the close's
    /// index without one, or the live level.
    fn futures_step_index(&self) -> f64 {
        match (self.futures.phase, self.futures.night.as_ref()) {
            (FuturesPhase::Night, Some(b)) => b.now[0],
            (FuturesPhase::Night, None) if self.index_close > 0.0 => self.index_close,
            _ => self.futures_live_index(),
        }
    }

    /// `futures_impact_memory`: the metaorder memory's step for every
    /// contract, the equity book's (`Engine::plan_memory`) on the contract's
    /// daily volume and the index's daily sigma. On an open step both
    /// memories decay (the fast part at `futures_impact_memory_half_life`,
    /// or `impact_memory_half_life` with it at 0.0, the slow part at
    /// `impact_memory_slow_half_life`; none at night, so it holds over the
    /// night as the equity memory does); the flow against the house since
    /// the last step, over the daily volume, is added to both (the book
    /// priced it on the curve it moves along, so no cap is needed); and the
    /// contract's mark on the basis is the displacement, in index
    /// basis points. An empty memory reads nothing.
    fn futures_memory_step(&mut self, open_now: bool) {
        use crate::agent_book::{
            memory_decay, memory_position, MEMORY_BOOKED, MEMORY_FAST, MEMORY_FLOW, MEMORY_PAID, MEMORY_SLOW,
            MEMORY_WIDTH,
        };
        if self.futures.book.memory.iter().all(|r| r.iter().all(|x| *x == 0.0)) {
            for c in self.futures.contracts.iter_mut() {
                c.impact = 0.0;
            }
            return;
        }
        let n = self.futures.contracts.len();
        self.futures.book.memory.resize(n, [0.0; MEMORY_WIDTH]);
        let volume = self.futures_daily_volume(self.futures_step_index());
        let law = FuturesMemoryLaw::of(&self.params, self.market_vol.sigma_daily());
        let slow_on = self.params.impact_memory_slow_half_life > 0.0;
        // `futures_impact_memory_half_life`: 0.0 is a branch, the equity's
        // fast half-life.
        let fast_half_life = if self.params.futures_impact_memory_half_life == 0.0 {
            self.params.impact_memory_half_life
        } else {
            self.params.futures_impact_memory_half_life
        };
        let (fast, slow) = if open_now {
            (memory_decay(fast_half_life), memory_decay(self.params.impact_memory_slow_half_life))
        } else {
            (1.0, 1.0)
        };
        let snap = |x: f64| if x.abs() < 1e-12 { 0.0 } else { x };
        for slot in 0..n {
            let row = self.futures.book.memory[slot];
            if row.iter().all(|x| *x == 0.0) {
                self.futures.contracts[slot].impact = 0.0;
                continue;
            }
            let shares = row[MEMORY_FLOW];
            let net = if volume > 0.0 { shares / volume } else { 0.0 };
            let mut next = row;
            next[MEMORY_FAST] = snap(row[MEMORY_FAST] * fast);
            next[MEMORY_SLOW] = if slow_on { snap(row[MEMORY_SLOW] * slow) } else { 0.0 };
            next[MEMORY_PAID] = 0.0;
            next[MEMORY_FLOW] = 0.0;
            // No cap on what the flow adds: the book priced it on the
            // curve it moves along (`futures_book`), so it paid for it.
            if net != 0.0 {
                next[MEMORY_FAST] += net;
                if slow_on {
                    next[MEMORY_SLOW] += net;
                }
            }
            let d = law.displacement(memory_position(&self.params, &next));
            next[MEMORY_BOOKED] = d;
            self.futures.book.memory[slot] = next;
            self.futures.contracts[slot].impact = 1e4 * d;
        }
    }

    /// One step of the futures: the flow since the last step reaches the
    /// basis and the old flow's mark decays, the basis noise steps (one
    /// normal, always), the maker re-quotes whole and the latent depth
    /// refills, the night's path steps under `night`, and resting orders
    /// the new books cross fill.
    fn futures_step(&mut self, night: bool) {
        let spec = &INDEX_FUTURE;
        let steps_a_day = SESSION_TICKS + f64::from(self.night_steps());
        let rho = self.params.basis_persistence;
        let share = self.params.basis_night_share;
        let nights = f64::from(self.night_steps());
        let a = if !(rho > 0.0) {
            0.0
        } else if share != 0.0 && nights > 0.0 {
            // `basis_night_share`: the night's steps carry `rho^share` of a
            // day's decay and the session's ticks the rest; the noise below
            // keeps the basis's sd, so the close's reading is unchanged.
            let part = if night { share / nights } else { (1.0 - share) / SESSION_TICKS };
            crate::mathx::pow(rho, part)
        } else {
            crate::mathx::pow(rho, 1.0 / steps_a_day)
        };
        let sd = self.params.basis_sd * crate::mathx::sqrt(crate::mathx::max(0.0, 1.0 - a * a));
        self.futures.rng.site(Site::FuturesBasisZ, 0);
        let z = self.futures.rng.next_normal();
        self.futures.basis = a * self.futures.basis + sd * z;
        let memory = self.futures_memory_on();
        if !self.futures.contracts.is_empty() {
            // The flow's scale, read only when some contract has flow to
            // apply: an untraded step reads nothing but its own state. With
            // `futures_impact_memory` the memory marks the flow instead.
            let scale = if !memory && self.futures.contracts.iter().any(|c| c.pending != 0.0) {
                let volume = self.futures_daily_volume(self.futures_step_index());
                spec.impact_coefficient * 1e4 * self.market_vol.sigma_daily() / volume
            } else {
                0.0
            };
            let decay = crate::mathx::pow(0.5, 1.0 / spec.impact_half_life);
            let refill = crate::mathx::pow(0.5, 1.0 / spec.refill_half_life);
            for c in self.futures.contracts.iter_mut() {
                c.impact = c.impact * decay + scale * c.pending;
                if c.impact.abs() < 1e-12 {
                    c.impact = 0.0;
                }
                c.pending = 0.0;
                c.taken[TAKEN_MAKER_BID] = 0.0;
                c.taken[TAKEN_MAKER_ASK] = 0.0;
                for k in [TAKEN_DEPTH_BID, TAKEN_DEPTH_ASK] {
                    c.taken[k] = if c.taken[k] * refill < 1e-6 { 0.0 } else { c.taken[k] * refill };
                }
            }
            if memory {
                self.futures_memory_step(!night);
            }
        }
        if night {
            // The first step starts from the index the close's macro step
            // left and each contract's carry on it: the re-mark
            // (`macro_publication_repricing`) is public from the close, so it
            // enters whole here and not as a drift along the path that
            // anyone could see.
            let start = match self.futures.night.as_ref() {
                Some(b) if b.done == 0 => {
                    let index = self.futures_live_index();
                    let clock = self.futures_now();
                    let mut start = vec![index];
                    start.extend(b.expiries.iter().map(|e| self.futures_carry(*e, index, clock).0));
                    start.iter().all(|v| v.is_finite() && *v > 0.0).then_some(start)
                }
                _ => None,
            };
            self.futures.rng.site(Site::NightBridgeZ, 0);
            let z = self.futures.rng.next_normal();
            if let Some(b) = self.futures.night.as_mut() {
                if let Some(start) = start {
                    b.now = start;
                }
                let left = b.steps - b.done;
                if left <= 1 {
                    b.now.clone_from(&b.target);
                } else {
                    let left = f64::from(left);
                    let v = b.variance / f64::from(b.steps);
                    let sd = crate::mathx::sqrt(crate::mathx::max(0.0, v * (left - 1.0) / left));
                    for (now, target) in b.now.iter_mut().zip(b.target.iter()) {
                        let x = crate::mathx::log(*now);
                        let x = x + (crate::mathx::log(*target) - x) / left + sd * z;
                        *now = crate::mathx::exp(x);
                    }
                }
                b.done += 1;
            }
        }
        self.futures_cross_resting();
    }

    /// A tick's step, on every open tick. Nothing with the switch off.
    pub(super) fn futures_session_step(&mut self) {
        if !self.futures_on() {
            return;
        }
        self.futures_step(false);
    }

    /// Walk up to `steps` steps of the night (`night_session_steps`): each
    /// moves the futures' fair values along the night's path to the next
    /// open, steps the basis and fills resting orders the books cross. Only
    /// between a close and the next open, and never past the night's last
    /// step, which lands every future on the fair value the open will give
    /// it. Returns the steps walked: 0 with the switch off, in a session,
    /// or once the night is done. Stocks do not trade.
    pub fn night_tick(&mut self, steps: u32) -> u32 {
        if self.night_steps() == 0 || self.futures.phase != FuturesPhase::Night {
            return 0;
        }
        let mut walked = 0;
        while walked < steps {
            let Some(b) = self.futures.night.as_ref() else {
                break;
            };
            if b.done >= b.steps {
                break;
            }
            self.futures_step(true);
            walked += 1;
        }
        walked
    }

    // ── The day's boundaries ──────────────────────────────────────────────

    /// At an open, after the opening prints: the contract expiring at this
    /// session settles on the index of those prints, the next is listed,
    /// the night ends, the books open whole and resting orders the opening
    /// books cross fill at their prices. Nothing with the switch off.
    pub(super) fn futures_open(&mut self) {
        if !self.futures_on() {
            return;
        }
        self.futures.phase = FuturesPhase::Session;
        self.futures.night = None;
        let t = self.elapsed_days;
        let index = self.futures_live_index();
        // `futures_impact_memory`: each memory row follows its contract
        // through the roll; an expiring contract's row goes with it.
        let memory: Vec<(i64, [f64; crate::agent_book::MEMORY_WIDTH])> =
            if self.futures.book.memory.iter().any(|r| r.iter().any(|x| *x != 0.0)) {
                self.futures.contracts.iter().map(|c| c.expiry).zip(self.futures.book.memory.iter().copied()).collect()
            } else {
                Vec::new()
            };
        let mut kept = Vec::with_capacity(self.futures.contracts.len());
        for c in std::mem::take(&mut self.futures.contracts) {
            if c.expiry <= t {
                self.futures.settlements.push(settlement_of(c.expiry, index));
                let symbol = index_future_symbol(c.expiry);
                self.futures.book.orders.retain(|o| o.ticker != symbol);
            } else {
                kept.push(c);
            }
        }
        for expiry in calendar::index_future_expiries(t, calendar::INDEX_FUTURES_LISTED) {
            if !kept.iter().any(|c| c.expiry == expiry) {
                kept.push(ListedFuture::new(expiry));
            }
        }
        kept.sort_by_key(|c| c.expiry);
        kept.truncate(calendar::INDEX_FUTURES_LISTED);
        for c in kept.iter_mut() {
            c.taken = [0.0; 4];
        }
        if !memory.is_empty() {
            self.futures.book.memory = kept
                .iter()
                .map(|c| {
                    memory.iter().find(|(e, _)| *e == c.expiry).map(|(_, r)| *r)
                        .unwrap_or([0.0; crate::agent_book::MEMORY_WIDTH])
                })
                .collect();
            for (c, r) in kept.iter_mut().zip(self.futures.book.memory.iter()) {
                if r.iter().all(|x| *x == 0.0) {
                    c.impact = 0.0;
                }
            }
        }
        self.futures.contracts = kept;
        self.futures_cross_resting();
    }

    /// At a close, on the session's last prints: each contract's settlement
    /// mark, its price on the close's index. Nothing with the switch off.
    pub(super) fn futures_close_marks(&mut self) {
        if !self.futures_on() {
            return;
        }
        self.futures.night = None;
        self.futures.phase = FuturesPhase::Night;
        let index = if self.index_close > 0.0 { self.index_close } else { self.futures_live_index() };
        let now = self.futures_now();
        for slot in 0..self.futures.contracts.len() {
            let expiry = self.futures.contracts[slot].expiry;
            let (fair, ..) = self.futures_carry(expiry, index, now);
            let c = &mut self.futures.contracts[slot];
            let sessions = crate::mathx::max(0.0, expiry as f64 - now);
            let basis_bp = convergence(sessions) * (self.futures.basis + c.impact);
            c.fair_close = fair;
            c.mark = fair + basis_bp * index / 1e4;
        }
    }

    /// The next open on a copy of the engine: the index and each listed
    /// contract's fair value on its opening prints, in listing order. The
    /// copy takes the draws the open will take; the engine is left as it
    /// was.
    fn futures_preview(&mut self) -> (f64, Vec<f64>) {
        let t = self.elapsed_days;
        // Out of the copy, and back: what grows with the run and an open
        // neither reads nor needs to price.
        let marks = std::mem::take(&mut self.day_marks);
        let distributions = std::mem::take(&mut self.distribution_log);
        let population = self.population.take();
        let fills = std::mem::take(&mut self.futures.book.fills);
        let orders = std::mem::take(&mut self.futures.book.orders);
        let mut copy = self.clone();
        self.day_marks = marks;
        self.distribution_log = distributions;
        self.population = population;
        self.futures.book.fills = fills;
        self.futures.book.orders = orders;
        // The live marks move no price, so the copy need not project them.
        copy.params.vix_intraday_live = 0.0;
        copy.params.rate_intraday_live = 0.0;
        copy.set_day_label(self.current_day + 1);
        copy.set_elapsed_days(t + 1);
        copy.open_market();
        let index = copy.futures_live_index();
        let now = copy.futures_now();
        let fairs = self
            .futures
            .contracts
            .iter()
            .map(|c| if c.expiry <= t + 1 { index } else { copy.futures_carry(c.expiry, index, now).0 })
            .collect();
        (index, fairs)
    }

    /// After the close's macro step (`night_session_steps`): the night's
    /// targets from the next open on a copy, and its path from the close's
    /// marks. Nothing without a night session.
    pub(super) fn futures_night_setup(&mut self) {
        let steps = self.night_steps();
        if steps == 0 || self.futures.phase != FuturesPhase::Night || self.futures.contracts.is_empty() {
            return;
        }
        let (index_open, fairs) = self.futures_preview();
        // On the close's marks until the first step, which takes the close's
        // re-mark (`futures_step`).
        let index_close = if self.index_close > 0.0 { self.index_close } else { self.futures_live_index() };
        let mut now = vec![index_close];
        for c in &self.futures.contracts {
            now.push(c.fair_close);
        }
        let mut target = vec![index_open];
        target.extend(fairs);
        if now.iter().chain(target.iter()).any(|v| !(v.is_finite() && *v > 0.0)) {
            return;
        }
        let p = &self.params;
        let share = if p.overnight_market_share != 0.0 || p.overnight_idio_share != 0.0 {
            p.overnight_market_share
        } else if p.overnight_variance_ratio > 0.0 {
            p.overnight_variance_ratio / (1.0 + p.overnight_variance_ratio)
        } else {
            0.0
        };
        let variance = crate::mathx::max(0.0, share * self.index_conditional_variance_terms_now().total());
        self.futures.night = Some(NightBridge {
            steps,
            done: 0,
            variance: if variance.is_finite() { variance } else { 0.0 },
            expiries: self.futures.contracts.iter().map(|c| c.expiry).collect(),
            now,
            target,
        });
    }

    /// Rerun the next open on a copy and move the night's path by the
    /// change in its targets, at once: a pin or another change between
    /// sessions reaches the futures when it is made. Nothing outside a
    /// night.
    pub(crate) fn futures_retarget(&mut self) {
        if self.night_steps() == 0 || self.futures.phase != FuturesPhase::Night || self.futures.night.is_none() {
            return;
        }
        let (index_open, fairs) = self.futures_preview();
        let mut target = vec![index_open];
        target.extend(fairs);
        let Some(b) = self.futures.night.as_mut() else {
            return;
        };
        if target.len() != b.target.len() || target.iter().any(|v| !(v.is_finite() && *v > 0.0)) {
            return;
        }
        if target.iter().zip(b.target.iter()).all(|(a, b)| a.to_bits() == b.to_bits()) {
            return;
        }
        let done = b.done >= b.steps;
        for ((now, old), new) in b.now.iter_mut().zip(b.target.iter()).zip(target.iter()) {
            *now = if done { *new } else { *now * (new / old) };
        }
        b.target = target;
    }

    // ── Reads ─────────────────────────────────────────────────────────────

    /// The listed contracts: the front two index futures under
    /// `futures_index_listed`, then the six VIX futures under
    /// `futures_vix_listed`, then the thirteen policy-rate and eight
    /// term-rate futures under `futures_rates_listed`, then the twelve oil
    /// futures under `futures_oil_listed`, each family in expiry order, then
    /// the index options under `options_index_listed`, by expiry, strike and
    /// right; none with all off.
    pub fn contracts(&self) -> Vec<ContractSpec> {
        let mut out = self.index_futures_contracts();
        out.extend(self.vix_futures_contracts());
        out.extend(self.rate_futures_contracts());
        out.extend(self.oil_futures_contracts());
        out.extend(self.option_contracts());
        out.extend(self.name_option_contracts());
        out
    }

    fn index_futures_contracts(&self) -> Vec<ContractSpec> {
        if !self.futures_on() {
            return Vec::new();
        }
        let session = match self.futures.phase {
            FuturesPhase::Night => self.elapsed_days + 1,
            _ => self.elapsed_days,
        };
        let front = self.futures.contracts.iter().position(|c| calendar::roll_session(c.expiry) > session);
        self.futures
            .contracts
            .iter()
            .enumerate()
            .map(|(k, c)| {
                let kind = ContractKind::IndexFuture;
                ContractSpec {
                    symbol: index_future_symbol(c.expiry),
                    kind,
                    root: kind.root().to_string(),
                    expiry: c.expiry,
                    roll: calendar::roll_session(c.expiry),
                    multiplier: INDEX_FUTURE.multiplier,
                    tick: INDEX_FUTURE.tick,
                    settlement: kind.settlement(),
                    front: front == Some(k),
                    right: None,
                    strike: None,
                }
            })
            .collect()
    }

    /// A listed contract's quote now: `None` for a symbol not listed, and
    /// for every symbol with its family's switch off. Its `initial_margin` is
    /// the one the last close set (`margin_scan_coverage`).
    pub fn quote(&self, symbol: &str) -> Option<Quote> {
        let mut q = self.quote_unmargined(symbol)?;
        q.initial_margin = self.initial_margin(q.kind, q.expiry, q.multiplier);
        q.maintenance_margin = q.initial_margin.map(|m| m / super::margin::MAINTENANCE_RATIO);
        Some(q)
    }

    fn quote_unmargined(&self, symbol: &str) -> Option<Quote> {
        if self.vix_futures_on() {
            if let Some(slot) = self.vix_futures_slot(symbol) {
                return self.vix_futures_quote(slot);
            }
        }
        if self.rate_futures_on() {
            if let Some(slot) = self.rate_futures_slot(symbol) {
                return self.rate_futures_quote(slot);
            }
        }
        if self.oil_futures_on() {
            if let Some(slot) = self.oil_futures_slot(symbol) {
                return self.oil_futures_quote(slot);
            }
        }
        if !self.futures_on() {
            return None;
        }
        let slot = self.futures_slot(symbol)?;
        let c = &self.futures.contracts[slot];
        let v = self.futures_valuation(slot);
        let book = self.futures_book(slot, None);
        let (bid, ask) = (book.best_bid(), book.best_ask());
        Some(Quote {
            symbol: index_future_symbol(c.expiry),
            kind: ContractKind::IndexFuture,
            expiry: c.expiry,
            bid,
            ask,
            mid: match (bid, ask) {
                (Some(b), Some(a)) => Some(0.5 * (a + b)),
                _ => None,
            },
            price: v.price,
            fair: v.fair,
            mark: if c.mark.is_finite() { Some(c.mark) } else { None },
            basis_bp: v.basis_bp,
            index: v.index,
            rate: v.rate,
            dividends: v.dividends,
            sessions_to_expiry: v.sessions,
            multiplier: INDEX_FUTURE.multiplier,
            tick: INDEX_FUTURE.tick,
            daily_volume: self.futures_daily_volume(v.index),
            initial_margin: None,
            maintenance_margin: None,
            expected: None,
            premium: None,
            loading: None,
        })
    }

    /// The final settlements made at `session`'s open, or every one so far
    /// with `None`, in session order, an index future before a VIX future at
    /// the same open and the index options after the futures. Empty with
    /// every switch off.
    pub fn settlements(&self, session: Option<i64>) -> Vec<Settlement> {
        let mut out: Vec<Settlement> = if self.futures_on() {
            self.futures
                .settlements
                .iter()
                .filter(|s| session.is_none_or(|d| s.session == d))
                .cloned()
                .collect()
        } else {
            Vec::new()
        };
        out.extend(self.vix_futures_settlements(session));
        out.extend(self.rate_futures_settlements(session));
        out.extend(self.oil_futures_settlements(session));
        out.extend(self.option_settlements(session));
        out.extend(self.name_option_settlements(session));
        // Stable, so each family keeps its own order within a session.
        out.sort_by_key(|s| s.session);
        out
    }

    // ── Snapshot ──────────────────────────────────────────────────────────

    /// The futures' numbers for the snapshot and the hash: `None` with the
    /// switch off.
    pub fn futures_words(&self) -> Option<Vec<f64>> {
        if self.futures_on() {
            Some(self.futures.to_words())
        } else {
            None
        }
    }

    /// The futures' generator, `stream::DERIVATIVES`: `None` with the
    /// switch off.
    pub fn derivatives_rng_state(&self) -> Option<RngState> {
        if self.futures_on() {
            Some(self.futures.rng.snapshot())
        } else {
            None
        }
    }

    /// Agents' resting orders on contracts and their undelivered fills:
    /// `None` with the switch off and while no agent has traded one.
    pub fn futures_book_state(&self) -> Option<&BookState> {
        if self.futures_on() && !self.futures.book.is_pristine() {
            Some(&self.futures.book)
        } else {
            None
        }
    }

    /// The night's path, while one is walked: `None` otherwise.
    pub fn night_bridge_words(&self) -> Option<Vec<f64>> {
        if self.night_steps() == 0 {
            return None;
        }
        self.futures.night.as_ref().map(NightBridge::to_words)
    }

    /// Put the futures back (a restore): their numbers, their generator,
    /// their book and the night, each refused where the switch it belongs
    /// to is off.
    pub fn set_futures_state(
        &mut self,
        words: Option<&[f64]>,
        rng: Option<RngState>,
        book: Option<BookState>,
        night: Option<&[f64]>,
    ) -> Result<(), String> {
        let off = |key: &str, dial: &str| {
            format!(
                "this snapshot carries {key}, which only an engine with {dial} on keeps, and \
                 this engine's model has it off"
            )
        };
        if !self.futures_on() {
            if words.is_some() {
                return Err(off("the index futures (futures)", "futures_index_listed"));
            }
            if rng.is_some() {
                return Err(off("the derivatives' generator (derivatives_rng)", "futures_index_listed"));
            }
            if book.is_some() {
                return Err(off("the futures' book (futures_book)", "futures_index_listed"));
            }
            if night.is_some() {
                return Err(off("the night's path (night_bridge)", "night_session_steps"));
            }
            self.futures = FuturesState::new(self.root_seed);
            return Ok(());
        }
        let (Some(words), Some(rng)) = (words, rng) else {
            return Err("an engine with futures_index_listed on carries its futures and their \
                        generator (futures, derivatives_rng); this snapshot lacks one"
                .to_string());
        };
        if night.is_some() && self.night_steps() == 0 {
            return Err(off("the night's path (night_bridge)", "night_session_steps"));
        }
        let mut state = FuturesState::new(self.root_seed);
        state.set_words(words)?;
        state.rng = GameRng::restore(rng);
        state.book = book.unwrap_or_default();
        state.night = match night {
            Some(w) => Some(NightBridge::from_words(w)?),
            None => None,
        };
        self.futures = state;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::economy::{create_initial_central_bank_state, create_initial_economy_state, InitialEconomyOptions};

    fn roster() -> Vec<TickCompany> {
        crate::universe::random_universe(12, 5)
            .iter()
            .enumerate()
            .map(|(i, g)| g.to_init().to_tick_company(i))
            .collect()
    }

    fn with(dials: &[(&str, f64)]) -> ModelParams {
        let mut p = crate::params::PT_V21;
        for (name, value) in dials {
            p = p.with_override(name, *value).unwrap();
        }
        p
    }

    const ON: &[(&str, f64)] = &[
        ("index_level_listed", 1.0),
        ("forecast_horizon_sessions", 252.0),
        ("futures_index_listed", 1.0),
        ("basis_sd", 3.7),
        ("basis_persistence", 0.42),
        ("night_session_steps", 8.0),
    ];

    fn engine(seed: u64, dials: &[(&str, f64)]) -> Engine {
        Engine::with_params(
            seed,
            roster(),
            create_initial_economy_state(&InitialEconomyOptions::default()),
            create_initial_central_bank_state(0),
            crate::sectors::keys().iter().map(|s| s.to_string()).collect(),
            with(dials),
        )
    }

    fn day(e: &mut Engine, d: i64, buffer: &mut SessionBuffer) {
        e.run_numbered_day(d, 390, buffer);
    }

    #[test]
    fn off_nothing_is_listed_and_nothing_is_kept() {
        let mut e = engine(3, &[]);
        let mut buffer = SessionBuffer::new();
        day(&mut e, 0, &mut buffer);
        assert!(e.contracts().is_empty());
        assert!(e.quote("IDX.F0056").is_none());
        assert!(e.settlements(None).is_empty());
        assert_eq!(e.night_tick(5), 0);
        assert!(e.futures_words().is_none());
        assert!(e.derivatives_rng_state().is_none());
        assert!(e.night_bridge_words().is_none());
    }

    #[test]
    fn the_front_two_are_listed_and_roll_at_expiry_onto_the_opening_prints() {
        let mut e = engine(5, ON);
        let mut buffer = SessionBuffer::new();
        let listed: Vec<String> = e.contracts().into_iter().map(|c| c.symbol).collect();
        assert_eq!(listed, vec!["IDX.F0056", "IDX.F0119"]);
        for d in 0..56 {
            day(&mut e, d, &mut buffer);
            e.night_tick(8);
        }
        // The night before expiry lands the expiring contract on the open.
        let last_night = e.quote("IDX.F0056").unwrap();
        e.set_current_day(56);
        e.open_market();
        let opening = e.index_level().unwrap().level;
        let s = e.settlements(Some(56));
        assert_eq!(s.len(), 1);
        assert_eq!(s[0].symbol, "IDX.F0056");
        assert_eq!(s[0].value.to_bits(), opening.to_bits());
        assert_eq!(s[0].reference.to_bits(), opening.to_bits());
        // IF3: the future at expiry less the opening-print index.
        assert!((last_night.price - opening).abs() / opening <= 1e-12, "{} {opening}", last_night.price);
        let listed: Vec<String> = e.contracts().into_iter().map(|c| c.symbol).collect();
        assert_eq!(listed, vec!["IDX.F0119", "IDX.F0182"]);
        assert!(e.quote("IDX.F0056").is_none());
    }

    #[test]
    fn a_whole_night_lands_every_future_on_its_opening_fair_value() {
        let mut e = engine(7, ON);
        let mut buffer = SessionBuffer::new();
        for d in 0..30 {
            day(&mut e, d, &mut buffer);
            if d + 1 < 30 {
                e.night_tick(8);
            }
        }
        let close = e.quote("IDX.F0056").unwrap();
        // NS2 by construction: after the night the quote's fair is the fair
        // value on the opening index, to the bit, and the price is that plus
        // the basis.
        assert_eq!(e.night_tick(3), 3);
        let partway = e.quote("IDX.F0056").unwrap();
        assert_ne!(partway.fair.to_bits(), close.fair.to_bits(), "the night moved nothing");
        assert_eq!(e.night_tick(100), 5, "the night has eight steps");
        assert_eq!(e.night_tick(1), 0);
        let night = e.quote("IDX.F0056").unwrap();
        e.set_current_day(30);
        e.open_market();
        let open = e.quote("IDX.F0056").unwrap();
        assert_eq!(night.fair.to_bits(), open.fair.to_bits());
        assert_eq!(night.index.to_bits(), open.index.to_bits());
        assert!((night.price - open.price).abs() <= 1e-12 * open.price);
        assert_eq!(open.mark.unwrap().to_bits(), close.mark.unwrap().to_bits());
    }

    #[test]
    fn the_night_keeps_the_close_until_its_first_step_takes_the_remark() {
        // No night variance: the path has no noise, so each step is its
        // drift alone.
        let quiet: Vec<(&str, f64)> = ON
            .iter()
            .copied()
            .chain([("overnight_variance_ratio", 0.0), ("overnight_market_share", 0.0), ("overnight_idio_share", 0.0)])
            .collect();
        let mut e = engine(21, &quiet);
        let mut buffer = SessionBuffer::new();
        let mut remarked = 0;
        for d in 0..20 {
            day(&mut e, d, &mut buffer);
            let level = e.index_level().unwrap();
            let front = e.contracts()[0].symbol.clone();
            let q = e.quote(&front).unwrap();
            // Before any step: the quote stands on the close's print, as its
            // mark does.
            let print = level.close.unwrap_or(level.level);
            assert_eq!(q.index.to_bits(), print.to_bits());
            // The first step starts from the published level, re-mark and
            // all, and takes one step of the drift to the open.
            assert_eq!(e.night_tick(1), 1);
            let words = e.night_bridge_words().unwrap();
            assert_eq!(words[2], 0.0);
            let n = words[3] as usize;
            let (now, target) = (words[4 + n], words[4 + n + n + 1]);
            let expect = (level.level.ln() + (target.ln() - level.level.ln()) / 8.0).exp();
            assert!((now - expect).abs() <= 1e-9 * expect, "{now} {expect}");
            if print != level.level {
                remarked += 1;
            }
            e.night_tick(7);
        }
        assert!(remarked > 0, "no close re-marked the index");
    }

    #[test]
    fn an_order_before_the_nights_first_step_meets_the_book_after_it() {
        use crate::order_book::Side;
        let mut e = engine(23, ON);
        let mut buffer = SessionBuffer::new();
        for d in 0..5 {
            day(&mut e, d, &mut buffer);
            if d + 1 < 5 {
                e.night_tick(8);
            }
        }
        let front = e.contracts()[0].symbol.clone();
        let stale = e.quote(&front).unwrap();
        let mut walked = e.clone();
        assert_eq!(walked.night_tick(1), 1);
        let first = walked.quote(&front).unwrap();
        assert_ne!(stale.index.to_bits(), first.index.to_bits(), "the first step moved nothing");
        // The order walks the first step, then fills where an order placed
        // after it would.
        let a = e.submit_order("a", &front, Side::Buy, 1.0, None, None).unwrap();
        let b = walked.submit_order("a", &front, Side::Buy, 1.0, None, None).unwrap();
        assert!(e.night_walked());
        assert_eq!(a.average_price.unwrap().to_bits(), b.average_price.unwrap().to_bits());
        assert_eq!(e.night_tick(100), 7);
    }

    #[test]
    fn a_stock_refuses_orders_once_the_night_has_moved() {
        use crate::order_book::Side;
        let mut e = engine(7, ON);
        let mut buffer = SessionBuffer::new();
        for d in 0..5 {
            day(&mut e, d, &mut buffer);
            if d + 1 < 5 {
                e.night_tick(8);
            }
        }
        let ticker = e.companies[0].ticker.clone();
        // At the close, before the night moves, a stock order is taken.
        assert!(!e.night_walked());
        assert!(e.submit_order("a", &ticker, Side::Buy, 1.0, None, None).is_ok());
        // Once it moves the futures show the next open: stocks refuse, and
        // the front future still trades.
        assert_eq!(e.night_tick(1), 1);
        assert!(e.night_walked());
        let refused = e.submit_order("a", &ticker, Side::Buy, 1.0, None, None);
        assert!(refused.is_err_and(|m| m.contains("does not trade at night")));
        assert!(e.submit_order("a", "IDX.F0056", Side::Buy, 1.0, None, None).is_ok());
        e.night_tick(100);
        e.set_current_day(5);
        e.open_market();
        assert!(!e.night_walked());
        assert!(e.submit_order("a", &ticker, Side::Buy, 1.0, None, None).is_ok());
    }

    #[test]
    fn the_carry_reads_declared_dividends_inside_21_sessions_and_projects_beyond() {
        use crate::market::dividends as dv;
        let mut e = engine(11, ON);
        let mut buffer = SessionBuffer::new();
        for d in 0..3 {
            day(&mut e, d, &mut buffer);
        }
        e.set_current_day(3);
        e.open_market();
        let t = e.elapsed_days();
        let rate = 0.03;
        let now = e.futures_now();
        // By hand: each payer's ex-dates after the open, declared amount
        // inside 21 sessions, the rule's projection beyond.
        let by_hand = |expiry: i64| -> (f64, usize, usize) {
            let mut total = 0.0;
            let (mut declared, mut projected) = (0, 0);
            for c in e.companies() {
                let Some(d) = c.stock.dividend else { continue };
                if !(d.target_yield > 0.0) || !c.is_public || c.is_bankrupt {
                    continue;
                }
                let k = dv::sessions_since_ex(&c.ticker, t);
                let mut ex = t + 63 - k;
                let mut state = d;
                let mut amount = if d.declared {
                    assert!(ex - t <= 21, "declared more than 21 sessions ahead");
                    declared += 1;
                    d.amount
                } else {
                    assert!(ex - t > 21, "undeclared inside 21 sessions");
                    projected += 1;
                    dv::declare(e.params(), &d, c.stock.price)
                };
                while ex <= expiry {
                    total += c.stock.shares_outstanding * amount / e.index_divisor
                        * (-rate * (ex as f64 - now) / 252.0).exp();
                    state.amount = amount;
                    amount = dv::declare(e.params(), &state, c.stock.price);
                    ex += 63;
                }
            }
            (total, declared, projected)
        };
        for expiry in [56, 119, 182] {
            let (want, declared, projected) = by_hand(expiry);
            let got = e.futures_dividends(expiry, rate, now);
            assert!(want > 0.0);
            assert!((got - want).abs() <= 1e-12 * want, "{expiry}: {got} {want}");
            assert!(declared > 0 && projected > 0, "{declared} {projected}");
        }
        // Further out holds more, and an expiry before any ex-date none.
        assert!(e.futures_dividends(182, rate, now) > e.futures_dividends(56, rate, now));
        assert_eq!(e.futures_dividends(t, rate, now), 0.0);
    }

    #[test]
    fn the_words_round_trip_and_are_refused_where_the_switch_is_off() {
        let mut e = engine(13, ON);
        let mut buffer = SessionBuffer::new();
        for d in 0..4 {
            day(&mut e, d, &mut buffer);
        }
        e.night_tick(3);
        e.submit_order("a", "IDX.F0056", Side::Buy, 25.0, None, None).unwrap();
        e.submit_order("a", "IDX.F0119", Side::Sell, 5.0, Some(1e6), None).unwrap();
        let words = e.futures_words().unwrap();
        let rng = e.derivatives_rng_state().unwrap();
        let night = e.night_bridge_words().unwrap();
        let book = e.futures_book_state().cloned();
        assert!(book.is_some());
        let mut other = engine(99, ON);
        other.set_futures_state(Some(&words), Some(rng), book.clone(), Some(&night)).unwrap();
        assert_eq!(other.futures_words().unwrap(), words);
        assert_eq!(other.night_bridge_words().unwrap(), night);
        assert_eq!(other.derivatives_rng_state().unwrap(), rng);
        let mut off = engine(13, &[]);
        assert!(off.set_futures_state(Some(&words), Some(rng), None, None).is_err());
        assert!(NightBridge::from_words(&night[..night.len() - 1]).is_err());
        let mut short = FuturesState::new(1);
        assert!(short.set_words(&words[..words.len() - 1]).is_err());
    }

    #[test]
    fn the_basis_reads_its_persistence_and_sd_at_the_close() {
        // One close-to-close AR(1): a = rho^(1/K) over K steps a day.
        let mut e = engine(17, &[
            ("index_level_listed", 1.0),
            ("futures_index_listed", 1.0),
            ("basis_sd", 4.0),
            ("basis_persistence", 0.5),
        ]);
        e.futures.phase = FuturesPhase::Session;
        let mut closes = Vec::new();
        for _ in 0..6000 {
            for _ in 0..390 {
                e.futures_step(false);
            }
            closes.push(e.futures.basis);
        }
        let n = closes.len() as f64;
        let mean = closes.iter().sum::<f64>() / n;
        let var = closes.iter().map(|x| (x - mean) * (x - mean)).sum::<f64>() / n;
        let lag = closes.windows(2).map(|w| (w[0] - mean) * (w[1] - mean)).sum::<f64>() / (n - 1.0) / var;
        assert!((var.sqrt() - 4.0).abs() < 0.15, "sd {}", var.sqrt());
        assert!((lag - 0.5).abs() < 0.04, "lag-1 {lag}");
    }
}