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
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
//! The single-name options (`options_names_listed`): the engine's half.
//!
//! American calls and puts on every public roster name, 100 shares a
//! contract, physically settled. Each name's surface is built when asked
//! from public state: the forecast's expected variance of the name to each
//! tenor (`forecast_horizon_sessions`), scaled by the single-name premium
//! (`surface_name_premium`), with the index surface's term premia and the
//! variance of the name's own reports before the tenor
//! (`surface_earnings_weight`, the calendar the engine publishes); its
//! smile has the index surface's curvature and a share of its correlation
//! (`surface_name_skew`). It never reads the cycle's true phase, a crisis,
//! a pending surprise or a draw.
//!
//! # The chain
//!
//! From the first close, every public name lists calls and puts at the next
//! three monthly expiries and the three quarterlies after them
//! (`derivatives::calendar::name_option_expiries`), the index options'
//! sessions. A name option settles at its expiry session's close, so it
//! trades through its last session. An expiry's strikes sit on the name's
//! price tier when it lists ([`crate::derivatives::NameOptionSpec`]), ten
//! either side of the price, widened at each open and close, so a symbol
//! listed once stays listed to its expiry. A name whose ticker is a
//! contract family's root (`IDX`, `VIX`, `FF`, `TR3`, `OIL`) lists none.
//!
//! # The price
//!
//! An option expiring at the close of session `E` has `E + 1 - now`
//! sessions left, `now` counting a session that has run `k` of its 390
//! ticks as `k / 390` used and a close as the next session's start. The
//! dealer quotes the American value a Leisen-Reimer tree gives
//! (`derivatives::american`) on the escrowed price: the name's price less
//! the present value of the dividends going ex before expiry, the declared
//! amount inside the declaration lead and the rule's projection beyond
//! (`market::dividends::lookahead`), each at its ex-date's eve. The tree's
//! volatility is the surface's at the strike's log-moneyness on the
//! escrowed forward, moved by the dealer's pressure on the expiry, and its
//! rate the policy rate the forecast expects to expiry. A tree is priced
//! only when a quote is asked for, and kept for the session: a chain is
//! about 250 trees a name, and an untraded run prices none.
//!
//! # Exercise and delivery
//!
//! The dealer is the other side of every trade. At each close:
//!
//! - an agent's instructions ([`Engine::exercise`]) are exercised, up to
//!   the long position it still holds;
//! - the dealer exercises its net long in a contract where its own tree
//!   says exercising beats holding (`american::early_exercise`): a call only
//!   on the eve of an ex-date whose dividend is worth more than the call's
//!   time value, a put when it is deep enough that the interest on the
//!   strike outweighs the insurance left;
//! - at an expiry session's close every option in the money by a cent or
//!   more on the closing print is exercised, the OCC's exercise by
//!   exception;
//! - the contracts exercised are assigned to every short position, agents'
//!   and the dealer's, pro rata: in whole contracts by largest remainder
//!   when every position is whole, in exact shares otherwise.
//!
//! Each exercise and assignment is a row of [`Engine::settlements`]: the
//! party, the contracts, the shares it receives or delivers (100 a
//! contract) and the cash, the strike times the shares. The dealer's rows
//! are among them, so a close's rows sum to no shares and no cash. Nothing
//! trades in the stock's book: a delivery moves no price, sends no flow and
//! takes no draw.

use super::options::{strike_at, Clock, ListedExpiry, OptionsPhase, SURFACE_MIN_KNOTS};
use super::*;
use crate::agent_book::{AgentFill, AgentOrder, BookState, Liquidity, RestMode, AGENT_BOOK_CAP};
use crate::derivatives::american::{self, CashDividend, Lattice, OptionSpec, Style};
use crate::derivatives::calendar;
use crate::derivatives::dealer::{
    american_ladder, dealer_thetas, moneyness_factor, pressure_after, price_ladder, read_smile, walk, ExpirySummary, Ladder,
    OptionQuote,
};
use crate::derivatives::pricing::{black_greeks, intrinsic, Greeks};
use crate::derivatives::surface::{term_shape, Ssvi, Surface, TermInputs, ThetaCurve};
use crate::derivatives::{
    name_option_symbol, ContractKind, ContractSpec, ContractSymbol, Right, Settlement, SettlementEvent, SymbolKind,
    NAME_OPTION, NAME_OPTION_DEALER,
};
use crate::market_maker::MARKET_MAKER_ID;
use crate::order_book::{OrderBook, Side};

/// Open ticks a session (`option_name_inventory_half_life` counts sessions).
const SESSION_TICKS: f64 = 390.0;

/// The contract families' roots: a name with one of these tickers lists no
/// options, so a symbol names one contract.
const RESERVED_ROOTS: [&str; 5] = ["IDX", "VIX", "FF", "TR3", "OIL"];

/// An exercise instruction as the snapshot and the reads carry it:
/// `(agent, symbol, contracts)`.
pub type InstructionRow = (String, String, f64);

/// A held option the margin scans: its expiry's slot in its chain, right,
/// strike and signed contracts.
type HeldOption = (usize, Right, f64, f64);

/// A tree's cache is emptied when it holds this many entries.
const TREE_CACHE_CAP: usize = 50_000;

/// One name's listed expiries.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct NameChain {
    /// The name's roster slot.
    pub(crate) slot: usize,
    /// In expiry order.
    pub(crate) expiries: Vec<ListedExpiry>,
}

/// One name's settled expiry: the closing print it settled on, and its
/// strikes.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct SettledName {
    pub(crate) slot: usize,
    pub(crate) expiry: i64,
    pub(crate) reference: f64,
    pub(crate) step: f64,
    pub(crate) lo: i64,
    pub(crate) hi: i64,
}

/// An agent's instruction to exercise, waiting for the close.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Instruction {
    pub(crate) agent: String,
    pub(crate) symbol: String,
    pub(crate) quantity: f64,
}

/// What a tree said.
#[derive(Debug, Clone, Copy, PartialEq)]
struct TreeRead {
    value: f64,
    delta: f64,
    gamma: f64,
}

/// Trees priced this session, by their inputs' bits: a quote, the order
/// that follows it and the margin on it read one tree. Emptied at each open
/// and close; a copy of the engine starts it empty, since it holds nothing
/// a tree would not give again.
#[derive(Debug, Default)]
pub(crate) struct TreeCache(std::sync::Mutex<std::collections::HashMap<Vec<u64>, TreeRead>>);

impl Clone for TreeCache {
    fn clone(&self) -> Self {
        TreeCache::default()
    }
}

impl TreeCache {
    fn clear(&self) {
        if let Ok(mut guard) = self.0.lock() {
            guard.clear();
        }
    }
}

/// Everything the single-name options carry.
#[derive(Debug, Clone, Default)]
pub(crate) struct NameOptionsState {
    /// The listed names, in roster order.
    pub(crate) chains: Vec<NameChain>,
    /// Every settled expiry, in order.
    pub(crate) settled: Vec<SettledName>,
    /// Agents' waiting orders and undelivered fills; its `flow` holds each
    /// agent's contracts bought and sold of each listed option, an exercise
    /// counting as a sale and an assignment as a purchase, so `bought -
    /// sold` is the position.
    pub(crate) book: BookState,
    /// Exercise instructions waiting for the close, in arrival order.
    pub(crate) instructions: Vec<Instruction>,
    /// Every exercise and assignment so far, and the cash settlement of a
    /// delisted name's options, in order.
    pub(crate) events: Vec<Settlement>,
    pub(crate) cache: TreeCache,
}

const EXPIRY_WIDTH: usize = 5;
const SETTLED_WIDTH: usize = 6;

impl NameOptionsState {
    /// The numbers the snapshot's `name_options` key and the state hash
    /// carry: the chain count, each chain's slot and expiry count and each
    /// expiry's session, strike step, strike range and pressure, then the
    /// settled count and each settled expiry's slot, session, reference,
    /// step and range.
    pub(crate) fn to_words(&self) -> Vec<f64> {
        let mut out = vec![self.chains.len() as f64];
        for c in &self.chains {
            out.push(c.slot as f64);
            out.push(c.expiries.len() as f64);
            for e in &c.expiries {
                out.extend_from_slice(&[e.expiry as f64, e.step, e.lo as f64, e.hi as f64, e.pressure]);
            }
        }
        out.push(self.settled.len() as f64);
        for s in &self.settled {
            out.extend_from_slice(&[s.slot as f64, s.expiry as f64, s.reference, s.step, s.lo as f64, s.hi as f64]);
        }
        out
    }

    /// Put back what [`NameOptionsState::to_words`] wrote, refusing a list
    /// that is not one. The book and the exercises are carried apart.
    pub(crate) fn set_words(&mut self, words: &[f64]) -> Result<(), String> {
        let bad = |why: &str| format!("this snapshot's name_options do not parse: {why}");
        let whole = |v: f64, what: &str| -> Result<i64, String> {
            if v.is_finite() && v.fract() == 0.0 && v.abs() <= 1e15 {
                Ok(v as i64)
            } else {
                Err(bad(&format!("{what} is {v}")))
            }
        };
        let count = |v: f64, what: &str| -> Result<usize, String> {
            let n = whole(v, what)?;
            if n < 0 {
                Err(bad(&format!("{what} is negative")))
            } else {
                Ok(n as usize)
            }
        };
        let mut at = 0;
        let mut take = |n: usize| -> Result<&[f64], String> {
            if at + n > words.len() {
                return Err(bad("it is shorter than its counts say"));
            }
            let s = &words[at..at + n];
            at += n;
            Ok(s)
        };
        let chains_n = count(take(1)?[0], "the chain count")?;
        let mut chains = Vec::with_capacity(chains_n);
        for _ in 0..chains_n {
            let head = take(2)?;
            let slot = count(head[0], "a slot")?;
            let n = count(head[1], "an expiry count")?;
            let mut expiries = Vec::with_capacity(n);
            for _ in 0..n {
                let w = take(EXPIRY_WIDTH)?;
                let (expiry, lo, hi) = (whole(w[0], "an expiry")?, whole(w[2], "a strike index")?, whole(w[3], "a strike index")?);
                if !(w[1].is_finite() && w[1] > 0.0 && w[4].is_finite() && lo >= 1 && hi >= lo) {
                    return Err(bad("an expiry's step, range or pressure is not one"));
                }
                expiries.push(ListedExpiry { expiry, step: w[1], lo, hi, pressure: w[4] });
            }
            chains.push(NameChain { slot, expiries });
        }
        let settled_n = count(take(1)?[0], "the settled count")?;
        let mut settled = Vec::with_capacity(settled_n);
        for _ in 0..settled_n {
            let w = take(SETTLED_WIDTH)?;
            let (slot, expiry) = (count(w[0], "a slot")?, whole(w[1], "an expiry")?);
            let (lo, hi) = (whole(w[4], "a strike index")?, whole(w[5], "a strike index")?);
            if !(w[2].is_finite() && w[2] >= 0.0 && w[3].is_finite() && w[3] > 0.0 && lo >= 1 && hi >= lo) {
                return Err(bad("a settled expiry's reference, step or range is not one"));
            }
            settled.push(SettledName { slot, expiry, reference: w[2], step: w[3], lo, hi });
        }
        if at != words.len() {
            return Err(bad(&format!("{} numbers, more than its counts say", words.len())));
        }
        self.chains = chains;
        self.settled = settled;
        Ok(())
    }
}

/// What one name's options read now.
struct NameContext {
    chain: usize,
    slot: usize,
    ticker: String,
    spot: f64,
    surface: Surface,
    /// Each listed expiry's surface theta and the dealer's, in listing order.
    thetas: Vec<(f64, f64)>,
}

/// What one expiry of a name's options reads.
struct NameExpiry {
    expiry: i64,
    /// Sessions to the expiry session's close.
    sessions: f64,
    years: f64,
    rate: f64,
    dividends: Vec<CashDividend>,
    /// The dividends' present value, per share.
    pv: f64,
    carry_spot: f64,
    forward: f64,
    discount: f64,
    theta: f64,
    dealer_theta: f64,
}

/// The whole contracts each short position is assigned of `exercised`
/// contracts, pro rata to `shorts`: whole contracts by largest remainder
/// when `exercised` and every short are whole numbers (ties to the larger
/// short, then the earlier), exact shares otherwise. Sums to `exercised`,
/// and no short is assigned more than it holds.
fn allocate(exercised: f64, shorts: &[f64]) -> Vec<f64> {
    let total: f64 = shorts.iter().sum();
    if !(total > 0.0) || !(exercised > 0.0) {
        return vec![0.0; shorts.len()];
    }
    let exercised = crate::mathx::min(exercised, total);
    let integral = exercised.fract() == 0.0 && shorts.iter().all(|s| s.fract() == 0.0);
    if !integral {
        let mut out: Vec<f64> = shorts.iter().map(|s| exercised * s / total).collect();
        // The last takes the rounding, so the sum is exact.
        let n = out.len();
        let rest: f64 = out[..n - 1].iter().sum();
        out[n - 1] = crate::mathx::max(0.0, crate::mathx::min(shorts[n - 1], exercised - rest));
        return out;
    }
    let mut out = Vec::with_capacity(shorts.len());
    let mut fractions = Vec::with_capacity(shorts.len());
    let mut given = 0.0;
    for (i, &s) in shorts.iter().enumerate() {
        let share = exercised * s / total;
        let whole = share.floor();
        out.push(whole);
        given += whole;
        fractions.push((share - whole, s, i));
    }
    // Largest remainder first; ties to the larger short, then the earlier.
    fractions.sort_by(|a, b| b.0.total_cmp(&a.0).then(b.1.total_cmp(&a.1)).then(a.2.cmp(&b.2)));
    let mut left = (exercised - given).round() as i64;
    for &(_, s, i) in &fractions {
        if left <= 0 {
            break;
        }
        if out[i] + 1.0 <= s {
            out[i] += 1.0;
            left -= 1;
        }
    }
    out
}

impl Engine {
    // ── Switch ────────────────────────────────────────────────────────────

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

    /// Whether this model lists single-name options (`options_names_listed`):
    /// a property of the model, as [`Engine::lists_options`] is. False on
    /// every shipped preset.
    pub fn lists_name_options(&self) -> bool {
        self.names_on()
    }

    /// The tickers whose options are listed now, in roster order: empty with
    /// the switch off and before the first close.
    pub fn name_option_roots(&self) -> Vec<String> {
        if !self.names_on() {
            return Vec::new();
        }
        self.name_options.chains.iter().map(|c| self.companies[c.slot].ticker.clone()).collect()
    }

    fn name_chain_of(&self, ticker: &str) -> Option<usize> {
        self.name_options.chains.iter().position(|c| self.companies.get(c.slot).is_some_and(|x| x.ticker == ticker))
    }

    /// The listed name option `symbol` names: its chain, its expiry's slot
    /// in the chain, its right and its strike.
    fn name_option_slot(&self, symbol: &str) -> Option<(usize, usize, Right, f64)> {
        if !self.names_on() {
            return None;
        }
        let parsed = ContractSymbol::parse(symbol).ok()?;
        let SymbolKind::Option { right, .. } = parsed.kind else {
            return None;
        };
        let strike = parsed.strike()?;
        let chain = self.name_chain_of(&parsed.root)?;
        let e = self.name_options.chains[chain].expiries.iter().position(|e| e.expiry == parsed.expiry)?;
        if !self.name_options.chains[chain].expiries[e].lists(strike) {
            return None;
        }
        Some((chain, e, right, strike))
    }

    /// Whether `symbol` names a listed single-name option.
    pub(super) fn is_listed_name_option(&self, symbol: &str) -> bool {
        self.name_option_slot(symbol).is_some()
    }

    // ── The surface ───────────────────────────────────────────────────────

    /// The name in `slot`'s reports the surface reads: `(knot, variance)` for
    /// each reaction session `d` from the next unprinted one on, at knot `d -
    /// base + 1`, so a report at an open counts for an option settling at
    /// that session's close and not the one before, its variance
    /// `earnings_surprise_sigma` squared times the forecast's expected
    /// one-session variance of the name at the report.
    fn name_events(&self, clock: &Clock, slot: usize, series: &[f64], day: i64, n_max: usize) -> Vec<(usize, f64)> {
        let p = &self.params;
        if p.surface_name_earnings_premium != 0.0 {
            return self.name_events_drawn(clock, slot, series, day, n_max);
        }
        if p.surface_earnings_weight == 0.0 || !self.carries_earnings() || series.is_empty() {
            return Vec::new();
        }
        let c = &self.companies[slot];
        if !c.is_public || c.is_bankrupt {
            return Vec::new();
        }
        let h = crate::earnings::id_hash(&c.id);
        let q = crate::earnings::QUARTER_SESSIONS;
        let from = clock.unprinted;
        let last = clock.base + n_max as i64 - 1;
        let sigma = p.earnings_surprise_sigma;
        let mut out = Vec::new();
        if last < from {
            return out;
        }
        for quarter in std::cmp::max(from, 0).div_euclid(q)..=std::cmp::max(last, 0).div_euclid(q) {
            let d = crate::earnings::reaction_session(self.earnings_key, h, quarter);
            if d < from || d > last {
                continue;
            }
            let n = d - clock.base + 1;
            if n < 1 || n as usize > n_max {
                continue;
            }
            let at = std::cmp::max(1, d - day + 1) as usize;
            let v = series[std::cmp::min(at, series.len()) - 1];
            out.push((n as usize, sigma * sigma * v));
        }
        out
    }

    /// The name in `slot`'s reports as the engine draws them
    /// (`surface_name_earnings_premium`): `(knot, variance)` at each reaction
    /// session `d` from the next unprinted one on, `earnings_surprise_sigma`
    /// squared plus `earnings_session_sigma` squared, and at `d + 1`,
    /// `earnings_followthrough_sigma` squared, each times the name's
    /// non-market daily variance now ([`Engine::name_drawn_variance`])
    /// carried to the session on the forecast's path of the name's variance,
    /// `series` at the session over `series` at the next. A report printed
    /// at today's open still has its session after ahead.
    fn name_events_drawn(&self, clock: &Clock, slot: usize, series: &[f64], day: i64, n_max: usize) -> Vec<(usize, f64)> {
        let p = &self.params;
        if !self.carries_earnings() || series.is_empty() || series[0] <= 0.0 {
            return Vec::new();
        }
        let c = &self.companies[slot];
        if !c.is_public || c.is_bankrupt {
            return Vec::new();
        }
        let unit = self.name_drawn_variance(slot);
        let h = crate::earnings::id_hash(&c.id);
        let q = crate::earnings::QUARTER_SESSIONS;
        let from = clock.unprinted;
        let last = clock.base + n_max as i64 - 1;
        let report = p.earnings_surprise_sigma * p.earnings_surprise_sigma
            + p.earnings_session_sigma * p.earnings_session_sigma;
        let after = p.earnings_followthrough_sigma * p.earnings_followthrough_sigma;
        let mut out = Vec::new();
        if last < from {
            return out;
        }
        let mut push = |d: i64, sigma2: f64| {
            if sigma2 == 0.0 || d < from || d > last {
                return;
            }
            let n = d - clock.base + 1;
            if n < 1 || n as usize > n_max {
                return;
            }
            let at = std::cmp::max(1, d - day + 1) as usize;
            let v = series[std::cmp::min(at, series.len()) - 1];
            out.push((n as usize, sigma2 * unit * (v / series[0])));
        };
        for quarter in std::cmp::max(from - 1, 0).div_euclid(q)..=std::cmp::max(last, 0).div_euclid(q) {
            let d = crate::earnings::reaction_session(self.earnings_key, h, quarter);
            push(d, report);
            push(d + 1, after);
        }
        out
    }

    /// The name in `slot`'s non-market daily variance now, in the unit its
    /// opening print draws the earnings surprise in
    /// (`Engine::apply_night_split`): its GARCH variance, floored, at its
    /// idiosyncratic scale, size and idiosyncratic state, and its sector
    /// loading on the sector's sigma, in quadrature.
    pub(crate) fn name_drawn_variance(&self, slot: usize) -> f64 {
        let p = &self.params;
        let c = &self.companies[slot];
        let beta = c.stock.beta.unwrap_or(1.0);
        let sector_sigma = match self.sector_keys.iter().position(|k| *k == c.sector) {
            Some(k) => {
                let shared = crate::market::tick::sector_sigma_at(p, &self.economy, self.vix_anchor);
                let sigma = if p.sector_vol_alpha != 0.0 || p.sector_vol_beta != 0.0 {
                    match self.sector_variance.get(k) {
                        Some(v) if *v > 0.0 => shared * crate::mathx::sqrt(*v),
                        _ => shared,
                    }
                } else {
                    shared
                };
                crate::market::factors::sector_loading_for(p, beta) * sigma
            }
            None => 0.0,
        };
        let daily_sigma = crate::mathx::sqrt(crate::mathx::max(c.stock.garch_variance, p.idio_sigma_floor));
        let own_unit = crate::market::factors::idio_scale_for(p, beta)
            * crate::market::factors::cap_size_multiplier_with(p, c.stock.market_cap);
        let own_unit = if self.idio_state_on() { own_unit * crate::mathx::sqrt(self.idio_ratio_at(slot)) } else { own_unit };
        (daily_sigma * own_unit) * (daily_sigma * own_unit) + sector_sigma * sector_sigma
    }

    /// The surface of the name in `slot` now, on the index surface `index`:
    /// `None` without a forecast of the name.
    fn name_surface_at(&self, clock: &Clock, slot: usize, index: &Surface) -> Option<Surface> {
        let p = &self.params;
        let f = self.forecast()?;
        let series = f.name_variance.get(slot).filter(|s| !s.is_empty())?;
        let scaled: Vec<f64>;
        let variance: &[f64] = if p.surface_name_premium == 0.0 {
            series
        } else {
            let k = (1.0 + p.surface_name_premium) * (1.0 + p.surface_name_premium);
            scaled = series.iter().map(|v| v * k).collect();
            &scaled
        };
        let last = self
            .name_chain_of(&self.companies[slot].ticker)
            .and_then(|c| self.name_options.chains[c].expiries.last())
            .map_or(0, |e| e.expiry + 1 - clock.base);
        let n_max = std::cmp::max(SURFACE_MIN_KNOTS, std::cmp::max(last, 0) as usize + 2);
        let events = self.name_events(clock, slot, series, f.day, n_max);
        let inputs = TermInputs::new(
            variance,
            clock.elapsed,
            p.surface_term_premium_short,
            p.surface_term_premium_long,
            &events,
            if p.surface_name_earnings_premium != 0.0 { p.surface_name_earnings_premium } else { p.surface_earnings_weight },
        );
        let inputs = if p.surface_name_term_slope == 0.0 { inputs } else { inputs.with_name_slope(p.surface_name_term_slope) };
        let shape = term_shape(&inputs, n_max);
        let curve = ThetaCurve::from_shape(&shape, clock.elapsed, 1.0)?;
        // The names' curvature and the correlation theirs is a share of: the
        // index's own, or under `surface_index_curvature` the index's VIX and
        // skewness fitted at the shared curvature.
        let (eta, reference) = self.name_reference_fit(index)?;
        let rho = if p.surface_name_skew == 0.0 { 0.0 } else { p.surface_name_skew * reference.rho };
        Some(Surface {
            root: self.companies[slot].ticker.clone(),
            ssvi: Ssvi::new(rho, eta, index.ssvi.gamma),
            curve,
            vix: index.vix,
            skewness_target: None,
            fit: reference,
            hold: None,
        })
    }

    /// The surface of `ticker`'s options now (`options_names_listed`): its
    /// smile's correlation, the index's curvature and its term structure
    /// (`curve`). `vix` and `fit` are the index surface's, which its
    /// correlation is a share of. `None` with the switch off, for a name
    /// without listed options and before the first close's forecast.
    pub fn name_surface(&self, ticker: &str) -> Option<Surface> {
        let chain = self.name_chain_of(ticker)?;
        let clock = self.options_clock();
        let index = self.surface_at(&clock)?;
        self.name_surface_at(&clock, self.name_options.chains[chain].slot, &index)
    }

    /// `ticker`'s carry `sessions` from now: `(forward, rate, dividends)`, the
    /// forward of its escrowed price, the policy rate the forecast expects to
    /// the session nearest `now + sessions` (a fraction a year) and the
    /// present value of the dividends going ex before it, per share. `None`
    /// where [`Engine::name_surface`] is.
    pub fn name_carry(&self, ticker: &str, sessions: f64) -> Option<(f64, f64, f64)> {
        let chain = self.name_chain_of(ticker)?;
        if !sessions.is_finite() {
            return None;
        }
        let clock = self.options_clock();
        let slot = self.name_options.chains[chain].slot;
        let s = crate::mathx::max(0.0, sessions);
        let end = (clock.now + s).round() as i64;
        let x = self.name_carry_to(&clock, slot, end, s);
        Some((x.forward, x.rate, x.pv))
    }

    /// The carry of the name in `slot` to `end`, the session-start the
    /// option's life ends at, `sessions` away: the dividends going ex at or
    /// before `end - 1`'s open.
    fn name_carry_to(&self, clock: &Clock, slot: usize, end: i64, sessions: f64) -> NameExpiry {
        let c = &self.companies[slot];
        let spot = c.stock.price;
        let years = sessions / 252.0;
        let rate = self.futures_rate(end, clock.now);
        let mut dividends = Vec::new();
        let mut pv = 0.0;
        for u in crate::market::dividends::lookahead(&self.params, &c.ticker, c.stock.dividend.as_ref(), clock.after, spot, end - 1) {
            let t = crate::mathx::max(0.0, u.ex_day as f64 - clock.now) / 252.0;
            if t < years {
                dividends.push(CashDividend::new(t, u.amount));
                pv += u.amount * crate::mathx::exp(-rate * t);
            }
        }
        let carry_spot = spot - pv;
        NameExpiry {
            expiry: end - 1,
            sessions,
            years,
            rate,
            dividends,
            pv,
            carry_spot,
            forward: carry_spot * crate::mathx::exp(rate * years),
            discount: crate::mathx::exp(-rate * years),
            theta: 0.0,
            dealer_theta: 0.0,
        }
    }

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

    /// What the name in chain `chain` reads now, on the index surface `index`.
    fn name_context(&self, clock: &Clock, chain: usize, index: &Surface) -> Option<NameContext> {
        let c = &self.name_options.chains[chain];
        let company = &self.companies[c.slot];
        let spot = company.stock.price;
        if !(spot > 0.0 && spot.is_finite()) {
            return None;
        }
        let surface = self.name_surface_at(clock, c.slot, index)?;
        let spec = &NAME_OPTION_DEALER;
        let base = self.params.option_dealer_spread;
        let mut plain = Vec::with_capacity(c.expiries.len());
        let mut shifts = Vec::with_capacity(c.expiries.len());
        for e in &c.expiries {
            let sessions = crate::mathx::max(0.0, (e.expiry + 1) as f64 - clock.now);
            let theta = surface.theta(sessions);
            plain.push((sessions, theta));
            let half_atm = spec.half_spread(base, surface.vix, 0.0);
            let slope = self.params.option_name_inventory_slope;
            shifts.push(if slope != 0.0 {
                // `option_name_inventory_slope`: a share of the expiry's
                // at-the-money volatility per contract, under the same cap
                let vol = if sessions > 0.0 && theta > 0.0 { crate::mathx::sqrt(theta * 252.0 / sessions) } else { 0.0 };
                if e.pressure == 0.0 || !(half_atm > 0.0) {
                    0.0
                } else {
                    let cap = spec.inventory_cap * half_atm;
                    crate::mathx::clamp(slope * vol * e.pressure, -cap, cap)
                }
            } else {
                spec.inventory_shift(e.pressure, half_atm)
            });
        }
        let dealer = dealer_thetas(&plain, &shifts);
        let thetas = plain.iter().zip(dealer).map(|(&(_, theta), d)| (theta, d)).collect();
        Some(NameContext { chain, slot: c.slot, ticker: company.ticker.clone(), spot, surface, thetas })
    }

    fn name_expiry(&self, clock: &Clock, ctx: &NameContext, slot: usize) -> NameExpiry {
        let e = &self.name_options.chains[ctx.chain].expiries[slot];
        let sessions = crate::mathx::max(0.0, (e.expiry + 1) as f64 - clock.now);
        let mut x = self.name_carry_to(clock, ctx.slot, e.expiry + 1, sessions);
        let (theta, dealer_theta) = ctx.thetas[slot];
        x.theta = theta;
        x.dealer_theta = dealer_theta;
        x
    }

    /// The American value, delta and gamma of an option on the name at
    /// `spot`, at the volatility `vol`, from the session's cache.
    #[allow(clippy::too_many_arguments)]
    fn name_tree(&self, right: Right, spot: f64, strike: f64, x: &NameExpiry, vol: f64, style: Style) -> Option<TreeRead> {
        if !(x.years > 0.0 && vol > 0.0) {
            let value = match right {
                Right::Call => crate::mathx::max(0.0, spot - strike),
                Right::Put => crate::mathx::max(0.0, strike - spot),
            };
            let delta = match (right, value > 0.0) {
                (Right::Call, true) => 1.0,
                (Right::Put, true) => -1.0,
                _ => 0.0,
            };
            return Some(TreeRead { value, delta, gamma: 0.0 });
        }
        let steps = american::steps_for(x.sessions.ceil() as i64);
        let mut key = Vec::with_capacity(8 + 2 * x.dividends.len());
        key.push(match (right, style) {
            (Right::Call, Style::American) => 0,
            (Right::Put, Style::American) => 1,
            (Right::Call, _) => 2,
            (Right::Put, _) => 3,
        });
        for v in [spot, strike, x.rate, vol, x.years] {
            key.push(v.to_bits());
        }
        key.push(steps as u64);
        for d in &x.dividends {
            key.push(d.time.to_bits());
            key.push(d.amount.to_bits());
        }
        if let Ok(guard) = self.name_options.cache.0.lock() {
            if let Some(read) = guard.get(&key) {
                return Some(*read);
            }
        }
        let spec = OptionSpec::new(right, spot, strike, x.rate, vol, x.years);
        let tree = american::price(&spec, &x.dividends, steps, style, Lattice::LeisenReimer).ok()?;
        let read = TreeRead { value: tree.value, delta: tree.delta, gamma: tree.gamma };
        if let Ok(mut guard) = self.name_options.cache.0.lock() {
            if guard.len() >= TREE_CACHE_CAP {
                guard.clear();
            }
            guard.insert(key, read);
        }
        Some(read)
    }

    /// The volatility `theta` gives a strike at log-moneyness `k`.
    fn name_vol(surface: &Surface, x: &NameExpiry, k: f64, theta: f64) -> f64 {
        if x.years > 0.0 && theta > 0.0 {
            crate::mathx::sqrt(surface.ssvi.total_variance(k, theta) / x.years)
        } else {
            0.0
        }
    }

    /// The dealer's ladder and quote for one name option.
    fn name_priced(&self, ctx: &NameContext, x: &NameExpiry, right: Right, strike: f64) -> Option<(Ladder, OptionQuote)> {
        let spec = &NAME_OPTION_DEALER;
        let k = crate::mathx::log(strike / x.forward);
        let iv = Self::name_vol(&ctx.surface, x, k, x.dealer_theta);
        let surface_iv = Self::name_vol(&ctx.surface, x, k, x.theta);
        let tree = self.name_tree(right, ctx.spot, strike, x, iv, Style::American)?;
        let fair = if surface_iv.to_bits() == iv.to_bits() {
            tree.value
        } else {
            self.name_tree(right, ctx.spot, strike, x, surface_iv, Style::American)?.value
        };
        let z = if x.theta > 0.0 { k / crate::mathx::sqrt(x.theta) } else { 0.0 };
        let mut half = spec.half_spread(self.params.option_dealer_spread, ctx.surface.vix, z);
        let exponent = self.params.option_name_spread_tenor_exponent;
        let tenor = if exponent != 0.0 {
            crate::mathx::pow(crate::mathx::max(x.sessions, 1.0) / 21.0, exponent)
        } else {
            1.0
        };
        let (otm, itm) = (self.params.option_name_spread_otm, self.params.option_name_spread_itm);
        let atm_vol = if x.years > 0.0 && x.dealer_theta > 0.0 {
            crate::mathx::sqrt(x.dealer_theta / x.years)
        } else {
            0.0
        };
        let l = if (otm != 0.0 || itm != 0.0) && atm_vol > 0.0 {
            // `option_name_spread_otm` and `_itm`: the half-spread a share of
            // the price, the at-the-money share times the moneyness factor
            let side = |v: f64| if v != 0.0 { v } else { 1.0 };
            let share = spec.half_spread(self.params.option_dealer_spread, ctx.surface.vix, 0.0) / atm_vol
                * moneyness_factor(tree.delta.abs(), side(otm), side(itm))
                * tenor;
            price_ladder(spec, tree.value, share * tree.value, iv)
        } else {
            if exponent != 0.0 {
                half *= tenor;
            }
            american_ladder(spec, right, x.forward, strike, x.years, x.discount, iv, half, tree.value)
        };
        let (_, euro) = black_greeks(right, x.carry_spot, strike, x.years, iv, x.rate);
        let greeks = Greeks { delta: tree.delta, gamma: tree.gamma, vega: euro.vega, theta: euro.theta };
        let symbol = name_option_symbol(&ctx.ticker, x.expiry, right, strike);
        let open_interest = self.name_open_interest(&symbol);
        let q = OptionQuote {
            root: ctx.ticker.clone(),
            expiry: x.expiry,
            right,
            strike,
            bid: l.bids.first().map(|b| b.price),
            ask: l.asks.first().map(|a| a.price),
            bid_size: l.bids.first().map_or(0.0, |b| b.size),
            ask_size: l.asks.first().map_or(0.0, |a| a.size),
            mid: tree.value,
            fair,
            iv,
            surface_iv,
            greeks,
            forward: x.forward,
            index: ctx.spot,
            rate: x.rate,
            dividends: x.pv,
            sessions_to_expiry: x.sessions,
            multiplier: NAME_OPTION.multiplier,
            tick: spec.tick_at(tree.value),
            open_interest,
            initial_margin: None,
            maintenance_margin: None,
            symbol,
        };
        Some((l, q))
    }

    /// A row of an expiry's summary: the dealer's volatility and the
    /// European delta on the escrowed price, which need no tree.
    fn name_light(&self, ctx: &NameContext, x: &NameExpiry, right: Right, strike: f64) -> OptionQuote {
        let k = crate::mathx::log(strike / x.forward);
        let iv = Self::name_vol(&ctx.surface, x, k, x.dealer_theta);
        let (mid, greeks) = black_greeks(right, x.carry_spot, strike, x.years, iv, x.rate);
        OptionQuote {
            symbol: String::new(),
            root: ctx.ticker.clone(),
            expiry: x.expiry,
            right,
            strike,
            bid: None,
            ask: None,
            bid_size: 0.0,
            ask_size: 0.0,
            mid,
            fair: mid,
            iv,
            surface_iv: iv,
            greeks,
            forward: x.forward,
            index: ctx.spot,
            rate: x.rate,
            dividends: x.pv,
            sessions_to_expiry: x.sessions,
            multiplier: NAME_OPTION.multiplier,
            tick: NAME_OPTION_DEALER.tick,
            open_interest: 0.0,
            initial_margin: None,
            maintenance_margin: None,
        }
    }

    /// Contracts open in `symbol`: every agent's long position plus the
    /// dealer's, which is the other side of all of theirs.
    fn name_open_interest(&self, symbol: &str) -> f64 {
        let mut longs = 0.0;
        let mut net = 0.0;
        for (_, ticker, bought, sold) in &self.name_options.book.flow {
            if ticker == symbol {
                let pos = bought - sold;
                longs += crate::mathx::max(0.0, pos);
                net += pos;
            }
        }
        longs + crate::mathx::max(0.0, -net)
    }

    /// A listed name option's quote now: `None` for a symbol not listed,
    /// with the switch off and where the surface does not price.
    pub(super) fn name_option_quote(&self, symbol: &str) -> Option<OptionQuote> {
        let (chain, slot, right, strike) = self.name_option_slot(symbol)?;
        let clock = self.options_clock();
        let index = self.surface_at(&clock)?;
        let ctx = self.name_context(&clock, chain, &index)?;
        let x = self.name_expiry(&clock, &ctx, slot);
        let (_, mut q) = self.name_priced(&ctx, &x, right, strike)?;
        q.initial_margin = self.option_margin(&[(symbol.to_string(), -1.0)]);
        q.maintenance_margin = q.initial_margin.map(|m| m / super::margin::MAINTENANCE_RATIO);
        Some(q)
    }

    /// `ticker`'s options at `expiry`, by strike, the call before the put.
    pub(super) fn name_chain(&self, ticker: &str, expiry: i64) -> Option<Vec<OptionQuote>> {
        let chain = self.name_chain_of(ticker)?;
        let slot = self.name_options.chains[chain].expiries.iter().position(|e| e.expiry == expiry)?;
        let clock = self.options_clock();
        let index = self.surface_at(&clock)?;
        let ctx = self.name_context(&clock, chain, &index)?;
        let x = self.name_expiry(&clock, &ctx, slot);
        let e = &self.name_options.chains[chain].expiries[slot];
        let mut out = Vec::with_capacity(2 * (e.hi - e.lo + 1) as usize);
        for strike in e.strikes() {
            for right in [Right::Call, Right::Put] {
                out.push(self.name_priced(&ctx, &x, right, strike)?.1);
            }
        }
        Some(out)
    }

    /// `ticker`'s listed expiries as a trader reads them now (see
    /// [`Engine::option_expiries`]): each read on the dealer's volatilities
    /// and the European deltas on the escrowed price, so it prices no tree.
    pub(super) fn name_option_expiries(&self, ticker: &str) -> Vec<ExpirySummary> {
        let Some(chain) = self.name_chain_of(ticker) else {
            return Vec::new();
        };
        let clock = self.options_clock();
        let Some(index) = self.surface_at(&clock) else {
            return Vec::new();
        };
        let Some(ctx) = self.name_context(&clock, chain, &index) else {
            return Vec::new();
        };
        let expiries = &self.name_options.chains[chain].expiries;
        (0..expiries.len())
            .map(|slot| {
                let e = &expiries[slot];
                let x = self.name_expiry(&clock, &ctx, slot);
                let mut rows = Vec::new();
                for strike in e.strikes() {
                    for right in [Right::Call, Right::Put] {
                        rows.push(self.name_light(&ctx, &x, right, strike));
                    }
                }
                let (atm_iv, put_25d_iv, call_25d_iv) = read_smile(&rows);
                ExpirySummary {
                    root: ticker.to_string(),
                    expiry: e.expiry,
                    sessions_to_expiry: x.sessions,
                    forward: x.forward,
                    strike_low: strike_at(e.lo, e.step),
                    strike_high: strike_at(e.hi, e.step),
                    strike_step: e.step,
                    atm_iv,
                    put_25d_iv,
                    call_25d_iv,
                    skew_25d: match (put_25d_iv, call_25d_iv) {
                        (Some(p), Some(c)) => Some(p - c),
                        _ => None,
                    },
                }
            })
            .collect()
    }

    /// The scan's initial margin on the name options and stock in
    /// `positions` (`(symbol or ticker, signed contracts or shares)`), name
    /// by name: the worst loss over the scan's moves of the name, sized to
    /// the margin's quantile of its one-session sd, at each volatility
    /// shift, the stock's gain in each move netted against the options' loss
    /// there (`option_scan_loss_with`), so stock that covers an option's risk
    /// nets it and stock alone asks nothing. With `margin_option_scan_range`
    /// set, the moves are instead the portfolio-margin rule's ten points over
    /// plus and minus that share of the name's price. Each
    /// option is revalued as a European on its escrowed price. 0 where no
    /// name option is held; `None` where a held name does not price.
    pub(super) fn name_option_margin(&self, positions: &[(String, f64)]) -> Option<f64> {
        let mut by_chain: Vec<(usize, Vec<HeldOption>)> = Vec::new();
        for (symbol, quantity) in positions {
            if *quantity == 0.0 {
                continue;
            }
            let Some((chain, slot, right, strike)) = self.name_option_slot(symbol) else {
                continue;
            };
            match by_chain.iter_mut().find(|(c, _)| *c == chain) {
                Some((_, v)) => v.push((slot, right, strike, *quantity)),
                None => by_chain.push((chain, vec![(slot, right, strike, *quantity)])),
            }
        }
        if by_chain.is_empty() {
            return Some(0.0);
        }
        let clock = self.options_clock();
        let index = self.surface_at(&clock)?;
        let p = &self.params;
        let z = super::margin::normal_quantile(0.5 + 0.5 * p.margin_scan_coverage)
            * super::margin::option_scan_tail(p);
        let mut total = 0.0;
        for (chain, held) in by_chain {
            let ctx = self.name_context(&clock, chain, &index)?;
            let mut scan = Vec::with_capacity(held.len());
            for (slot, right, strike, quantity) in held {
                let x = self.name_expiry(&clock, &ctx, slot);
                let k = crate::mathx::log(strike / x.forward);
                scan.push(super::margin::ScanOption {
                    right,
                    carry_spot: x.carry_spot,
                    strike,
                    years: x.years,
                    vol: Self::name_vol(&ctx.surface, &x, k, x.theta),
                    rate: x.rate,
                    units: quantity * NAME_OPTION.multiplier,
                });
            }
            let shares: f64 = positions.iter().filter(|(s, _)| *s == ctx.ticker).map(|(_, q)| *q).sum();
            let loss = if p.margin_option_scan_range != 0.0 {
                // The portfolio-margin rule's range (`margin_option_scan_range`):
                // ten equidistant moves of the name over plus and minus that
                // share of its price.
                let size = p.margin_option_scan_range * ctx.spot;
                let moves = super::margin::RANGE_POINTS.map(|x| x * size);
                super::margin::option_scan_loss_at(&scan, shares, &moves, z * super::margin::SCAN_VOL_SD)
            } else {
                let spot_move = self.name_spot_move(ctx.slot, ctx.spot, z);
                super::margin::option_scan_loss_with(&scan, shares, spot_move, z * super::margin::SCAN_VOL_SD)
            };
            // The portfolio-margin rule's per-contract minimum
            // (`margin_option_minimum`).
            total += if p.margin_option_minimum != 0.0 {
                crate::mathx::max(loss, super::margin::option_contract_minimum(&scan, p.margin_option_minimum))
            } else {
                loss
            };
        }
        Some(total)
    }

    /// The scan's move of the name in company slot `slot` at price `spot`,
    /// dollars a share: `z` times the name's one-session sd, the forecast's
    /// for the next session.
    fn name_spot_move(&self, slot: usize, spot: f64, z: f64) -> f64 {
        let variance = self
            .forecast()
            .and_then(|f| f.name_variance.get(slot))
            .and_then(|s| s.first().copied())
            .unwrap_or(0.0);
        z * crate::mathx::sqrt(crate::mathx::max(0.0, variance)) * spot
    }

    /// The scan's margin on stock held alone in listed names, dollars:
    /// `(ticker, signed shares)` pairs, each charged its shares times the
    /// scan's move of the name (`Engine::name_option_margin`'s), the stock's
    /// worst loss over the scan's moves. The option scan charges stock only
    /// as cover for an option's risk; this is the stock's own scan, for the
    /// shares an exercise or an assignment delivered into an account that no
    /// longer holds the name's options. With `margin_option_scan_range` set,
    /// each is charged that share of its value instead. Tickers without a
    /// listed chain, or without a price, are left out. `None` with margin or
    /// the name options off. A read.
    pub fn stock_scan_margin(&self, positions: &[(String, f64)]) -> Option<f64> {
        if !self.margin_on() || !self.names_on() {
            return None;
        }
        let p = &self.params;
        let z = super::margin::normal_quantile(0.5 + 0.5 * p.margin_scan_coverage)
            * super::margin::option_scan_tail(p);
        let mut total = 0.0;
        for (ticker, shares) in positions {
            if *shares == 0.0 {
                continue;
            }
            let Some(chain) = self.name_chain_of(ticker) else {
                continue;
            };
            let slot = self.name_options.chains[chain].slot;
            let spot = self.companies[slot].stock.price;
            if !(spot > 0.0 && spot.is_finite()) {
                continue;
            }
            // At the portfolio-margin rule's range the stock alone loses that
            // share of its value at the range's end.
            total += if p.margin_option_scan_range != 0.0 {
                shares.abs() * p.margin_option_scan_range * spot
            } else {
                shares.abs() * self.name_spot_move(slot, spot, z)
            };
        }
        Some(total)
    }

    // ── Listing ───────────────────────────────────────────────────────────

    /// List, for every public name, the expiries at or after session `from`
    /// it does not list, on its price tier, and widen every range around
    /// each name's price.
    fn name_options_list(&mut self, from: i64) {
        let expiries = calendar::name_option_expiries(from);
        let mut chains = std::mem::take(&mut self.name_options.chains);
        for (slot, c) in self.companies.iter().enumerate() {
            let price = c.stock.price;
            if !c.is_public || c.is_bankrupt || RESERVED_ROOTS.contains(&c.ticker.as_str()) || !(price > 0.0) {
                continue;
            }
            let at = match chains.iter().position(|x| x.slot == slot) {
                Some(at) => at,
                None => {
                    chains.push(NameChain { slot, expiries: Vec::new() });
                    chains.len() - 1
                }
            };
            let chain = &mut chains[at];
            for &expiry in &expiries {
                if !chain.expiries.iter().any(|e| e.expiry == expiry) {
                    let step = NAME_OPTION.strike_step(price);
                    chain.expiries.push(ListedExpiry { expiry, step, lo: 1, hi: 0, pressure: 0.0 });
                }
            }
            chain.expiries.sort_by_key(|e| e.expiry);
        }
        chains.sort_by_key(|c| c.slot);
        self.name_options.chains = chains;
        self.name_options_widen();
    }

    /// Widen every listed range to keep ten strikes either side of its
    /// name's price.
    fn name_options_widen(&mut self) {
        for chain in self.name_options.chains.iter_mut() {
            let price = self.companies[chain.slot].stock.price;
            if !(price > 0.0 && price.is_finite()) {
                continue;
            }
            for e in chain.expiries.iter_mut() {
                e.widen(price, NAME_OPTION.strikes_each_side);
            }
        }
    }

    // ── Orders ────────────────────────────────────────────────────────────

    fn name_options_record_flow(&mut self, agent: &str, symbol: &str, bought: f64, sold: f64) {
        match self.name_options.book.flow.iter_mut().find(|(a, t, _, _)| a == agent && t == symbol) {
            Some(r) => {
                r.2 += bought;
                r.3 += sold;
            }
            None => self.name_options.book.flow.push((agent.to_string(), symbol.to_string(), bought, sold)),
        }
    }

    /// An order meets the dealer, as an index option's does
    /// (`Engine::submit_order`): it walks the ladder, the position is
    /// recorded, and the expiry's pressure moves. The fills and the mid.
    fn name_options_meet(
        &mut self,
        symbol: &str,
        agent: &str,
        order_id: &str,
        side: Side,
        quantity: f64,
        limit: Option<f64>,
    ) -> (Vec<AgentFill>, f64) {
        let Some((chain, slot, right, strike)) = self.name_option_slot(symbol) else {
            return (Vec::new(), f64::NAN);
        };
        let clock = self.options_clock();
        let Some(index) = self.surface_at(&clock) else {
            return (Vec::new(), f64::NAN);
        };
        let Some(ctx) = self.name_context(&clock, chain, &index) else {
            return (Vec::new(), f64::NAN);
        };
        let x = self.name_expiry(&clock, &ctx, slot);
        let Some((l, q)) = self.name_priced(&ctx, &x, right, strike) else {
            return (Vec::new(), f64::NAN);
        };
        let levels = match side {
            Side::Buy => &l.asks,
            Side::Sell => &l.bids,
        };
        let taken = walk(levels, quantity, limit, side == Side::Buy);
        let (day, tick) = (self.current_day, self.ticks_today());
        let mut fills = Vec::with_capacity(taken.len());
        let mut total = 0.0;
        for (price, contracts) in taken {
            total += contracts;
            let mut fill = AgentFill {
                agent: agent.to_string(),
                order_id: order_id.to_string(),
                ticker: symbol.to_string(),
                side,
                quantity: contracts,
                price,
                liquidity: Liquidity::Taker,
                counterparty: MARKET_MAKER_ID.to_string(),
                reference: q.mid,
                day,
                tick,
                sequence: self.name_options.book.fill_sequence,
            };
            self.name_options.book.fill_sequence += 1;
            self.name_options.book.fills.push(fill.clone());
            fill.sequence = self.name_options.book.fill_sequence - 1;
            fills.push(fill);
        }
        if total > 0.0 {
            match side {
                Side::Buy => self.name_options_record_flow(agent, symbol, total, 0.0),
                Side::Sell => self.name_options_record_flow(agent, symbol, 0.0, total),
            }
            // The expiry's at-the-money vega, a share.
            let atm_vol = crate::mathx::sqrt(x.theta / crate::mathx::max(x.years, 1e-12));
            let atm = black_greeks(Right::Call, x.carry_spot, x.forward, x.years, atm_vol, x.rate).1.vega;
            let sold = match side {
                Side::Buy => total,
                Side::Sell => -total,
            };
            let e = &mut self.name_options.chains[chain].expiries[slot];
            e.pressure = pressure_after(e.pressure, sold, q.greeks.vega, atm);
        }
        (fills, q.mid)
    }

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

    /// Fill every waiting order the dealer's quote now crosses, in arrival
    /// order.
    fn name_options_cross_resting(&mut self) {
        if self.name_options.book.orders.is_empty() || self.options.phase != OptionsPhase::Session {
            return;
        }
        let ids: Vec<String> = self.name_options.book.orders.iter().map(|o| o.id.clone()).collect();
        for id in ids {
            let Some(o) = self.name_options.book.orders.iter().find(|o| o.id == id).cloned() else {
                continue;
            };
            let (fills, _) = self.name_options_meet(&o.ticker, &o.agent, &o.id, o.side, o.remaining, Some(o.limit));
            let filled: f64 = fills.iter().map(|f| f.quantity).sum();
            if filled > 0.0 {
                self.name_options_reduce_order(&o.id, filled);
            }
        }
    }

    /// An agent's order on a listed name option, in the session only, as
    /// an index option's is.
    pub(super) fn submit_name_option_order(
        &mut self,
        agent: &str,
        symbol: &str,
        side: Side,
        quantity: f64,
        limit: Option<f64>,
        order_id: Option<String>,
    ) -> Result<OrderReport, String> {
        if self.name_option_slot(symbol).is_none() {
            return Err(format!("{symbol} is not a listed contract: Engine.contracts() lists the ones that trade"));
        }
        if self.options.phase != OptionsPhase::Session {
            return Err(format!("{symbol} is an option, which trades in the session only"));
        }
        let depth = NAME_OPTION_DEALER.levels as f64 * NAME_OPTION_DEALER.displayed_size;
        self.option_limit_check(&self.name_options.book, depth, agent, symbol, side, quantity)?;
        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.name_options.book.sequence),
        };
        if self.contract_order_id_taken(&id) {
            return Err(format!("order id {id:?} is already waiting in the book"));
        }
        let sequence = self.name_options.book.sequence;
        self.name_options.book.sequence += 1;
        self.name_options_cross_resting();
        let (fills, reference) = self.name_options_meet(symbol, agent, &id, side, quantity, limit);
        if !reference.is_finite() {
            return Err(format!("{symbol} has no quote: its name's surface does not price"));
        }
        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.name_options.book.orders.push(AgentOrder {
                    id: id.clone(),
                    agent: agent.to_string(),
                    ticker: symbol.to_string(),
                    side,
                    limit: p,
                    quantity,
                    remaining: remainder,
                    sequence,
                    mode: RestMode::Range,
                });
                (remainder, Some(RestMode::Range))
            }
            _ => (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,
        })
    }

    pub(super) fn cancel_name_option_order(&mut self, order_id: &str, agent: Option<&str>) -> bool {
        let before = self.name_options.book.orders.len();
        self.name_options.book.orders.retain(|o| !(o.id == order_id && agent.is_none_or(|a| a == o.agent)));
        self.name_options.book.orders.len() != before
    }

    pub(super) fn name_option_orders(&self) -> &[AgentOrder] {
        &self.name_options.book.orders
    }

    pub(super) fn take_name_option_fills(&mut self, agent: Option<&str>) -> Vec<AgentFill> {
        if self.name_options.book.fills.is_empty() {
            return Vec::new();
        }
        let (taken, kept): (Vec<_>, Vec<_>) =
            std::mem::take(&mut self.name_options.book.fills).into_iter().partition(|f| agent.is_none_or(|a| a == f.agent));
        self.name_options.book.fills = kept;
        taken
    }

    /// The dealer's ladder for a listed name option as a book, with every
    /// agent's waiting order on it.
    pub(super) fn name_option_book(&self, symbol: &str) -> Option<OrderBook> {
        let (chain, slot, right, strike) = self.name_option_slot(symbol)?;
        let clock = self.options_clock();
        let index = self.surface_at(&clock)?;
        let ctx = self.name_context(&clock, chain, &index)?;
        let x = self.name_expiry(&clock, &ctx, slot);
        let (l, q) = self.name_priced(&ctx, &x, right, strike)?;
        let mut book = OrderBook::new(symbol.to_string(), Some(q.mid)).with_cap(AGENT_BOOK_CAP);
        if self.options.phase == OptionsPhase::Session {
            for b in &l.bids {
                book.rest_limit(Side::Buy, b.price, b.size, MARKET_MAKER_ID);
            }
            for a in &l.asks {
                book.rest_limit(Side::Sell, a.price, a.size, MARKET_MAKER_ID);
            }
        }
        for o in &self.name_options.book.orders {
            if o.ticker == symbol {
                book.post_limit(o.side, o.limit, o.remaining, &o.agent, Some(o.id.clone()));
            }
        }
        Some(book)
    }

    // ── Exercise ──────────────────────────────────────────────────────────

    /// `agent`'s position in `symbol`, contracts, long positive.
    fn name_position(&self, agent: &str, symbol: &str) -> f64 {
        self.name_options
            .book
            .flow
            .iter()
            .filter(|(a, t, _, _)| a == agent && t == symbol)
            .fold(0.0, |acc, (_, _, b, s)| acc + (b - s))
    }

    /// Instruct the exercise of `quantity` contracts of `agent`'s long
    /// position in the listed name option `symbol` at the next close
    /// (`options_names_listed`). The holder pays the strike for 100 shares a
    /// call, or delivers 100 shares for the strike a put, and the contracts
    /// are assigned to the short positions pro rata (see `engine::name_options`).
    /// Instructions add up; at the close each is exercised up to the long
    /// position held then. Returns the contracts now instructed for the
    /// close. Refused for an index option, which is European, for a
    /// quantity that is not positive and finite, and for more than the
    /// position held less what is already instructed.
    pub fn exercise(&mut self, agent: &str, symbol: &str, quantity: f64) -> Result<f64, String> {
        if agent.is_empty() || crate::agent_book::is_house(agent) {
            return Err(format!(
                "{agent:?} cannot exercise: agent labels are non-empty and not one of the book's own owners"
            ));
        }
        if !(quantity > 0.0 && quantity.is_finite()) {
            return Err(format!("quantity must be finite and greater than zero, got {quantity}"));
        }
        if self.is_listed_option(symbol) {
            return Err(format!(
                "{symbol} is an index option, which is European: it settles in cash at expiry and cannot be exercised"
            ));
        }
        if self.name_option_slot(symbol).is_none() {
            return Err(if self.names_on() {
                format!("{symbol} is not a listed single-name option: Engine.contracts() lists the ones that trade")
            } else {
                format!("{symbol} cannot be exercised: no single-name option is listed with options_names_listed off")
            });
        }
        let held = self.name_position(agent, symbol);
        let pending = self
            .name_options
            .instructions
            .iter()
            .filter(|i| i.agent == agent && i.symbol == symbol)
            .fold(0.0, |acc, i| acc + i.quantity);
        if quantity > held - pending + 1e-9 {
            return Err(format!(
                "{agent} holds {held} contracts of {symbol} long and has instructed {pending} already: \
                 it can exercise at most {} more",
                crate::mathx::max(0.0, held - pending)
            ));
        }
        self.name_options.instructions.push(Instruction {
            agent: agent.to_string(),
            symbol: symbol.to_string(),
            quantity,
        });
        Ok(pending + quantity)
    }

    /// Withdraw `agent`'s exercise instructions on `symbol` waiting for the
    /// close. Returns the contracts withdrawn.
    pub fn cancel_exercise(&mut self, agent: &str, symbol: &str) -> f64 {
        let mut gone = 0.0;
        self.name_options.instructions.retain(|i| {
            let hit = i.agent == agent && i.symbol == symbol;
            if hit {
                gone += i.quantity;
            }
            !hit
        });
        gone
    }

    /// The exercise instructions waiting for the next close, for one agent
    /// or all: `(agent, symbol, contracts)`, in arrival order.
    pub fn exercise_instructions(&self, agent: Option<&str>) -> Vec<InstructionRow> {
        self.name_options
            .instructions
            .iter()
            .filter(|i| agent.is_none_or(|a| a == i.agent))
            .map(|i| (i.agent.clone(), i.symbol.clone(), i.quantity))
            .collect()
    }

    /// The dealer's decision on a long it holds, at the close, from its tree
    /// at the surface's volatility.
    fn name_dealer_decision(
        &self,
        ctx: &NameContext,
        x: &NameExpiry,
        right: Right,
        strike: f64,
    ) -> Option<american::ExerciseDecision> {
        let k = crate::mathx::log(strike / x.forward);
        let vol = Self::name_vol(&ctx.surface, x, k, x.theta);
        if !(vol > 0.0 && x.years > 0.0) {
            return None;
        }
        let spec = OptionSpec::new(right, ctx.spot, strike, x.rate, vol, x.years);
        american::early_exercise(&spec, &x.dividends, american::steps_for(x.sessions.ceil() as i64)).ok()
    }

    /// The close's exercises: agents' instructions, the dealer's own and, at
    /// an expiry, every option in the money by a cent on the closing print;
    /// each contract exercised assigned pro rata to the short positions. The
    /// rows join `events` and the positions move.
    fn name_options_exercise_at_close(&mut self) {
        let t = self.elapsed_days;
        let clock = self.options_clock();
        // Every symbol with a position or an instruction, in chain, expiry,
        // strike and right order.
        let mut symbols: Vec<(usize, usize, u64, u8, String)> = Vec::new();
        let mut seen = std::collections::HashSet::new();
        let candidates = self
            .name_options
            .book
            .flow
            .iter()
            .filter(|(_, _, b, s)| b - s != 0.0)
            .map(|(_, t, _, _)| t.clone())
            .chain(self.name_options.instructions.iter().map(|i| i.symbol.clone()))
            .collect::<Vec<_>>();
        for sym in candidates {
            if !seen.insert(sym.clone()) {
                continue;
            }
            let Some((chain, slot, right, strike)) = self.name_option_slot(&sym) else {
                continue;
            };
            let cents = (strike * 100.0).round() as u64;
            symbols.push((chain, slot, cents, if right == Right::Call { 0 } else { 1 }, sym));
        }
        symbols.sort();
        let instructions = std::mem::take(&mut self.name_options.instructions);
        if symbols.is_empty() {
            return;
        }
        let index = self.surface_at(&clock);
        let mut contexts: Vec<(usize, Option<NameContext>)> = Vec::new();
        for (chain, slot, cents, rcode, sym) in symbols {
            let right = if rcode == 0 { Right::Call } else { Right::Put };
            let strike = cents as f64 / 100.0;
            let company_slot = self.name_options.chains[chain].slot;
            let expiry = self.name_options.chains[chain].expiries[slot].expiry;
            let expiring = expiry <= t;
            let spot = self.companies[company_slot].stock.price;
            let reference = if expiring {
                self.last_closes.get(company_slot).copied().unwrap_or(spot)
            } else {
                spot
            };
            let positions: Vec<(String, f64)> = self
                .name_options
                .book
                .flow
                .iter()
                .filter(|(_, s, _, _)| *s == sym)
                .map(|(a, _, b, s)| (a.clone(), b - s))
                .collect();
            let dealer = -positions.iter().map(|(_, p)| p).sum::<f64>();
            let itm = american::exercised_at_expiry(right, strike, reference);
            let mut exercises: Vec<(String, f64)> = Vec::new();
            for (agent, p) in &positions {
                if *p <= 0.0 {
                    continue;
                }
                let e = if expiring && itm {
                    *p
                } else {
                    let asked: f64 = instructions.iter().filter(|i| i.agent == *agent && i.symbol == sym).map(|i| i.quantity).sum();
                    crate::mathx::min(asked, *p)
                };
                if e > 0.0 {
                    exercises.push((agent.clone(), e));
                }
            }
            if dealer > 0.0 {
                let dealer_exercises = if expiring {
                    itm
                } else {
                    if !contexts.iter().any(|(c, _)| *c == chain) {
                        let ctx = index.as_ref().and_then(|ix| self.name_context(&clock, chain, ix));
                        contexts.push((chain, ctx));
                    }
                    let ctx = contexts.iter().find(|(c, _)| *c == chain).and_then(|(_, x)| x.as_ref());
                    match ctx {
                        Some(ctx) => {
                            let x = self.name_expiry(&clock, ctx, slot);
                            self.name_dealer_decision(ctx, &x, right, strike).is_some_and(|d| d.exercise)
                        }
                        None => false,
                    }
                };
                if dealer_exercises {
                    exercises.push((MARKET_MAKER_ID.to_string(), dealer));
                }
            }
            let exercised: f64 = exercises.iter().map(|(_, e)| e).sum();
            if !(exercised > 0.0) {
                continue;
            }
            let mut shorts: Vec<(String, f64)> =
                positions.iter().filter(|(_, p)| *p < 0.0).map(|(a, p)| (a.clone(), -p)).collect();
            if dealer < 0.0 {
                shorts.push((MARKET_MAKER_ID.to_string(), -dealer));
            }
            let sizes: Vec<f64> = shorts.iter().map(|(_, s)| *s).collect();
            let assigned = allocate(exercised, &sizes);
            let ticker = self.companies[company_slot].ticker.clone();
            let value = intrinsic(right, reference, strike);
            let row = |agent: &str, event: SettlementEvent, contracts: f64| {
                // A call's holder receives the shares and pays the strike, a
                // put's delivers them and receives it; the writer the reverse.
                let sign = match (right, event) {
                    (Right::Call, SettlementEvent::Exercise) | (Right::Put, SettlementEvent::Assignment) => 1.0,
                    _ => -1.0,
                };
                let shares = sign * NAME_OPTION.multiplier * contracts;
                let cash = -sign * cents as f64 * contracts;
                let mut s = Settlement::new(sym.clone(), ContractKind::NameOption, ticker.clone(), t, value, reference);
                s.event = event;
                s.agent = Some(agent.to_string());
                s.quantity = contracts;
                s.shares = shares;
                s.cash = cash;
                s
            };
            for (agent, e) in &exercises {
                self.name_options.events.push(row(agent, SettlementEvent::Exercise, *e));
                if agent != MARKET_MAKER_ID {
                    self.name_options_record_flow(agent, &sym, 0.0, *e);
                }
            }
            for ((agent, _), a) in shorts.iter().zip(assigned) {
                if a > 0.0 {
                    self.name_options.events.push(row(agent, SettlementEvent::Assignment, a));
                    if agent != MARKET_MAKER_ID {
                        self.name_options_record_flow(agent, &sym, a, 0.0);
                    }
                }
            }
        }
    }

    /// The close's expiries settle: each is recorded on its name's closing
    /// print, and its orders and positions go.
    fn name_options_expire(&mut self) {
        let t = self.elapsed_days;
        let mut gone: Vec<String> = Vec::new();
        let mut settled = Vec::new();
        for chain in self.name_options.chains.iter_mut() {
            let slot = chain.slot;
            let reference = self.last_closes.get(slot).copied().unwrap_or(self.companies[slot].stock.price);
            let ticker = &self.companies[slot].ticker;
            chain.expiries.retain(|e| {
                if e.expiry <= t {
                    gone.push(format!("{ticker}.O{:04}.", e.expiry));
                    settled.push(SettledName { slot, expiry: e.expiry, reference, step: e.step, lo: e.lo, hi: e.hi });
                    false
                } else {
                    true
                }
            });
        }
        if gone.is_empty() {
            return;
        }
        let hit = |s: &str| gone.iter().any(|p| s.starts_with(p.as_str()));
        self.name_options.book.orders.retain(|o| !hit(&o.ticker));
        self.name_options.book.flow.retain(|(_, s, _, _)| !hit(s));
        self.name_options.settled.extend(settled);
    }

    // ── Steps and the day's boundaries ────────────────────────────────────

    /// A tick's step: the dealer's pressures decay, and waiting orders its
    /// quotes now cross fill. Nothing with the switch off.
    pub(super) fn name_options_session_step(&mut self) {
        if !self.names_on() || self.options.phase != OptionsPhase::Session {
            return;
        }
        let life = self.params.option_name_inventory_half_life;
        let d = if life != 0.0 {
            // `option_name_inventory_half_life`, sessions of open ticks
            crate::mathx::pow(0.5, 1.0 / (life * SESSION_TICKS))
        } else {
            NAME_OPTION_DEALER.decay()
        };
        for chain in self.name_options.chains.iter_mut() {
            for e in chain.expiries.iter_mut() {
                e.pressure *= d;
                if e.pressure.abs() < 1e-9 {
                    e.pressure = 0.0;
                }
            }
        }
        self.name_options_cross_resting();
    }

    /// At an open, after the opening prints: every range widens around its
    /// name's price, and waiting orders the opening quotes cross fill.
    /// Nothing with the switch off.
    pub(super) fn name_options_open(&mut self) {
        if !self.names_on() {
            return;
        }
        self.name_options.cache.clear();
        self.name_options_widen();
        self.name_options_cross_resting();
    }

    /// At a close, on the forecast the close computed: the exercises and
    /// assignments, the expiries due settle, the next are listed and every
    /// range widened. Nothing with the switch off.
    pub(super) fn name_options_close(&mut self) {
        if !self.names_on() {
            return;
        }
        self.name_options.cache.clear();
        self.name_options_exercise_at_close();
        self.name_options_expire();
        let t = self.elapsed_days;
        self.name_options_list(t + 1);
        self.name_options.cache.clear();
    }

    /// A name leaves the roster: its options are withdrawn, each position
    /// paid in cash at its intrinsic value on the name's last price (a
    /// settlement row with the agent named, the dealer's beside them), and
    /// its chain, orders and instructions go.
    pub(super) fn name_options_remove(&mut self, index: usize) {
        if !self.names_on() {
            return;
        }
        let c = &self.companies[index];
        let (ticker, price) = (c.ticker.clone(), c.stock.price);
        let prefix = format!("{ticker}.O");
        let ours = |s: &str| {
            s.starts_with(&prefix) && ContractSymbol::parse(s).is_ok_and(|p| p.root == ticker)
        };
        let t = self.elapsed_days;
        let mut net: Vec<(String, f64)> = Vec::new();
        let rows: Vec<(String, String, f64)> = self
            .name_options
            .book
            .flow
            .iter()
            .filter(|(_, s, b, sold)| ours(s) && b - sold != 0.0)
            .map(|(a, s, b, sold)| (a.clone(), s.clone(), b - sold))
            .collect();
        for (agent, symbol, position) in rows {
            let Ok(parsed) = ContractSymbol::parse(&symbol) else {
                continue;
            };
            let (SymbolKind::Option { right, .. }, Some(strike)) = (parsed.kind, parsed.strike()) else {
                continue;
            };
            let value = intrinsic(right, price, strike);
            let mut s = Settlement::new(symbol.clone(), ContractKind::NameOption, ticker.clone(), t, value, price);
            s.agent = Some(agent);
            s.quantity = position;
            s.cash = position * NAME_OPTION.multiplier * value;
            self.name_options.events.push(s);
            match net.iter_mut().find(|(x, _)| *x == symbol) {
                Some((_, n)) => *n += position,
                None => net.push((symbol, position)),
            }
        }
        for (symbol, position) in net {
            if position == 0.0 {
                continue;
            }
            let Ok(parsed) = ContractSymbol::parse(&symbol) else {
                continue;
            };
            let (SymbolKind::Option { right, .. }, Some(strike)) = (parsed.kind, parsed.strike()) else {
                continue;
            };
            let value = intrinsic(right, price, strike);
            let mut s = Settlement::new(symbol, ContractKind::NameOption, ticker.clone(), t, value, price);
            s.agent = Some(MARKET_MAKER_ID.to_string());
            s.quantity = -position;
            s.cash = -position * NAME_OPTION.multiplier * value;
            self.name_options.events.push(s);
        }
        self.name_options.book.orders.retain(|o| !ours(&o.ticker));
        self.name_options.book.flow.retain(|(_, s, _, _)| !ours(s));
        self.name_options.instructions.retain(|i| !ours(&i.symbol));
        self.name_options.chains.retain(|c| c.slot != index);
        self.name_options.settled.retain(|s| s.slot != index);
        for c in self.name_options.chains.iter_mut() {
            if c.slot > index {
                c.slot -= 1;
            }
        }
        for s in self.name_options.settled.iter_mut() {
            if s.slot > index {
                s.slot -= 1;
            }
        }
        self.name_options.cache.clear();
    }

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

    /// The listed name options, by name, expiry, strike, the call before the
    /// put: empty with the switch off.
    pub(super) fn name_option_contracts(&self) -> Vec<ContractSpec> {
        if !self.names_on() {
            return Vec::new();
        }
        let kind = ContractKind::NameOption;
        let mut out = Vec::new();
        for c in &self.name_options.chains {
            let ticker = &self.companies[c.slot].ticker;
            for e in &c.expiries {
                for strike in e.strikes() {
                    for right in [Right::Call, Right::Put] {
                        out.push(ContractSpec {
                            symbol: name_option_symbol(ticker, e.expiry, right, strike),
                            kind,
                            root: ticker.clone(),
                            expiry: e.expiry,
                            roll: e.expiry,
                            multiplier: NAME_OPTION.multiplier,
                            tick: NAME_OPTION_DEALER.tick,
                            settlement: kind.settlement(),
                            front: false,
                            right: Some(right),
                            strike: Some(strike),
                        });
                    }
                }
            }
        }
        out
    }

    /// The name options settled at `session`'s close, or every one with
    /// `None`: each listed contract at its intrinsic value on its name's
    /// closing print, then the session's exercises and assignments.
    pub(super) fn name_option_settlements(&self, session: Option<i64>) -> Vec<Settlement> {
        if !self.names_on() {
            return Vec::new();
        }
        let kind = ContractKind::NameOption;
        let mut out = Vec::new();
        for s in &self.name_options.settled {
            if session.is_some_and(|d| d != s.expiry) {
                continue;
            }
            let Some(c) = self.companies.get(s.slot) else {
                continue;
            };
            for n in s.lo..=s.hi {
                let strike = strike_at(n, s.step);
                for right in [Right::Call, Right::Put] {
                    out.push(Settlement::new(
                        name_option_symbol(&c.ticker, s.expiry, right, strike),
                        kind,
                        c.ticker.clone(),
                        s.expiry,
                        intrinsic(right, s.reference, strike),
                        s.reference,
                    ));
                }
            }
        }
        out.extend(self.name_options.events.iter().filter(|e| session.is_none_or(|d| d == e.session)).cloned());
        out.sort_by_key(|s| s.session);
        out
    }

    /// Construction rows AX1 and AX2 at the last close, as sentences: empty
    /// when every check holds. `None` with the switch off.
    ///
    /// - AX2: each exercise and assignment row moves 100 shares a contract
    ///   and the strike times the shares in cash, to the cent, and the
    ///   close's rows, the dealer's among them, sum to no shares and no cash
    ///   in every contract.
    /// - AX1, read while the close's state stands (between the close and the
    ///   next open): every early exercise of the dealer's is one its tree,
    ///   priced again now, still makes, a call's only on the eve of an
    ///   ex-date; every long the dealer still holds is one its tree holds;
    ///   and every option held is worth at least the European on the same
    ///   tree.
    pub fn exercise_violations(&self) -> Option<Vec<String>> {
        if !self.names_on() {
            return None;
        }
        let mut out = Vec::new();
        let Some(last) = self.name_options.events.iter().filter(|e| e.event != SettlementEvent::Settlement).map(|e| e.session).max() else {
            return Some(self.name_hold_violations());
        };
        let rows: Vec<&Settlement> = self.name_options.events.iter().filter(|e| e.session == last).collect();
        let mut symbols: Vec<&str> = rows.iter().map(|r| r.symbol.as_str()).collect();
        symbols.sort_unstable();
        symbols.dedup();
        for sym in &symbols {
            let (mut shares, mut cash) = (0.0, 0.0);
            for r in rows.iter().filter(|r| r.symbol == *sym) {
                if r.event == SettlementEvent::Settlement {
                    continue;
                }
                shares += r.shares;
                cash += r.cash;
                let cents = ContractSymbol::parse(sym).ok().and_then(|p| p.strike()).map_or(f64::NAN, |k| (k * 100.0).round());
                if (r.shares.abs() - NAME_OPTION.multiplier * r.quantity).abs() > 1e-9
                    || (r.cash.abs() - cents * r.quantity).abs() > 0.005
                    || r.shares * r.cash > 0.0
                {
                    out.push(format!(
                        "AX2: session {last}, {sym}: {} of {} contracts moves {} shares and {} cash",
                        r.event.as_str(), r.quantity, r.shares, r.cash
                    ));
                }
            }
            if shares.abs() > 1e-9 || cash.abs() > 0.005 {
                out.push(format!("AX2: session {last}, {sym}: the rows sum to {shares} shares and {cash} cash"));
            }
        }
        // AX1 needs the close's state: between that close and the next open.
        if self.options.phase == OptionsPhase::Night && self.elapsed_days == last {
            let clock = self.options_clock();
            if let Some(index) = self.surface_at(&clock) {
                for r in rows.iter().filter(|r| {
                    r.event == SettlementEvent::Exercise && r.agent.as_deref() == Some(MARKET_MAKER_ID)
                }) {
                    let Ok(parsed) = ContractSymbol::parse(&r.symbol) else {
                        continue;
                    };
                    if parsed.expiry <= last {
                        continue;
                    }
                    let (SymbolKind::Option { right, .. }, Some(strike)) = (parsed.kind, parsed.strike()) else {
                        continue;
                    };
                    // The contract is still listed: it expires later.
                    let Some((chain, slot, _, _)) = self.name_option_slot(&r.symbol) else {
                        out.push(format!("AX1: {} exercised early is no longer listed", r.symbol));
                        continue;
                    };
                    let Some(ctx) = self.name_context(&clock, chain, &index) else {
                        continue;
                    };
                    let x = self.name_expiry(&clock, &ctx, slot);
                    match self.name_dealer_decision(&ctx, &x, right, strike) {
                        Some(d) if d.exercise => {}
                        d => out.push(format!("AX1: the dealer exercised {} where its tree holds ({d:?})", r.symbol)),
                    }
                    if right == Right::Call && !x.dividends.iter().any(|d| d.time == 0.0) {
                        out.push(format!("AX1: the dealer exercised the call {} with no ex-date at the next open", r.symbol));
                    }
                }
            }
            out.extend(self.name_hold_violations());
        }
        Some(out)
    }

    /// AX1 on the positions standing at a close: each long the dealer holds
    /// is one its tree holds, and each option held is worth at least the
    /// European on the same tree.
    fn name_hold_violations(&self) -> Vec<String> {
        let mut out = Vec::new();
        if self.options.phase != OptionsPhase::Night {
            return out;
        }
        let clock = self.options_clock();
        let Some(index) = self.surface_at(&clock) else {
            return out;
        };
        let mut symbols: Vec<String> = self.name_options.book.flow.iter().map(|(_, s, _, _)| s.clone()).collect();
        symbols.sort();
        symbols.dedup();
        for sym in symbols {
            let net: f64 = self.name_options.book.flow.iter().filter(|(_, s, _, _)| *s == sym).map(|(_, _, b, s)| b - s).sum();
            let Some((chain, slot, right, strike)) = self.name_option_slot(&sym) else {
                continue;
            };
            let Some(ctx) = self.name_context(&clock, chain, &index) else {
                continue;
            };
            let x = self.name_expiry(&clock, &ctx, slot);
            if -net > 0.0 {
                if let Some(d) = self.name_dealer_decision(&ctx, &x, right, strike) {
                    if d.exercise {
                        out.push(format!("AX1: the dealer holds {} long where its tree exercises ({d:?})", sym));
                    }
                }
            }
            let k = crate::mathx::log(strike / x.forward);
            let vol = Self::name_vol(&ctx.surface, &x, k, x.theta);
            let a = self.name_tree(right, ctx.spot, strike, &x, vol, Style::American);
            let e = self.name_tree(right, ctx.spot, strike, &x, vol, Style::European);
            if let (Some(a), Some(e)) = (a, e) {
                if a.value < e.value - 1e-12 * strike {
                    out.push(format!("AX1: {sym}: American {} below European {}", a.value, e.value));
                }
            }
        }
        out
    }

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

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

    /// Agents' waiting name option orders, fills and positions: `None` with
    /// the switch off and while no agent has traded one.
    pub fn name_options_book_state(&self) -> Option<&BookState> {
        if self.names_on() && !self.name_options.book.is_pristine() {
            Some(&self.name_options.book)
        } else {
            None
        }
    }

    /// The exercise instructions waiting for the close, `(agent, symbol,
    /// contracts)`, and every exercise, assignment and delisting settlement
    /// so far: `None` with the switch off and while both are empty.
    pub fn name_options_exercise_state(&self) -> Option<(Vec<InstructionRow>, &[Settlement])> {
        if self.names_on() && !(self.name_options.instructions.is_empty() && self.name_options.events.is_empty()) {
            Some((self.exercise_instructions(None), &self.name_options.events))
        } else {
            None
        }
    }

    /// Put the name options back (a restore), each part refused where the
    /// switch is off.
    pub fn set_name_options_state(
        &mut self,
        words: Option<&[f64]>,
        book: Option<BookState>,
        exercise: Option<(Vec<InstructionRow>, Vec<Settlement>)>,
    ) -> Result<(), String> {
        if !self.names_on() {
            if words.is_some() || book.is_some() || exercise.is_some() {
                return Err("this snapshot carries single-name options (name_options), which only an engine \
                            with options_names_listed on keeps, and this engine's model has it off"
                    .to_string());
            }
            self.name_options = NameOptionsState::default();
            return Ok(());
        }
        let Some(words) = words else {
            return Err("an engine with options_names_listed on carries its listing (name_options); this \
                        snapshot lacks it"
                .to_string());
        };
        let mut state = NameOptionsState::default();
        state.set_words(words)?;
        if let Some(bad) = state.chains.iter().map(|c| c.slot).chain(state.settled.iter().map(|s| s.slot)).find(|s| *s >= self.companies.len()) {
            return Err(format!("this snapshot's name_options list slot {bad}, past the roster's {}", self.companies.len()));
        }
        state.book = book.unwrap_or_default();
        if let Some((instructions, events)) = exercise {
            state.instructions = instructions
                .into_iter()
                .map(|(agent, symbol, quantity)| Instruction { agent, symbol, quantity })
                .collect();
            state.events = events;
        }
        self.name_options = state;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::derivatives::pricing::black_price;
    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()
    }

    const NAMES: &[(&str, f64)] = &[
        ("options_names_listed", 1.0),
        ("surface_name_premium", 0.1),
        ("surface_name_skew", 0.5),
    ];

    fn engine(seed: u64, names: bool) -> Engine {
        let mut p = crate::params::PT_V21;
        for (name, value) in super::super::options::tests::ON {
            p = p.with_override(name, *value).unwrap();
        }
        if names {
            for (name, value) in NAMES {
                p = p.with_override(name, *value).unwrap();
            }
        }
        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(),
            p,
        )
    }

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

    /// Open session `d`, run `f` in it before any tick, and close it.
    fn trading_day(e: &mut Engine, d: i64, buffer: &mut SessionBuffer, f: impl FnOnce(&mut Engine)) {
        e.set_current_day(d);
        e.open_market();
        f(e);
        e.run_session(&SessionRequest::new(crate::market::GameTime::new(9, 30, 3), 390), buffer);
        e.close_day(d + 1);
    }

    fn sym(e: &Engine, chain: usize, slot: usize, right: Right, n: i64) -> String {
        let c = &e.name_options.chains[chain];
        let x = &c.expiries[slot];
        name_option_symbol(&e.companies[c.slot].ticker, x.expiry, right, strike_at(n, x.step))
    }

    /// The listed strike index nearest the name's price.
    fn atm(e: &Engine, chain: usize, slot: usize) -> i64 {
        let c = &e.name_options.chains[chain];
        (e.companies[c.slot].stock.price / c.expiries[slot].step).round() as i64
    }

    #[test]
    fn off_nothing_is_listed_and_nothing_is_kept() {
        let mut e = engine(3, false);
        let mut buffer = SessionBuffer::new();
        day(&mut e, 0, &mut buffer);
        assert!(!e.lists_name_options());
        assert!(e.name_option_roots().is_empty());
        assert!(e.name_options_words().is_none());
        assert!(e.name_options_exercise_state().is_none());
        let ticker = e.companies[0].ticker.clone();
        assert!(e.name_surface(&ticker).is_none());
        assert!(e.exercise("a", &format!("{ticker}.O0014.C10.00"), 1.0).is_err());
        assert!(e.exercise_violations().is_none());
        assert!(e.contracts().iter().all(|c| c.kind != ContractKind::NameOption));
    }

    #[test]
    fn every_name_lists_six_expiries_on_its_tier_from_the_first_close() {
        let mut e = engine(5, true);
        let mut buffer = SessionBuffer::new();
        assert!(e.name_option_roots().is_empty(), "nothing before the first close");
        day(&mut e, 0, &mut buffer);
        let public = e.companies.iter().filter(|c| c.is_public && !c.is_bankrupt).count();
        assert_eq!(e.name_options.chains.len(), public);
        for c in &e.name_options.chains {
            let price = e.companies[c.slot].stock.price;
            assert_eq!(c.expiries.iter().map(|x| x.expiry).collect::<Vec<_>>(), calendar::name_option_expiries(1));
            for x in &c.expiries {
                assert_eq!(x.step, NAME_OPTION.strike_step(price));
                let centre = (price / x.step).round() as i64;
                assert!(x.lo <= std::cmp::max(1, centre - 10) && x.hi >= centre + 10, "{x:?} at {price}");
            }
        }
        let listed: i64 = e.name_options.chains.iter().flat_map(|c| &c.expiries).map(|x| 2 * (x.hi - x.lo + 1)).sum();
        let names = e.contracts().into_iter().filter(|c| c.kind == ContractKind::NameOption).count();
        assert_eq!(names as i64, listed);
        assert_eq!(NAME_OPTION.strike_step(4.0), 0.5);
        assert_eq!(NAME_OPTION.strike_step(49.99), 1.0);
        assert_eq!(NAME_OPTION.strike_step(50.0), 2.5);
        assert_eq!(NAME_OPTION.strike_step(150.0), 5.0);
        assert_eq!(NAME_OPTION.strike_step(250.0), 10.0);
    }

    #[test]
    fn a_name_surface_reads_its_forecast_variance_times_the_premium() {
        let mut e = engine(7, true);
        let mut buffer = SessionBuffer::new();
        for d in 0..3 {
            day(&mut e, d, &mut buffer);
        }
        let f = e.forecast().unwrap().clone();
        for c in &e.name_options.chains {
            let ticker = e.companies[c.slot].ticker.clone();
            let s = e.name_surface(&ticker).unwrap();
            let series = &f.name_variance[c.slot];
            // Ten sessions out, with no report among them where the curve is
            // the premium on the expected variance summed.
            let reports = e.earnings_calendar(4, 11).iter().filter(|(slot, _)| *slot == c.slot).count();
            if reports == 0 {
                let premium = crate::derivatives::surface::term_premium(10.0, -0.03, 0.1);
                let expected: f64 = series[..10].iter().sum::<f64>() * 1.1 * 1.1 * crate::mathx::exp(premium);
                assert!((s.theta(10.0) - expected).abs() < 1e-12 * expected, "{ticker}: {} {expected}", s.theta(10.0));
            }
            assert_eq!(s.ssvi.rho, 0.5 * e.surface("IDX").unwrap().ssvi.rho);
            s.check().unwrap();
        }
    }

    #[test]
    fn quotes_are_american_monotone_and_inside_the_dealers_spread() {
        let mut e = engine(11, true);
        let mut buffer = SessionBuffer::new();
        for d in 0..3 {
            day(&mut e, d, &mut buffer);
        }
        e.set_current_day(3);
        e.open_market();
        for chain in 0..3 {
            let c = &e.name_options.chains[chain];
            let ticker = e.companies[c.slot].ticker.clone();
            let spot = e.companies[c.slot].stock.price;
            for x in c.expiries.clone().iter().take(3) {
                let quotes = e.chain(&ticker, x.expiry).unwrap();
                assert_eq!(quotes.len() as i64, 2 * (x.hi - x.lo + 1));
                for q in &quotes {
                    let intrinsic = match q.right {
                        Right::Call => (spot - q.strike).max(0.0),
                        Right::Put => (q.strike - spot).max(0.0),
                    };
                    assert!(q.mid >= intrinsic - 1e-12, "{}: {} under {intrinsic}", q.symbol, q.mid);
                    assert!(q.ask.unwrap() > q.mid, "{q:?}");
                    if let Some(b) = q.bid {
                        assert!(b < q.mid);
                    }
                    // At least the European on the escrowed forward, less the
                    // tree's discretisation.
                    let years = q.sessions_to_expiry / 252.0;
                    let euro = black_price(q.right, q.forward, q.strike, years, q.iv, crate::mathx::exp(-q.rate * years));
                    assert!(q.mid >= euro - 2e-3 * spot / 100.0, "{}: {} below {euro}", q.symbol, q.mid);
                    assert_eq!(q.multiplier, 100.0);
                    assert!(q.greeks.delta.abs() <= 1.0 + 1e-9);
                }
                let side = |r: Right| quotes.iter().filter(|q| q.right == r).collect::<Vec<_>>();
                for w in side(Right::Call).windows(2) {
                    assert!(w[1].mid <= w[0].mid + 1e-12, "calls fall in the strike");
                }
                for w in side(Right::Put).windows(2) {
                    assert!(w[1].mid >= w[0].mid - 1e-12, "puts rise in the strike");
                }
                for w in side(Right::Call).windows(3) {
                    assert!(w[0].mid + w[2].mid >= 2.0 * w[1].mid - 1e-9, "convex");
                }
            }
            // The summary reads the chain without a tree.
            let summary = e.option_expiries(&ticker);
            assert_eq!(summary.len(), e.name_options.chains[chain].expiries.len());
            assert!(summary[0].atm_iv.unwrap() > 0.0);
        }
    }

    #[test]
    fn an_agents_exercise_delivers_at_the_strike_and_conserves() {
        let mut e = engine(13, true);
        let mut buffer = SessionBuffer::new();
        for d in 0..3 {
            day(&mut e, d, &mut buffer);
        }
        let call = sym(&e, 0, 1, Right::Call, atm(&e, 0, 1) - 3);
        let put = sym(&e, 1, 1, Right::Put, atm(&e, 1, 1) + 3);
        trading_day(&mut e, 3, &mut buffer, |e| {
            assert_eq!(e.submit_order("a", &call, Side::Buy, 7.0, None, None).unwrap().filled, 7.0);
            assert_eq!(e.submit_order("b", &call, Side::Sell, 3.0, None, None).unwrap().filled, 3.0);
            assert_eq!(e.submit_order("a", &put, Side::Buy, 2.0, None, None).unwrap().filled, 2.0);
            assert!(e.exercise("a", &call, 8.0).is_err(), "more than held");
            assert_eq!(e.exercise("a", &call, 4.0).unwrap(), 4.0);
            assert_eq!(e.exercise("a", &call, 1.0).unwrap(), 5.0);
            assert!(e.exercise("a", &call, 3.0).is_err(), "more than held less instructed");
            assert!(e.exercise("b", &call, 1.0).is_err(), "a writer has nothing to exercise");
            assert_eq!(e.exercise("a", &put, 2.0).unwrap(), 2.0);
        });
        let rows: Vec<Settlement> = e.settlements(Some(3)).into_iter().filter(|s| s.event != SettlementEvent::Settlement).collect();
        let strike = ContractSymbol::parse(&call).unwrap().strike().unwrap();
        let mine: Vec<&Settlement> = rows.iter().filter(|r| r.symbol == call).collect();
        // a exercises 5; the shorts are b's 3 and the dealer's 4 (it sold 7
        // and bought 3), assigned 5 pro rata: 2 and 3, or 3 and 2.
        let ex = mine.iter().find(|r| r.event == SettlementEvent::Exercise).unwrap();
        assert_eq!((ex.agent.as_deref(), ex.quantity, ex.shares), (Some("a"), 5.0, 500.0));
        assert_eq!(ex.cash, -500.0 * strike);
        let assigned: f64 = mine.iter().filter(|r| r.event == SettlementEvent::Assignment).map(|r| r.quantity).sum();
        assert_eq!(assigned, 5.0);
        assert_eq!(mine.iter().map(|r| r.shares).sum::<f64>(), 0.0);
        assert_eq!(mine.iter().map(|r| r.cash).sum::<f64>(), 0.0);
        assert_eq!(e.name_position("a", &call), 2.0);
        let b_assigned = mine.iter().find(|r| r.agent.as_deref() == Some("b")).map_or(0.0, |r| r.quantity);
        assert_eq!(e.name_position("b", &call), -3.0 + b_assigned);
        // The put delivers shares for the strike.
        let p = rows.iter().find(|r| r.symbol == put && r.event == SettlementEvent::Exercise).unwrap();
        assert_eq!(p.shares, -200.0);
        assert!(p.cash > 0.0);
        assert!(e.exercise_instructions(None).is_empty(), "instructions are spent at the close");
        assert_eq!(e.exercise_violations().unwrap(), Vec::<String>::new());
    }

    #[test]
    fn the_dealer_exercises_a_deep_call_on_the_eve_of_a_dividend_and_assigns_its_writer() {
        let mut e = engine(17, true);
        let mut buffer = SessionBuffer::new();
        day(&mut e, 0, &mut buffer);
        // A payer whose next ex-date leaves a few sessions to trade in, and
        // whose front expiry runs past it.
        let mut found = None;
        for (i, c) in e.name_options.chains.iter().enumerate() {
            let company = &e.companies[c.slot];
            let Some(d) = company.stock.dividend else {
                continue;
            };
            if !(d.target_yield > 0.0) {
                continue;
            }
            let k = crate::market::dividends::sessions_since_ex(&company.ticker, 1);
            let ex = 1 + crate::market::dividends::DIVIDEND_PERIOD - k;
            if let Some(slot) = c.expiries.iter().position(|x| x.expiry >= ex + 3) {
                if ex >= 3 {
                    found = Some((i, slot, ex));
                    break;
                }
            }
        }
        let (chain, slot, ex) = found.expect("a payer on this roster");
        for d in 1..ex - 1 {
            day(&mut e, d, &mut buffer);
        }
        let deep = sym(&e, chain, slot, Right::Call, e.name_options.chains[chain].expiries[slot].lo);
        let near = sym(&e, chain, slot, Right::Call, atm(&e, chain, slot) + 2);
        trading_day(&mut e, ex - 1, &mut buffer, |e| {
            assert!(e.submit_order("w", &deep, Side::Sell, 10.0, None, None).unwrap().filled > 0.0);
            let _ = e.submit_order("w", &near, Side::Sell, 10.0, None, None);
        });
        let rows: Vec<Settlement> = e.settlements(Some(ex - 1)).into_iter().filter(|s| s.event != SettlementEvent::Settlement).collect();
        let on = |s: &str| rows.iter().filter(|r| r.symbol == s).collect::<Vec<_>>();
        let deep_rows = on(&deep);
        assert!(
            deep_rows.iter().any(|r| r.event == SettlementEvent::Exercise && r.agent.as_deref() == Some(MARKET_MAKER_ID)),
            "the dealer exercises the deep call on the eve: {deep_rows:?}"
        );
        let w = deep_rows.iter().find(|r| r.agent.as_deref() == Some("w")).unwrap();
        assert_eq!((w.event, w.quantity, w.shares), (SettlementEvent::Assignment, 10.0, -1000.0));
        assert!(on(&near).is_empty(), "an out-of-the-money call is held");
        assert_eq!(e.exercise_violations().unwrap(), Vec::<String>::new());
        // At the ex-date's open nothing more is exercised for it.
        day(&mut e, ex, &mut buffer);
        assert!(e.settlements(Some(ex)).iter().all(|s| s.symbol != deep));
    }

    #[test]
    fn at_expiry_the_money_is_exercised_by_exception_and_the_rest_expires() {
        let mut e = engine(19, true);
        let mut buffer = SessionBuffer::new();
        day(&mut e, 0, &mut buffer);
        let front = e.name_options.chains[0].expiries[0].expiry;
        assert_eq!(front, 14);
        for d in 1..front {
            day(&mut e, d, &mut buffer);
        }
        let n = atm(&e, 0, 0);
        let lo = sym(&e, 0, 0, Right::Call, n - 5);
        let hi = sym(&e, 0, 0, Right::Call, n + 5);
        let put = sym(&e, 0, 0, Right::Put, n - 5);
        trading_day(&mut e, front, &mut buffer, |e| {
            e.submit_order("a", &lo, Side::Buy, 4.0, None, None).unwrap();
            e.submit_order("a", &hi, Side::Buy, 4.0, None, None).unwrap();
            e.submit_order("b", &put, Side::Sell, 4.0, None, None).unwrap();
        });
        let close = e.last_closes[e.name_options.chains[0].slot];
        let rows = e.settlements(Some(front));
        let strike = |s: &str| ContractSymbol::parse(s).unwrap().strike().unwrap();
        // Every listed contract of the expiry has its settlement row.
        let ticker = e.companies[e.name_options.chains[0].slot].ticker.clone();
        let settled = e.name_options.settled.iter().find(|x| x.slot == e.name_options.chains[0].slot).unwrap().clone();
        assert_eq!(settled.reference.to_bits(), close.to_bits());
        let listed = rows.iter().filter(|r| r.event == SettlementEvent::Settlement && r.root == ticker).count() as i64;
        assert_eq!(listed, 2 * (settled.hi - settled.lo + 1));
        assert!(settled.hi - settled.lo + 1 >= 11);
        let ex = |s: &str, agent: &str| {
            rows.iter().find(|r| r.symbol == s && r.agent.as_deref() == Some(agent) && r.event != SettlementEvent::Settlement).cloned()
        };
        if close >= strike(&lo) + 0.01 {
            assert_eq!(ex(&lo, "a").unwrap().quantity, 4.0);
        }
        if close <= strike(&hi) - 0.01 {
            assert!(ex(&hi, "a").is_none());
        }
        if close <= strike(&put) - 0.01 {
            assert_eq!(ex(&put, "b").unwrap().event, SettlementEvent::Assignment);
        } else {
            assert!(ex(&put, "b").is_none());
        }
        // Positions and the expiry are gone; the next is listed.
        assert!(e.name_options.book.flow.iter().all(|(_, s, _, _)| !s.contains(".O0014.")));
        assert!(e.name_options.chains[0].expiries.iter().all(|x| x.expiry > front));
        assert_eq!(e.name_options.chains[0].expiries.len(), 6);
        assert!(e.option_quote(&lo).is_none());
        assert_eq!(e.exercise_violations().unwrap(), Vec::<String>::new());
    }

    #[test]
    fn the_construction_rows_hold_at_every_close_and_prices_do_not_move() {
        let mut on = engine(23, true);
        let mut off = engine(23, false);
        let mut b1 = SessionBuffer::new();
        let mut b2 = SessionBuffer::new();
        day(&mut on, 0, &mut b1);
        day(&mut off, 0, &mut b2);
        for d in 1..30 {
            trading_day(&mut on, d, &mut b1, |e| {
                // Write and buy across names and expiries, and exercise.
                for chain in 0..4 {
                    let slot = (d as usize + chain) % 3;
                    let n = atm(e, chain, slot);
                    let k = (d % 5) - 2;
                    let c = sym(e, chain, slot, Right::Call, n + k);
                    let p = sym(e, chain, slot, Right::Put, n - k);
                    let _ = e.submit_order("w", &c, Side::Sell, 3.0, None, None);
                    let _ = e.submit_order("w", &p, Side::Sell, 3.0, None, None);
                    let _ = e.submit_order("h", &c, Side::Buy, 2.0, None, None);
                    if d % 4 == 0 {
                        let held = e.name_position("h", &c);
                        if held > 0.0 {
                            e.exercise("h", &c, held).unwrap();
                        }
                    }
                }
            });
            day(&mut off, d, &mut b2);
            assert_eq!(on.exercise_violations().unwrap(), Vec::<String>::new(), "{d}");
            for (a, b) in on.companies.iter().zip(off.companies.iter()) {
                assert_eq!(a.stock.price.to_bits(), b.stock.price.to_bits(), "{d}: {}", a.ticker);
            }
        }
        // Words round-trip, and an engine with the switch off refuses them.
        let words = on.name_options_words().unwrap();
        let book = on.name_options_book_state().cloned();
        let exercise = on.name_options_exercise_state().map(|(i, e)| (i, e.to_vec()));
        let mut other = engine(99, true);
        other.set_name_options_state(Some(&words), book, exercise.clone()).unwrap();
        assert_eq!(other.name_options_words().unwrap(), words);
        assert_eq!(other.name_options_exercise_state().map(|(i, e)| (i, e.to_vec())), exercise);
        assert!(off.set_name_options_state(Some(&words), None, None).is_err());
        let mut short = NameOptionsState::default();
        assert!(short.set_words(&words[..words.len() - 1]).is_err());
    }

    #[test]
    fn the_position_limit_caps_a_position_and_its_waiting_orders() {
        let mut p = crate::params::PT_V21;
        for (name, value) in super::super::options::tests::ON.iter().chain(NAMES) {
            p = p.with_override(name, *value).unwrap();
        }
        p = p.with_override("option_position_limit", 0.1).unwrap();
        let mut e = Engine::with_params(
            31,
            roster(),
            create_initial_economy_state(&InitialEconomyOptions::default()),
            create_initial_central_bank_state(0),
            crate::sectors::keys().iter().map(|s| s.to_string()).collect(),
            p,
        );
        let mut buffer = SessionBuffer::new();
        day(&mut e, 0, &mut buffer);
        let call = sym(&e, 0, 1, Right::Call, atm(&e, 0, 1));
        assert_eq!(e.option_position_limit(&call), Some(20.0));
        let index = e.option_contracts()[0].symbol.clone();
        assert_eq!(e.option_position_limit(&index), Some(50.0));
        assert_eq!(e.option_position_limit("IDX.O0014.C1.00"), None);
        trading_day(&mut e, 1, &mut buffer, |e| {
            assert!(e.submit_order("a", &call, Side::Buy, 15.0, None, None).is_ok());
            let refused = e.submit_order("a", &call, Side::Buy, 6.0, None, None).unwrap_err();
            assert!(refused.contains("position limit"), "{refused}");
            // A waiting order counts as filled.
            let q = e.option_quote(&call).unwrap();
            assert!(e.submit_order("a", &call, Side::Buy, 5.0, Some(q.bid.unwrap_or(0.01)), None).is_ok());
            assert!(e.submit_order("a", &call, Side::Buy, 1.0, None, None).is_err());
            // Selling back toward zero always passes; another agent has its own limit.
            assert!(e.submit_order("a", &call, Side::Sell, 30.0, None, None).is_ok());
            assert!(e.submit_order("b", &call, Side::Sell, 20.0, None, None).is_ok());
            assert!(e.submit_order("b", &call, Side::Sell, 1.0, None, None).is_err());
        });
    }

    #[test]
    fn pro_rata_assignment_is_whole_and_sums_to_the_exercise() {
        assert_eq!(allocate(20.0, &[30.0]), vec![20.0]);
        assert_eq!(allocate(30.0, &[30.0, 10.0]), vec![23.0, 7.0]);
        assert_eq!(allocate(40.0, &[30.0, 10.0]), vec![30.0, 10.0]);
        // 10 over three shorts of 1: 3.33 each; the remainder goes to the
        // first by order.
        assert_eq!(allocate(10.0, &[10.0, 10.0, 10.0]), vec![4.0, 3.0, 3.0]);
        for x in 1..60 {
            let shorts = [7.0, 19.0, 3.0, 31.0];
            let a = allocate(x as f64, &shorts);
            assert_eq!(a.iter().sum::<f64>(), x as f64);
            for (got, s) in a.iter().zip(shorts) {
                assert!(*got <= s && got.fract() == 0.0);
                // Within one contract of the exact share.
                assert!((got - x as f64 * s / 60.0).abs() < 1.0);
            }
        }
        // Fractional positions share exactly.
        let a = allocate(1.5, &[0.5, 2.5]);
        assert!((a.iter().sum::<f64>() - 1.5).abs() < 1e-15);
        assert!((a[0] - 0.25).abs() < 1e-15);
        assert_eq!(allocate(5.0, &[]), Vec::<f64>::new());
    }
}