longport/quote/
types.rs

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
use longport_proto::quote::{self, Period, TradeSession, TradeStatus};
use num_enum::{FromPrimitive, IntoPrimitive, TryFromPrimitive};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumString};
use time::{Date, OffsetDateTime, Time};

use crate::{
    quote::{utils::parse_date, SubFlags},
    serde_utils, Error, Market, Result,
};

/// Subscription
#[derive(Debug, Clone)]
pub struct Subscription {
    /// Security code
    pub symbol: String,
    /// Subscription flags
    pub sub_types: SubFlags,
    /// Candlesticks
    pub candlesticks: Vec<Period>,
}

/// Depth
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Depth {
    /// Position
    pub position: i32,
    /// Price
    pub price: Option<Decimal>,
    /// Volume
    pub volume: i64,
    /// Number of orders
    pub order_num: i64,
}

impl TryFrom<quote::Depth> for Depth {
    type Error = Error;

    fn try_from(depth: quote::Depth) -> Result<Self> {
        Ok(Self {
            position: depth.position,
            price: depth.price.parse().ok(),
            volume: depth.volume,
            order_num: depth.order_num,
        })
    }
}

/// Brokers
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Brokers {
    /// Position
    pub position: i32,
    /// Broker IDs
    pub broker_ids: Vec<i32>,
}

impl From<quote::Brokers> for Brokers {
    fn from(brokers: quote::Brokers) -> Self {
        Self {
            position: brokers.position,
            broker_ids: brokers.broker_ids,
        }
    }
}

/// Trade direction
#[derive(Debug, FromPrimitive, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
#[repr(i32)]
pub enum TradeDirection {
    /// Neutral
    #[num_enum(default)]
    Neutral = 0,
    /// Down
    Down = 1,
    /// Up
    Up = 2,
}

/// Trade
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Trade {
    /// Price
    pub price: Decimal,
    /// Volume
    pub volume: i64,
    /// Time of trading
    #[serde(with = "serde_utils::timestamp")]
    pub timestamp: OffsetDateTime,
    /// Trade type
    ///
    /// HK
    ///
    /// - `*` - Overseas trade
    /// - `D` - Odd-lot trade
    /// - `M` - Non-direct off-exchange trade
    /// - `P` - Late trade (Off-exchange previous day)
    /// - `U` - Auction trade
    /// - `X` - Direct off-exchange trade
    /// - `Y` - Automatch internalized
    /// - `<empty string>` -  Automatch normal
    ///
    /// US
    ///
    /// - `<empty string>` - Regular sale
    /// - `A` - Acquisition
    /// - `B` - Bunched trade
    /// - `D` - Distribution
    /// - `F` - Intermarket sweep
    /// - `G` - Bunched sold trades
    /// - `H` - Price variation trade
    /// - `I` - Odd lot trade
    /// - `K` - Rule 155 trde(NYSE MKT)
    /// - `M` - Market center close price
    /// - `P` - Prior reference price
    /// - `Q` - Market center open price
    /// - `S` - Split trade
    /// - `V` - Contingent trade
    /// - `W` - Average price trade
    /// - `X` - Cross trade
    /// - `1` - Stopped stock(Regular trade)
    pub trade_type: String,
    /// Trade direction
    pub direction: TradeDirection,
    /// Trade session
    pub trade_session: TradeSession,
}

impl TryFrom<quote::Trade> for Trade {
    type Error = Error;

    fn try_from(trade: quote::Trade) -> Result<Self> {
        Ok(Self {
            price: trade.price.parse().unwrap_or_default(),
            volume: trade.volume,
            timestamp: OffsetDateTime::from_unix_timestamp(trade.timestamp)
                .map_err(|err| Error::parse_field_error("timestamp", err))?,
            trade_type: trade.trade_type,
            direction: trade.direction.into(),
            trade_session: TradeSession::try_from(trade.trade_session).unwrap_or_default(),
        })
    }
}

bitflags::bitflags! {
    /// Derivative type
    #[derive(Debug, Copy, Clone, Serialize,Deserialize)]
    pub struct DerivativeType: u8 {
        /// US stock options
        const OPTION = 0x1;

        /// HK warrants
        const WARRANT = 0x2;
    }
}

/// Security board
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, EnumString, Display, Serialize, Deserialize)]
#[allow(clippy::upper_case_acronyms)]
pub enum SecurityBoard {
    /// Unknown
    #[strum(disabled)]
    Unknown,
    /// US Main Board
    USMain,
    /// US Pink Board
    USPink,
    /// Dow Jones Industrial Average
    USDJI,
    /// Nasdsaq Index
    USNSDQ,
    /// US Industry Board
    USSector,
    /// US Option
    USOption,
    /// US Sepecial Option
    USOptionS,
    /// Hong Kong Equity Securities
    HKEquity,
    /// HK PreIPO Security
    HKPreIPO,
    /// HK Warrant
    HKWarrant,
    /// Hang Seng Index
    HKHS,
    /// HK Industry Board
    HKSector,
    /// SH Main Board(Connect)
    SHMainConnect,
    /// SH Main Board(Non Connect)
    SHMainNonConnect,
    /// SH Science and Technology Innovation Board
    SHSTAR,
    /// CN Index
    CNIX,
    /// CN Industry Board
    CNSector,
    /// SZ Main Board(Connect)
    SZMainConnect,
    /// SZ Main Board(Non Connect)
    SZMainNonConnect,
    /// SZ Gem Board(Connect)
    SZGEMConnect,
    /// SZ Gem Board(Non Connect)
    SZGEMNonConnect,
    /// SG Main Board
    SGMain,
    /// Singapore Straits Index
    STI,
    /// SG Industry Board
    SGSector,
}

/// The basic information of securities
#[derive(Debug, Serialize, Deserialize)]
pub struct SecurityStaticInfo {
    /// Security code
    pub symbol: String,
    /// Security name (zh-CN)
    pub name_cn: String,
    /// Security name (en)
    pub name_en: String,
    /// Security name (zh-HK)
    pub name_hk: String,
    /// Exchange which the security belongs to
    pub exchange: String,
    /// Trading currency
    pub currency: String,
    /// Lot size
    pub lot_size: i32,
    /// Total shares
    pub total_shares: i64,
    /// Circulating shares
    pub circulating_shares: i64,
    /// HK shares (only HK stocks)
    pub hk_shares: i64,
    /// Earnings per share
    pub eps: Decimal,
    /// Earnings per share (TTM)
    pub eps_ttm: Decimal,
    /// Net assets per share
    pub bps: Decimal,
    /// Dividend yield
    pub dividend_yield: Decimal,
    /// Types of supported derivatives
    pub stock_derivatives: DerivativeType,
    /// Board
    pub board: SecurityBoard,
}

impl TryFrom<quote::StaticInfo> for SecurityStaticInfo {
    type Error = Error;

    fn try_from(resp: quote::StaticInfo) -> Result<Self> {
        Ok(SecurityStaticInfo {
            symbol: resp.symbol,
            name_cn: resp.name_cn,
            name_en: resp.name_en,
            name_hk: resp.name_hk,
            exchange: resp.exchange,
            currency: resp.currency,
            lot_size: resp.lot_size,
            total_shares: resp.total_shares,
            circulating_shares: resp.circulating_shares,
            hk_shares: resp.hk_shares,
            eps: resp.eps.parse().unwrap_or_default(),
            eps_ttm: resp.eps_ttm.parse().unwrap_or_default(),
            bps: resp.bps.parse().unwrap_or_default(),
            dividend_yield: resp.dividend_yield.parse().unwrap_or_default(),
            stock_derivatives: resp.stock_derivatives.into_iter().fold(
                DerivativeType::empty(),
                |acc, value| match value {
                    1 => acc | DerivativeType::OPTION,
                    2 => acc | DerivativeType::WARRANT,
                    _ => acc,
                },
            ),
            board: resp.board.parse().unwrap_or(SecurityBoard::Unknown),
        })
    }
}

/// Real-time quote
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RealtimeQuote {
    /// Security code
    pub symbol: String,
    /// Latest price
    pub last_done: Decimal,
    /// Open
    pub open: Decimal,
    /// High
    pub high: Decimal,
    /// Low
    pub low: Decimal,
    /// Time of latest price
    pub timestamp: OffsetDateTime,
    /// Volume
    pub volume: i64,
    /// Turnover
    pub turnover: Decimal,
    /// Security trading status
    pub trade_status: TradeStatus,
}

/// Quote of US pre/post market
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrePostQuote {
    /// Latest price
    pub last_done: Decimal,
    /// Time of latest price
    #[serde(with = "serde_utils::timestamp")]
    pub timestamp: OffsetDateTime,
    /// Volume
    pub volume: i64,
    /// Turnover
    pub turnover: Decimal,
    /// High
    pub high: Decimal,
    /// Low
    pub low: Decimal,
    /// Close of the last trade session
    pub prev_close: Decimal,
}

impl TryFrom<quote::PrePostQuote> for PrePostQuote {
    type Error = Error;

    fn try_from(quote: quote::PrePostQuote) -> Result<Self> {
        Ok(Self {
            last_done: quote.last_done.parse().unwrap_or_default(),
            timestamp: OffsetDateTime::from_unix_timestamp(quote.timestamp)
                .map_err(|err| Error::parse_field_error("timestamp", err))?,
            volume: quote.volume,
            turnover: quote.turnover.parse().unwrap_or_default(),
            high: quote.high.parse().unwrap_or_default(),
            low: quote.low.parse().unwrap_or_default(),
            prev_close: quote.prev_close.parse().unwrap_or_default(),
        })
    }
}

/// Quote of securitity
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityQuote {
    /// Security code
    pub symbol: String,
    /// Latest price
    pub last_done: Decimal,
    /// Yesterday's close
    pub prev_close: Decimal,
    /// Open
    pub open: Decimal,
    /// High
    pub high: Decimal,
    /// Low
    pub low: Decimal,
    /// Time of latest price
    #[serde(with = "serde_utils::timestamp")]
    pub timestamp: OffsetDateTime,
    /// Volume
    pub volume: i64,
    /// Turnover
    pub turnover: Decimal,
    /// Security trading status
    pub trade_status: TradeStatus,
    /// Quote of US pre market
    pub pre_market_quote: Option<PrePostQuote>,
    /// Quote of US post market
    pub post_market_quote: Option<PrePostQuote>,
    /// Quote of US overnight market
    pub overnight_quote: Option<PrePostQuote>,
}

impl TryFrom<quote::SecurityQuote> for SecurityQuote {
    type Error = Error;

    fn try_from(quote: quote::SecurityQuote) -> Result<Self> {
        Ok(Self {
            symbol: quote.symbol,
            last_done: quote.last_done.parse().unwrap_or_default(),
            prev_close: quote.prev_close.parse().unwrap_or_default(),
            open: quote.open.parse().unwrap_or_default(),
            high: quote.high.parse().unwrap_or_default(),
            low: quote.low.parse().unwrap_or_default(),
            timestamp: OffsetDateTime::from_unix_timestamp(quote.timestamp)
                .map_err(|err| Error::parse_field_error("timestamp", err))?,
            volume: quote.volume,
            turnover: quote.turnover.parse().unwrap_or_default(),
            trade_status: TradeStatus::try_from(quote.trade_status).unwrap_or_default(),
            pre_market_quote: quote.pre_market_quote.map(TryInto::try_into).transpose()?,
            post_market_quote: quote.post_market_quote.map(TryInto::try_into).transpose()?,
            overnight_quote: quote.over_night_quote.map(TryInto::try_into).transpose()?,
        })
    }
}

/// Option type
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, EnumString, Serialize, Deserialize)]
pub enum OptionType {
    /// Unknown
    #[strum(disabled)]
    Unknown,
    /// American
    #[strum(serialize = "A")]
    American,
    /// Europe
    #[strum(serialize = "U")]
    Europe,
}

/// Option direction
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, EnumString, Serialize, Deserialize)]
pub enum OptionDirection {
    /// Unknown
    #[strum(disabled)]
    Unknown,
    /// Put
    #[strum(serialize = "P")]
    Put,
    /// Call
    #[strum(serialize = "C")]
    Call,
}

/// Quote of option
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptionQuote {
    /// Security code
    pub symbol: String,
    /// Latest price
    pub last_done: Decimal,
    /// Yesterday's close
    pub prev_close: Decimal,
    /// Open
    pub open: Decimal,
    /// High
    pub high: Decimal,
    /// Low
    pub low: Decimal,
    /// Time of latest price
    #[serde(with = "serde_utils::timestamp")]
    pub timestamp: OffsetDateTime,
    /// Volume
    pub volume: i64,
    /// Turnover
    pub turnover: Decimal,
    /// Security trading status
    pub trade_status: TradeStatus,
    /// Implied volatility
    pub implied_volatility: Decimal,
    /// Number of open positions
    pub open_interest: i64,
    /// Exprity date
    pub expiry_date: Date,
    /// Strike price
    pub strike_price: Decimal,
    /// Contract multiplier
    pub contract_multiplier: Decimal,
    /// Option type
    pub contract_type: OptionType,
    /// Contract size
    pub contract_size: Decimal,
    /// Option direction
    pub direction: OptionDirection,
    /// Underlying security historical volatility of the option
    pub historical_volatility: Decimal,
    /// Underlying security symbol of the option
    pub underlying_symbol: String,
}

impl TryFrom<quote::OptionQuote> for OptionQuote {
    type Error = Error;

    fn try_from(quote: quote::OptionQuote) -> Result<Self> {
        let option_extend = quote.option_extend.unwrap_or_default();

        Ok(Self {
            symbol: quote.symbol,
            last_done: quote.last_done.parse().unwrap_or_default(),
            prev_close: quote.prev_close.parse().unwrap_or_default(),
            open: quote.open.parse().unwrap_or_default(),
            high: quote.high.parse().unwrap_or_default(),
            low: quote.low.parse().unwrap_or_default(),
            timestamp: OffsetDateTime::from_unix_timestamp(quote.timestamp)
                .map_err(|err| Error::parse_field_error("timestamp", err))?,
            volume: quote.volume,
            turnover: quote.turnover.parse().unwrap_or_default(),
            trade_status: TradeStatus::try_from(quote.trade_status).unwrap_or_default(),
            implied_volatility: option_extend.implied_volatility.parse().unwrap_or_default(),
            open_interest: option_extend.open_interest,
            expiry_date: parse_date(&option_extend.expiry_date)
                .map_err(|err| Error::parse_field_error("expiry_date", err))?,
            strike_price: option_extend.strike_price.parse().unwrap_or_default(),
            contract_multiplier: option_extend
                .contract_multiplier
                .parse()
                .unwrap_or_default(),
            contract_type: option_extend.contract_type.parse().unwrap_or_default(),
            contract_size: option_extend.contract_size.parse().unwrap_or_default(),
            direction: option_extend.contract_type.parse().unwrap_or_default(),
            historical_volatility: option_extend
                .historical_volatility
                .parse()
                .unwrap_or_default(),
            underlying_symbol: option_extend.underlying_symbol,
        })
    }
}

/// Warrant type
#[derive(
    Debug,
    Copy,
    Clone,
    Hash,
    Eq,
    PartialEq,
    EnumString,
    IntoPrimitive,
    TryFromPrimitive,
    Serialize,
    Deserialize,
)]
#[repr(i32)]
pub enum WarrantType {
    /// Unknown
    #[strum(disabled)]
    Unknown = -1,
    /// Call
    Call = 0,
    /// Put
    Put = 1,
    /// Bull
    Bull = 2,
    /// Bear
    Bear = 3,
    /// Inline
    Inline = 4,
}

/// Quote of warrant
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WarrantQuote {
    /// Security code
    pub symbol: String,
    /// Latest price
    pub last_done: Decimal,
    /// Yesterday's close
    pub prev_close: Decimal,
    /// Open
    pub open: Decimal,
    /// High
    pub high: Decimal,
    /// Low
    pub low: Decimal,
    /// Time of latest price
    #[serde(with = "serde_utils::timestamp")]
    pub timestamp: OffsetDateTime,
    /// Volume
    pub volume: i64,
    /// Turnover
    pub turnover: Decimal,
    /// Security trading status
    pub trade_status: TradeStatus,
    /// Implied volatility
    pub implied_volatility: Decimal,
    /// Exprity date
    pub expiry_date: Date,
    /// Last tradalbe date
    pub last_trade_date: Date,
    /// Outstanding ratio
    pub outstanding_ratio: Decimal,
    /// Outstanding quantity
    pub outstanding_quantity: i64,
    /// Conversion ratio
    pub conversion_ratio: Decimal,
    /// Warrant type
    pub category: WarrantType,
    /// Strike price
    pub strike_price: Decimal,
    /// Upper bound price
    pub upper_strike_price: Decimal,
    /// Lower bound price
    pub lower_strike_price: Decimal,
    /// Call price
    pub call_price: Decimal,
    /// Underlying security symbol of the warrant
    pub underlying_symbol: String,
}

impl TryFrom<quote::WarrantQuote> for WarrantQuote {
    type Error = Error;

    fn try_from(quote: quote::WarrantQuote) -> Result<Self> {
        let warrant_extend = quote.warrant_extend.unwrap_or_default();

        Ok(Self {
            symbol: quote.symbol,
            last_done: quote.last_done.parse().unwrap_or_default(),
            prev_close: quote.prev_close.parse().unwrap_or_default(),
            open: quote.open.parse().unwrap_or_default(),
            high: quote.high.parse().unwrap_or_default(),
            low: quote.low.parse().unwrap_or_default(),
            timestamp: OffsetDateTime::from_unix_timestamp(quote.timestamp)
                .map_err(|err| Error::parse_field_error("timestamp", err))?,
            volume: quote.volume,
            turnover: quote.turnover.parse().unwrap_or_default(),
            trade_status: TradeStatus::try_from(quote.trade_status).unwrap_or_default(),
            implied_volatility: warrant_extend
                .implied_volatility
                .parse()
                .unwrap_or_default(),
            expiry_date: parse_date(&warrant_extend.expiry_date)
                .map_err(|err| Error::parse_field_error("expiry_date", err))?,
            last_trade_date: parse_date(&warrant_extend.last_trade_date)
                .map_err(|err| Error::parse_field_error("last_trade_date", err))?,
            outstanding_ratio: warrant_extend.outstanding_ratio.parse().unwrap_or_default(),
            outstanding_quantity: warrant_extend.outstanding_qty,
            conversion_ratio: warrant_extend.conversion_ratio.parse().unwrap_or_default(),
            category: warrant_extend.category.parse().unwrap_or_default(),
            strike_price: warrant_extend.strike_price.parse().unwrap_or_default(),
            upper_strike_price: warrant_extend
                .upper_strike_price
                .parse()
                .unwrap_or_default(),
            lower_strike_price: warrant_extend
                .lower_strike_price
                .parse()
                .unwrap_or_default(),
            call_price: warrant_extend.call_price.parse().unwrap_or_default(),
            underlying_symbol: warrant_extend.underlying_symbol,
        })
    }
}

/// Security depth
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SecurityDepth {
    /// Ask depth
    pub asks: Vec<Depth>,
    /// Bid depth
    pub bids: Vec<Depth>,
}

/// Security brokers
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SecurityBrokers {
    /// Ask brokers
    pub ask_brokers: Vec<Brokers>,
    /// Bid brokers
    pub bid_brokers: Vec<Brokers>,
}

/// Participant info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParticipantInfo {
    /// Broker IDs
    pub broker_ids: Vec<i32>,
    /// Participant name (zh-CN)
    pub name_cn: String,
    /// Participant name (en)
    pub name_en: String,
    /// Participant name (zh-HK)
    pub name_hk: String,
}

impl From<quote::ParticipantInfo> for ParticipantInfo {
    fn from(info: quote::ParticipantInfo) -> Self {
        Self {
            broker_ids: info.broker_ids,
            name_cn: info.participant_name_cn,
            name_en: info.participant_name_en,
            name_hk: info.participant_name_hk,
        }
    }
}

/// Intraday line
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IntradayLine {
    /// Close price of the minute
    pub price: Decimal,
    /// Start time of the minute
    #[serde(with = "serde_utils::timestamp")]
    pub timestamp: OffsetDateTime,
    /// Volume
    pub volume: i64,
    /// Turnover
    pub turnover: Decimal,
    /// Average price
    pub avg_price: Decimal,
}

impl TryFrom<quote::Line> for IntradayLine {
    type Error = Error;

    fn try_from(value: quote::Line) -> Result<Self> {
        Ok(Self {
            price: value.price.parse().unwrap_or_default(),
            timestamp: OffsetDateTime::from_unix_timestamp(value.timestamp)
                .map_err(|err| Error::parse_field_error("timestamp", err))?,
            volume: value.volume,
            turnover: value.turnover.parse().unwrap_or_default(),
            avg_price: value.avg_price.parse().unwrap_or_default(),
        })
    }
}

/// Candlestick
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct Candlestick {
    /// Close price
    pub close: Decimal,
    /// Open price
    pub open: Decimal,
    /// Low price
    pub low: Decimal,
    /// High price
    pub high: Decimal,
    /// Volume
    pub volume: i64,
    /// Turnover
    pub turnover: Decimal,
    /// Timestamp
    #[serde(with = "serde_utils::timestamp")]
    pub timestamp: OffsetDateTime,
}

impl TryFrom<quote::Candlestick> for Candlestick {
    type Error = Error;

    fn try_from(value: quote::Candlestick) -> Result<Self> {
        Ok(Self {
            close: value.close.parse().unwrap_or_default(),
            open: value.open.parse().unwrap_or_default(),
            low: value.low.parse().unwrap_or_default(),
            high: value.high.parse().unwrap_or_default(),
            volume: value.volume,
            turnover: value.turnover.parse().unwrap_or_default(),
            timestamp: OffsetDateTime::from_unix_timestamp(value.timestamp)
                .map_err(|err| Error::parse_field_error("timestamp", err))?,
        })
    }
}

impl From<longport_candlesticks::Candlestick> for Candlestick {
    #[inline]
    fn from(candlestick: longport_candlesticks::Candlestick) -> Self {
        Self {
            close: candlestick.close,
            open: candlestick.open,
            low: candlestick.low,
            high: candlestick.high,
            volume: candlestick.volume,
            turnover: candlestick.turnover,
            timestamp: candlestick.time,
        }
    }
}

impl From<Candlestick> for longport_candlesticks::Candlestick {
    #[inline]
    fn from(candlestick: Candlestick) -> Self {
        Self {
            time: candlestick.timestamp,
            open: candlestick.open,
            high: candlestick.high,
            low: candlestick.low,
            close: candlestick.close,
            volume: candlestick.volume,
            turnover: candlestick.turnover,
        }
    }
}

/// Strike price info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StrikePriceInfo {
    /// Strike price
    pub price: Decimal,
    /// Security code of call option
    pub call_symbol: String,
    /// Security code of put option
    pub put_symbol: String,
    /// Is standard
    pub standard: bool,
}

impl TryFrom<quote::StrikePriceInfo> for StrikePriceInfo {
    type Error = Error;

    fn try_from(value: quote::StrikePriceInfo) -> Result<Self> {
        Ok(Self {
            price: value.price.parse().unwrap_or_default(),
            call_symbol: value.call_symbol,
            put_symbol: value.put_symbol,
            standard: value.standard,
        })
    }
}

/// Issuer info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IssuerInfo {
    /// Issuer ID
    pub issuer_id: i32,
    /// Issuer name (zh-CN)
    pub name_cn: String,
    /// Issuer name (en)
    pub name_en: String,
    /// Issuer name (zh-HK)
    pub name_hk: String,
}

impl From<quote::IssuerInfo> for IssuerInfo {
    fn from(info: quote::IssuerInfo) -> Self {
        Self {
            issuer_id: info.id,
            name_cn: info.name_cn,
            name_en: info.name_en,
            name_hk: info.name_hk,
        }
    }
}

/// Sort order type
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, IntoPrimitive)]
#[repr(i32)]
pub enum SortOrderType {
    /// Ascending
    Ascending = 0,
    /// Descending
    Descending = 1,
}

/// Warrant sort by
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, IntoPrimitive)]
#[repr(i32)]
pub enum WarrantSortBy {
    /// Last done
    LastDone = 0,
    /// Change rate
    ChangeRate = 1,
    /// Change value
    ChangeValue = 2,
    /// Volume
    Volume = 3,
    /// Turnover
    Turnover = 4,
    /// Expiry date
    ExpiryDate = 5,
    /// Strike price
    StrikePrice = 6,
    /// Upper strike price
    UpperStrikePrice = 7,
    /// Lower strike price
    LowerStrikePrice = 8,
    /// Outstanding quantity
    OutstandingQuantity = 9,
    /// Outstanding ratio
    OutstandingRatio = 10,
    /// Premium
    Premium = 11,
    /// In/out of the bound
    ItmOtm = 12,
    /// Implied volatility
    ImpliedVolatility = 13,
    /// Greek value Delta
    Delta = 14,
    /// Call price
    CallPrice = 15,
    /// Price interval from the call price
    ToCallPrice = 16,
    /// Effective leverage
    EffectiveLeverage = 17,
    /// Leverage ratio
    LeverageRatio = 18,
    /// Conversion ratio
    ConversionRatio = 19,
    /// Breakeven point
    BalancePoint = 20,
    /// Status
    Status = 21,
}

/// Filter warrant expiry date type
#[allow(non_camel_case_types)]
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, IntoPrimitive)]
#[repr(i32)]
pub enum FilterWarrantExpiryDate {
    /// Less than 3 months
    LT_3 = 1,
    /// 3 - 6 months
    Between_3_6 = 2,
    /// 6 - 12 months
    Between_6_12 = 3,
    /// Greater than 12 months
    GT_12 = 4,
}

/// Filter warrant in/out of the bounds type
#[allow(non_camel_case_types)]
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, IntoPrimitive)]
#[repr(i32)]
pub enum FilterWarrantInOutBoundsType {
    /// In bounds
    In = 1,
    /// Out bounds
    Out = 2,
}

/// Warrant status
#[derive(
    Debug, Copy, Clone, Hash, Eq, PartialEq, IntoPrimitive, TryFromPrimitive, Serialize, Deserialize,
)]
#[repr(i32)]
pub enum WarrantStatus {
    /// Suspend
    Suspend = 2,
    /// Prepare List
    PrepareList = 3,
    /// Normal
    Normal = 4,
}

/// Warrant info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WarrantInfo {
    /// Security code
    pub symbol: String,
    /// Warrant type
    pub warrant_type: WarrantType,
    /// Security name
    pub name: String,
    /// Latest price
    pub last_done: Decimal,
    /// Quote change rate
    pub change_rate: Decimal,
    /// Quote change
    pub change_value: Decimal,
    /// Volume
    pub volume: i64,
    /// Turnover
    pub turnover: Decimal,
    /// Expiry date
    pub expiry_date: Date,
    /// Strike price
    pub strike_price: Option<Decimal>,
    /// Upper strike price
    pub upper_strike_price: Option<Decimal>,
    /// Lower strike price
    pub lower_strike_price: Option<Decimal>,
    /// Outstanding quantity
    pub outstanding_qty: i64,
    /// Outstanding ratio
    pub outstanding_ratio: Decimal,
    /// Premium
    pub premium: Decimal,
    /// In/out of the bound
    pub itm_otm: Option<Decimal>,
    /// Implied volatility
    pub implied_volatility: Option<Decimal>,
    /// Delta
    pub delta: Option<Decimal>,
    /// Call price
    pub call_price: Option<Decimal>,
    /// Price interval from the call price
    pub to_call_price: Option<Decimal>,
    /// Effective leverage
    pub effective_leverage: Option<Decimal>,
    /// Leverage ratio
    pub leverage_ratio: Decimal,
    /// Conversion ratio
    pub conversion_ratio: Option<Decimal>,
    /// Breakeven point
    pub balance_point: Option<Decimal>,
    /// Status
    pub status: WarrantStatus,
}

impl TryFrom<quote::FilterWarrant> for WarrantInfo {
    type Error = Error;

    fn try_from(info: quote::FilterWarrant) -> Result<Self> {
        let r#type = WarrantType::try_from(info.r#type)
            .map_err(|err| Error::parse_field_error("type", err))?;

        match r#type {
            WarrantType::Unknown => unreachable!(),
            WarrantType::Call | WarrantType::Put => Ok(Self {
                symbol: info.symbol,
                warrant_type: r#type,
                name: info.name,
                last_done: info.last_done.parse().unwrap_or_default(),
                change_rate: info.change_rate.parse().unwrap_or_default(),
                change_value: info.change_val.parse().unwrap_or_default(),
                volume: info.volume,
                turnover: info.turnover.parse().unwrap_or_default(),
                expiry_date: parse_date(&info.expiry_date)
                    .map_err(|err| Error::parse_field_error("expiry_date", err))?,
                strike_price: Some(info.last_done.parse().unwrap_or_default()),
                upper_strike_price: None,
                lower_strike_price: None,
                outstanding_qty: info.outstanding_qty.parse().unwrap_or_default(),
                outstanding_ratio: info.outstanding_ratio.parse().unwrap_or_default(),
                premium: info.premium.parse().unwrap_or_default(),
                itm_otm: Some(info.last_done.parse().unwrap_or_default()),
                implied_volatility: Some(info.last_done.parse().unwrap_or_default()),
                delta: Some(info.last_done.parse().unwrap_or_default()),
                call_price: None,
                to_call_price: None,
                effective_leverage: Some(info.last_done.parse().unwrap_or_default()),
                leverage_ratio: info.leverage_ratio.parse().unwrap_or_default(),
                conversion_ratio: Some(info.last_done.parse().unwrap_or_default()),
                balance_point: Some(info.last_done.parse().unwrap_or_default()),
                status: WarrantStatus::try_from(info.status)
                    .map_err(|err| Error::parse_field_error("state", err))?,
            }),
            WarrantType::Bull | WarrantType::Bear => Ok(Self {
                symbol: info.symbol,
                warrant_type: r#type,
                name: info.name,
                last_done: info.last_done.parse().unwrap_or_default(),
                change_rate: info.change_rate.parse().unwrap_or_default(),
                change_value: info.change_val.parse().unwrap_or_default(),
                volume: info.volume,
                turnover: info.turnover.parse().unwrap_or_default(),
                expiry_date: parse_date(&info.expiry_date)
                    .map_err(|err| Error::parse_field_error("expiry_date", err))?,
                strike_price: Some(info.last_done.parse().unwrap_or_default()),
                upper_strike_price: None,
                lower_strike_price: None,
                outstanding_qty: info.outstanding_qty.parse().unwrap_or_default(),
                outstanding_ratio: info.outstanding_ratio.parse().unwrap_or_default(),
                premium: info.premium.parse().unwrap_or_default(),
                itm_otm: Some(info.last_done.parse().unwrap_or_default()),
                implied_volatility: None,
                delta: None,
                call_price: Some(info.call_price.parse().unwrap_or_default()),
                to_call_price: Some(info.to_call_price.parse().unwrap_or_default()),
                effective_leverage: None,
                leverage_ratio: info.leverage_ratio.parse().unwrap_or_default(),
                conversion_ratio: Some(info.last_done.parse().unwrap_or_default()),
                balance_point: Some(info.last_done.parse().unwrap_or_default()),
                status: WarrantStatus::try_from(info.status)
                    .map_err(|err| Error::parse_field_error("state", err))?,
            }),
            WarrantType::Inline => Ok(Self {
                symbol: info.symbol,
                warrant_type: r#type,
                name: info.name,
                last_done: info.last_done.parse().unwrap_or_default(),
                change_rate: info.change_rate.parse().unwrap_or_default(),
                change_value: info.change_val.parse().unwrap_or_default(),
                volume: info.volume,
                turnover: info.turnover.parse().unwrap_or_default(),
                expiry_date: parse_date(&info.expiry_date)
                    .map_err(|err| Error::parse_field_error("expiry_date", err))?,
                strike_price: None,
                upper_strike_price: Some(info.upper_strike_price.parse().unwrap_or_default()),
                lower_strike_price: Some(info.lower_strike_price.parse().unwrap_or_default()),
                outstanding_qty: info.outstanding_qty.parse().unwrap_or_default(),
                outstanding_ratio: info.outstanding_ratio.parse().unwrap_or_default(),
                premium: info.premium.parse().unwrap_or_default(),
                itm_otm: None,
                implied_volatility: None,
                delta: None,
                call_price: None,
                to_call_price: None,
                effective_leverage: None,
                leverage_ratio: info.leverage_ratio.parse().unwrap_or_default(),
                conversion_ratio: None,
                balance_point: None,
                status: WarrantStatus::try_from(info.status)
                    .map_err(|err| Error::parse_field_error("state", err))?,
            }),
        }
    }
}

/// The information of trading session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TradingSessionInfo {
    /// Being trading time
    pub begin_time: Time,
    /// End trading time
    pub end_time: Time,
    /// Trading session
    pub trade_session: TradeSession,
}

impl TryFrom<quote::TradePeriod> for TradingSessionInfo {
    type Error = Error;

    fn try_from(value: quote::TradePeriod) -> Result<Self> {
        #[inline]
        fn parse_time(value: i32) -> ::std::result::Result<Time, time::error::ComponentRange> {
            Time::from_hms(((value / 100) % 100) as u8, (value % 100) as u8, 0)
        }

        Ok(Self {
            begin_time: parse_time(value.beg_time)
                .map_err(|err| Error::parse_field_error("beg_time", err))?,
            end_time: parse_time(value.end_time)
                .map_err(|err| Error::parse_field_error("end_time", err))?,
            trade_session: TradeSession::try_from(value.trade_session).unwrap_or_default(),
        })
    }
}

/// Market trading session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketTradingSession {
    /// Market
    pub market: Market,
    /// Trading session
    pub trade_sessions: Vec<TradingSessionInfo>,
}

impl TryFrom<quote::MarketTradePeriod> for MarketTradingSession {
    type Error = Error;

    fn try_from(value: quote::MarketTradePeriod) -> Result<Self> {
        Ok(Self {
            market: value.market.parse().unwrap_or_default(),
            trade_sessions: value
                .trade_session
                .into_iter()
                .map(TryInto::try_into)
                .collect::<Result<Vec<_>>>()?,
        })
    }
}

/// Market trading days
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketTradingDays {
    /// Trading days
    pub trading_days: Vec<Date>,
    /// Half trading days
    pub half_trading_days: Vec<Date>,
}

/// Capital flow line
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CapitalFlowLine {
    /// Inflow capital data
    pub inflow: Decimal,
    /// Time
    pub timestamp: OffsetDateTime,
}

impl TryFrom<quote::capital_flow_intraday_response::CapitalFlowLine> for CapitalFlowLine {
    type Error = Error;

    fn try_from(value: quote::capital_flow_intraday_response::CapitalFlowLine) -> Result<Self> {
        Ok(Self {
            inflow: value.inflow.parse().unwrap_or_default(),
            timestamp: OffsetDateTime::from_unix_timestamp(value.timestamp)
                .map_err(|err| Error::parse_field_error("timestamp", err))?,
        })
    }
}

/// Capital distribution
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CapitalDistribution {
    /// Large order
    pub large: Decimal,
    /// Medium order
    pub medium: Decimal,
    /// Small order
    pub small: Decimal,
}

impl TryFrom<quote::capital_distribution_response::CapitalDistribution> for CapitalDistribution {
    type Error = Error;

    fn try_from(value: quote::capital_distribution_response::CapitalDistribution) -> Result<Self> {
        Ok(Self {
            large: value.large.parse().unwrap_or_default(),
            medium: value.medium.parse().unwrap_or_default(),
            small: value.small.parse().unwrap_or_default(),
        })
    }
}

/// Capital distribution response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CapitalDistributionResponse {
    /// Time
    pub timestamp: OffsetDateTime,
    /// Inflow capital data
    pub capital_in: CapitalDistribution,
    /// Outflow capital data
    pub capital_out: CapitalDistribution,
}

impl TryFrom<quote::CapitalDistributionResponse> for CapitalDistributionResponse {
    type Error = Error;

    fn try_from(value: quote::CapitalDistributionResponse) -> Result<Self> {
        Ok(Self {
            timestamp: OffsetDateTime::from_unix_timestamp(value.timestamp)
                .map_err(|err| Error::parse_field_error("timestamp", err))?,
            capital_in: value
                .capital_in
                .map(TryInto::try_into)
                .transpose()?
                .unwrap_or_default(),
            capital_out: value
                .capital_out
                .map(TryInto::try_into)
                .transpose()?
                .unwrap_or_default(),
        })
    }
}

/// Watchlist security
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WatchlistSecurity {
    /// Security symbol
    pub symbol: String,
    /// Market
    pub market: Market,
    /// Security name
    pub name: String,
    /// Watched price
    #[serde(with = "serde_utils::decimal_opt_empty_is_none")]
    pub watched_price: Option<Decimal>,
    /// Watched time
    #[serde(with = "serde_utils::timestamp")]
    pub watched_at: OffsetDateTime,
}

/// Watchlist group
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WatchlistGroup {
    /// Group id
    #[serde(with = "serde_utils::int64_str")]
    pub id: i64,
    /// Group name
    pub name: String,
    /// Securities
    pub securities: Vec<WatchlistSecurity>,
}

impl_default_for_enum_string!(OptionType, OptionDirection, WarrantType, SecurityBoard);

/// An request for create watchlist group
#[derive(Debug, Clone)]
pub struct RequestCreateWatchlistGroup {
    /// Group name
    pub name: String,
    /// Securities
    pub securities: Option<Vec<String>>,
}

impl RequestCreateWatchlistGroup {
    /// Create a new request for create watchlist group
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            securities: None,
        }
    }

    /// Set securities to the request
    pub fn securities<I, T>(self, securities: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: Into<String>,
    {
        Self {
            securities: Some(securities.into_iter().map(Into::into).collect()),
            ..self
        }
    }
}

/// Securities update mode
#[derive(Debug, Copy, Clone, Default, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum SecuritiesUpdateMode {
    /// Add securities
    Add,
    /// Remove securities
    Remove,
    /// Replace securities
    #[default]
    Replace,
}

/// An request for update watchlist group
#[derive(Debug, Clone)]
pub struct RequestUpdateWatchlistGroup {
    /// Group id
    pub id: i64,
    /// Group name
    pub name: Option<String>,
    /// Securities
    pub securities: Option<Vec<String>>,
    /// Securities Update mode
    pub mode: SecuritiesUpdateMode,
}

impl RequestUpdateWatchlistGroup {
    /// Create a new request for update watchlist group
    #[inline]
    pub fn new(id: i64) -> Self {
        Self {
            id,
            name: None,
            securities: None,
            mode: SecuritiesUpdateMode::default(),
        }
    }

    /// Set group name to the request
    pub fn name(self, name: impl Into<String>) -> Self {
        Self {
            name: Some(name.into()),
            ..self
        }
    }

    /// Set securities to the request
    pub fn securities<I, T>(self, securities: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: Into<String>,
    {
        Self {
            securities: Some(securities.into_iter().map(Into::into).collect()),
            ..self
        }
    }

    /// Set securities update mode to the request
    pub fn mode(self, mode: SecuritiesUpdateMode) -> Self {
        Self { mode, ..self }
    }
}

/// Calc index
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum CalcIndex {
    /// Latest price
    LastDone,
    /// Change value
    ChangeValue,
    /// Change rate
    ChangeRate,
    /// Volume
    Volume,
    /// Turnover
    Turnover,
    /// Year-to-date change ratio
    YtdChangeRate,
    /// Turnover rate
    TurnoverRate,
    /// Total market value
    TotalMarketValue,
    /// Capital flow
    CapitalFlow,
    /// Amplitude
    Amplitude,
    /// Volume ratio
    VolumeRatio,
    /// PE (TTM)
    PeTtmRatio,
    /// PB
    PbRatio,
    /// Dividend ratio (TTM)
    DividendRatioTtm,
    /// Five days change ratio
    FiveDayChangeRate,
    /// Ten days change ratio
    TenDayChangeRate,
    /// Half year change ratio
    HalfYearChangeRate,
    /// Five minutes change ratio
    FiveMinutesChangeRate,
    /// Expiry date
    ExpiryDate,
    /// Strike price
    StrikePrice,
    /// Upper bound price
    UpperStrikePrice,
    /// Lower bound price
    LowerStrikePrice,
    /// Outstanding quantity
    OutstandingQty,
    /// Outstanding ratio
    OutstandingRatio,
    /// Premium
    Premium,
    /// In/out of the bound
    ItmOtm,
    /// Implied volatility
    ImpliedVolatility,
    /// Warrant delta
    WarrantDelta,
    /// Call price
    CallPrice,
    /// Price interval from the call price
    ToCallPrice,
    /// Effective leverage
    EffectiveLeverage,
    /// Leverage ratio
    LeverageRatio,
    /// Conversion ratio
    ConversionRatio,
    /// Breakeven point
    BalancePoint,
    /// Open interest
    OpenInterest,
    /// Delta
    Delta,
    /// Gamma
    Gamma,
    /// Theta
    Theta,
    /// Vega
    Vega,
    /// Rho
    Rho,
}

impl From<CalcIndex> for longport_proto::quote::CalcIndex {
    fn from(value: CalcIndex) -> Self {
        use longport_proto::quote::CalcIndex::*;

        match value {
            CalcIndex::LastDone => CalcindexLastDone,
            CalcIndex::ChangeValue => CalcindexChangeVal,
            CalcIndex::ChangeRate => CalcindexChangeRate,
            CalcIndex::Volume => CalcindexVolume,
            CalcIndex::Turnover => CalcindexTurnover,
            CalcIndex::YtdChangeRate => CalcindexYtdChangeRate,
            CalcIndex::TurnoverRate => CalcindexTurnoverRate,
            CalcIndex::TotalMarketValue => CalcindexTotalMarketValue,
            CalcIndex::CapitalFlow => CalcindexCapitalFlow,
            CalcIndex::Amplitude => CalcindexAmplitude,
            CalcIndex::VolumeRatio => CalcindexVolumeRatio,
            CalcIndex::PeTtmRatio => CalcindexPeTtmRatio,
            CalcIndex::PbRatio => CalcindexPbRatio,
            CalcIndex::DividendRatioTtm => CalcindexDividendRatioTtm,
            CalcIndex::FiveDayChangeRate => CalcindexFiveDayChangeRate,
            CalcIndex::TenDayChangeRate => CalcindexTenDayChangeRate,
            CalcIndex::HalfYearChangeRate => CalcindexHalfYearChangeRate,
            CalcIndex::FiveMinutesChangeRate => CalcindexFiveMinutesChangeRate,
            CalcIndex::ExpiryDate => CalcindexExpiryDate,
            CalcIndex::StrikePrice => CalcindexStrikePrice,
            CalcIndex::UpperStrikePrice => CalcindexUpperStrikePrice,
            CalcIndex::LowerStrikePrice => CalcindexLowerStrikePrice,
            CalcIndex::OutstandingQty => CalcindexOutstandingQty,
            CalcIndex::OutstandingRatio => CalcindexOutstandingRatio,
            CalcIndex::Premium => CalcindexPremium,
            CalcIndex::ItmOtm => CalcindexItmOtm,
            CalcIndex::ImpliedVolatility => CalcindexImpliedVolatility,
            CalcIndex::WarrantDelta => CalcindexWarrantDelta,
            CalcIndex::CallPrice => CalcindexCallPrice,
            CalcIndex::ToCallPrice => CalcindexToCallPrice,
            CalcIndex::EffectiveLeverage => CalcindexEffectiveLeverage,
            CalcIndex::LeverageRatio => CalcindexLeverageRatio,
            CalcIndex::ConversionRatio => CalcindexConversionRatio,
            CalcIndex::BalancePoint => CalcindexBalancePoint,
            CalcIndex::OpenInterest => CalcindexOpenInterest,
            CalcIndex::Delta => CalcindexDelta,
            CalcIndex::Gamma => CalcindexGamma,
            CalcIndex::Theta => CalcindexTheta,
            CalcIndex::Vega => CalcindexVega,
            CalcIndex::Rho => CalcindexRho,
        }
    }
}

/// Security calc index response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityCalcIndex {
    /// Security code
    pub symbol: String,
    /// Latest price
    pub last_done: Option<Decimal>,
    /// Change value
    pub change_value: Option<Decimal>,
    /// Change ratio
    pub change_rate: Option<Decimal>,
    /// Volume
    pub volume: Option<i64>,
    /// Turnover
    pub turnover: Option<Decimal>,
    /// Year-to-date change ratio
    pub ytd_change_rate: Option<Decimal>,
    /// Turnover rate
    pub turnover_rate: Option<Decimal>,
    /// Total market value
    pub total_market_value: Option<Decimal>,
    /// Capital flow
    pub capital_flow: Option<Decimal>,
    /// Amplitude
    pub amplitude: Option<Decimal>,
    /// Volume ratio
    pub volume_ratio: Option<Decimal>,
    /// PE (TTM)
    pub pe_ttm_ratio: Option<Decimal>,
    /// PB
    pub pb_ratio: Option<Decimal>,
    /// Dividend ratio (TTM)
    pub dividend_ratio_ttm: Option<Decimal>,
    /// Five days change ratio
    pub five_day_change_rate: Option<Decimal>,
    /// Ten days change ratio
    pub ten_day_change_rate: Option<Decimal>,
    /// Half year change ratio
    pub half_year_change_rate: Option<Decimal>,
    /// Five minutes change ratio
    pub five_minutes_change_rate: Option<Decimal>,
    /// Expiry date
    pub expiry_date: Option<Date>,
    /// Strike price
    pub strike_price: Option<Decimal>,
    /// Upper bound price
    pub upper_strike_price: Option<Decimal>,
    /// Lower bound price
    pub lower_strike_price: Option<Decimal>,
    /// Outstanding quantity
    pub outstanding_qty: Option<i64>,
    /// Outstanding ratio
    pub outstanding_ratio: Option<Decimal>,
    /// Premium
    pub premium: Option<Decimal>,
    /// In/out of the bound
    pub itm_otm: Option<Decimal>,
    /// Implied volatility
    pub implied_volatility: Option<Decimal>,
    /// Warrant delta
    pub warrant_delta: Option<Decimal>,
    /// Call price
    pub call_price: Option<Decimal>,
    /// Price interval from the call price
    pub to_call_price: Option<Decimal>,
    /// Effective leverage
    pub effective_leverage: Option<Decimal>,
    /// Leverage ratio
    pub leverage_ratio: Option<Decimal>,
    /// Conversion ratio
    pub conversion_ratio: Option<Decimal>,
    /// Breakeven point
    pub balance_point: Option<Decimal>,
    /// Open interest
    pub open_interest: Option<i64>,
    /// Delta
    pub delta: Option<Decimal>,
    /// Gamma
    pub gamma: Option<Decimal>,
    /// Theta
    pub theta: Option<Decimal>,
    /// Vega
    pub vega: Option<Decimal>,
    /// Rho
    pub rho: Option<Decimal>,
}

impl SecurityCalcIndex {
    pub(crate) fn from_proto(
        resp: longport_proto::quote::SecurityCalcIndex,
        indexes: &[CalcIndex],
    ) -> Self {
        let mut output = SecurityCalcIndex {
            symbol: resp.symbol,
            last_done: None,
            change_value: None,
            change_rate: None,
            volume: None,
            turnover: None,
            ytd_change_rate: None,
            turnover_rate: None,
            total_market_value: None,
            capital_flow: None,
            amplitude: None,
            volume_ratio: None,
            pe_ttm_ratio: None,
            pb_ratio: None,
            dividend_ratio_ttm: None,
            five_day_change_rate: None,
            ten_day_change_rate: None,
            half_year_change_rate: None,
            five_minutes_change_rate: None,
            expiry_date: None,
            strike_price: None,
            upper_strike_price: None,
            lower_strike_price: None,
            outstanding_qty: None,
            outstanding_ratio: None,
            premium: None,
            itm_otm: None,
            implied_volatility: None,
            warrant_delta: None,
            call_price: None,
            to_call_price: None,
            effective_leverage: None,
            leverage_ratio: None,
            conversion_ratio: None,
            balance_point: None,
            open_interest: None,
            delta: None,
            gamma: None,
            theta: None,
            vega: None,
            rho: None,
        };

        for index in indexes {
            match index {
                CalcIndex::LastDone => output.last_done = resp.last_done.parse().ok(),
                CalcIndex::ChangeValue => output.change_value = resp.change_val.parse().ok(),
                CalcIndex::ChangeRate => output.change_rate = resp.change_rate.parse().ok(),
                CalcIndex::Volume => output.volume = Some(resp.volume),
                CalcIndex::Turnover => output.turnover = resp.turnover.parse().ok(),
                CalcIndex::YtdChangeRate => {
                    output.ytd_change_rate = resp.ytd_change_rate.parse().ok()
                }
                CalcIndex::TurnoverRate => output.turnover_rate = resp.turnover_rate.parse().ok(),
                CalcIndex::TotalMarketValue => {
                    output.total_market_value = resp.total_market_value.parse().ok()
                }
                CalcIndex::CapitalFlow => output.capital_flow = resp.capital_flow.parse().ok(),
                CalcIndex::Amplitude => output.amplitude = resp.amplitude.parse().ok(),
                CalcIndex::VolumeRatio => output.volume_ratio = resp.volume_ratio.parse().ok(),
                CalcIndex::PeTtmRatio => output.pe_ttm_ratio = resp.pe_ttm_ratio.parse().ok(),
                CalcIndex::PbRatio => output.pb_ratio = resp.pb_ratio.parse().ok(),
                CalcIndex::DividendRatioTtm => {
                    output.dividend_ratio_ttm = resp.dividend_ratio_ttm.parse().ok()
                }
                CalcIndex::FiveDayChangeRate => {
                    output.five_day_change_rate = resp.five_day_change_rate.parse().ok()
                }
                CalcIndex::TenDayChangeRate => {
                    output.ten_day_change_rate = resp.ten_day_change_rate.parse().ok()
                }
                CalcIndex::HalfYearChangeRate => {
                    output.half_year_change_rate = resp.half_year_change_rate.parse().ok()
                }
                CalcIndex::FiveMinutesChangeRate => {
                    output.five_minutes_change_rate = resp.five_minutes_change_rate.parse().ok()
                }
                CalcIndex::ExpiryDate => output.expiry_date = parse_date(&resp.expiry_date).ok(),
                CalcIndex::StrikePrice => output.strike_price = resp.strike_price.parse().ok(),
                CalcIndex::UpperStrikePrice => {
                    output.upper_strike_price = resp.upper_strike_price.parse().ok()
                }
                CalcIndex::LowerStrikePrice => {
                    output.lower_strike_price = resp.lower_strike_price.parse().ok()
                }
                CalcIndex::OutstandingQty => output.outstanding_qty = Some(resp.outstanding_qty),
                CalcIndex::OutstandingRatio => {
                    output.outstanding_ratio = resp.outstanding_ratio.parse().ok()
                }
                CalcIndex::Premium => output.premium = resp.premium.parse().ok(),
                CalcIndex::ItmOtm => output.itm_otm = resp.itm_otm.parse().ok(),
                CalcIndex::ImpliedVolatility => {
                    output.implied_volatility = resp.implied_volatility.parse().ok()
                }
                CalcIndex::WarrantDelta => output.warrant_delta = resp.warrant_delta.parse().ok(),
                CalcIndex::CallPrice => output.call_price = resp.call_price.parse().ok(),
                CalcIndex::ToCallPrice => output.to_call_price = resp.to_call_price.parse().ok(),
                CalcIndex::EffectiveLeverage => {
                    output.effective_leverage = resp.effective_leverage.parse().ok()
                }
                CalcIndex::LeverageRatio => {
                    output.leverage_ratio = resp.leverage_ratio.parse().ok()
                }
                CalcIndex::ConversionRatio => {
                    output.conversion_ratio = resp.conversion_ratio.parse().ok()
                }
                CalcIndex::BalancePoint => output.balance_point = resp.balance_point.parse().ok(),
                CalcIndex::OpenInterest => output.open_interest = Some(resp.open_interest),
                CalcIndex::Delta => output.delta = resp.delta.parse().ok(),
                CalcIndex::Gamma => output.gamma = resp.gamma.parse().ok(),
                CalcIndex::Theta => output.theta = resp.theta.parse().ok(),
                CalcIndex::Vega => output.vega = resp.vega.parse().ok(),
                CalcIndex::Rho => output.rho = resp.rho.parse().ok(),
            }
        }

        output
    }
}

/// Security list category
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, EnumString, Display)]
pub enum SecurityListCategory {
    /// Overnight
    Overnight,
}

impl_serialize_for_enum_string!(SecurityListCategory);

/// The basic information of securities
#[derive(Debug, Serialize, Deserialize)]
pub struct Security {
    /// Security code
    pub symbol: String,
    /// Security name (zh-CN)
    pub name_cn: String,
    /// Security name (en)
    pub name_en: String,
    /// Security name (zh-HK)
    pub name_hk: String,
}

/// Quote package detail
#[derive(Debug, Clone)]
pub struct QuotePackageDetail {
    /// Key
    pub key: String,
    /// Name
    pub name: String,
    /// Description
    pub description: String,
    /// Start time
    pub start_at: OffsetDateTime,
    /// End time
    pub end_at: OffsetDateTime,
}

impl TryFrom<quote::user_quote_level_detail::PackageDetail> for QuotePackageDetail {
    type Error = Error;

    fn try_from(quote: quote::user_quote_level_detail::PackageDetail) -> Result<Self> {
        Ok(Self {
            key: quote.key,
            name: quote.name,
            description: quote.description,
            start_at: OffsetDateTime::from_unix_timestamp(quote.start)
                .map_err(|err| Error::parse_field_error("start_at", err))?,
            end_at: OffsetDateTime::from_unix_timestamp(quote.end)
                .map_err(|err| Error::parse_field_error("end_at", err))?,
        })
    }
}