ctrader-connector 0.3.10

Client library for the cTrader Open API and the cTrader FIX 4.4 API
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
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
//! Verification against the demo environment.
//!
//! The tests need the `BROKER_*` credentials in the process environment and fail when they are
//! absent. They share one lock so that only one of them talks to the server at a time.
//!
//! A refresh replaces the token pair, so the pair is read from the file named by
//! `BROKER_ENV_FILE` and the refresh test writes the new pair back there before it checks
//! anything else.

#[macro_use]
mod live_support;

use std::collections::{BTreeMap, BTreeSet};
use std::path::PathBuf;
use std::time::Duration;

use ctrader_connector::common::config::{
    ConfigurationOpenApi, ENV_ACCESS_TOKEN, ENV_REFRESH_TOKEN, Environment,
};
use ctrader_connector::common::decimals::{Price, Volume};
use ctrader_connector::common::errors::{
    ConnectorError, ErrorCode, SessionEndCause, SessionEnded, TokenError,
};
use ctrader_connector::common::ids::{AccountId, OrderId, PositionId, SymbolId};
use ctrader_connector::common::time::{TimeRange, Timestamp};
use ctrader_connector::open_api::account::AccessRights;
use ctrader_connector::open_api::client::{Account, OpenApiClient};
use ctrader_connector::open_api::connection::{Connection, ConnectionOptions, Event};
use ctrader_connector::open_api::depth::{DepthBook, DepthStream, DepthUpdate};
use ctrader_connector::open_api::executions::{ExecutionStream, TradeEvent};
use ctrader_connector::open_api::history::{CashFlow, CashFlowKind};
use ctrader_connector::open_api::margin::MarginChange;
use ctrader_connector::open_api::messages::{
    ProtoMessage, ProtoOaApplicationAuthReq, ProtoOaApplicationAuthRes, ProtoOaDepthEvent,
    ProtoOaExecutionEvent, ProtoOaGetAccountListByAccessTokenReq, ProtoOaGetTickDataReq,
    ProtoOaMarginChangedEvent, ProtoOaSpotEvent, ProtoOaVersionReq,
};
use ctrader_connector::open_api::payload::Payload;
use ctrader_connector::open_api::quotes::QuoteSide;
use ctrader_connector::open_api::symbols::{Symbol, SymbolsParams};
use ctrader_connector::open_api::ticks::TickDataParams;
use ctrader_connector::open_api::trading::{
    Comment, Deal, DealStatus, Execution, ExecutionType, Label, NewOrder, Order, OrderAmendment,
    OrderStatus, OrderType, Position, PositionAmendment, PositionStatus, TimeInForce, TradeSide,
};
use ctrader_connector::open_api::transport::{Connect, Framed, Tls};
use ctrader_connector::open_api::trendbars::{Period, TrendbarsParams};
use prost::Message;
use rust_decimal::Decimal;
use tokio::sync::broadcast;
use tokio::time::timeout;

fn veil(accounts: &[Account]) {
    for account in accounts {
        live_support::veil(account.id);
        if let Some(login) = account.login {
            live_support::veil(login);
        }
    }
}

const STEP: Duration = Duration::from_secs(20);

fn env_file() -> PathBuf {
    let path = PathBuf::from(std::env::var("BROKER_ENV_FILE").unwrap_or_else(|_| {
        panic!("live tests need BROKER_ENV_FILE to name the file that holds the token pair")
    }));
    assert!(
        path.is_absolute(),
        "BROKER_ENV_FILE must be an absolute path"
    );
    path
}

fn file_value(text: &str, key: &str) -> Option<String> {
    let prefix = format!("export {key}=");
    text.lines()
        .find_map(|line| line.strip_prefix(&prefix))
        .map(|value| value.trim().trim_matches('"').to_owned())
        .filter(|value| !value.is_empty())
}

fn store_value(text: &str, key: &str, value: &str) -> String {
    let prefix = format!("export {key}=");
    let mut stored = String::with_capacity(text.len() + value.len());
    for line in text.split_inclusive('\n') {
        match line.strip_prefix(&prefix) {
            Some(old) => {
                let quote = if old.trim_start().starts_with('"') {
                    "\""
                } else {
                    ""
                };
                let end = if line.ends_with('\n') { "\n" } else { "" };
                for part in [prefix.as_str(), quote, value, quote, end] {
                    stored.push_str(part);
                }
            }
            None => stored.push_str(line),
        }
    }
    stored
}

#[test]
fn env_file_lines_are_rewritten_in_place() {
    let text = "export A=\"1\"\nexport BROKER_ACCESS_TOKEN=\"old\"\nexport B=2\nexport BROKER_REFRESH_TOKEN=old\n";
    assert_eq!(file_value(text, ENV_ACCESS_TOKEN).as_deref(), Some("old"));
    assert_eq!(file_value(text, "MISSING"), None);
    let stored = store_value(text, ENV_ACCESS_TOKEN, "new");
    let stored = store_value(&stored, ENV_REFRESH_TOKEN, "next");
    assert_eq!(
        stored,
        "export A=\"1\"\nexport BROKER_ACCESS_TOKEN=\"new\"\nexport B=2\nexport BROKER_REFRESH_TOKEN=next\n"
    );
    assert_eq!(store_value("export X=1", "X", "2"), "export X=2");
}

fn configuration() -> ConfigurationOpenApi {
    let stored = std::fs::read_to_string(env_file())
        .unwrap_or_else(|error| panic!("the file named by BROKER_ENV_FILE is unreadable: {error}"));
    let configuration = ConfigurationOpenApi::from_lookup(|key| {
        let current = if key == ENV_ACCESS_TOKEN || key == ENV_REFRESH_TOKEN {
            file_value(&stored, key)
        } else {
            None
        };
        current.or_else(|| std::env::var(key).ok())
    })
    .unwrap_or_else(|error| {
        panic!("live tests need the BROKER_* credentials in the environment: {error}")
    });
    assert_eq!(
        configuration.environment(),
        Environment::Demo,
        "live tests run against the demo environment only"
    );
    configuration
}

#[tokio::test]
async fn live_framed_round_trip_over_tls() {
    let _guard = live_support::enter().await;
    let configuration = configuration();
    let tls = Tls::new().expect("tls configuration");
    let stream = tls
        .connect(configuration.host(), configuration.port())
        .await
        .expect("connect to the demo host");
    let mut framed = Framed::new(stream);
    let request = ProtoOaApplicationAuthReq {
        payload_type: None,
        client_id: configuration.client_id().as_str().to_owned(),
        client_secret: configuration.client_secret().expose().to_owned(),
    };
    framed
        .send(&ProtoMessage {
            payload_type: 2100,
            payload: Some(request.encode_to_vec()),
            client_msg_id: Some("round-trip".to_owned()),
        })
        .await
        .expect("send");
    let reply = timeout(STEP, framed.receive())
        .await
        .expect("reply in time")
        .expect("reply");
    say!(
        "live round trip: payload type {} id {:?} body {} bytes",
        reply.payload_type,
        reply.client_msg_id,
        reply.payload.as_ref().map_or(0, Vec::len)
    );
    assert_eq!(reply.payload_type, 2101);
    assert_eq!(reply.client_msg_id.as_deref(), Some("round-trip"));
    ProtoOaApplicationAuthRes::decode(reply.payload.unwrap_or_default().as_slice())
        .expect("application authorisation response");
    framed.close().await;
}

async fn open(configuration: &ConfigurationOpenApi) -> Connection {
    let tls = Tls::new().expect("tls configuration");
    Connection::open(
        tls,
        configuration.host(),
        configuration.port(),
        ConnectionOptions::from(configuration),
    )
    .await
    .expect("connect to the demo host")
}

#[tokio::test]
async fn live_concurrent_requests_and_heartbeats_over_an_idle_period() {
    let _guard = live_support::enter().await;
    let configuration = configuration();
    let connection = open(&configuration).await;
    connection
        .request(&ProtoOaApplicationAuthReq {
            payload_type: None,
            client_id: configuration.client_id().as_str().to_owned(),
            client_secret: configuration.client_secret().expose().to_owned(),
        })
        .await
        .expect("application authorisation");
    let version = ProtoOaVersionReq::default();
    let accounts = ProtoOaGetAccountListByAccessTokenReq {
        payload_type: None,
        access_token: configuration.access_token().expose().to_owned(),
    };
    let (v1, a1, v2, a2, v3, a3) = tokio::join!(
        connection.request(&version),
        connection.request(&accounts),
        connection.request(&version),
        connection.request(&accounts),
        connection.request(&version),
        connection.request(&accounts),
    );
    let versions: Vec<String> = [v1, v2, v3]
        .into_iter()
        .map(|response| response.expect("version").version)
        .collect();
    let counts: Vec<usize> = [a1, a2, a3]
        .into_iter()
        .map(|response| response.expect("account list").ctid_trader_account.len())
        .collect();
    say!("live concurrent requests: versions {versions:?} account counts {counts:?}");
    assert!(versions.iter().all(|text| !text.is_empty()));
    assert!(counts.iter().all(|count| *count == counts[0] && *count > 0));
    let idle = Duration::from_secs(45);
    tokio::time::sleep(idle).await;
    let stats = connection.stats();
    assert!(stats.heartbeats_received >= 1);
    assert_eq!(
        stats.reconnects, 0,
        "an idle connection must outlast the period of silence allowed by default"
    );
    let after = timeout(STEP, connection.request(&version))
        .await
        .expect("reply in time")
        .expect("request after idling");
    say!(
        "live idle {idle:?}: heartbeats sent {} received {} reconnects {} then version {}",
        stats.heartbeats_sent,
        stats.heartbeats_received,
        stats.reconnects,
        after.version
    );
    assert!(stats.heartbeats_sent >= 3);
    connection.shutdown().await;
}

fn code(outcome: Result<(), ConnectorError>) -> Option<ErrorCode> {
    match outcome {
        Err(ConnectorError::Server(error)) => Some(error.code),
        _ => None,
    }
}

async fn demo_account(client: &OpenApiClient) -> Account {
    let accounts = client.accounts().await.expect("account list");
    veil(&accounts);
    let demo: Vec<&Account> = accounts.iter().filter(|a| !a.is_live).collect();
    say!(
        "live accounts: {} in total, {} demo",
        accounts.len(),
        demo.len()
    );
    **demo
        .first()
        .expect("the access token grants at least one demo account")
}

#[tokio::test]
async fn live_application_and_demo_account_are_authorised() {
    let _guard = live_support::enter().await;
    let client = OpenApiClient::connect(configuration())
        .await
        .expect("connect to the demo host");
    client
        .authorise_application()
        .await
        .expect("application authorisation");
    let account = demo_account(&client).await;
    client
        .authorise_account(account.id)
        .await
        .expect("account authorisation");
    let named = format!("account {} of login {:?}", account.id, account.login);
    let hidden = live_support::veiled(&named);
    assert!(!hidden.contains(&account.id.to_string()));
    assert!(
        account
            .login
            .is_none_or(|login| !hidden.contains(&login.to_string()))
    );
    assert!(hidden.contains(live_support::VEIL));
    say!("live authorisation: application and {hidden} authorised");
    client.shutdown().await;
}

#[tokio::test]
async fn live_bad_secret_yields_the_typed_error() {
    let _guard = live_support::enter().await;
    let good = configuration();
    let bad = ConfigurationOpenApi::demo()
        .host(good.host())
        .port(good.port())
        .client_id(good.client_id().as_str())
        .client_secret("not-the-secret-of-this-application")
        .access_token(good.access_token().expose())
        .build()
        .expect("configuration");
    let client = OpenApiClient::connect(bad)
        .await
        .expect("connect to the demo host");
    let refused = code(client.authorise_application().await);
    say!("live bad secret: {refused:?}");
    assert_eq!(refused, Some(ErrorCode::ChClientAuthFailure));
    client.shutdown().await;
}

fn writable_token_file() -> (PathBuf, String) {
    let path = env_file();
    let text = std::fs::read_to_string(&path).unwrap_or_else(|error| {
        panic!("refusing to refresh: the token file is unreadable: {error}")
    });
    for key in [ENV_ACCESS_TOKEN, ENV_REFRESH_TOKEN] {
        assert!(
            file_value(&text, key).is_some(),
            "refusing to refresh: the token file has no `export {key}=` line"
        );
    }
    std::fs::OpenOptions::new()
        .write(true)
        .open(&path)
        .unwrap_or_else(|error| {
            panic!("refusing to refresh: the token file is not writable: {error}")
        });
    (path, text)
}

async fn holder(configuration: ConfigurationOpenApi) -> (OpenApiClient, AccountId) {
    let client = OpenApiClient::connect(configuration)
        .await
        .expect("connect to the demo host");
    client
        .authorise_application()
        .await
        .expect("application authorisation");
    let held = demo_account(&client).await.id;
    client
        .authorise_account(held)
        .await
        .expect("account authorisation with the old token");
    (client, held)
}

async fn session_end(events: &mut broadcast::Receiver<Event>) -> Option<SessionEnded> {
    timeout(STEP, async {
        loop {
            match events.recv().await {
                Ok(Event::SessionEnded(ended)) => break Some(ended),
                Ok(_) => {}
                Err(_) => break None,
            }
        }
    })
    .await
    .ok()
    .flatten()
}

#[tokio::test]
async fn live_refresh_rotates_the_pair_and_the_new_token_authorises() {
    let _guard = live_support::enter().await;
    let (path, text) = writable_token_file();
    let before = configuration();
    let client = OpenApiClient::connect(before.clone())
        .await
        .expect("connect to the demo host");
    client
        .authorise_application()
        .await
        .expect("application authorisation");
    let (old, held) = holder(before.clone()).await;
    let mut events = old.events();
    let pair = match client.refresh_token().await {
        Ok(pair) => pair,
        Err(error) => panic!("live refresh failed, the stored pair is unchanged: {error}"),
    };
    let stored = store_value(&text, ENV_ACCESS_TOKEN, pair.access.expose());
    let stored = store_value(&stored, ENV_REFRESH_TOKEN, pair.refresh.expose());
    std::fs::write(&path, stored)
        .unwrap_or_else(|error| panic!("the new token pair could not be stored: {error}"));
    let after = configuration();
    let persisted =
        after.access_token() == &pair.access && after.refresh_token() == Some(&pair.refresh);
    let rotated =
        before.access_token() != &pair.access && before.refresh_token() != Some(&pair.refresh);
    client.shutdown().await;
    let ended = session_end(&mut events).await;
    let later = old.trader(held).await.map(|_| ());
    old.shutdown().await;
    let fresh = OpenApiClient::connect(after)
        .await
        .expect("connect to the demo host");
    fresh
        .authorise_application()
        .await
        .expect("application authorisation");
    let account = demo_account(&fresh).await;
    let authorised = fresh.authorise_account(account.id).await;
    say!(
        "live refresh: token type {} expires in {} s, rotated {rotated}, persisted {persisted}, account with the new token: {}; old session: {}; later request on it: {}",
        pair.token_type,
        pair.expires_in.as_secs(),
        authorised.as_ref().map_or_else(
            |error| live_support::veiled(&error.to_string()),
            |()| "authorised".to_owned()
        ),
        ended.as_ref().map_or_else(
            || "no event".to_owned(),
            |ended| live_support::veiled(&ended.cause.to_string())
        ),
        later.as_ref().map_or_else(
            |error| live_support::veiled(&error.to_string()),
            |()| "answered".to_owned()
        )
    );
    assert!(persisted, "the stored pair differs from the pair received");
    assert!(
        rotated,
        "the refresh returned a token of the pair it was asked to replace"
    );
    assert!(pair.expires_in > Duration::from_secs(60));
    let ended = ended.expect("the session opened with the old token was not reported as ended");
    assert_eq!(ended.account, Some(held));
    assert!(
        matches!(ended.cause, SessionEndCause::TokenInvalidated { .. }),
        "{}",
        live_support::veiled(&ended.cause.to_string())
    );
    let Err(ConnectorError::SessionEnded(refused)) = later else {
        panic!("a request on the ended session was not refused as such");
    };
    assert_eq!(refused.account, Some(held));
    assert!(
        matches!(
            refused.cause,
            SessionEndCause::TokenInvalidated { .. } | SessionEndCause::AccountDisconnected
        ),
        "{}",
        live_support::veiled(&refused.cause.to_string())
    );
    assert!(
        authorised.is_ok(),
        "the new token did not authorise the account"
    );
    fresh.shutdown().await;
}

#[tokio::test]
async fn live_refresh_over_http_with_an_unknown_token_is_refused() {
    let _guard = live_support::enter().await;
    let good = configuration();
    let unknown = ConfigurationOpenApi::demo()
        .host(good.host())
        .port(good.port())
        .client_id(good.client_id().as_str())
        .client_secret(good.client_secret().expose())
        .access_token(good.access_token().expose())
        .refresh_token("not-a-refresh-token-of-this-application")
        .token_url(good.token_url())
        .build()
        .expect("configuration");
    let client = OpenApiClient::connect(unknown)
        .await
        .expect("connect to the demo host");
    let outcome = client.refresh_token_over_http().await.map(|_| ());
    say!("live refresh over HTTP with an unknown token: {outcome:?}");
    assert!(
        matches!(
            outcome,
            Err(ConnectorError::Token(
                TokenError::Refused { .. } | TokenError::Status(_)
            ))
        ),
        "{outcome:?}"
    );
    assert!(client.access_token() == *good.access_token());
    client.shutdown().await;
}

async fn next_spot(
    events: &mut broadcast::Receiver<Event>,
    symbols: &[SymbolId],
) -> ProtoOaSpotEvent {
    timeout(STEP, async {
        loop {
            let event = events.recv().await.expect("event");
            if let Some(spot) = event.decode::<ProtoOaSpotEvent>() {
                if symbols.iter().any(|symbol| symbol.get() == spot.symbol_id) {
                    return spot;
                }
            }
        }
    })
    .await
    .unwrap_or_else(|_| {
        panic!("none of {QUOTING_CANDIDATES:?} sent a spot event in {STEP:?}; the markets may be closed")
    })
}

#[tokio::test]
async fn live_forced_disconnect_recovers_authorisation_and_spot_subscription() {
    let _guard = live_support::enter().await;
    let client = OpenApiClient::connect(configuration())
        .await
        .expect("connect to the demo host");
    client
        .authorise_application()
        .await
        .expect("application authorisation");
    let account = demo_account(&client).await;
    client
        .authorise_account(account.id)
        .await
        .expect("account authorisation");
    let symbols: Vec<SymbolId> = candidates(&client, &account)
        .await
        .into_iter()
        .map(|(id, _)| id)
        .collect();
    let mut events = client.events();
    client
        .subscribe_spots(account.id, &symbols)
        .await
        .expect("spot subscription");
    let before = next_spot(&mut events, &symbols).await;
    client
        .connection()
        .force_reconnect()
        .await
        .expect("forced reconnection");
    let mut disconnected = false;
    let mut failures = Vec::new();
    let attempt = timeout(Duration::from_secs(90), async {
        loop {
            match events.recv().await.expect("event") {
                Event::Disconnected(_) => disconnected = true,
                Event::ReconnectFailed { reason, .. } => failures.push(reason.to_string()),
                Event::ReplayFailed { key, error } => {
                    failures.push(live_support::veiled(&format!("{key:?}: {error}")));
                }
                Event::Reconnected { attempt } => break attempt,
                _ => {}
            }
        }
    })
    .await
    .expect("reconnection in time");
    let after = next_spot(&mut events, &symbols).await;
    let version = client.version().await.expect("version after reconnection");
    let reconnects = client.connection().stats().reconnects;
    say!(
        "live reconnect: disconnected {disconnected}, reconnected on attempt {attempt}, failures {failures:?}, reconnects {reconnects}, spot for symbol {} before {} and for symbol {} after {}, version {version}",
        before.symbol_id,
        before.bid.is_some() || before.ask.is_some(),
        after.symbol_id,
        after.bid.is_some() || after.ask.is_some()
    );
    assert!(disconnected);
    assert!(failures.is_empty(), "{failures:?}");
    assert_eq!(reconnects, 1);
    client.shutdown().await;
}

#[tokio::test]
async fn live_burst_above_the_limit_completes_without_a_limit_error() {
    const BURST: usize = 120;
    let _guard = live_support::enter().await;
    let client = OpenApiClient::connect(configuration())
        .await
        .expect("connect to the demo host");
    client
        .authorise_application()
        .await
        .expect("application authorisation");
    let started = std::time::Instant::now();
    let tasks: Vec<_> = (0..BURST)
        .map(|_| {
            let client = client.clone();
            tokio::spawn(async move { client.version().await })
        })
        .collect();
    let mut failures = Vec::new();
    let mut answered = 0_usize;
    for task in tasks {
        match task.await.expect("task") {
            Ok(_) => answered += 1,
            Err(error) => failures.push(error),
        }
    }
    let elapsed = started.elapsed();
    let stats = client.connection().stats();
    let limited = failures
        .iter()
        .filter(|error| {
            matches!(
                error,
                ConnectorError::Server(refusal) if matches!(
                    refusal.code,
                    ErrorCode::RequestFrequencyExceeded | ErrorCode::BlockedPayloadType
                )
            )
        })
        .count();
    say!(
        "live burst: {answered} of {BURST} version requests answered in {elapsed:.1?}, failures {failures:?}, limit errors {limited}, requests sent {}, throttle retries {}",
        stats.requests_sent,
        stats.throttle_retries
    );
    assert_eq!(limited, 0, "a limit error reached the caller: {failures:?}");
    assert!(failures.is_empty(), "{failures:?}");
    assert_eq!(answered, BURST, "{answered} of {BURST} requests answered");
    let burst = u64::try_from(BURST).expect("burst size");
    assert_eq!(
        stats.requests_sent,
        burst + 1 + stats.throttle_retries,
        "every request is sent once, and once more for each retry after a limit error"
    );
    assert!(elapsed >= Duration::from_secs(2));
    client.shutdown().await;
}

async fn authorised() -> (OpenApiClient, Account) {
    let client = OpenApiClient::connect(configuration())
        .await
        .expect("connect to the demo host");
    client
        .authorise_application()
        .await
        .expect("application authorisation");
    let account = demo_account(&client).await;
    client
        .authorise_account(account.id)
        .await
        .expect("account authorisation");
    (client, account)
}

#[tokio::test]
async fn live_accounts_assets_symbols_and_details_parse_into_typed_values() {
    let _guard = live_support::enter().await;
    let (client, account) = authorised().await;
    let assets = client.assets(account.id).await.expect("assets");
    let list = client
        .symbols(SymbolsParams::new(account.id).include_archived(true))
        .await
        .expect("symbols");
    let named = |name: &str| {
        list.symbols
            .iter()
            .find(|symbol| symbol.name.as_deref() == Some(name))
            .unwrap_or_else(|| panic!("the demo account lists no symbol named {name}"))
    };
    let light = named("EURUSD");
    let ids = [light.id, named("BTCUSD").id];
    let details = client
        .symbol_details(account.id, &ids)
        .await
        .expect("symbol details");
    let asset_name = |id| {
        assets
            .iter()
            .find(|asset| Some(asset.id) == id)
            .map(|asset| asset.name.clone())
    };
    say!(
        "live reference data: {} assets, {} symbols ({} enabled, {} archived), EURUSD id {} base {:?} quote {:?}",
        assets.len(),
        list.symbols.len(),
        list.symbols.iter().filter(|symbol| symbol.enabled).count(),
        list.archived.len(),
        light.id,
        asset_name(light.base_asset),
        asset_name(light.quote_asset),
    );
    for symbol in &details {
        say!(
            "live symbol details: id {} digits {} pip position {} volume min {:?} max {:?} step {:?} lot {:?} mode {:?} units {:?}",
            symbol.id,
            symbol.digits,
            symbol.pip_position,
            symbol.min_volume.map(|volume| volume.to_string()),
            symbol.max_volume.map(|volume| volume.to_string()),
            symbol.step_volume.map(|volume| volume.to_string()),
            symbol.lot_size.map(|volume| volume.to_string()),
            symbol.trading_mode,
            symbol.measurement_units,
        );
    }
    assert!(!assets.is_empty());
    assert!(list.symbols.len() > 1);
    assert_eq!(asset_name(light.base_asset).as_deref(), Some("EUR"));
    assert_eq!(asset_name(light.quote_asset).as_deref(), Some("USD"));
    assert_eq!(details.len(), 2);
    let euro = details
        .iter()
        .find(|symbol| symbol.id == light.id)
        .expect("details of EURUSD");
    assert_eq!(euro.digits, 5);
    assert_eq!(euro.pip_position, 4);
    assert!(euro.min_volume <= euro.max_volume);
    assert!(euro.min_volume.is_some() && euro.step_volume.is_some());
    client.shutdown().await;
}

const QUOTING_CANDIDATES: [&str; 3] = ["BTCUSD", "ETHUSD", "EURUSD"];

async fn candidates(client: &OpenApiClient, account: &Account) -> Vec<(SymbolId, String)> {
    let list = client
        .symbols(SymbolsParams::new(account.id))
        .await
        .expect("symbols");
    let found: Vec<(SymbolId, String)> = QUOTING_CANDIDATES
        .iter()
        .filter_map(|name| {
            list.symbols
                .iter()
                .find(|symbol| symbol.enabled && symbol.name.as_deref() == Some(*name))
                .map(|symbol| (symbol.id, (*name).to_owned()))
        })
        .collect();
    assert!(
        !found.is_empty(),
        "the demo account lists none of {QUOTING_CANDIDATES:?}"
    );
    found
}

#[tokio::test]
async fn live_quotes_arrive_while_subscribed_and_stop_after_unsubscribing() {
    let _guard = live_support::enter().await;
    let (client, account) = authorised().await;
    let candidates = candidates(&client, &account).await;
    let ids: Vec<SymbolId> = candidates.iter().map(|(id, _)| *id).collect();
    let mut quotes = client
        .subscribe_quotes(account.id, &ids)
        .await
        .expect("spot subscription");
    let mut received = 0_usize;
    let mut sample = None;
    let window = tokio::time::Instant::now() + Duration::from_secs(60);
    while received < 5 || sample.is_none() {
        let quote = tokio::time::timeout_at(window, quotes.next())
            .await
            .unwrap_or_else(|_| {
                panic!("none of {QUOTING_CANDIDATES:?} quoted both sides five times in 60 s (received {received}); the markets may be closed")
            })
            .expect("quote");
        received += 1;
        if quote.bid.is_some() && quote.ask.is_some() {
            sample = Some(quote);
        }
    }
    let sample = sample.expect("a quote with both sides");
    let name = candidates
        .iter()
        .find(|(id, _)| *id == sample.symbol)
        .map_or("?", |(_, name)| name.as_str());
    quotes.unsubscribe().await.expect("end of subscription");
    let mut after = client.quotes(account.id, &ids);
    let quiet = Duration::from_secs(8);
    let late = timeout(quiet, after.next()).await.ok();
    say!(
        "live quotes: {received} quotes while subscribed to {ids:?}; sample {name} ({}) bid {} ask {} at {:?}; after unsubscribing {} in {quiet:?}",
        sample.symbol,
        sample
            .bid
            .map_or_else(String::new, |price| price.to_string()),
        sample
            .ask
            .map_or_else(String::new, |price| price.to_string()),
        sample.timestamp.map(Timestamp::as_millis),
        late.as_ref().map_or("none", |_| "a quote arrived"),
    );
    assert!(sample.bid <= sample.ask, "bid above ask: {sample:?}");
    assert!(sample.timestamp.is_some());
    assert!(late.is_none(), "quote after unsubscribing: {late:?}");
    client.shutdown().await;
}

#[tokio::test]
async fn live_depth_updates_rebuild_a_book_with_bids_below_asks() {
    let _guard = live_support::enter().await;
    let (client, account) = authorised().await;
    let candidates = candidates(&client, &account).await;
    let ids: Vec<SymbolId> = candidates.iter().map(|(id, _)| *id).collect();
    let mut depth = client
        .subscribe_depth(account.id, &ids)
        .await
        .expect("depth subscription");
    let window = tokio::time::Instant::now() + Duration::from_secs(60);
    let mut updates = 0_usize;
    let (mut added, mut deleted) = (0_usize, 0_usize);
    let symbol = loop {
        let update = tokio::time::timeout_at(window, depth.next())
            .await
            .unwrap_or_else(|_| {
                panic!("none of {QUOTING_CANDIDATES:?} sent five depth updates with both sides in 60 s (received {updates}); the markets may be closed")
            })
            .expect("depth update");
        updates += 1;
        added += update.new_quotes.len();
        deleted += update.deleted_quotes.len();
        let book = depth.book(update.symbol).expect("book of the symbol");
        if updates >= 5 && book.best_bid().is_some() && book.best_ask().is_some() {
            break update.symbol;
        }
    };
    let book = depth.book(symbol).expect("book of the symbol").clone();
    let name = candidates
        .iter()
        .find(|(id, _)| *id == symbol)
        .map_or("?", |(_, name)| name.as_str());
    let (bids, asks) = (book.bids(), book.asks());
    say!(
        "live depth: {updates} updates ({added} new, {deleted} deleted quotes); book of {name} ({symbol}) holds {} bids and {} asks; best bid {} x {} / best ask {} x {}",
        bids.len(),
        asks.len(),
        bids[0].price,
        bids[0].size,
        asks[0].price,
        asks[0].size,
    );
    assert!(book.best_bid() <= book.best_ask(), "crossed book: {book:?}");
    assert!(bids.windows(2).all(|pair| pair[0].price >= pair[1].price));
    assert!(asks.windows(2).all(|pair| pair[0].price <= pair[1].price));
    depth.resync().await.expect("fresh subscription");
    fresh_depth(&mut depth).await;
    depth.unsubscribe().await.expect("end of subscription");
    client.shutdown().await;
}

async fn fresh_depth(depth: &mut DepthStream) {
    let window = tokio::time::Instant::now() + Duration::from_secs(30);
    let mut seen = BTreeSet::new();
    loop {
        let first = tokio::time::timeout_at(window, depth.next())
            .await
            .expect("whole depth of a symbol within 30 s of the fresh subscription")
            .expect("depth update");
        if !seen.insert(first.symbol) {
            continue;
        }
        let entries = depth.book(first.symbol).map_or(0, DepthBook::len);
        say!(
            "live depth resync: the first update of {} carries {} new and {} deleted quotes; its book holds {entries} entries",
            first.symbol,
            first.new_quotes.len(),
            first.deleted_quotes.len(),
        );
        assert!(first.deleted_quotes.is_empty());
        assert_eq!(entries, first.new_quotes.len());
        if entries > 0 {
            break;
        }
    }
}

#[tokio::test]
async fn live_trendbars_are_ordered_and_a_live_bar_arrives() {
    let _guard = live_support::enter().await;
    let (client, account) = authorised().await;
    let now = Timestamp::now();
    let recent = TimeRange::ending_at(now, Duration::from_secs(600));
    let mut quoting = None;
    for (id, name) in candidates(&client, &account).await {
        let params = TrendbarsParams::new(account.id, id, Period::M1, recent);
        if !client
            .trendbars(&params)
            .await
            .expect("recent bars")
            .is_empty()
        {
            quoting = Some((id, name));
            break;
        }
    }
    let (symbol, name) = quoting.unwrap_or_else(|| {
        panic!("none of {QUOTING_CANDIDATES:?} has a one minute bar in the last ten minutes; the markets may be closed")
    });
    let settled = now.checked_sub(Duration::from_secs(120)).expect("moment");
    let range = TimeRange::ending_at(settled, Duration::from_secs(6 * 3_600));
    let params = TrendbarsParams::new(account.id, symbol, Period::M1, range);
    let bars = client.trendbars(&params).await.expect("history");
    assert!(bars.len() > 60, "only {} bars in six hours", bars.len());
    assert!(
        bars.windows(2)
            .all(|pair| pair[0].timestamp < pair[1].timestamp)
    );
    for bar in &bars {
        assert!(
            bar.low <= bar.open && bar.low <= bar.close,
            "low above the body: {bar:?}"
        );
        assert!(
            bar.open <= bar.high && bar.close <= bar.high,
            "high below the body: {bar:?}"
        );
        assert_eq!((bar.symbol, bar.period), (symbol, Period::M1));
        assert!(
            bar.timestamp.as_millis() + 60_000 > range.from().as_millis()
                && bar.timestamp <= range.to(),
            "bar outside the range: {bar:?}"
        );
    }
    let latest = client.trendbars(&params.count(10)).await.expect("history");
    assert_eq!(latest.as_slice(), &bars[bars.len() - 10..]);
    let (first, last) = (&bars[0], &bars[bars.len() - 1]);
    say!(
        "live trendbars: {} M1 bars of {name} ({symbol}) from {} to {}; last bar open {} high {} low {} close {} volume {}",
        bars.len(),
        first.timestamp,
        last.timestamp,
        last.open,
        last.high,
        last.low,
        last.close,
        last.volume,
    );
    let mut stream = client
        .subscribe_live_trendbars(account.id, symbol, Period::M1)
        .await
        .expect("live trendbar subscription");
    let bar = timeout(Duration::from_secs(90), stream.next())
        .await
        .unwrap_or_else(|_| panic!("no live M1 bar of {name} in 90 s; the market may be closed"))
        .expect("live bar");
    say!(
        "live trendbar: {name} ({symbol}) M1 at {} open {} high {} low {} close {} volume {}",
        bar.timestamp,
        bar.open,
        bar.high,
        bar.low,
        bar.close,
        bar.volume,
    );
    assert_eq!((bar.symbol, bar.period), (symbol, Period::M1));
    assert!(bar.low <= bar.open && bar.open <= bar.high);
    assert!(bar.low <= bar.close && bar.close <= bar.high);
    assert!(bar.timestamp >= last.timestamp);
    stream.unsubscribe().await.expect("end of subscription");
    client
        .unsubscribe_spots(account.id, &[symbol])
        .await
        .expect("end of spot subscription");
    client.shutdown().await;
}

#[tokio::test]
async fn live_tick_history_of_both_sides_falls_inside_the_range() {
    const PART_MILLIS: i64 = 1_200_000;
    let _guard = live_support::enter().await;
    let (client, account) = authorised().await;
    let settled = Timestamp::now()
        .checked_sub(Duration::from_secs(60))
        .expect("a clock past the epoch");
    let range = TimeRange::ending_at(settled, Duration::from_secs(4 * 3_600));
    let mut evidence = None;
    for (symbol, name) in candidates(&client, &account).await {
        let mut sides = Vec::new();
        for side in [QuoteSide::Bid, QuoteSide::Ask] {
            let params = TickDataParams::new(account.id, symbol, side, range);
            let ticks = client.tick_data(&params).await.expect("tick history");
            for tick in &ticks {
                assert!(
                    range.contains(tick.timestamp),
                    "{side:?} tick outside {range:?}: {tick:?}"
                );
            }
            assert!(
                ticks
                    .windows(2)
                    .all(|pair| pair[0].timestamp <= pair[1].timestamp)
            );
            let latest = client.tick_data(&params.limit(25)).await.expect("ticks");
            assert_eq!(
                latest.as_slice(),
                &ticks[ticks.len().saturating_sub(25)..],
                "the latest ticks differ from the end of the whole range"
            );
            let mut parts = Vec::new();
            let mut first = range.from().as_millis();
            while first <= range.to().as_millis() {
                let last = (first + PART_MILLIS - 1).min(range.to().as_millis());
                let at = |millis| Timestamp::from_millis(millis).expect("timestamp");
                let part = TimeRange::new(at(first), at(last)).expect("range");
                let params = TickDataParams::new(account.id, symbol, side, part);
                parts.extend(client.tick_data(&params).await.expect("part of the ticks"));
                first = last + 1;
            }
            assert!(
                parts == ticks,
                "{side:?}: {} ticks fetched in one piece, {} fetched in parts",
                ticks.len(),
                parts.len()
            );
            sides.push((side, ticks));
        }
        if sides.iter().all(|(_, ticks)| !ticks.is_empty()) {
            evidence = Some((symbol, name, sides));
            break;
        }
    }
    let (symbol, name, sides) = evidence.unwrap_or_else(|| {
        panic!("none of {QUOTING_CANDIDATES:?} has bid and ask ticks in the last four hours; the markets may be closed")
    });
    for (side, ticks) in &sides {
        let (first, last) = (&ticks[0], &ticks[ticks.len() - 1]);
        say!(
            "live ticks: {} {side:?} ticks of {name} ({symbol}) in {}..{}; first {} at {}, last {} at {}",
            ticks.len(),
            range.from(),
            range.to(),
            first.price,
            first.timestamp,
            last.price,
            last.timestamp,
        );
    }
    let (bids, asks) = (&sides[0].1, &sides[1].1);
    assert!(bids[bids.len() - 1].price < asks[asks.len() - 1].price);
    client.shutdown().await;
}

async fn raw_ticks(
    client: &OpenApiClient,
    account: &Account,
    symbol: SymbolId,
    span: (i64, i64),
) -> (Vec<i64>, bool) {
    let request = ProtoOaGetTickDataReq {
        payload_type: None,
        ctid_trader_account_id: account.id.get(),
        symbol_id: symbol.get(),
        r#type: 1,
        from_timestamp: span.0,
        to_timestamp: span.1,
    };
    let response = client
        .connection()
        .request(&request)
        .await
        .expect("tick data");
    let mut at = 0_i64;
    let times = response
        .tick_data
        .iter()
        .map(|tick| {
            at += tick.timestamp;
            at
        })
        .collect();
    (times, response.has_more)
}

#[tokio::test]
async fn live_tick_requests_end_before_the_millisecond_named_as_their_end() {
    const MINUTE: i64 = 60_000;
    let _guard = live_support::enter().await;
    let (client, account) = authorised().await;
    let now = Timestamp::now().as_millis();
    let mut observed = false;
    for (symbol, name) in candidates(&client, &account).await {
        let span = (now - 240 * MINUTE, now);
        let (page, more) = raw_ticks(&client, &account, symbol, span).await;
        if page.len() < 3 {
            continue;
        }
        assert!(
            page.windows(2).all(|pair| pair[0] >= pair[1]),
            "an answer does not start with its latest tick"
        );
        assert!(page.iter().all(|at| span.0 <= *at && *at < span.1));
        let probe = page[page.len() / 2];
        assert!(
            probe > page[page.len() - 1],
            "one millisecond fills half an answer"
        );
        let held = page.iter().filter(|at| **at == probe).count();
        let oldest = page[page.len() - 1];
        let (short, _) = raw_ticks(&client, &account, symbol, (oldest, probe)).await;
        assert!(
            !short.is_empty(),
            "no tick between the oldest of an answer and its middle"
        );
        assert!(
            short.iter().all(|at| *at < probe),
            "a request includes the millisecond named as its end"
        );
        let (ending, _) = raw_ticks(&client, &account, symbol, (oldest, probe + 1)).await;
        assert_eq!(ending[..held], vec![probe; held]);
        assert_eq!(ending[held..], short);
        let (single, _) = raw_ticks(&client, &account, symbol, (probe, probe + 1)).await;
        assert_eq!(single, vec![probe; held]);
        let moment = Timestamp::from_millis(probe).expect("timestamp");
        let range = TimeRange::new(moment, moment).expect("range");
        let params = TickDataParams::new(account.id, symbol, QuoteSide::Bid, range);
        let ticks = client.tick_data(&params).await.expect("tick history");
        assert_eq!(ticks.len(), held);
        assert!(ticks.iter().all(|tick| tick.timestamp == moment));
        say!(
            "live tick bounds: {name} ({symbol}) answer of {} ticks, more announced: {more}; fromTimestamp {probe} toTimestamp {probe} + 1 returned the {held} tick(s) of that millisecond, toTimestamp {probe} none of them",
            page.len()
        );
        observed = true;
        break;
    }
    assert!(
        observed,
        "none of {QUOTING_CANDIDATES:?} has three bid ticks in the last four hours; the markets may be closed"
    );
    client.shutdown().await;
}

fn depth_ids(event: &ProtoOaDepthEvent) -> (u64, Vec<u64>, Vec<u64>) {
    (
        event.symbol_id,
        event.new_quotes.iter().map(|quote| quote.id).collect(),
        event.deleted_quotes.clone(),
    )
}

fn update_ids(update: &DepthUpdate) -> (u64, Vec<u64>, Vec<u64>) {
    (
        u64::try_from(update.symbol.get()).expect("symbol id"),
        update
            .new_quotes
            .iter()
            .map(|quote| quote.id.get())
            .collect(),
        update.deleted_quotes.iter().map(|id| id.get()).collect(),
    )
}

/// Depth events of a connection around a reconnection, split at the last report of a loss.
struct DepthAround {
    order: Vec<&'static str>,
    /// Events delivered before the last loss, from the first event `events` holds.
    before: Vec<ProtoOaDepthEvent>,
    /// Events delivered since the last loss.
    replayed: Vec<ProtoOaDepthEvent>,
    /// Number of the events of `replayed` delivered before the reconnection was reported.
    during: usize,
}

async fn depth_around_the_loss(
    events: &mut broadcast::Receiver<Event>,
    settle: Duration,
) -> DepthAround {
    let mut order = Vec::new();
    let mut before: Vec<ProtoOaDepthEvent> = Vec::new();
    let mut replayed: Vec<ProtoOaDepthEvent> = Vec::new();
    let mut during = 0_usize;
    timeout(Duration::from_secs(90), async {
        loop {
            match events.recv().await.expect("event") {
                Event::Disconnected(_) => {
                    order.push("disconnected");
                    before.append(&mut replayed);
                }
                Event::Reconnected { .. } => {
                    order.push("reconnected");
                    during = replayed.len();
                    break;
                }
                Event::ReconnectFailed { .. } => {
                    order.push("failed");
                    before.append(&mut replayed);
                }
                event => {
                    if let Some(event) = event.decode::<ProtoOaDepthEvent>() {
                        if order.last() != Some(&"depth") {
                            order.push("depth");
                        }
                        replayed.push(event);
                    }
                }
            }
        }
    })
    .await
    .expect("reconnection in time");
    let settled = tokio::time::Instant::now() + settle;
    while let Ok(event) = tokio::time::timeout_at(settled, events.recv()).await {
        if let Some(event) = event.expect("event").decode::<ProtoOaDepthEvent>() {
            replayed.push(event);
        }
    }
    DepthAround {
        order,
        before,
        replayed,
        during,
    }
}

#[tokio::test]
async fn live_depth_book_holds_the_replayed_depth_after_a_forced_reconnect() {
    const SETTLE: Duration = Duration::from_secs(5);
    let _guard = live_support::enter().await;
    let (client, account) = authorised().await;
    let candidates = candidates(&client, &account).await;
    let ids: Vec<SymbolId> = candidates.iter().map(|(id, _)| *id).collect();
    // Listening starts before the stream does, and no depth is sent before the subscription:
    // the stream delivers one update for every depth event held here, in the same order. The
    // updates are matched to the events by their position, whatever they hold.
    let mut events = client.events();
    let mut depth = client
        .subscribe_depth(account.id, &ids)
        .await
        .expect("depth subscription");
    let mut read = 0_usize;
    timeout(Duration::from_secs(60), async {
        loop {
            let update = depth.next().await.expect("depth update");
            read += 1;
            if depth
                .book(update.symbol)
                .is_some_and(|book| !book.is_empty())
            {
                break;
            }
        }
    })
    .await
    .unwrap_or_else(|_| panic!("none of {QUOTING_CANDIDATES:?} sent depth in 60 s"));
    client
        .connection()
        .force_reconnect()
        .await
        .expect("forced reconnection");
    let DepthAround {
        order,
        before,
        replayed,
        during,
    } = depth_around_the_loss(&mut events, SETTLE).await;
    assert!(
        !replayed.is_empty(),
        "no depth arrived after the reconnection; the markets may be closed"
    );
    let unread = before
        .get(read..)
        .expect("the stream read no more than the connection delivered");
    let mut expected: BTreeMap<u64, BTreeSet<u64>> = BTreeMap::new();
    timeout(STEP, async {
        for (index, event) in unread.iter().enumerate() {
            let update = depth.next().await.expect("depth update");
            assert_eq!(
                update_ids(&update),
                depth_ids(event),
                "update {index} of those left unread before the loss"
            );
        }
        for (index, event) in replayed.iter().enumerate() {
            let update = depth.next().await.expect("depth update");
            assert_eq!(update_ids(&update), depth_ids(event));
            let ids = expected.entry(event.symbol_id).or_default();
            for id in &event.deleted_quotes {
                ids.remove(id);
            }
            ids.extend(event.new_quotes.iter().map(|quote| quote.id));
            let book = depth.book(update.symbol).expect("book of the symbol");
            let held: BTreeSet<u64> = book
                .bids()
                .iter()
                .chain(book.asks().iter())
                .map(|quote| quote.id.get())
                .collect();
            assert_eq!(
                &held, ids,
                "book of {} differs from the depth sent since the loss, at event {index}",
                update.symbol
            );
        }
    })
    .await
    .expect("the stream delivers what the connection delivered");
    let entries: usize = expected.values().map(BTreeSet::len).sum();
    say!(
        "live depth reconnect: order {order:?}, {} depth events before the loss, {} of them left unread by the stream, {during} during the replay, {} until {SETTLE:?} after it, books of {} symbols hold {entries} entries",
        before.len(),
        unread.len(),
        replayed.len(),
        expected.len()
    );
    assert!(entries > 0, "the books are empty after the reconnection");
    depth.unsubscribe().await.expect("end of subscription");
    client.shutdown().await;
}

const TRADED: &str = "BTCUSD";

const SWEEP_ROUNDS: usize = 10;

#[derive(Clone)]
struct Desk {
    client: OpenApiClient,
    account: Account,
    symbol: Symbol,
    label: String,
}

impl Desk {
    fn label(&self) -> Label {
        Label::new(self.label.clone()).expect("label")
    }

    fn smallest(&self) -> Volume {
        self.symbol
            .min_volume
            .expect("the traded symbol states its smallest volume")
    }

    async fn own(&self) -> (Vec<Position>, Vec<Order>) {
        let reconciled = self
            .client
            .reconcile(self.account.id)
            .await
            .expect("reconcile");
        assert_eq!(reconciled.account, self.account.id);
        let label = Some(self.label.as_str());
        let positions = reconciled
            .positions
            .into_iter()
            .filter(|position| position.label.as_deref() == label)
            .collect();
        let orders = reconciled
            .orders
            .into_iter()
            .filter(|order| order.label.as_deref() == label)
            .collect();
        (positions, orders)
    }
}

async fn sweeper() -> Result<(OpenApiClient, AccountId), ConnectorError> {
    let client = OpenApiClient::connect(configuration()).await?;
    client.authorise_application().await?;
    let demo = client
        .accounts()
        .await?
        .into_iter()
        .find(|account| !account.is_live);
    let Some(account) = demo else {
        client.shutdown().await;
        return Err(ConnectorError::Closed);
    };
    match client.authorise_account(account.id).await {
        Ok(()) => Ok((client, account.id)),
        Err(error) => {
            client.shutdown().await;
            Err(error)
        }
    }
}

async fn sweep_once(
    client: &OpenApiClient,
    account: AccountId,
    label: &str,
) -> Result<usize, ConnectorError> {
    let reconciled = client.reconcile(account).await?;
    let own = Some(label);
    let orders: Vec<&Order> = reconciled
        .orders
        .iter()
        .filter(|order| order.label.as_deref() == own)
        .collect();
    let positions: Vec<&Position> = reconciled
        .positions
        .iter()
        .filter(|position| position.label.as_deref() == own)
        .collect();
    for order in orders.iter().filter(|order| !order.closing) {
        let outcome = client.cancel_order(account, order.id).await;
        say!(
            "live sweep: cancel order {} answered {:?}",
            order.id,
            outcome.map(|execution| execution.kind)
        );
    }
    for position in &positions {
        let symbol = client
            .symbol_details(account, &[position.symbol])
            .await?
            .pop();
        let Some(symbol) = symbol else {
            say!(
                "live sweep: no details of symbol {} to close position {}",
                position.symbol,
                position.id
            );
            continue;
        };
        let outcome = client
            .close_position(account, position, position.volume, &symbol)
            .await;
        say!(
            "live sweep: close position {} answered {:?}",
            position.id,
            outcome.map(|execution| execution.kind)
        );
    }
    Ok(orders.len() + positions.len())
}

async fn sweep(label: &str) -> Result<(), String> {
    let mut last = "the sweep did not run".to_owned();
    let mut link: Option<(OpenApiClient, AccountId)> = None;
    for round in 0..SWEEP_ROUNDS {
        if round > 0 {
            tokio::time::sleep(Duration::from_secs(1)).await;
        }
        let (client, account) = match link.take() {
            Some(open) => open,
            None => match sweeper().await {
                Ok(open) => open,
                Err(error) => {
                    last = format!("no fresh connection for the sweep: {error}");
                    continue;
                }
            },
        };
        match sweep_once(&client, account, label).await {
            Ok(0) => {
                client.shutdown().await;
                return Ok(());
            }
            Ok(left) => {
                last = format!("{left} orders or positions labelled {label} were still listed");
                link = Some((client, account));
            }
            Err(error) => {
                last = format!("the sweep of label {label} failed: {error}");
                client.shutdown().await;
            }
        }
    }
    if let Some((client, _)) = link {
        client.shutdown().await;
    }
    Err(last)
}

fn panic_text(failure: tokio::task::JoinError) -> String {
    let Ok(payload) = failure.try_into_panic() else {
        return "the test was cancelled".to_owned();
    };
    payload
        .downcast_ref::<String>()
        .cloned()
        .or_else(|| {
            payload
                .downcast_ref::<&str>()
                .map(|text| (*text).to_owned())
        })
        .unwrap_or_else(|| "the test panicked".to_owned())
}

async fn trading<B, F>(item: &str, body: B)
where
    B: FnOnce(Desk) -> F,
    F: Future<Output = ()> + Send + 'static,
{
    let _guard = live_support::enter().await;
    let (client, account) = authorised().await;
    let list = client
        .symbols(SymbolsParams::new(account.id))
        .await
        .expect("symbols");
    let traded = list
        .symbols
        .iter()
        .find(|symbol| symbol.name.as_deref() == Some(TRADED))
        .unwrap_or_else(|| panic!("the demo account lists no symbol named {TRADED}"))
        .id;
    let symbol = client
        .symbol_details(account.id, &[traded])
        .await
        .expect("symbol details")
        .pop()
        .expect("details of the traded symbol");
    let desk = Desk {
        client,
        account,
        symbol,
        label: format!("{item}-{}", Timestamp::now().as_millis()),
    };
    let outcome = tokio::spawn(body(desk.clone())).await;
    desk.client.shutdown().await;
    let swept = sweep(&desk.label).await;
    match (outcome, swept) {
        (Ok(()), swept) => assert_eq!(swept, Ok(()), "the test left orders or positions behind"),
        (Err(failure), Ok(())) => panic!("{}", panic_text(failure)),
        (Err(failure), Err(left)) => panic!(
            "{}; and the sweep after the failure did not leave the account clean: {left}",
            panic_text(failure)
        ),
    }
}

async fn next_execution(events: &mut broadcast::Receiver<Event>, order: OrderId) -> Execution {
    timeout(STEP, async {
        loop {
            let event = events.recv().await.expect("event");
            let Event::Message(message) = event else {
                continue;
            };
            if message.payload_type != ProtoOaExecutionEvent::PAYLOAD_TYPE {
                continue;
            }
            let execution = Execution::try_from(
                ProtoOaExecutionEvent::from_message(&message).expect("execution event"),
            )
            .expect("execution");
            if execution.order.as_ref().map(|order| order.id) == Some(order) {
                return execution;
            }
        }
    })
    .await
    .expect("execution event in time")
}

#[tokio::test]
async fn live_market_order_opens_a_position_that_reconcile_reads_back() {
    trading("ct18", |desk| async move {
        let mut events = desk.client.events();
        let volume = desk.smallest();
        let order = NewOrder::market(desk.account.id, desk.symbol.id, TradeSide::Buy, volume)
            .label(desk.label())
            .comment(Comment::new("market order").expect("comment"));
        let accepted = desk
            .client
            .place_order(&order, &desk.symbol)
            .await
            .expect("market order");
        let placed = accepted.order.clone().expect("accepted order");
        assert_eq!(accepted.kind, ExecutionType::Accepted);
        assert_eq!(placed.kind, OrderType::Market);
        assert_eq!(placed.side, TradeSide::Buy);
        assert_eq!(placed.symbol, desk.symbol.id);
        assert_eq!(placed.volume, volume);
        assert_eq!(placed.label.as_deref(), Some(desk.label.as_str()));
        let first = next_execution(&mut events, placed.id).await;
        assert_eq!(first.kind, ExecutionType::Accepted);
        let filled = next_execution(&mut events, placed.id).await;
        assert_eq!(filled.kind, ExecutionType::Filled);
        let deal = filled.deal.expect("deal of the fill");
        assert_eq!(deal.order, placed.id);
        assert_eq!(deal.filled_volume, volume);
        assert_eq!(deal.status, DealStatus::Filled);
        assert!(deal.execution_price.is_some());
        let (positions, orders) = desk.own().await;
        say!(
            "live market order: order {} deal {} position {:?}, reconcile lists {} own positions ({:?}) and {} own orders",
            placed.id,
            deal.id,
            filled.position.as_ref().map(|position| position.id),
            positions.len(),
            positions
                .iter()
                .map(|position| (
                    position.side,
                    position.symbol,
                    position.volume.to_string(),
                    position.status,
                    position.price.map(|price| price.to_string()),
                ))
                .collect::<Vec<_>>(),
            orders.len(),
        );
        assert_eq!(positions.len(), 1);
        assert!(orders.is_empty());
        let position = &positions[0];
        assert_eq!(position.id, deal.position);
        assert_eq!(position.side, TradeSide::Buy);
        assert_eq!(position.symbol, desk.symbol.id);
        assert_eq!(position.volume, volume);
        assert_eq!(position.status, PositionStatus::Open);
        assert_eq!(position.price, deal.execution_price);
        assert_eq!(position.label.as_deref(), Some(desk.label.as_str()));
        assert_eq!(position.comment.as_deref(), Some("market order"));
    })
    .await;
}

impl Desk {
    async fn market(&self) -> (Price, Price) {
        let mut quotes = self
            .client
            .subscribe_quotes(self.account.id, &[self.symbol.id])
            .await
            .expect("spot subscription");
        let both = timeout(Duration::from_secs(60), async {
            loop {
                let quote = quotes.next().await.expect("quote");
                if let (Some(bid), Some(ask)) = (quote.bid, quote.ask) {
                    return (bid, ask);
                }
            }
        })
        .await
        .unwrap_or_else(|_| panic!("{TRADED} did not quote both sides in 60 s"));
        quotes.unsubscribe().await.expect("end of subscription");
        both
    }

    fn scaled(&self, price: Price, percent: i64) -> Price {
        let scaled = price
            .value()
            .checked_mul(Decimal::new(percent, 2))
            .expect("scaled price")
            .round_dp(self.symbol.digits);
        Price::new(scaled).expect("price")
    }
}

#[tokio::test]
async fn live_limit_and_stop_orders_wait_far_from_the_market() {
    trading("ct19", |desk| async move {
        let (bid, ask) = desk.market().await;
        let volume = desk.smallest();
        let below = desk.scaled(bid, 50);
        let above = desk.scaled(ask, 150);
        let expiry = Timestamp::now()
            .checked_add(Duration::from_secs(3_600))
            .expect("expiry");
        let limit = NewOrder::limit(
            desk.account.id,
            desk.symbol.id,
            TradeSide::Buy,
            volume,
            below,
        )
        .good_till(expiry)
        .label(desk.label());
        let stop = NewOrder::stop(desk.account.id, desk.symbol.id, TradeSide::Buy, volume, above)
            .label(desk.label());
        let mut placed = Vec::new();
        for order in [&limit, &stop] {
            let accepted = desk
                .client
                .place_order(order, &desk.symbol)
                .await
                .expect("pending order");
            assert_eq!(accepted.kind, ExecutionType::Accepted);
            placed.push(accepted.order.expect("accepted order").id);
        }
        let (positions, orders) = desk.own().await;
        say!(
            "live pending orders: bid {bid} ask {ask}, reconcile lists {} own positions and own orders {:?}",
            positions.len(),
            orders
                .iter()
                .map(|order| (
                    order.id,
                    order.kind,
                    order.side,
                    order.volume.to_string(),
                    order.limit_price.map(|price| price.to_string()),
                    order.stop_price.map(|price| price.to_string()),
                    order.time_in_force,
                    order.status,
                ))
                .collect::<Vec<_>>(),
        );
        assert!(positions.is_empty());
        assert_eq!(orders.len(), 2);
        let read = |id: OrderId| {
            orders
                .iter()
                .find(|order| order.id == id)
                .expect("order read back")
        };
        let limit = read(placed[0]);
        assert_eq!(limit.kind, OrderType::Limit);
        assert_eq!(limit.side, TradeSide::Buy);
        assert_eq!(limit.symbol, desk.symbol.id);
        assert_eq!(limit.volume, volume);
        assert_eq!(limit.limit_price, Some(below));
        assert_eq!(limit.stop_price, None);
        assert_eq!(limit.time_in_force, TimeInForce::GoodTillDate(expiry));
        assert_eq!(limit.status, OrderStatus::Accepted);
        let stop = read(placed[1]);
        assert_eq!(stop.kind, OrderType::Stop);
        assert_eq!(stop.side, TradeSide::Buy);
        assert_eq!(stop.symbol, desk.symbol.id);
        assert_eq!(stop.volume, volume);
        assert_eq!(stop.stop_price, Some(above));
        assert_eq!(stop.limit_price, None);
        assert_eq!(stop.time_in_force, TimeInForce::GoodTillCancel);
        assert_eq!(stop.status, OrderStatus::Accepted);
    })
    .await;
}

#[tokio::test]
async fn live_stop_limit_order_waits_with_its_stop_price_and_slippage() {
    trading("ct20", |desk| async move {
        let (bid, ask) = desk.market().await;
        let volume = desk.smallest();
        let above = desk.scaled(ask, 150);
        let slippage = 40;
        let order = NewOrder::stop_limit(
            desk.account.id,
            desk.symbol.id,
            TradeSide::Buy,
            volume,
            above,
            slippage,
        )
        .label(desk.label());
        let accepted = desk
            .client
            .place_order(&order, &desk.symbol)
            .await
            .expect("stop-limit order");
        assert_eq!(accepted.kind, ExecutionType::Accepted);
        let placed = accepted.order.expect("accepted order");
        let (positions, orders) = desk.own().await;
        say!(
            "live stop-limit order: bid {bid} ask {ask}, reconcile lists {} own positions and own orders {:?}",
            positions.len(),
            orders
                .iter()
                .map(|order| (
                    order.id,
                    order.kind,
                    order.side,
                    order.volume.to_string(),
                    order.stop_price.map(|price| price.to_string()),
                    order.limit_price.map(|price| price.to_string()),
                    order.slippage_points,
                    order.time_in_force,
                ))
                .collect::<Vec<_>>(),
        );
        assert!(positions.is_empty());
        assert_eq!(orders.len(), 1);
        let read = &orders[0];
        assert_eq!(read.id, placed.id);
        assert_eq!(read.kind, OrderType::StopLimit);
        assert_eq!(read.side, TradeSide::Buy);
        assert_eq!(read.symbol, desk.symbol.id);
        assert_eq!(read.volume, volume);
        assert_eq!(read.stop_price, Some(above));
        assert_eq!(read.limit_price, None);
        assert_eq!(read.slippage_points, Some(slippage));
        assert_eq!(read.status, OrderStatus::Accepted);
    })
    .await;
}

#[tokio::test]
async fn live_amendments_change_a_pending_order_and_the_protection_of_a_position() {
    trading("ct21", |desk| async move {
        let (bid, ask) = desk.market().await;
        amend_pending(&desk, bid, ask).await;
        amend_protection(&desk, bid, ask).await;
    })
    .await;
}

async fn amend_pending(desk: &Desk, bid: Price, ask: Price) {
    let volume = desk.smallest();
    let hour = Duration::from_secs(3_600);
    let expiry = Timestamp::now().checked_add(hour).expect("expiry");
    let order = NewOrder::limit(
        desk.account.id,
        desk.symbol.id,
        TradeSide::Buy,
        volume,
        desk.scaled(bid, 50),
    )
    .good_till(expiry)
    .label(desk.label());
    let placed = desk
        .client
        .place_order(&order, &desk.symbol)
        .await
        .expect("limit order")
        .order
        .expect("accepted order");
    let doubled = Volume::new(volume.value() + volume.value()).expect("volume");
    let later = expiry.checked_add(hour).expect("expiry");
    let (price, stop_loss, take_profit) = (
        desk.scaled(bid, 45),
        desk.scaled(bid, 40),
        desk.scaled(ask, 160),
    );
    let amendment = OrderAmendment::of(desk.account.id, &placed)
        .price(price)
        .volume(doubled)
        .good_till(later)
        .stop_loss(Some(stop_loss))
        .take_profit(Some(take_profit));
    assert_eq!(amendment.order(), placed.id);
    let replaced = desk
        .client
        .amend_order(&amendment, &desk.symbol)
        .await
        .expect("amended order");
    assert_eq!(replaced.kind, ExecutionType::Replaced);
    let read = desk.order(placed.id).await;
    say!(
        "live amended order: {} volume {} price {:?} till {:?} stop loss {:?} take profit {:?}",
        read.id,
        read.volume,
        read.limit_price.map(|price| price.to_string()),
        read.time_in_force,
        read.stop_loss.map(|price| price.to_string()),
        read.take_profit.map(|price| price.to_string()),
    );
    assert_eq!(read.volume, doubled);
    assert_eq!(read.limit_price, Some(price));
    assert_eq!(read.time_in_force, TimeInForce::GoodTillDate(later));
    assert_eq!(read.stop_loss, Some(stop_loss));
    assert_eq!(read.take_profit, Some(take_profit));
    let bare = OrderAmendment::of(desk.account.id, &read)
        .stop_loss(None)
        .good_till_cancelled();
    let replaced = desk
        .client
        .amend_order(&bare, &desk.symbol)
        .await
        .expect("amended order");
    assert_eq!(replaced.kind, ExecutionType::Replaced);
    let read = desk.order(placed.id).await;
    say!(
        "live amended order without stop loss and expiry: till {:?} stop loss {:?} take profit {:?}",
        read.time_in_force,
        read.stop_loss.map(|price| price.to_string()),
        read.take_profit.map(|price| price.to_string()),
    );
    assert_eq!(read.volume, doubled);
    assert_eq!(read.limit_price, Some(price));
    assert_eq!(read.stop_loss, None);
    assert_eq!(read.take_profit, Some(take_profit));
    assert_eq!(read.time_in_force, TimeInForce::GoodTillCancel);
}

async fn amend_protection(desk: &Desk, bid: Price, ask: Price) {
    let volume = desk.smallest();
    let mut events = desk.client.events();
    let market = NewOrder::market(desk.account.id, desk.symbol.id, TradeSide::Buy, volume)
        .label(desk.label());
    let accepted = desk
        .client
        .place_order(&market, &desk.symbol)
        .await
        .expect("market order")
        .order
        .expect("accepted order");
    let opened = opened_by(&mut events, accepted.id).await;
    assert_eq!((opened.stop_loss, opened.take_profit), (None, None));
    let (stop_loss, take_profit) = (desk.scaled(bid, 50), desk.scaled(ask, 150));
    let amendment = PositionAmendment::of(desk.account.id, &opened)
        .stop_loss(Some(stop_loss))
        .take_profit(Some(take_profit));
    assert_eq!(amendment.position(), opened.id);
    let protected = desk
        .client
        .amend_position(&amendment, &desk.symbol)
        .await
        .expect("amended position");
    let read = desk.position(opened.id).await;
    say!(
        "live amended position: {} answered {:?}, stop loss {:?} take profit {:?}",
        read.id,
        protected.kind,
        read.stop_loss.map(|price| price.to_string()),
        read.take_profit.map(|price| price.to_string()),
    );
    assert_eq!(protected.kind, ExecutionType::Accepted);
    assert_eq!(read.stop_loss, Some(stop_loss));
    assert_eq!(read.take_profit, Some(take_profit));
    assert_eq!(read.volume, volume);
    let replaced = desk
        .client
        .amend_position(
            &PositionAmendment::of(desk.account.id, &read).take_profit(None),
            &desk.symbol,
        )
        .await
        .expect("amended position");
    let read = desk.position(opened.id).await;
    say!(
        "live amended position without take profit: answered {:?}, stop loss {:?} take profit {:?}",
        replaced.kind,
        read.stop_loss.map(|price| price.to_string()),
        read.take_profit.map(|price| price.to_string()),
    );
    assert_eq!(replaced.kind, ExecutionType::Replaced);
    assert_eq!(read.stop_loss, Some(stop_loss));
    assert_eq!(read.take_profit, None);
}

impl Desk {
    async fn order(&self, id: OrderId) -> Order {
        let (_, orders) = self.own().await;
        orders
            .into_iter()
            .find(|order| order.id == id)
            .expect("order read back")
    }

    async fn position(&self, id: PositionId) -> Position {
        let (positions, _) = self.own().await;
        positions
            .into_iter()
            .find(|position| position.id == id)
            .expect("position read back")
    }
}

#[tokio::test]
async fn live_cancelled_pending_order_is_no_longer_listed() {
    trading("ct22", |desk| async move {
        let (bid, _) = desk.market().await;
        let order = NewOrder::limit(
            desk.account.id,
            desk.symbol.id,
            TradeSide::Buy,
            desk.smallest(),
            desk.scaled(bid, 50),
        )
        .label(desk.label());
        let placed = desk
            .client
            .place_order(&order, &desk.symbol)
            .await
            .expect("limit order")
            .order
            .expect("accepted order");
        let (_, listed) = desk.own().await;
        assert_eq!(
            listed.iter().map(|order| order.id).collect::<Vec<_>>(),
            vec![placed.id]
        );
        let cancelled = desk
            .client
            .cancel_order(desk.account.id, placed.id)
            .await
            .expect("cancelled order");
        assert_eq!(cancelled.kind, ExecutionType::Cancelled);
        let last = cancelled.order.expect("order of the cancellation");
        assert_eq!(last.id, placed.id);
        assert_eq!(last.status, OrderStatus::Cancelled);
        let (positions, orders) = desk.own().await;
        say!(
            "live cancel: order {} answered {:?} with status {:?}, reconcile lists {} own orders and {} own positions",
            placed.id,
            cancelled.kind,
            last.status,
            orders.len(),
            positions.len(),
        );
        assert!(orders.is_empty());
        assert!(positions.is_empty());
        let again = desk.client.cancel_order(desk.account.id, placed.id).await;
        let code = match again {
            Err(ConnectorError::Server(error)) => Some(error.code),
            _ => None,
        };
        say!("live cancel of an order no longer in force: {code:?}");
        assert_eq!(code, Some(ErrorCode::OrderNotFound));
    })
    .await;
}

#[tokio::test]
async fn live_reconcile_lists_exactly_the_orders_and_positions_of_the_run() {
    trading("ct23", |desk| async move {
        let (bid, ask) = desk.market().await;
        let volume = desk.smallest();
        let (below, above, guard) = (
            desk.scaled(bid, 50),
            desk.scaled(ask, 150),
            desk.scaled(bid, 40),
        );
        let (account, symbol) = (desk.account.id, desk.symbol.id);
        let comment = |text: &str| Comment::new(text).expect("comment");
        let limit = NewOrder::limit(account, symbol, TradeSide::Buy, volume, below)
            .label(desk.label())
            .comment(comment("limit"));
        let stop = NewOrder::stop(account, symbol, TradeSide::Buy, volume, above)
            .label(desk.label())
            .comment(comment("stop"));
        let market = NewOrder::market(account, symbol, TradeSide::Buy, volume)
            .label(desk.label())
            .comment(comment("market"));
        let mut events = desk.client.events();
        let mut placed = Vec::new();
        for order in [&limit, &stop, &market] {
            let accepted = desk
                .client
                .place_order(order, &desk.symbol)
                .await
                .expect("order");
            placed.push(accepted.order.expect("accepted order").id);
        }
        let opened = opened_by(&mut events, placed[2]).await;
        desk.client
            .amend_position(
                &PositionAmendment::of(account, &opened).stop_loss(Some(guard)),
                &desk.symbol,
            )
            .await
            .expect("protected position");
        let (positions, orders) = desk.own().await;
        say!(
            "live reconcile: own positions {:?}, own orders {:?}",
            positions
                .iter()
                .map(|position| (position.id, position.volume.to_string(), position.status))
                .collect::<Vec<_>>(),
            orders
                .iter()
                .map(|order| (order.id, order.kind, order.closing, order.comment.clone()))
                .collect::<Vec<_>>(),
        );
        assert_eq!(positions.len(), 1);
        let position = &positions[0];
        assert_eq!(position.id, opened.id);
        assert_eq!((position.side, position.volume), (TradeSide::Buy, volume));
        assert_eq!(position.status, PositionStatus::Open);
        assert_eq!(
            (position.stop_loss, position.take_profit),
            (Some(guard), None)
        );
        assert_eq!(position.comment.as_deref(), Some("market"));
        assert!(
            position
                .used_margin
                .is_some_and(|margin| margin > Decimal::ZERO)
        );
        let (protection, pending): (Vec<_>, Vec<_>) =
            orders.iter().partition(|order| order.closing);
        assert_eq!(
            pending
                .iter()
                .map(|order| order.id)
                .collect::<BTreeSet<_>>(),
            BTreeSet::from([placed[0], placed[1]])
        );
        for order in &pending {
            let limit = order.id == placed[0];
            assert_eq!(
                order.kind,
                if limit {
                    OrderType::Limit
                } else {
                    OrderType::Stop
                }
            );
            assert_eq!(order.limit_price, limit.then_some(below));
            assert_eq!(order.stop_price, (!limit).then_some(above));
            assert_eq!(
                order.comment.as_deref(),
                Some(if limit { "limit" } else { "stop" })
            );
            assert_eq!((order.side, order.volume), (TradeSide::Buy, volume));
            assert_eq!(order.status, OrderStatus::Accepted);
            assert_eq!(order.symbol, symbol);
        }
        assert_eq!(protection.len(), 1);
        assert_eq!(protection[0].kind, OrderType::StopLossTakeProfit);
        assert_eq!(protection[0].position, Some(opened.id));
        assert_eq!(protection[0].side, TradeSide::Sell);
        assert_eq!(protection[0].stop_price, Some(guard));
    })
    .await;
}

async fn opened_by(events: &mut broadcast::Receiver<Event>, order: OrderId) -> Position {
    loop {
        let execution = next_execution(events, order).await;
        if execution.kind == ExecutionType::Filled {
            break execution.position.expect("opened position");
        }
    }
}

async fn closed_by(events: &mut broadcast::Receiver<Event>, order: OrderId) -> Execution {
    loop {
        let execution = next_execution(events, order).await;
        if execution.kind == ExecutionType::Filled {
            break execution;
        }
    }
}

#[tokio::test]
async fn live_position_is_closed_in_part_and_then_in_whole() {
    trading("ct24", |desk| async move {
        let part = desk.smallest();
        let whole = Volume::new(part.value() + part.value()).expect("volume");
        let mut events = desk.client.events();
        let market = NewOrder::market(desk.account.id, desk.symbol.id, TradeSide::Buy, whole)
            .label(desk.label());
        let accepted = desk
            .client
            .place_order(&market, &desk.symbol)
            .await
            .expect("market order")
            .order
            .expect("accepted order");
        let opened = opened_by(&mut events, accepted.id).await;
        assert_eq!(opened.volume, whole);
        let first = desk
            .client
            .close_position(desk.account.id, &opened, part, &desk.symbol)
            .await
            .expect("partial close");
        assert_eq!(first.kind, ExecutionType::Accepted);
        let closing = first.order.expect("closing order");
        assert!(closing.closing);
        assert_eq!((closing.side, closing.volume), (TradeSide::Sell, part));
        assert_eq!(closing.position, Some(opened.id));
        let reduced = closed_by(&mut events, closing.id).await;
        let deal = reduced.deal.expect("closing deal");
        let detail = deal.closing.expect("closing detail");
        assert_eq!((deal.position, deal.filled_volume), (opened.id, part));
        assert_eq!(Some(detail.entry_price), opened.price);
        let read = desk.position(opened.id).await;
        say!(
            "live partial close: position {} of {} closed by {}, deal {} at {:?}, reconcile reads volume {}",
            opened.id,
            opened.volume,
            part,
            deal.id,
            deal.execution_price.map(|price| price.to_string()),
            read.volume,
        );
        assert_eq!(read.volume, part);
        assert_eq!(read.status, PositionStatus::Open);
        assert_eq!(reduced.position.map(|position| position.volume), Some(part));
        let second = desk
            .client
            .close_position(desk.account.id, &read, read.volume, &desk.symbol)
            .await
            .expect("close");
        let closing = second.order.expect("closing order");
        let closed = closed_by(&mut events, closing.id).await;
        let last = closed.position.expect("closed position");
        let (positions, orders) = desk.own().await;
        say!(
            "live close: position {} ends {:?}, reconcile lists {} own positions and {} own orders",
            last.id,
            last.status,
            positions.len(),
            orders.len(),
        );
        assert_eq!(last.status, PositionStatus::Closed);
        assert!(positions.is_empty());
        assert!(orders.is_empty());
        let code = match desk
            .client
            .close_position(desk.account.id, &read, read.volume, &desk.symbol)
            .await
        {
            Err(ConnectorError::Server(error)) => Some(error.code),
            _ => None,
        };
        say!("live close of a position already closed: {code:?}");
        assert_eq!(code, Some(ErrorCode::PositionNotFound));
    })
    .await;
}

async fn next_of(stream: &mut ExecutionStream, order: OrderId) -> Execution {
    timeout(STEP, async {
        loop {
            let event = stream.next().await.expect("trade event");
            assert!(
                !matches!(event, TradeEvent::OrderError(_)),
                "the server refused a request: {event:?}"
            );
            if event.order() == Some(order) {
                break *match event {
                    TradeEvent::Execution(execution) => execution,
                    other => panic!("unexpected event {other:?}"),
                };
            }
        }
    })
    .await
    .expect("execution event in time")
}

#[tokio::test]
async fn live_execution_stream_reports_the_life_of_orders_in_order() {
    trading("ct25", |desk| async move {
        let (bid, _) = desk.market().await;
        let volume = desk.smallest();
        let (account, symbol) = (desk.account.id, desk.symbol.id);
        let mut stream = desk.client.executions(account);
        let market = NewOrder::market(account, symbol, TradeSide::Buy, volume).label(desk.label());
        let taken = desk
            .client
            .place_order(&market, &desk.symbol)
            .await
            .expect("market order")
            .order
            .expect("accepted order");
        let accepted = next_of(&mut stream, taken.id).await;
        let filled = next_of(&mut stream, taken.id).await;
        say!(
            "live execution stream: market order {} reported {:?} then {:?} with deal {:?}",
            taken.id,
            accepted.kind,
            filled.kind,
            filled.deal.as_ref().map(|deal| deal.id),
        );
        assert_eq!(accepted.kind, ExecutionType::Accepted);
        assert_eq!(filled.kind, ExecutionType::Filled);
        assert_eq!(accepted.account, account);
        let deal = filled.deal.expect("deal of the fill");
        assert_eq!((deal.order, deal.filled_volume), (taken.id, volume));
        assert_eq!(
            filled.position.map(|position| position.status),
            Some(PositionStatus::Open)
        );
        let limit = NewOrder::limit(
            account,
            symbol,
            TradeSide::Buy,
            volume,
            desk.scaled(bid, 50),
        )
        .label(desk.label());
        let waiting = desk
            .client
            .place_order(&limit, &desk.symbol)
            .await
            .expect("limit order")
            .order
            .expect("accepted order");
        desk.client
            .cancel_order(account, waiting.id)
            .await
            .expect("cancelled order");
        let accepted = next_of(&mut stream, waiting.id).await;
        let cancelled = next_of(&mut stream, waiting.id).await;
        say!(
            "live execution stream: limit order {} reported {:?} then {:?}",
            waiting.id,
            accepted.kind,
            cancelled.kind,
        );
        assert_eq!(accepted.kind, ExecutionType::Accepted);
        assert_eq!(cancelled.kind, ExecutionType::Cancelled);
        assert_eq!(
            cancelled.order.map(|order| order.status),
            Some(OrderStatus::Cancelled)
        );
        let refused = desk.client.cancel_order(account, waiting.id).await;
        assert!(matches!(refused, Err(ConnectorError::Server(_))));
        let event = timeout(STEP, stream.next())
            .await
            .expect("order error event in time")
            .expect("trade event");
        let TradeEvent::OrderError(error) = event else {
            panic!("expected an order error, got {event:?}");
        };
        say!(
            "live execution stream: second cancellation reported order error {:?}",
            error.code
        );
        assert_eq!(error.code, ErrorCode::OrderNotFound);
        assert_eq!(error.account, account);
    })
    .await;
}

#[tokio::test]
async fn live_trader_reports_the_balance_and_currency_of_the_demo_account() {
    let _guard = live_support::enter().await;
    let (client, account) = authorised().await;
    let trader = client.trader(account.id).await.expect("trader");
    let assets = client.assets(account.id).await.expect("assets");
    say!(
        "live trader: balance {} {} with {} money digits, leverage {:?}, {:?}, {:?}",
        trader.balance,
        trader.currency,
        trader.money_digits,
        trader.leverage.map(|leverage| leverage.to_string()),
        trader.account_type,
        trader.access_rights,
    );
    assert_eq!(trader.account, account.id);
    assert!(trader.balance > Decimal::ZERO);
    assert_eq!(trader.balance.scale(), trader.money_digits);
    assert!(trader.currency.len() >= 3 && trader.currency.chars().all(|c| c.is_ascii_uppercase()));
    assert_eq!(
        assets
            .iter()
            .find(|asset| asset.id == trader.deposit_asset)
            .map(|asset| asset.name.as_str()),
        Some(trader.currency.as_str())
    );
    assert!(
        trader
            .leverage
            .is_some_and(|leverage| leverage >= Decimal::ONE)
    );
    assert_eq!(trader.access_rights, AccessRights::FullAccess);
    client.shutdown().await;
}

#[tokio::test]
async fn live_history_holds_the_deals_and_orders_of_the_run() {
    trading("ct27", |desk| async move {
        let margin = Duration::from_secs(120);
        let started = Timestamp::now().checked_sub(margin).expect("start");
        let (bid, _) = desk.market().await;
        let (account, symbol, volume) = (desk.account.id, desk.symbol.id, desk.smallest());
        let limit =
            NewOrder::limit(account, symbol, TradeSide::Buy, volume, desk.scaled(bid, 50))
                .label(desk.label());
        let resting = desk
            .client
            .place_order(&limit, &desk.symbol)
            .await
            .expect("limit order")
            .order
            .expect("accepted order");
        desk.client
            .cancel_order(account, resting.id)
            .await
            .expect("cancelled order");
        let mut events = desk.client.events();
        let market = NewOrder::market(account, symbol, TradeSide::Buy, volume).label(desk.label());
        let accepted = desk
            .client
            .place_order(&market, &desk.symbol)
            .await
            .expect("market order")
            .order
            .expect("accepted order");
        let opened = opened_by(&mut events, accepted.id).await;
        let closing = desk
            .client
            .close_position(account, &opened, opened.volume, &desk.symbol)
            .await
            .expect("close")
            .order
            .expect("closing order");
        let closed = closed_by(&mut events, closing.id).await;
        let last = closed.deal.expect("closing deal");
        let ended = Timestamp::now().checked_add(margin).expect("end");
        let range = TimeRange::new(started, ended).expect("range");
        let deals = desk.client.deal_history(account, range).await.expect("deals");
        let orders = desk
            .client
            .order_history(account, range)
            .await
            .expect("orders");
        let own: Vec<&Deal> = deals
            .iter()
            .filter(|deal| deal.position == opened.id)
            .collect();
        let labelled: Vec<&Order> = orders
            .iter()
            .filter(|order| order.label.as_deref() == Some(desk.label.as_str()))
            .collect();
        say!(
            "live history: {} deals and {} orders in range, own deals {:?} of orders {:?}, own orders {:?}",
            deals.len(),
            orders.len(),
            own.iter().map(|deal| deal.id.get()).collect::<Vec<_>>(),
            own.iter().map(|deal| deal.order.get()).collect::<Vec<_>>(),
            labelled
                .iter()
                .map(|order| (order.id.get(), order.status))
                .collect::<Vec<_>>(),
        );
        assert_eq!(
            own.iter().map(|deal| deal.order).collect::<Vec<_>>(),
            vec![accepted.id, closing.id]
        );
        assert_eq!(own.last().map(|deal| deal.id), Some(last.id));
        assert_eq!(own.last().copied(), Some(&last));
        assert!(own.iter().all(|deal| deal.filled_volume == volume));
        assert!(deals.windows(2).all(|pair| pair[0].executed <= pair[1].executed));
        assert!(orders.windows(2).all(|pair| pair[0].updated <= pair[1].updated));
        let status = |id: OrderId| {
            labelled
                .iter()
                .find(|order| order.id == id)
                .map(|order| order.status)
        };
        assert_eq!(status(resting.id), Some(OrderStatus::Cancelled));
        assert_eq!(status(accepted.id), Some(OrderStatus::Filled));
        assert_eq!(status(closing.id), Some(OrderStatus::Filled));
        let (positions, pending) = desk.own().await;
        assert!(positions.is_empty() && pending.is_empty());
    })
    .await;
}

const CASH_FLOW_WINDOW: Duration = Duration::from_secs(28 * 86_400);

const CASH_FLOW_WINDOWS: u32 = 13;

async fn latest_cash_flow(
    client: &OpenApiClient,
    account: AccountId,
) -> (TimeRange, Vec<CashFlow>) {
    let mut end = Timestamp::now();
    for _ in 0..CASH_FLOW_WINDOWS {
        let window = TimeRange::ending_at(end, CASH_FLOW_WINDOW);
        let flows = client
            .cash_flow_history(account, window)
            .await
            .expect("cash flow");
        if !flows.is_empty() {
            return (window, flows);
        }
        end = window
            .from()
            .checked_sub(Duration::from_millis(1))
            .expect("earlier window");
    }
    panic!(
        "the demo account shows no deposit or withdrawal in {CASH_FLOW_WINDOWS} windows of four weeks"
    );
}

#[tokio::test]
async fn live_margin_calls_and_cash_flow_are_read() {
    let _guard = live_support::enter().await;
    let (client, account) = authorised().await;
    let calls = client.margin_calls(account.id).await.expect("margin calls");
    say!(
        "live margin calls: {:?}",
        calls
            .iter()
            .map(|call| (call.level, call.threshold.to_string()))
            .collect::<Vec<_>>()
    );
    assert!(
        !calls.is_empty(),
        "the demo account states no margin call threshold"
    );
    for call in &calls {
        assert!(call.threshold > Decimal::ZERO, "threshold of {call:?}");
        assert_eq!(
            calls
                .iter()
                .filter(|other| other.level == call.level)
                .count(),
            1,
            "level stated twice: {:?}",
            call.level
        );
    }
    let (window, flows) = latest_cash_flow(&client, account.id).await;
    say!(
        "live cash flow: {} changes in the four weeks up to {}: {:?}",
        flows.len(),
        window.to(),
        flows
            .iter()
            .map(|flow| (flow.kind, flow.amount.to_string(), flow.at.as_millis()))
            .collect::<Vec<_>>()
    );
    assert!(!flows.is_empty(), "no cash flow was read");
    for flow in &flows {
        assert!(
            window.from() <= flow.at && flow.at <= window.to(),
            "cash flow outside the window"
        );
        assert!(flow.amount > Decimal::ZERO, "amount of a cash flow");
    }
    assert!(flows.windows(2).all(|pair| pair[0].at <= pair[1].at));
    assert!(
        flows.iter().any(|flow| flow.kind == CashFlowKind::Deposit),
        "the demo account was funded by a deposit"
    );
    client.shutdown().await;
}

#[tokio::test]
async fn live_sweep_cancels_what_a_failed_test_left_over_a_fresh_connection() {
    const FAILURE: &str = "deliberate failure after the connection of the test was shut down";
    let failed = tokio::spawn(trading("ct53", |desk| async move {
        let (bid, _) = desk.market().await;
        let order = NewOrder::limit(
            desk.account.id,
            desk.symbol.id,
            TradeSide::Buy,
            desk.smallest(),
            desk.scaled(bid, 50),
        )
        .label(desk.label());
        desk.client
            .place_order(&order, &desk.symbol)
            .await
            .expect("limit order");
        let (_, orders) = desk.own().await;
        assert_eq!(orders.len(), 1);
        desk.client.shutdown().await;
        panic!("{FAILURE}");
    }))
    .await
    .expect_err("the failure of the test is not masked");
    assert_eq!(panic_text(failed), FAILURE);
    let _guard = live_support::enter().await;
    let (client, account) = authorised().await;
    let reconciled = client.reconcile(account.id).await.expect("reconcile");
    let left = reconciled
        .orders
        .iter()
        .filter_map(|order| order.label.as_deref())
        .chain(
            reconciled
                .positions
                .iter()
                .filter_map(|position| position.label.as_deref()),
        )
        .filter(|label| label.starts_with("ct53-"))
        .count();
    say!("live sweep after a failed test: {left} orders or positions of the run are listed");
    assert_eq!(left, 0);
    client.shutdown().await;
}

const SECOND_TRADED: &str = "ETHUSD";

const MARGIN_SETTLE: Duration = Duration::from_secs(4);

fn tally(
    events: &mut broadcast::Receiver<Event>,
    seen: &mut BTreeMap<u32, usize>,
    changes: &mut Vec<MarginChange>,
) {
    loop {
        match events.try_recv() {
            Ok(Event::Message(message)) => {
                *seen.entry(message.payload_type).or_default() += 1;
                if message.payload_type == ProtoOaMarginChangedEvent::PAYLOAD_TYPE {
                    let event = ProtoOaMarginChangedEvent::from_message(&message)
                        .expect("margin-changed event");
                    changes.push(MarginChange::try_from(event).expect("margin change"));
                }
            }
            Ok(_) => {}
            Err(broadcast::error::TryRecvError::Lagged(skipped)) => {
                panic!("{skipped} raw events were dropped unread")
            }
            Err(_) => break,
        }
    }
}

impl Desk {
    async fn second_symbol(&self) -> Symbol {
        let list = self
            .client
            .symbols(SymbolsParams::new(self.account.id))
            .await
            .expect("symbols");
        let id = list
            .symbols
            .iter()
            .find(|symbol| symbol.name.as_deref() == Some(SECOND_TRADED))
            .unwrap_or_else(|| panic!("the demo account lists no symbol named {SECOND_TRADED}"))
            .id;
        self.client
            .symbol_details(self.account.id, &[id])
            .await
            .expect("symbol details")
            .pop()
            .expect("details of the second symbol")
    }

    async fn bought(
        &self,
        events: &mut broadcast::Receiver<Event>,
        symbol: &Symbol,
        volume: Volume,
    ) -> Position {
        let market = NewOrder::market(self.account.id, symbol.id, TradeSide::Buy, volume)
            .label(self.label());
        let accepted = self
            .client
            .place_order(&market, symbol)
            .await
            .expect("market order")
            .order
            .expect("accepted order");
        let opened = opened_by(events, accepted.id).await;
        tokio::time::sleep(MARGIN_SETTLE).await;
        opened
    }

    async fn sold(
        &self,
        events: &mut broadcast::Receiver<Event>,
        position: &Position,
        volume: Volume,
        symbol: &Symbol,
    ) {
        let closing = self
            .client
            .close_position(self.account.id, position, volume, symbol)
            .await
            .expect("close")
            .order
            .expect("closing order");
        closed_by(events, closing.id).await;
        tokio::time::sleep(MARGIN_SETTLE).await;
    }
}

#[tokio::test]
async fn live_margin_changed_event_is_not_emitted_by_the_demo_server_while_the_exposure_changes() {
    trading("ct28", |desk| async move {
        let second = desk.second_symbol().await;
        let smallest = desk.smallest();
        let double = Volume::new(smallest.value() + smallest.value()).expect("volume");
        let other = second.min_volume.expect("smallest volume of the second symbol");
        let mut raw = desk.client.events();
        let mut events = desk.client.events();
        let mut margin = desk.client.margin_events(desk.account.id);
        let first = desk.bought(&mut events, &desk.symbol, double).await;
        let hedge = desk.bought(&mut events, &second, other).await;
        let added = desk.bought(&mut events, &desk.symbol, smallest).await;
        desk.sold(&mut events, &hedge, hedge.volume, &second).await;
        desk.sold(&mut events, &first, smallest, &desk.symbol).await;
        let mut seen = BTreeMap::new();
        let mut changes = Vec::new();
        tally(&mut raw, &mut seen, &mut changes);
        let typed = timeout(Duration::from_millis(200), margin.next()).await;
        say!(
            "live margin change: positions {} {} {} opened, one closed, one halved; raw payload types and counts {seen:?}; {} margin-changed events; typed stream {}",
            first.id,
            hedge.id,
            added.id,
            changes.len(),
            if typed.is_ok() { "delivered" } else { "silent" },
        );
        assert_eq!(
            seen.keys().copied().collect::<Vec<u32>>(),
            vec![ProtoOaExecutionEvent::PAYLOAD_TYPE],
            "the demo server pushed something besides execution events"
        );
        assert!(
            changes.is_empty(),
            "the demo server now emits the margin-changed event: assert its content here"
        );
        assert!(
            typed.is_err(),
            "the typed margin stream delivered an event"
        );
    })
    .await;
}