rustyschwab-types 0.2.0

Unofficial wire types for the Charles Schwab Trader and Market Data APIs
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
//! Trading types including orders, transactions, and execution reports.
//!
//! This module contains types for trading operations:
//!
//! - Order creation and management
//! - Order status and responses
//! - Transaction history
//! - Execution reports
//! - Query parameters for the order and transaction listings
//!   ([`OrdersParams`](crate::trading::OrdersParams),
//!   [`TransactionsParams`](crate::trading::TransactionsParams))
//!
//! # Building orders
//!
//! [`OrderRequest`](crate::trading::OrderRequest) is the strict outbound
//! shape. Its constructors assemble it the way Schwab's order examples are
//! written, so callers never hand-nest `childOrderStrategies`: single orders
//! start from a leg
//! ([`OrderRequestLeg::equity`](crate::trading::OrderRequestLeg::equity),
//! [`OrderRequestLeg::option`](crate::trading::OrderRequestLeg::option)) and
//! an order type ([`OrderRequest::market`](crate::trading::OrderRequest::market),
//! [`OrderRequest::limit`](crate::trading::OrderRequest::limit), …), default
//! to the `NORMAL` session and `DAY` duration, and take `with_*` setters;
//! [`OrderRequest::one_cancels_other`](crate::trading::OrderRequest::one_cancels_other),
//! [`OrderRequest::triggers`](crate::trading::OrderRequest::triggers) and
//! [`OrderRequest::bracket`](crate::trading::OrderRequest::bracket) compose
//! them. [`OptionSymbol`](crate::trading::OptionSymbol) formats and parses
//! OSI option symbols.
//! [`OrderRequest::validate`](crate::trading::OrderRequest::validate) checks
//! structure before submission.
//!
//! ```
//! use rustyschwab_types::Decimal;
//! use rustyschwab_types::trading::{OrderDuration, OrderInstruction, OrderRequest, OrderRequestLeg};
//!
//! let entry = OrderRequest::limit(
//!     OrderRequestLeg::equity(OrderInstruction::Buy, "AAPL", 10),
//!     Decimal::new(15025, 2),
//! )
//! .with_duration(OrderDuration::Gtc)
//! .with_tag("swing-1");
//! // Buy at 150.25; once filled, sell at 160 or stop out at 145.
//! let bracket = entry.bracket(Decimal::from(160), Decimal::from(145)).unwrap();
//! bracket.validate().unwrap();
//! ```

use crate::common::{AccountsInstrument, AssetType, PutCall};
use crate::validation::{ValidationError, ValidationError as VE, ValidationErrorKind as K};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use std::collections::HashMap;

mod builder;
mod payoff;
mod spec;
pub use builder::OptionSymbol;
pub use payoff::{DefinedRisk, defined_risk};
pub use spec::{InstrumentSpec, LegSpec, OrderSettings, OrderSpec};

/// An order as Schwab reports it. Tolerant: every enum decodes a value this
/// release does not know as `Unknown` (see [`Wire`](crate::common::Wire)),
/// and undocumented fields land in `extra`. Times are the broker's ISO-8601
/// strings, kept verbatim.
///
/// `session`, `duration` and `order_type` are `None` when Schwab omits
/// them, as it does on an `OCO` parent: nothing is inferred.
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Order {
    #[serde(default, deserialize_with = "crate::common::optional_id")]
    pub order_id: Option<String>,
    pub session: Option<OrderSession>,
    pub duration: Option<OrderDuration>,
    pub order_type: Option<OrderType>,
    pub complex_order_strategy_type: Option<ComplexOrderStrategyType>,
    pub quantity: Option<f64>,
    pub filled_quantity: Option<f64>,
    pub remaining_quantity: Option<f64>,
    pub requested_destination: Option<RequestedDestination>,
    pub destination_link_name: Option<String>,
    pub price: Option<f64>,
    #[serde(default)]
    pub order_leg_collection: Vec<OrderLeg>,
    pub order_strategy_type: Option<OrderStrategyType>,
    pub cancelable: Option<bool>,
    pub editable: Option<bool>,
    pub status: Option<OrderStatus>,
    pub status_description: Option<String>,
    pub entered_time: Option<String>,
    pub close_time: Option<String>,
    pub cancel_time: Option<String>,
    pub release_time: Option<String>,
    pub stop_price: Option<f64>,
    pub stop_price_link_basis: Option<PriceLinkBasis>,
    pub stop_price_link_type: Option<PriceLinkType>,
    pub stop_price_offset: Option<f64>,
    pub stop_type: Option<StopType>,
    pub price_link_basis: Option<PriceLinkBasis>,
    pub price_link_type: Option<PriceLinkType>,
    pub activation_price: Option<f64>,
    pub tax_lot_method: Option<TaxLotMethod>,
    pub special_instruction: Option<SpecialInstruction>,
    pub tag: Option<String>,
    #[serde(default)]
    pub child_order_strategies: Vec<Order>,
    #[serde(default)]
    pub replacing_order_collection: Vec<Order>,
    #[serde(default)]
    pub order_activity_collection: Vec<OrderActivity>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
    #[serde(default, deserialize_with = "crate::common::optional_id")]
    pub account_number: Option<String>,
}

#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
// One leg Schwab sends without a field must not fail the whole order list.
#[serde(rename_all = "camelCase", default)]
#[non_exhaustive]
pub struct OrderLeg {
    pub order_leg_type: OrderLegType,
    pub leg_id: Option<i64>,
    pub instrument: AccountsInstrument,
    pub instruction: OrderInstruction,
    pub position_effect: Option<PositionEffect>,
    pub quantity: f64,
    pub quantity_type: Option<QuantityType>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// One entry of `orderActivityCollection`: an execution (partial or full
/// fill) or an order action. The real payload carries more than the spec
/// documents (an `activityId`, `executionType: CANCELED`), so
/// `execution_type` stays a string and the rest lands in `extra`.
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct OrderActivity {
    pub activity_type: Option<OrderActivityType>,
    /// `FILL` is the only documented value; `CANCELED` has been observed.
    pub execution_type: Option<String>,
    pub quantity: Option<f64>,
    pub order_remaining_quantity: Option<f64>,
    #[serde(default)]
    pub execution_legs: Vec<ExecutionLeg>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum OrderActivityType {
    Execution,
    OrderAction,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

/// Per-leg detail of an [`OrderActivity`] execution.
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ExecutionLeg {
    pub leg_id: Option<i64>,
    pub price: Option<f64>,
    pub quantity: Option<f64>,
    pub mismarked_quantity: Option<f64>,
    pub instrument_id: Option<i64>,
    pub time: Option<String>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// How an order leg's `quantity` is denominated.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum QuantityType {
    AllShares,
    Dollars,
    Shares,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum OrderSession {
    #[default]
    Normal,
    Am,
    Pm,
    Seamless,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum OrderDuration {
    #[default]
    Day,
    #[serde(rename = "GOOD_TILL_CANCEL")]
    Gtc,
    FillOrKill,
    ImmediateOrCancel,
    EndOfWeek,
    EndOfMonth,
    NextEndOfMonth,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

/// `CABINET`, `NON_MARKETABLE` and `EXERCISE` appear on orders Schwab
/// reports back but cannot be submitted, so the request schema leaves them
/// out.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum OrderType {
    Market,
    Limit,
    Stop,
    StopLimit,
    TrailingStop,
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Cabinet,
    #[cfg_attr(feature = "schemars", schemars(skip))]
    NonMarketable,
    MarketOnClose,
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Exercise,
    TrailingStopLimit,
    NetDebit,
    NetCredit,
    NetZero,
    LimitOnClose,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum ComplexOrderStrategyType {
    None,
    Covered,
    Vertical,
    BackRatio,
    Calendar,
    Diagonal,
    Straddle,
    Strangle,
    CollarSynthetic,
    Butterfly,
    Condor,
    IronCondor,
    VerticalRoll,
    CollarWithStock,
    DoubleDiagonal,
    UnbalancedButterfly,
    UnbalancedCondor,
    UnbalancedIronCondor,
    UnbalancedVerticalRoll,
    MutualFundSwap,
    Custom,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

/// Only [`Single`](Self::Single), [`Oco`](Self::Oco), and
/// [`Trigger`](Self::Trigger) can be submitted; the rest appear on orders
/// Schwab reports back, and are left out of the request schema.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum OrderStrategyType {
    #[default]
    Single,
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Cancel,
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Recall,
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Pair,
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Flatten,
    #[cfg_attr(feature = "schemars", schemars(skip))]
    TwoDaySwap,
    #[cfg_attr(feature = "schemars", schemars(skip))]
    BlastAll,
    Oco,
    Trigger,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum OrderStatus {
    AwaitingParentOrder,
    AwaitingCondition,
    AwaitingStopCondition,
    AwaitingManualReview,
    Accepted,
    AwaitingUrOut,
    PendingActivation,
    Queued,
    Working,
    Rejected,
    PendingCancel,
    Canceled,
    PendingReplace,
    Replaced,
    Filled,
    Expired,
    New,
    AwaitingReleaseTime,
    PendingAcknowledgement,
    PendingRecall,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

impl OrderStatus {
    /// Schwab will not change the order again: it filled, was canceled,
    /// rejected, expired or replaced. Every other status, including one
    /// this crate does not know, may still fill.
    pub const fn is_terminal(self) -> bool {
        matches!(
            self,
            Self::Filled | Self::Canceled | Self::Rejected | Self::Expired | Self::Replaced
        )
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum OrderLegType {
    #[default]
    Equity,
    Option,
    Index,
    MutualFund,
    CashEquivalent,
    FixedIncome,
    Currency,
    CollectiveInvestment,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum OrderInstruction {
    #[default]
    Buy,
    Sell,
    BuyToCover,
    SellShort,
    BuyToOpen,
    BuyToClose,
    SellToOpen,
    SellToClose,
    Exchange,
    SellShortExempt,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum PositionEffect {
    Opening,
    Closing,
    #[default]
    Automatic,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

/// What a linked (trailing) price follows: `priceLinkBasis` and
/// `stopPriceLinkBasis`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum PriceLinkBasis {
    Manual,
    Base,
    Trigger,
    Last,
    Bid,
    Ask,
    AskBid,
    Mark,
    Average,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

/// How a linked price's offset is expressed: `priceLinkType` and
/// `stopPriceLinkType`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum PriceLinkType {
    Value,
    Percent,
    Tick,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

/// Which price a stop order triggers on (`stopType`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum StopType {
    Standard,
    Bid,
    Ask,
    Last,
    Mark,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum TaxLotMethod {
    Fifo,
    Lifo,
    HighCost,
    LowCost,
    AverageCost,
    SpecificLot,
    LossHarvester,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum SpecialInstruction {
    AllOrNone,
    DoNotReduce,
    AllOrNoneDoNotReduce,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

/// Routing venue for `requestedDestination`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum RequestedDestination {
    Inet,
    EcnArca,
    Cboe,
    Amex,
    Phlx,
    Ise,
    Box,
    Nyse,
    Nasdaq,
    Bats,
    C2,
    Auto,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

/// The answer to `POST .../previewOrder`: Schwab's verdict on an order
/// without placing it.
///
/// A preview Schwab would refuse still answers 200; read
/// [`is_rejected`](Self::is_rejected) (and the `rejects`) rather than the
/// HTTP status. Fields Schwab adds land in `extra`.
#[skip_serializing_none]
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", default)]
#[non_exhaustive]
pub struct OrderPreview {
    #[serde(deserialize_with = "crate::common::optional_id")]
    pub order_id: Option<String>,
    pub order_strategy: Option<PreviewOrderStrategy>,
    pub order_validation_result: OrderValidationResult,
    /// Commission and fee legs, as Schwab reports them.
    pub commission_and_fee: Option<serde_json::Value>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

impl OrderPreview {
    /// Whether Schwab would refuse the order: a `reject` finding, or the
    /// previewed strategy's status `REJECTED`.
    pub fn is_rejected(&self) -> bool {
        !self.order_validation_result.rejects.is_empty()
            || self
                .order_strategy
                .as_ref()
                .is_some_and(|s| s.status == Some(OrderStatus::Rejected))
    }
    /// The reasons Schwab gave for refusing it.
    pub fn rejection_reasons(&self) -> Vec<&str> {
        self.order_validation_result
            .rejects
            .iter()
            .filter_map(ValidationFinding::text)
            .collect()
    }
}

/// The order as Schwab previewed it: its status and projected balances;
/// the rest of the strategy is in `extra`.
#[skip_serializing_none]
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", default)]
#[non_exhaustive]
pub struct PreviewOrderStrategy {
    #[serde(deserialize_with = "crate::common::lenient")]
    pub status: Option<OrderStatus>,
    pub order_balance: Option<PreviewOrderBalance>,
    pub order_value: Option<f64>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// What the order would do to the account's balances.
#[skip_serializing_none]
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", default)]
#[non_exhaustive]
pub struct PreviewOrderBalance {
    pub order_value: Option<f64>,
    pub projected_available_fund: Option<f64>,
    pub projected_buying_power: Option<f64>,
    pub projected_commission: Option<f64>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// Schwab's validation findings, by severity.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", default)]
#[non_exhaustive]
pub struct OrderValidationResult {
    pub alerts: Vec<ValidationFinding>,
    pub accepts: Vec<ValidationFinding>,
    pub rejects: Vec<ValidationFinding>,
    pub reviews: Vec<ValidationFinding>,
    pub warns: Vec<ValidationFinding>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// One validation finding on a preview.
#[skip_serializing_none]
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", default)]
#[non_exhaustive]
pub struct ValidationFinding {
    pub validation_rule_name: Option<String>,
    pub message: Option<String>,
    pub activity_message: Option<String>,
    pub original_severity: Option<String>,
    pub override_name: Option<String>,
    pub override_severity: Option<String>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

impl ValidationFinding {
    /// The finding's text: `activityMessage`, else `message`.
    pub fn text(&self) -> Option<&str> {
        self.activity_message.as_deref().or(self.message.as_deref())
    }
}

/// Result of submitting an order via `POST .../orders` or `PUT .../orders/{id}`.
///
/// Schwab returns **201 Created with an empty body**; the only payload is a
/// `Location` header pointing at the new order resource. The order ID is the
/// last path segment of that URL. If the order filled immediately, Schwab may
/// omit the header entirely — hence both fields are optional.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct OrderSubmitResponse {
    /// Order ID parsed from the `Location` header's last path segment.
    pub order_id: Option<String>,
    /// Full `Location` header value, when present.
    pub location: Option<String>,
}

/// Query parameters for `GET .../orders` (one account) and `GET /orders`
/// (every linked account).
///
/// Both bounds are required by Schwab, which formats them as
/// [`schwab_timestamp`](crate::common::schwab_timestamp) on the wire and
/// lists only orders entered in the last 60 days, however far back the
/// window reaches. Callers that want the whole horizon should stay just
/// inside it; the SDK's own reads use 59 days.
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct OrdersParams {
    /// No orders entered before this instant are returned.
    #[serde(serialize_with = "crate::common::serialize_timestamp")]
    pub from_entered_time: DateTime<Utc>,
    /// No orders entered after this instant are returned.
    #[serde(serialize_with = "crate::common::serialize_timestamp")]
    pub to_entered_time: DateTime<Utc>,
    /// Page size (≥ 1); Schwab's default is 3000.
    pub max_results: Option<u32>,
    /// Only orders in this status.
    pub status: Option<OrderStatus>,
}

impl OrdersParams {
    /// Every order entered in the window, in Schwab's default page size.
    pub fn new(from_entered_time: DateTime<Utc>, to_entered_time: DateTime<Utc>) -> Self {
        Self {
            from_entered_time,
            to_entered_time,
            max_results: None,
            status: None,
        }
    }

    /// Check the request against Schwab's documented bounds.
    pub fn validate(&self) -> Result<(), ValidationError> {
        if self.from_entered_time > self.to_entered_time {
            return Err(VE::new(
                K::OutOfRange,
                Some("fromEnteredTime"),
                "fromEnteredTime must not be after toEnteredTime",
            ));
        }
        if self.max_results == Some(0) {
            return Err(VE::new(
                K::OutOfRange,
                Some("maxResults"),
                "maxResults must be at least 1",
            ));
        }
        crate::common::known("status", self.status.as_ref())
    }
}

/// A transaction as Schwab reports it: header fields plus one
/// [`TransferItem`] per instrument moved and per fee charged. There is no
/// top-level instrument or fee map; a trade's security leg and each of its
/// fees are separate items, and the fees are the ones whose
/// [`fee_type`](TransferItem::fee_type) is set.
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Transaction {
    pub activity_id: Option<i64>,
    pub time: Option<String>,
    pub user: Option<TransactionUser>,
    pub description: Option<String>,
    #[serde(default, deserialize_with = "crate::common::optional_id")]
    pub account_number: Option<String>,
    /// `Unknown` for a category this release does not know.
    #[serde(rename = "type")]
    pub type_: Option<TransactionType>,
    pub status: Option<TransactionStatus>,
    pub sub_account: Option<SubAccount>,
    pub trade_date: Option<String>,
    pub settlement_date: Option<String>,
    pub position_id: Option<i64>,
    #[serde(default, deserialize_with = "crate::common::optional_id")]
    pub order_id: Option<String>,
    pub net_amount: Option<f64>,
    pub activity_type: Option<TransactionActivityType>,
    #[serde(default)]
    pub transfer_items: Vec<TransferItem>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// Transaction category; the `type` of a [`Transaction`] and the value set
/// of the `types` filter on `GET .../transactions`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum TransactionType {
    Trade,
    ReceiveAndDeliver,
    DividendOrInterest,
    AchReceipt,
    AchDisbursement,
    CashReceipt,
    CashDisbursement,
    ElectronicFund,
    WireOut,
    WireIn,
    Journal,
    Memorandum,
    MarginCall,
    MoneyMarket,
    SmaAdjustment,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

impl TransactionType {
    /// Wire name used in the `types` query parameter.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Trade => "TRADE",
            Self::ReceiveAndDeliver => "RECEIVE_AND_DELIVER",
            Self::DividendOrInterest => "DIVIDEND_OR_INTEREST",
            Self::AchReceipt => "ACH_RECEIPT",
            Self::AchDisbursement => "ACH_DISBURSEMENT",
            Self::CashReceipt => "CASH_RECEIPT",
            Self::CashDisbursement => "CASH_DISBURSEMENT",
            Self::ElectronicFund => "ELECTRONIC_FUND",
            Self::WireOut => "WIRE_OUT",
            Self::WireIn => "WIRE_IN",
            Self::Journal => "JOURNAL",
            Self::Memorandum => "MEMORANDUM",
            Self::MarginCall => "MARGIN_CALL",
            Self::MoneyMarket => "MONEY_MARKET",
            Self::SmaAdjustment => "SMA_ADJUSTMENT",
            Self::Unknown => "UNKNOWN",
        }
    }
}

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

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum TransactionStatus {
    Valid,
    Invalid,
    Pending,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

/// Ledger a transaction settled against.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum SubAccount {
    Cash,
    Margin,
    Short,
    Div,
    Income,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum TransactionActivityType {
    ActivityCorrection,
    Execution,
    OrderAction,
    Transfer,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

/// One leg of a [`Transaction`]: a security or cash movement, or a fee.
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TransferItem {
    pub instrument: Option<TransactionInstrument>,
    /// Quantity moved (shares, contracts, or currency units).
    pub amount: Option<f64>,
    pub cost: Option<f64>,
    pub price: Option<f64>,
    /// Set on fee items only.
    pub fee_type: Option<FeeType>,
    /// `Unknown` when Schwab reports `UNKNOWN`.
    pub position_effect: Option<PositionEffect>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum FeeType {
    Commission,
    SecFee,
    StrFee,
    RFee,
    CdscFee,
    OptRegFee,
    AdditionalFee,
    MiscellaneousFee,
    Ftt,
    FuturesClearingFee,
    FuturesDeskOfficeFee,
    FuturesExchangeFee,
    FuturesGlobexFee,
    FuturesNfaFee,
    FuturesPitBrokerageFee,
    FuturesTransactionFee,
    LowProceedsCommission,
    BaseCharge,
    GeneralCharge,
    GstFee,
    TafFee,
    IndexOptionFee,
    TefraTax,
    StateTax,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TransactionUser {
    pub cd_domain_id: Option<String>,
    pub login: Option<String>,
    #[serde(rename = "type")]
    pub type_: Option<String>,
    pub user_id: Option<i64>,
    pub system_user_name: Option<String>,
    pub first_name: Option<String>,
    pub last_name: Option<String>,
    pub broker_rep_code: Option<String>,
}

/// The instrument on a [`TransferItem`] (`TransactionInstrument` in the
/// Trader API spec). The base fields plus the option, fixed-income and
/// future extensions are typed; the rest lands in `extra`.
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", default)]
#[non_exhaustive]
pub struct TransactionInstrument {
    pub asset_type: AssetType,
    pub symbol: String,
    pub cusip: Option<String>,
    pub description: Option<String>,
    pub instrument_id: Option<i64>,
    pub net_change: Option<f64>,
    /// Subtype whose value set depends on `asset_type` (`COMMON_STOCK`,
    /// `SWEEP_VEHICLE`, `VANILLA`, …).
    #[serde(rename = "type")]
    pub type_: Option<String>,
    /// Observed on live payloads (`ACTIVE`), undocumented in the spec.
    pub status: Option<String>,
    /// Observed on live payloads, undocumented in the spec.
    pub closing_price: Option<f64>,
    /// Options and futures.
    pub expiration_date: Option<String>,
    /// Options only. `None` when Schwab reports `UNKNOWN`.
    #[serde(default, deserialize_with = "crate::common::lenient")]
    pub put_call: Option<PutCall>,
    /// Options only.
    pub strike_price: Option<f64>,
    /// Options only.
    pub option_premium_multiplier: Option<i64>,
    /// Options only.
    pub underlying_symbol: Option<String>,
    /// Options only.
    pub underlying_cusip: Option<String>,
    /// Fixed income only.
    pub maturity_date: Option<String>,
    /// Fixed income only.
    pub factor: Option<f64>,
    /// Fixed income only.
    pub variable_rate: Option<f64>,
    /// Fixed income and futures.
    pub multiplier: Option<f64>,
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

/// Query parameters for `GET .../transactions`.
///
/// Both bounds and at least one type are required by Schwab, which formats
/// the bounds as [`schwab_timestamp`](crate::common::schwab_timestamp) on
/// the wire, joins the types with commas, and caps the window at one year.
/// Transaction history reaches back that far; the order list
/// ([`OrdersParams`]) reaches back only 60 days. The two limits are
/// different, and neither is enforced here: Schwab answers the request.
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct TransactionsParams {
    /// No transactions before this instant are returned.
    #[serde(serialize_with = "crate::common::serialize_timestamp")]
    pub start_date: DateTime<Utc>,
    /// No transactions after this instant are returned.
    #[serde(serialize_with = "crate::common::serialize_timestamp")]
    pub end_date: DateTime<Utc>,
    /// Transaction categories to include (at least one). Serializes as the
    /// comma-joined list the query string takes; deserializes from that or
    /// from a JSON array.
    #[serde(
        serialize_with = "serialize_transaction_types",
        deserialize_with = "deserialize_transaction_types"
    )]
    pub types: Vec<TransactionType>,
    /// Only activity in this symbol.
    pub symbol: Option<String>,
}

impl TransactionsParams {
    /// Every transaction of the given types in the window.
    pub fn new(
        start_date: DateTime<Utc>,
        end_date: DateTime<Utc>,
        types: impl IntoIterator<Item = TransactionType>,
    ) -> Self {
        Self {
            start_date,
            end_date,
            types: types.into_iter().collect(),
            symbol: None,
        }
    }

    /// Check the request against Schwab's documented bounds.
    pub fn validate(&self) -> Result<(), ValidationError> {
        if self.start_date > self.end_date {
            return Err(VE::new(
                K::OutOfRange,
                Some("startDate"),
                "startDate must not be after endDate",
            ));
        }
        if self.types.is_empty() {
            return Err(VE::new(
                K::Missing,
                Some("types"),
                "types must name at least one transaction type",
            ));
        }
        for t in &self.types {
            crate::common::known("types", Some(t))?;
        }
        if self.symbol.as_deref().is_some_and(|s| s.trim().is_empty()) {
            return Err(VE::new(
                K::Symbol,
                Some("symbol"),
                "symbol must not be blank",
            ));
        }
        Ok(())
    }
}

/// Comma-joined wire names, the only list encoding the query string takes.
fn serialize_transaction_types<S: serde::Serializer>(
    types: &[TransactionType],
    serializer: S,
) -> Result<S::Ok, S::Error> {
    let csv = types
        .iter()
        .map(|t| t.as_str())
        .collect::<Vec<_>>()
        .join(",");
    serializer.serialize_str(&csv)
}

/// Accept the comma-joined wire form back, so serialized params round-trip.
fn deserialize_transaction_types<'de, D: serde::Deserializer<'de>>(
    deserializer: D,
) -> Result<Vec<TransactionType>, D::Error> {
    use serde::de::IntoDeserializer;

    #[derive(Deserialize)]
    #[serde(untagged)]
    enum Types {
        List(Vec<TransactionType>),
        Csv(String),
    }

    match Types::deserialize(deserializer)? {
        Types::List(types) => Ok(types),
        Types::Csv(csv) => csv
            .split(',')
            .map(|name| TransactionType::deserialize(name.trim().into_deserializer()))
            .collect(),
    }
}

/// Outgoing orders have a strict schema, separate from tolerant broker responses.
/// Unsupported fields fail deserialization rather than silently altering a trade,
/// and decimals parse exactly (see [`exact_decimal`](crate::common::exact_decimal)).
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct OrderRequest {
    pub session: Option<OrderSession>,
    pub duration: Option<OrderDuration>,
    pub order_type: Option<OrderType>,
    pub complex_order_strategy_type: Option<ComplexOrderStrategyType>,
    /// The order's unit count, when present: a single leg trades exactly it,
    /// and each leg of a multi-leg order a whole multiple of it (a 1x2
    /// ratio of 5 has legs of 5 and 10). Sent as a JSON number.
    #[serde(
        default,
        deserialize_with = "crate::common::exact_decimal::option::deserialize",
        serialize_with = "crate::common::exact_decimal::option::serialize_number"
    )]
    #[cfg_attr(
        feature = "schemars",
        schemars(with = "Option<crate::common::QuantityNumber>")
    )]
    pub quantity: Option<rust_decimal::Decimal>,
    #[serde(
        default,
        deserialize_with = "crate::common::exact_decimal::option::deserialize",
        serialize_with = "crate::common::exact_decimal::option::serialize_string"
    )]
    #[cfg_attr(
        feature = "schemars",
        schemars(with = "Option<crate::common::DecimalString>")
    )]
    pub price: Option<rust_decimal::Decimal>,
    #[serde(
        default,
        deserialize_with = "crate::common::exact_decimal::option::deserialize",
        serialize_with = "crate::common::exact_decimal::option::serialize_string"
    )]
    #[cfg_attr(
        feature = "schemars",
        schemars(with = "Option<crate::common::DecimalString>")
    )]
    pub stop_price: Option<rust_decimal::Decimal>,
    pub stop_price_link_basis: Option<PriceLinkBasis>,
    pub stop_price_link_type: Option<PriceLinkType>,
    #[serde(
        default,
        deserialize_with = "crate::common::exact_decimal::option::deserialize",
        serialize_with = "crate::common::exact_decimal::option::serialize_string"
    )]
    #[cfg_attr(
        feature = "schemars",
        schemars(with = "Option<crate::common::DecimalString>")
    )]
    pub stop_price_offset: Option<rust_decimal::Decimal>,
    pub stop_type: Option<StopType>,
    pub price_link_basis: Option<PriceLinkBasis>,
    pub price_link_type: Option<PriceLinkType>,
    pub tax_lot_method: Option<TaxLotMethod>,
    pub tag: Option<String>,
    pub special_instruction: Option<SpecialInstruction>,
    pub requested_destination: Option<RequestedDestination>,
    /// Hold the order until this time (Schwab reports it
    /// `AWAITING_RELEASE_TIME` until then).
    #[serde(
        default,
        serialize_with = "crate::common::serialize_optional_timestamp"
    )]
    pub release_time: Option<DateTime<Utc>>,
    /// Cancel the order at this time if it has not filled.
    #[serde(
        default,
        serialize_with = "crate::common::serialize_optional_timestamp"
    )]
    pub cancel_time: Option<DateTime<Utc>>,
    /// The price at which a conditional order activates.
    #[serde(
        default,
        deserialize_with = "crate::common::exact_decimal::option::deserialize",
        serialize_with = "crate::common::exact_decimal::option::serialize_string"
    )]
    #[cfg_attr(
        feature = "schemars",
        schemars(with = "Option<crate::common::DecimalString>")
    )]
    pub activation_price: Option<rust_decimal::Decimal>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub order_leg_collection: Vec<OrderRequestLeg>,
    pub order_strategy_type: OrderStrategyType,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub child_order_strategies: Vec<OrderRequest>,
}

#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct OrderRequestLeg {
    pub instruction: OrderInstruction,
    /// Shares or contracts; whole numbers for equities and options. For a
    /// mutual fund, denominated as `quantity_type` says. Sent as a JSON number.
    #[serde(
        deserialize_with = "crate::common::exact_decimal::deserialize",
        serialize_with = "crate::common::exact_decimal::serialize_number"
    )]
    #[cfg_attr(feature = "schemars", schemars(with = "crate::common::QuantityNumber"))]
    pub quantity: rust_decimal::Decimal,
    pub instrument: OrderRequestInstrument,
    pub position_effect: Option<PositionEffect>,
    /// Mutual funds only: whether `quantity` is shares, dollars or the whole
    /// position.
    #[serde(default)]
    pub quantity_type: Option<QuantityType>,
    /// Mutual funds only: what happens to dividends and capital gains.
    #[serde(default)]
    pub div_cap_gains: Option<DivCapGains>,
    /// Mutual funds only: the fund an `EXCHANGE` moves into.
    #[serde(default)]
    pub to_symbol: Option<String>,
}

/// Dividend and capital-gain handling on a mutual-fund leg (`divCapGains`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum DivCapGains {
    Reinvest,
    Payout,
    #[serde(other)]
    #[cfg_attr(feature = "schemars", schemars(skip))]
    Unknown,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct OrderRequestInstrument {
    pub symbol: String,
    pub asset_type: AssetType,
}

/// The request that would place `order` again, to edit for a replacement:
/// its strategy, legs, prices and timing, without what Schwab assigned
/// (ID, status, fills) and without its tag, which belongs to the original
/// intent. Fails when a reported number has no exact decimal spelling.
impl TryFrom<&Order> for OrderRequest {
    type Error = ValidationError;

    fn try_from(order: &Order) -> Result<Self, ValidationError> {
        fn exact(field: &str, value: f64) -> Result<rust_decimal::Decimal, ValidationError> {
            crate::common::exact_decimal::parse(&value.to_string())
                .map_err(|e| VE::new(K::OutOfRange, Some(field), e))
        }
        let optional = |field: &str, value: Option<f64>| value.map(|v| exact(field, v)).transpose();
        let mut legs = Vec::with_capacity(order.order_leg_collection.len());
        for (i, leg) in order.order_leg_collection.iter().enumerate() {
            legs.push(OrderRequestLeg {
                instruction: leg.instruction,
                quantity: exact("quantity", leg.quantity)
                    .map_err(|e| e.within(&format!("orderLegCollection[{i}]")))?,
                instrument: OrderRequestInstrument {
                    symbol: leg.instrument.symbol.clone(),
                    asset_type: leg.instrument.asset_type,
                },
                position_effect: leg.position_effect,
                quantity_type: leg.quantity_type,
                div_cap_gains: None,
                to_symbol: None,
            });
        }
        let mut children = Vec::with_capacity(order.child_order_strategies.len());
        for (i, child) in order.child_order_strategies.iter().enumerate() {
            children.push(
                Self::try_from(child)
                    .map_err(|e| e.within(&format!("childOrderStrategies[{i}]")))?,
            );
        }
        let time = |value: &Option<String>| {
            value
                .as_deref()
                .and_then(|t| crate::common::parse_schwab_time(t).ok())
        };
        Ok(Self {
            session: order.session,
            duration: order.duration,
            order_type: order.order_type,
            complex_order_strategy_type: order.complex_order_strategy_type,
            quantity: optional("quantity", order.quantity)?,
            price: optional("price", order.price)?,
            stop_price: optional("stopPrice", order.stop_price)?,
            stop_price_link_basis: order.stop_price_link_basis,
            stop_price_link_type: order.stop_price_link_type,
            stop_price_offset: optional("stopPriceOffset", order.stop_price_offset)?,
            stop_type: order.stop_type,
            price_link_basis: order.price_link_basis,
            price_link_type: order.price_link_type,
            tax_lot_method: order.tax_lot_method,
            tag: None,
            special_instruction: order.special_instruction,
            requested_destination: order.requested_destination,
            release_time: time(&order.release_time),
            cancel_time: None,
            activation_price: optional("activationPrice", order.activation_price)?,
            order_leg_collection: legs,
            order_strategy_type: order.order_strategy_type.unwrap_or_default(),
            child_order_strategies: children,
        })
    }
}

crate::common::wire_enum!(
    QuantityType,
    OrderSession,
    OrderDuration,
    OrderType,
    ComplexOrderStrategyType,
    OrderStrategyType,
    OrderStatus,
    OrderLegType,
    OrderInstruction,
    PositionEffect,
    PriceLinkBasis,
    PriceLinkType,
    StopType,
    TaxLotMethod,
    SpecialInstruction,
    RequestedDestination,
    OrderActivityType,
    TransactionType,
    TransactionStatus,
    SubAccount,
    TransactionActivityType,
    FeeType,
    DivCapGains,
);

/// Strategies nest at most this many levels, the root counting as one.
const MAX_STRATEGY_DEPTH: usize = 8;

/// What an executable order type takes. `true` means required, `false`
/// forbidden; the trailing link fields are required together.
struct TypeRules {
    price: bool,
    stop_price: bool,
    trailing: bool,
    /// `priceLinkBasis`/`priceLinkType` (a linked limit) are allowed.
    price_link: bool,
    /// `stopType` is meaningful.
    stop_type: bool,
    /// Net-priced: needs two or more legs.
    net: bool,
}

impl OrderType {
    /// Submission rules for this type; `None` for types Schwab reports but
    /// does not accept on a new order.
    fn rules(self) -> Option<TypeRules> {
        let base = TypeRules {
            price: false,
            stop_price: false,
            trailing: false,
            price_link: false,
            stop_type: false,
            net: false,
        };
        Some(match self {
            Self::Market | Self::MarketOnClose => base,
            Self::Limit | Self::LimitOnClose => TypeRules {
                price: true,
                ..base
            },
            Self::Stop => TypeRules {
                stop_price: true,
                stop_type: true,
                ..base
            },
            Self::StopLimit => TypeRules {
                price: true,
                stop_price: true,
                stop_type: true,
                ..base
            },
            Self::TrailingStop => TypeRules {
                trailing: true,
                stop_type: true,
                ..base
            },
            Self::TrailingStopLimit => TypeRules {
                price: true,
                trailing: true,
                price_link: true,
                stop_type: true,
                ..base
            },
            Self::NetDebit | Self::NetCredit => TypeRules {
                price: true,
                net: true,
                ..base
            },
            Self::NetZero => TypeRules { net: true, ..base },
            Self::Cabinet | Self::NonMarketable | Self::Exercise | Self::Unknown => return None,
        })
    }
}

impl OrderRequest {
    /// Validates structure without silently rounding prices: every field
    /// the order type takes and none it does not, instructions that fit the
    /// asset, whole shares and contracts, canonical symbols, extended hours
    /// only for limit orders. Broker eligibility, buying power and
    /// asset-specific increments still require a preview.
    pub fn validate(&self) -> Result<(), ValidationError> {
        self.validate_depth(1)
    }

    fn validate_depth(&self, depth: usize) -> Result<(), ValidationError> {
        use crate::common::known;
        if depth > MAX_STRATEGY_DEPTH {
            return Err(VE::new(
                K::Structure,
                None,
                format!("Order strategies nest at most {MAX_STRATEGY_DEPTH} levels"),
            ));
        }
        known("session", self.session.as_ref())?;
        known("duration", self.duration.as_ref())?;
        known("orderType", self.order_type.as_ref())?;
        known(
            "complexOrderStrategyType",
            self.complex_order_strategy_type.as_ref(),
        )?;
        known("stopPriceLinkBasis", self.stop_price_link_basis.as_ref())?;
        known("stopPriceLinkType", self.stop_price_link_type.as_ref())?;
        known("stopType", self.stop_type.as_ref())?;
        known("priceLinkBasis", self.price_link_basis.as_ref())?;
        known("priceLinkType", self.price_link_type.as_ref())?;
        known("taxLotMethod", self.tax_lot_method.as_ref())?;
        known("specialInstruction", self.special_instruction.as_ref())?;
        known("requestedDestination", self.requested_destination.as_ref())?;
        known("orderStrategyType", Some(&self.order_strategy_type))?;
        for (name, value) in [
            ("quantity", self.quantity),
            ("price", self.price),
            ("stopPrice", self.stop_price),
            ("stopPriceOffset", self.stop_price_offset),
            ("activationPrice", self.activation_price),
        ] {
            if value.is_some_and(|v| v <= rust_decimal::Decimal::ZERO) {
                return Err(VE::new(
                    K::OutOfRange,
                    Some(name),
                    format!("{name} must be positive"),
                ));
            }
        }
        if let (Some(release), Some(cancel)) = (self.release_time, self.cancel_time)
            && cancel <= release
        {
            return Err(VE::new(
                K::Structure,
                Some("cancelTime"),
                "cancelTime must be after releaseTime",
            ));
        }
        match self.order_strategy_type {
            OrderStrategyType::Oco => self.validate_oco_parent()?,
            OrderStrategyType::Single | OrderStrategyType::Trigger => self.validate_executable()?,
            _ => {
                return Err(VE::new(
                    K::Unsupported,
                    Some("orderStrategyType"),
                    "Only SINGLE, OCO, and TRIGGER strategies can be submitted",
                ));
            }
        }
        for (i, child) in self.child_order_strategies.iter().enumerate() {
            child
                .validate_depth(depth + 1)
                .map_err(|e| e.within(&format!("childOrderStrategies[{i}]")))?;
        }
        Ok(())
    }

    /// An OCO parent only groups its two children; anything executable on
    /// it would be ignored or misread.
    fn validate_oco_parent(&self) -> Result<(), ValidationError> {
        let executable = self.session.is_some()
            || self.duration.is_some()
            || self.order_type.is_some()
            || self.complex_order_strategy_type.is_some()
            || self.quantity.is_some()
            || self.price.is_some()
            || self.stop_price.is_some()
            || self.stop_price_link_basis.is_some()
            || self.stop_price_link_type.is_some()
            || self.stop_price_offset.is_some()
            || self.stop_type.is_some()
            || self.price_link_basis.is_some()
            || self.price_link_type.is_some()
            || self.tax_lot_method.is_some()
            || self.special_instruction.is_some()
            || self.requested_destination.is_some()
            || self.release_time.is_some()
            || self.cancel_time.is_some()
            || self.activation_price.is_some()
            || !self.order_leg_collection.is_empty();
        if self.child_order_strategies.len() != 2 || executable {
            return Err(VE::new(
                K::Structure,
                None,
                "OCO must contain exactly two children and no executable parent",
            ));
        }
        Ok(())
    }

    fn validate_executable(&self) -> Result<(), ValidationError> {
        let missing = [
            ("orderType", self.order_type.is_none()),
            ("session", self.session.is_none()),
            ("duration", self.duration.is_none()),
            ("orderLegCollection", self.order_leg_collection.is_empty()),
        ]
        .into_iter()
        .find_map(|(field, absent)| absent.then_some(field));
        let (Some(kind), Some(session), Some(duration), None) =
            (self.order_type, self.session, self.duration, missing)
        else {
            return Err(VE::new(
                K::Missing,
                missing,
                "Executable orders require legs, session, duration, and orderType",
            ));
        };
        let name = serde_json::to_value(kind).unwrap_or_default();
        let name = name.as_str().unwrap_or("this order type");
        let rules = kind.rules().ok_or_else(|| {
            VE::new(
                K::Unsupported,
                Some("orderType"),
                format!("{name} orders cannot be submitted"),
            )
        })?;
        let field = |present: bool, required: bool, field: &str| -> Result<(), ValidationError> {
            match (present, required) {
                (false, true) => Err(VE::new(
                    K::Missing,
                    Some(field),
                    format!("{name} requires {field}"),
                )),
                (true, false) => Err(VE::new(
                    K::NotAllowed,
                    Some(field),
                    format!("{name} does not take {field}"),
                )),
                _ => Ok(()),
            }
        };
        field(self.price.is_some(), rules.price, "price")?;
        field(self.stop_price.is_some(), rules.stop_price, "stopPrice")?;
        for (present, f) in [
            (self.stop_price_offset.is_some(), "stopPriceOffset"),
            (self.stop_price_link_basis.is_some(), "stopPriceLinkBasis"),
            (self.stop_price_link_type.is_some(), "stopPriceLinkType"),
        ] {
            field(present, rules.trailing, f)?;
        }
        if self.price_link_basis.is_some() != self.price_link_type.is_some() {
            return Err(VE::new(
                K::Missing,
                Some(if self.price_link_basis.is_some() {
                    "priceLinkType"
                } else {
                    "priceLinkBasis"
                }),
                "priceLinkBasis and priceLinkType go together",
            ));
        }
        if self.price_link_basis.is_some() && !rules.price_link {
            field(true, false, "priceLinkBasis")?;
        }
        if self.stop_type.is_some() && !rules.stop_type {
            field(true, false, "stopType")?;
        }
        if rules.trailing {
            if self.stop_price_link_basis == Some(PriceLinkBasis::Manual) {
                return Err(VE::new(
                    K::OutOfRange,
                    Some("stopPriceLinkBasis"),
                    "A trailing stop cannot trail a MANUAL basis",
                ));
            }
            if self.stop_price_link_type == Some(PriceLinkType::Percent)
                && self
                    .stop_price_offset
                    .is_some_and(|o| o >= rust_decimal::Decimal::ONE_HUNDRED)
            {
                return Err(VE::new(
                    K::OutOfRange,
                    Some("stopPriceOffset"),
                    "A percentage trailing offset must be below 100",
                ));
            }
        }
        if rules.net && self.order_leg_collection.len() < 2 {
            return Err(VE::new(
                K::Structure,
                Some("orderLegCollection"),
                format!("{name} needs two or more legs"),
            ));
        }
        if session != OrderSession::Normal && kind != OrderType::Limit {
            return Err(VE::new(
                K::Structure,
                Some("session"),
                "Extended-hours sessions accept only LIMIT orders",
            ));
        }
        if matches!(kind, OrderType::MarketOnClose | OrderType::LimitOnClose)
            && (session != OrderSession::Normal || duration != OrderDuration::Day)
        {
            return Err(VE::new(
                K::Structure,
                Some(if session != OrderSession::Normal {
                    "session"
                } else {
                    "duration"
                }),
                format!("{name} requires the NORMAL session and DAY duration"),
            ));
        }
        let single = self.order_leg_collection.len() == 1;
        for (i, leg) in self.order_leg_collection.iter().enumerate() {
            leg.validate()
                .map_err(|e| e.within(&format!("orderLegCollection[{i}]")))?;
            // Schwab reports a complex order's unit count here: every leg
            // trades a whole multiple of it (a 1x2 ratio of 5 has legs of 5
            // and 10), and a single leg trades exactly it.
            let fits = |q: rust_decimal::Decimal| {
                if single {
                    q == leg.quantity
                } else {
                    (leg.quantity % q).is_zero()
                }
            };
            if self.quantity.is_some_and(|q| !fits(q)) {
                return Err(VE::new(
                    K::Structure,
                    Some("quantity"),
                    if single {
                        "quantity must equal the leg's quantity; size orders on the legs"
                    } else {
                        "quantity is the unit count; every leg's quantity must be a whole \
                         multiple of it"
                    },
                ));
            }
        }
        self.validate_leg_set(kind, session)?;
        match (
            self.order_strategy_type,
            self.child_order_strategies.is_empty(),
        ) {
            (OrderStrategyType::Single, false) => Err(VE::new(
                K::Structure,
                Some("childOrderStrategies"),
                "SINGLE cannot contain child strategies",
            )),
            (OrderStrategyType::Trigger, true) => Err(VE::new(
                K::Missing,
                Some("childOrderStrategies"),
                "TRIGGER requires child strategies",
            )),
            _ => Ok(()),
        }
    }

    /// Rules over the legs together, which Schwab enforces and answers 400
    /// for: options trade only in the regular session; several legs are
    /// priced as one (`NET_DEBIT`, `NET_CREDIT`, `NET_ZERO` or `MARKET`); no
    /// leg repeats another; a named strategy has its number of legs, on one
    /// underlying; a trailing offset in ticks is a whole number of ticks.
    fn validate_leg_set(
        &self,
        kind: OrderType,
        session: OrderSession,
    ) -> Result<(), ValidationError> {
        let legs = &self.order_leg_collection;
        let options: Vec<OptionSymbol> = legs
            .iter()
            .filter(|leg| leg.instrument.asset_type == AssetType::Option)
            .filter_map(|leg| leg.instrument.symbol.parse().ok())
            .collect();
        if !options.is_empty() && session != OrderSession::Normal {
            return Err(VE::new(
                K::Structure,
                Some("session"),
                "Options trade only in the NORMAL session",
            ));
        }
        if legs.len() > 1
            && !matches!(
                kind,
                OrderType::NetDebit | OrderType::NetCredit | OrderType::NetZero | OrderType::Market
            )
        {
            return Err(VE::new(
                K::Structure,
                Some("orderType"),
                "Several legs are priced together: NET_DEBIT, NET_CREDIT, NET_ZERO or MARKET",
            ));
        }
        for (i, leg) in legs.iter().enumerate() {
            if legs[..i].iter().any(|other| {
                other.instrument.symbol == leg.instrument.symbol
                    && other.instruction == leg.instruction
            }) {
                return Err(VE::new(
                    K::Structure,
                    Some(&format!("orderLegCollection[{i}]")),
                    "A leg repeats another; trade the combined quantity on one leg",
                ));
            }
        }
        use ComplexOrderStrategyType as C;
        if let Some(strategy) = self.complex_order_strategy_type {
            let expected = match strategy {
                C::Vertical
                | C::BackRatio
                | C::Calendar
                | C::Diagonal
                | C::Straddle
                | C::Strangle
                | C::CollarSynthetic
                | C::Covered => Some(2),
                C::Butterfly | C::UnbalancedButterfly | C::CollarWithStock => Some(3),
                C::Condor
                | C::IronCondor
                | C::VerticalRoll
                | C::DoubleDiagonal
                | C::UnbalancedCondor
                | C::UnbalancedIronCondor
                | C::UnbalancedVerticalRoll => Some(4),
                _ => None,
            };
            if expected.is_some_and(|n| n != legs.len()) {
                return Err(VE::new(
                    K::Structure,
                    Some("orderLegCollection"),
                    format!("{strategy:?} takes {} legs", expected.unwrap_or_default()),
                ));
            }
            // A named strategy is on one underlying; a covered or collared
            // position's stock is that underlying.
            if expected.is_some() {
                let mut underlyings = options.iter().map(OptionSymbol::underlying).chain(
                    legs.iter()
                        .filter(|leg| leg.instrument.asset_type != AssetType::Option)
                        .map(|leg| leg.instrument.symbol.as_str()),
                );
                if let Some(first) = underlyings.next()
                    && underlyings.any(|u| u != first)
                {
                    return Err(VE::new(
                        K::Structure,
                        Some("orderLegCollection"),
                        format!("{strategy:?} legs must share one underlying"),
                    ));
                }
            }
        }
        if self.stop_price_link_type == Some(PriceLinkType::Tick)
            && self.stop_price_offset.is_some_and(|o| !o.fract().is_zero())
        {
            return Err(VE::new(
                K::NotWhole,
                Some("stopPriceOffset"),
                "A trailing offset in ticks is a whole number of ticks",
            ));
        }
        Ok(())
    }

    /// Enforce a caller-supplied broker/asset tick size on every price and
    /// on a trailing offset expressed as a value; never round implicitly.
    pub fn validate_price_increment(
        &self,
        tick: rust_decimal::Decimal,
    ) -> Result<(), ValidationError> {
        self.validate()?;
        self.validate_ticks(tick)
    }

    fn validate_ticks(&self, tick: rust_decimal::Decimal) -> Result<(), ValidationError> {
        if tick <= rust_decimal::Decimal::ZERO {
            return Err(VE::new(
                K::OutOfRange,
                None,
                "Tick size must be finite and positive",
            ));
        }
        let value_offset = self
            .stop_price_offset
            .filter(|_| self.stop_price_link_type == Some(PriceLinkType::Value));
        for (field, price) in [
            ("price", self.price),
            ("stopPrice", self.stop_price),
            ("stopPriceOffset", value_offset),
        ] {
            if price.is_some_and(|price| price % tick != rust_decimal::Decimal::ZERO) {
                return Err(VE::new(
                    K::Tick,
                    Some(field),
                    "Price does not match the supplied tick size",
                ));
            }
        }
        for (i, child) in self.child_order_strategies.iter().enumerate() {
            child
                .validate_ticks(tick)
                .map_err(|e| e.within(&format!("childOrderStrategies[{i}]")))?;
        }
        Ok(())
    }
}

impl OrderRequestLeg {
    /// The leg-level rules [`OrderRequest::validate`] applies to each leg.
    ///
    /// Legs may be equities (and ETFs, which positions report as
    /// `COLLECTIVE_INVESTMENT`), options or mutual funds; other asset types
    /// are refused rather than sent unchecked.
    pub fn validate(&self) -> Result<(), ValidationError> {
        use crate::common::known;
        use OrderInstruction as I;
        known("instruction", Some(&self.instruction))?;
        known("positionEffect", self.position_effect.as_ref())?;
        known("instrument.assetType", Some(&self.instrument.asset_type))?;
        known("quantityType", self.quantity_type.as_ref())?;
        known("divCapGains", self.div_cap_gains.as_ref())?;
        let symbol = &self.instrument.symbol;
        if symbol.trim().is_empty()
            || symbol.trim() != symbol
            || symbol.bytes().any(|b| b.is_ascii_lowercase())
        {
            return Err(VE::new(
                K::Symbol,
                Some("instrument.symbol"),
                format!("Instrument symbol {symbol:?} must be non-empty, upper case and unpadded"),
            ));
        }
        if self.quantity <= rust_decimal::Decimal::ZERO {
            return Err(VE::new(
                K::OutOfRange,
                Some("quantity"),
                "Leg quantity must be positive",
            ));
        }
        let whole = self.quantity.fract().is_zero();
        let instruction = |allowed: bool, expected: &str| {
            if allowed {
                Ok(())
            } else {
                Err(VE::new(
                    K::NotAllowed,
                    Some("instruction"),
                    format!(
                        "{:?} does not fit this asset type; it takes {expected}",
                        self.instruction
                    ),
                ))
            }
        };
        let not_whole = |message: &str| VE::new(K::NotWhole, Some("quantity"), message);
        match self.instrument.asset_type {
            AssetType::Equity | AssetType::CollectiveInvestment => {
                instruction(
                    matches!(
                        self.instruction,
                        I::Buy | I::Sell | I::SellShort | I::BuyToCover | I::SellShortExempt
                    ),
                    "BUY, SELL, SELL_SHORT, SELL_SHORT_EXEMPT or BUY_TO_COVER",
                )?;
                if !whole {
                    return Err(not_whole("Equity orders are for whole shares"));
                }
            }
            AssetType::Option => {
                instruction(
                    matches!(
                        self.instruction,
                        I::BuyToOpen | I::BuyToClose | I::SellToOpen | I::SellToClose
                    ),
                    "BUY_TO_OPEN, BUY_TO_CLOSE, SELL_TO_OPEN or SELL_TO_CLOSE",
                )?;
                symbol
                    .parse::<OptionSymbol>()
                    .map_err(|e| VE::new(K::Symbol, Some("instrument.symbol"), e))?;
                if !whole {
                    return Err(not_whole("Option orders are for whole contracts"));
                }
            }
            AssetType::MutualFund => {
                instruction(
                    matches!(self.instruction, I::Buy | I::Sell | I::Exchange),
                    "BUY, SELL or EXCHANGE",
                )?;
            }
            _ => {
                return Err(VE::new(
                    K::Unsupported,
                    Some("instrument.assetType"),
                    "Order legs may be EQUITY, COLLECTIVE_INVESTMENT, OPTION or MUTUAL_FUND",
                ));
            }
        }
        if self.instrument.asset_type != AssetType::MutualFund {
            for (present, field) in [
                (self.quantity_type.is_some(), "quantityType"),
                (self.div_cap_gains.is_some(), "divCapGains"),
                (self.to_symbol.is_some(), "toSymbol"),
            ] {
                if present {
                    return Err(VE::new(
                        K::NotAllowed,
                        Some(field),
                        "quantityType, divCapGains and toSymbol apply to mutual funds only",
                    ));
                }
            }
        }
        if (self.instruction == I::Exchange) != self.to_symbol.is_some() {
            return Err(VE::new(
                K::Structure,
                Some("toSymbol"),
                "EXCHANGE takes a toSymbol, and only EXCHANGE does",
            ));
        }
        Ok(())
    }
}