trading-maid 1.0.2

A high-fidelity crypto futures backtesting and live trading framework with matching, margin, leverage and liquidation simulation.
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
use crate::data::{DataSource, KLine, Level};
use crate::order::{HistoryPosition, HistoryPositionSummary, OrderMessage, Side};
use anyhow::Context;
use anyhow::bail;
use chrono::{
    DateTime, Datelike, Duration, Local, Months, NaiveDate, NaiveDateTime, TimeZone, Timelike, Utc,
    Weekday,
};
use rand::RngExt;
use reqwest::Client;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use tokio::fs;
use tokio::io::AsyncWriteExt;
use zip::ZipArchive;

/// Downloads and merges K-line data from Binance into a single CSV file
///
/// # Arguments
/// - `format`       - Data format: `symbol/interval[/market_type]`
///   - Market type is optional, defaults to `futures`, can be `spot` or `futures`
///   - Examples: `"BTCUSDT/1m"` or `"ETHUSDT/1h/spot"`
///   - For interval format, see: `Level` enum.
/// - `month_count`  - Number of months to download, 0 means all available data (up to 120 months)
///
/// # Returns
/// - On success, returns the path to the merged CSV file
/// - Data storage location: `~/.trading-maid/<symbol>/<interval>.csv`
///
/// # Data Source
/// https://data.binance.vision
///
/// # Processing Pipeline
/// 1. Parse format string to extract symbol, interval, and market type
/// 2. Generate list of months to download (defaults to last 120 months)
/// 3. Check local cache for each month, download from Binance if missing
/// 4. Merge all monthly CSV files, removing duplicate headers
/// 5. Return path to merged file
///
/// # Note
/// During merging, only the first 6 columns are kept (timestamp, open, high, low, close, volume)
pub async fn get_or_download(format: &str, month_count: u32) -> anyhow::Result<PathBuf> {
    fn parse_symbol(input: &str) -> anyhow::Result<(&str, &str, &str)> {
        let part_list: Vec<&str> = input.split('/').collect();

        match part_list.as_slice() {
            [symbol, interval] => Ok((symbol, interval, "futures")),
            [symbol, interval, market_type]
                if *market_type == "spot" || *market_type == "futures" =>
            {
                Ok((symbol, interval, market_type))
            }
            _ => bail!("invalid format: '{}'", input),
        }
    }

    fn generate_month_list(count: u32) -> Vec<String> {
        let mut month_list = Vec::with_capacity(count as usize);

        let mut current_date = {
            let now = Utc::now();
            let previous_month = if now.month() > 1 { now.month() - 1 } else { 12 };
            let year = if now.month() == 1 {
                now.year() - 1
            } else {
                now.year()
            };

            chrono::NaiveDate::from_ymd_opt(year, previous_month, 1).unwrap()
        };

        for _ in 0..count {
            month_list.push(current_date.format("%Y-%m").to_string());
            current_date = current_date.pred_opt().unwrap().with_day(1).unwrap();
        }

        month_list.reverse();

        month_list
    }

    fn build_download_url(
        base_url: &str,
        symbol: &str,
        interval: &str,
        year_month: &str,
        market_type: &str,
    ) -> String {
        let url_prefix = if market_type == "spot" {
            "spot/monthly"
        } else {
            "futures/um/monthly"
        };

        format!(
            "{}/{}/klines/{}/{}/{}-{}-{}.zip",
            base_url, url_prefix, symbol, interval, symbol, interval, year_month
        )
    }

    async fn download_monthly_data(
        http_client: &Client,
        url: &str,
    ) -> anyhow::Result<Option<String>> {
        #[cfg(debug_assertions)]
        println!("download: {}", url);

        let response = http_client.get(url).send().await?;

        if response.status() == 404 {
            return Ok(None);
        }

        response.error_for_status_ref()?;

        let response_bytes = response.bytes().await?;

        let mut zip_archive = ZipArchive::new(std::io::Cursor::new(response_bytes))?;

        let csv_filename = zip_archive
            .file_names()
            .find(|filename| filename.ends_with(".csv"))
            .context("no csv in zip")?
            .to_owned();

        let mut csv_content = String::new();

        zip_archive
            .by_name(&csv_filename)?
            .read_to_string(&mut csv_content)?;

        Ok(Some(csv_content))
    }

    let (base_symbol, interval, market_type) = parse_symbol(format)?;

    let base_data_directory = dirs::home_dir()
        .map(|home_directory| home_directory.join(".trading-maid"))
        .context("can not find data dir")?;

    let symbol_directory = base_data_directory.join(base_symbol);
    let monthly_directory = symbol_directory.join(interval);
    let merged_file_path = symbol_directory.join(format!("{}.csv", interval));
    let marged_lock_path = symbol_directory.join(format!("{}.lock", interval));

    fs::create_dir_all(&monthly_directory).await?;

    let month_list = if month_count == 0 {
        generate_month_list(120)
    } else {
        generate_month_list(month_count)
    };

    let http_client = Client::builder()
        .timeout(std::time::Duration::from_secs(300))
        .build()?;

    const BASE_URL: &str = "https://data.binance.vision/data";

    for v in &month_list {
        let monthly_file_path = monthly_directory.join(format!("{}.csv", v));

        if monthly_file_path.exists() {
            continue;
        }

        let download_url = build_download_url(BASE_URL, base_symbol, interval, v, market_type);

        match download_monthly_data(&http_client, &download_url).await {
            Ok(Some(v)) => {
                fs::write(&monthly_file_path, v.as_bytes()).await?;
                tokio::fs::write(&marged_lock_path, "").await?;
            }
            Ok(None) => {
                #[cfg(debug_assertions)]
                println!("{} not available (404)", v)
            }
            Err(error) => {
                bail!(
                    "failed to download {}: {}",
                    v,
                    error.to_string().to_lowercase()
                );
            }
        }
    }

    async fn merge_monthly_files(
        monthly_directory: &Path,
        output_file_path: &Path,
        marged_lock_path: &Path,
    ) -> anyhow::Result<()> {
        if !marged_lock_path.exists() && output_file_path.exists() {
            return Ok(());
        }

        let mut csv_file_list = Vec::new();

        let mut directory_reader = fs::read_dir(monthly_directory).await?;

        while let Some(v) = directory_reader.next_entry().await? {
            let file_path = v.path();

            if file_path
                .extension()
                .is_some_and(|extension| extension == "csv")
            {
                csv_file_list.push(file_path);
            }
        }

        csv_file_list.sort();

        if csv_file_list.is_empty() {
            bail!(
                "no monthly files found in {:?}",
                monthly_directory.to_string_lossy().to_lowercase()
            );
        }

        let mut output_file = fs::File::create(output_file_path).await?;

        for v in csv_file_list.iter() {
            let file_content = fs::read_to_string(v).await?;

            let mut content_lines = file_content.lines();

            _ = content_lines.next();

            for v in content_lines {
                let Some((pos, _)) = v.match_indices(',').nth(5) else {
                    bail!("parse csv error: less than 6 commas");
                };

                output_file.write_all(&v.as_bytes()[..pos]).await?;
                output_file.write_all(b"\n").await?;
            }
        }

        if marged_lock_path.exists() {
            std::fs::remove_file(marged_lock_path)?;
        }

        Ok(())
    }

    merge_monthly_files(&monthly_directory, &merged_file_path, &marged_lock_path).await?;

    Ok(merged_file_path)
}

/// Calculates the liquidation price for a given position based on leverage, maintenance margin,
/// position side (buy or sell), the current price, position quantity, and available margin.
///
/// The liquidation price is the price at which the position will be closed to prevent further loss,
/// given the provided leverage, maintenance margin, and current account margin.
///
/// # Arguments
///
/// - `leverage`: The leverage used in the position. It is a multiplier that amplifies the exposure to the market.
/// - `maintenance`: The maintenance margin requirement, expressed as a percentage.
/// - `side`: The side of the position, either `Side::Buy` or `Side::Sell`. Determines the calculation logic.
/// - `price`: The current market price of the asset.
/// - `quantity`: The amount of the asset in the position.
/// - `margin`: The current margin available in the account to support the position.
///
/// # Returns
///
/// - The liquidation price as a floating-point number (`f64`). This price represents the point at which the position
///   will be liquidated if the market moves unfavorably, considering the leverage and maintenance margin.
///
/// # Calculation Logic
///
/// 1. The initial margin is calculated by dividing the position's value (`price * quantity`) by the leverage.
/// 2. The additional margin (`append_margin`) is the difference between the available margin and the initial margin.
/// 3. Depending on whether the position is a `Buy` or `Sell`:
///    - For a `Buy`, the liquidation price is adjusted downwards based on the initial margin rate and the maintenance margin.
///    - For a `Sell`, the liquidation price is adjusted upwards similarly, considering the maintenance margin.
pub fn calc_liquidation_price(
    leverage: u32,
    maintenance: f64,
    side: Side,
    price: f64,
    quantity: f64,
    margin: f64,
) -> f64 {
    let initial_margin_rate = 1.0 / leverage as f64;
    let initial_margin = calc_initial_margin(price, quantity, leverage);
    let append_margin = margin - initial_margin;

    if side == Side::Buy {
        price * (1.0 - initial_margin_rate + maintenance) - ((append_margin) / quantity)
    } else {
        price * (1.0 + initial_margin_rate - maintenance) + ((append_margin) / quantity)
    }
}

/// Calculates the initial margin required for a position based on its price, quantity, and leverage.
///
/// # Arguments
///
/// - `price`: The price of the asset.
/// - `quantity`: The amount of the asset in the position.
/// - `leverage`: The leverage used in the position.
///
/// # Returns
///
/// The initial margin as a floating-point number (`f64`).
pub fn calc_initial_margin(price: f64, quantity: f64, leverage: u32) -> f64 {
    price * quantity / leverage as f64
}

/// Calculates the slippage-adjusted price range based on the current price and slippage percentage.
///
/// # Arguments
///
/// - `price`: The current price of the asset.
/// - `slippage`: The slippage percentage.
///
/// # Returns
///
/// A tuple containing the minimum and maximum prices after accounting for slippage.
pub fn calc_slippage_price(price: f64, slippage: f64) -> (f64, f64) {
    let factor = slippage / 100.0;
    let min_price = price * (1.0 - factor);
    let max_price = price * (1.0 + factor);

    (min_price, max_price)
}

/// Generates a random price within the slippage-adjusted price range, snapped to the nearest tick size.
pub fn get_random_slippage_price(price: f64, slippage: f64, tick_size: f64) -> Option<f64> {
    let (min, max) = calc_slippage_price(price, slippage);

    if tick_size <= 0.0 || min > max {
        return None;
    }

    let min = (min / tick_size).round() as i64;
    let max = (max / tick_size).round() as i64;

    if min > max {
        return None;
    }

    Some(rand::rng().random_range(min..=max) as f64 * tick_size)
}

/// Checks if a given price value is aligned with the specified tick size, considering floating-point precision issues.
pub fn is_tick_aligned(value: f64, tick_size: f64) -> bool {
    if !value.is_finite() || !tick_size.is_finite() || tick_size <= 0.0 {
        return false;
    }

    let steps = value / tick_size;
    let tolerance = 1e-9_f64.max(f64::EPSILON * steps.abs() * 16.0);

    (steps - steps.round()).abs() <= tolerance
}

pub trait TickSnap {
    fn snap_to_tick(self, tick_size: f64) -> f64;
    fn snap_eq(self, other: f64, tick_size: f64) -> bool;
    fn snap_lt(self, other: f64, tick_size: f64) -> bool;
    fn snap_gt(self, other: f64, tick_size: f64) -> bool;
    fn snap_le(self, other: f64, tick_size: f64) -> bool;
    fn snap_ge(self, other: f64, tick_size: f64) -> bool;
    fn is_zero(self, tick_size: f64) -> bool;
}

impl TickSnap for f64 {
    fn is_zero(self, tick_size: f64) -> bool {
        if !tick_size.is_finite() || tick_size <= 0.0 {
            return self == 0.0;
        }

        self.abs() <= tick_size / 2.0
    }

    fn snap_to_tick(self, tick_size: f64) -> f64 {
        if !tick_size.is_finite() || tick_size <= 0.0 {
            return self;
        }

        (self / tick_size).round() * tick_size
    }

    fn snap_eq(self, other: f64, tick_size: f64) -> bool {
        if !tick_size.is_finite() || tick_size <= 0.0 {
            return self == other;
        }

        (self - other).abs() <= tick_size / 2.0
    }

    fn snap_lt(self, other: f64, tick_size: f64) -> bool {
        if !tick_size.is_finite() || tick_size <= 0.0 {
            return self < other;
        }

        self < other - tick_size / 2.0
    }

    fn snap_gt(self, other: f64, tick_size: f64) -> bool {
        if !tick_size.is_finite() || tick_size <= 0.0 {
            return self > other;
        }

        self > other + tick_size / 2.0
    }

    fn snap_le(self, other: f64, tick_size: f64) -> bool {
        self.snap_lt(other, tick_size) || self.snap_eq(other, tick_size)
    }

    fn snap_ge(self, other: f64, tick_size: f64) -> bool {
        self.snap_gt(other, tick_size) || self.snap_eq(other, tick_size)
    }
}

/// Converts a Unix timestamp in milliseconds to a formatted date-time string
/// in the **system local timezone**.
/// Returns the formatted string in `%Y/%m/%d %H:%M:%S%.f` format.
pub fn t2s(time: impl Into<u64>) -> String {
    Local
        .timestamp_millis_opt(time.into() as i64)
        .single()
        .unwrap_or_default()
        .format("%Y/%m/%d %H:%M:%S%.f")
        .to_string()
}

/// Converts a formatted date-time string (in local timezone) to Unix timestamp in milliseconds.
/// Input format: `%Y/%m/%d %H:%M:%S` or `%Y/%m/%d %H:%M:%S%.f`
/// Returns timestamp in milliseconds. Returns `0` if parsing fails.
pub fn s2t(time: impl AsRef<str>) -> u64 {
    NaiveDateTime::parse_from_str(time.as_ref(), "%Y/%m/%d %H:%M:%S%.f")
        .map(|v| {
            Local
                .from_local_datetime(&v)
                .single()
                .unwrap_or_default()
                .timestamp_millis() as u64
        })
        .unwrap_or(0)
}

/// Converts a formatted date-time string (in UTC timezone) to Unix timestamp in milliseconds.
/// Input format: `%Y/%m/%d %H:%M:%S` or `%Y/%m/%d %H:%M:%S%.f`
/// Returns timestamp in milliseconds. Returns `0` if parsing fails.
pub fn s2t_utc(time: impl AsRef<str>) -> u64 {
    NaiveDateTime::parse_from_str(time.as_ref(), "%Y/%m/%d %H:%M:%S%.f")
        .map(|v| v.and_utc().timestamp_millis() as u64)
        .unwrap_or(0)
}

/// Converts a Unix timestamp in milliseconds to a formatted date-time string in UTC timezone.
/// Returns the formatted string in `%Y/%m/%d %H:%M:%S%.f` format.
pub fn t2s_utc(time: impl Into<u64>) -> String {
    DateTime::<Utc>::from_timestamp_millis(time.into() as i64)
        .unwrap_or_default()
        .format("%Y/%m/%d %H:%M:%S%.f")
        .to_string()
}

/// Given an arbitrary time `time`, calculate the end time of a larger time level `max_level`
/// within the context of a smaller time level `min_level`.
/// If `max_level` and `min_level` are the same, return the current time.
/// If `max_level` is not a valid sampling target for `min_level`, return 0.
///
/// # Parameters
/// - `time`: The timestamp of the current k-line.
/// - `min_level`: The minimum time level to calculate the end time for.
/// - `max_level`: The time level to which the `time` should be adjusted.
///
/// # Returns
/// The end timestamp for the given `time` at the specified `min_level`, or `0` if the levels are incompatible.
///
/// # Example:
/// Let’s say the `min_level` is `Level::Minute1`, and `max_level` is `Level::Hour1`.
/// Given `time = 1678845600000` (representing `2023/03/15 10:00:00`).
/// The function should return `1678849599000`, which represents the timestamp for `2023/03/15 10:59:00`.
pub fn get_last_time(time: u64, min_level: Level, max_level: Level) -> anyhow::Result<u64> {
    if !min_level.is_valid_sampling_target(max_level) {
        bail!(
            "invalid sampling target level: min_level: {}, max_level: {}",
            min_level,
            max_level
        );
    }

    if max_level == min_level {
        return Ok(time);
    }

    Ok(get_time_range(get_time_range(time, max_level)?.1 - 1, min_level)?.0)
}

/// Calculates the start time of the current k-line time period and the start time of the next time period
/// based on the specified time level.
///
/// # Parameters
/// - `time`: The timestamp of the current k-line.
/// - `level`: The time level to convert to.
///
/// # Returns
/// A tuple containing:
/// - The start timestamp of the current k-line time period.
/// - The start timestamp of the next k-line time period.
pub fn get_time_range(time: u64, level: Level) -> anyhow::Result<(u64, u64)> {
    match level {
        Level::Minute1 => {
            let start = DateTime::from_timestamp_millis(time as i64)
                .context("get_time_range")?
                .with_second(0)
                .context("get_time_range")?
                .with_nanosecond(0)
                .context("get_time_range")?;

            let next = start + Duration::minutes(1);

            Ok((
                start.timestamp_millis() as u64,
                next.timestamp_millis() as u64,
            ))
        }
        Level::Minute3 => {
            let dt = DateTime::from_timestamp_millis(time as i64)
                .context("get_time_range")?
                .with_second(0)
                .context("get_time_range")?
                .with_nanosecond(0)
                .context("get_time_range")?;

            let start = dt - Duration::minutes((dt.minute() as i32 % 3) as i64);
            let next = start + Duration::minutes(3);

            Ok((
                start.timestamp_millis() as u64,
                next.timestamp_millis() as u64,
            ))
        }
        Level::Minute5 => {
            let dt = DateTime::from_timestamp_millis(time as i64)
                .context("get_time_range")?
                .with_second(0)
                .context("get_time_range")?
                .with_nanosecond(0)
                .context("get_time_range")?;

            let start = dt - Duration::minutes((dt.minute() as i32 % 5) as i64);
            let next = start + Duration::minutes(5);

            Ok((
                start.timestamp_millis() as u64,
                next.timestamp_millis() as u64,
            ))
        }
        Level::Minute15 => {
            let dt = DateTime::from_timestamp_millis(time as i64)
                .context("get_time_range")?
                .with_second(0)
                .context("get_time_range")?
                .with_nanosecond(0)
                .context("get_time_range")?;

            let start = dt - Duration::minutes((dt.minute() as i32 % 15) as i64);
            let next = start + Duration::minutes(15);

            Ok((
                start.timestamp_millis() as u64,
                next.timestamp_millis() as u64,
            ))
        }
        Level::Minute30 => {
            let dt = DateTime::from_timestamp_millis(time as i64)
                .context("get_time_range")?
                .with_second(0)
                .context("get_time_range")?
                .with_nanosecond(0)
                .context("get_time_range")?;

            let start = dt - Duration::minutes((dt.minute() as i32 % 30) as i64);
            let next = start + Duration::minutes(30);

            Ok((
                start.timestamp_millis() as u64,
                next.timestamp_millis() as u64,
            ))
        }
        Level::Hour1 => {
            let start = DateTime::from_timestamp_millis(time as i64)
                .context("get_time_range")?
                .with_minute(0)
                .context("get_time_range")?
                .with_second(0)
                .context("get_time_range")?
                .with_nanosecond(0)
                .context("get_time_range")?;

            let next = start + Duration::hours(1);

            Ok((
                start.timestamp_millis() as u64,
                next.timestamp_millis() as u64,
            ))
        }
        Level::Hour2 => {
            let dt = DateTime::from_timestamp_millis(time as i64)
                .context("get_time_range")?
                .with_minute(0)
                .context("get_time_range")?
                .with_second(0)
                .context("get_time_range")?
                .with_nanosecond(0)
                .context("get_time_range")?;

            let start = dt - Duration::hours((dt.hour() as i32 % 2) as i64);
            let next = start + Duration::hours(2);

            Ok((
                start.timestamp_millis() as u64,
                next.timestamp_millis() as u64,
            ))
        }
        Level::Hour4 => {
            let dt = DateTime::from_timestamp_millis(time as i64)
                .context("get_time_range")?
                .with_minute(0)
                .context("get_time_range")?
                .with_second(0)
                .context("get_time_range")?
                .with_nanosecond(0)
                .context("get_time_range")?;

            let start = dt - Duration::hours((dt.hour() as i32 % 4) as i64);
            let next = start + Duration::hours(4);

            Ok((
                start.timestamp_millis() as u64,
                next.timestamp_millis() as u64,
            ))
        }
        Level::Hour6 => {
            let dt = DateTime::from_timestamp_millis(time as i64)
                .context("get_time_range")?
                .with_minute(0)
                .context("get_time_range")?
                .with_second(0)
                .context("get_time_range")?
                .with_nanosecond(0)
                .context("get_time_range")?;

            let start = dt - Duration::hours((dt.hour() as i32 % 6) as i64);
            let next = start + Duration::hours(6);

            Ok((
                start.timestamp_millis() as u64,
                next.timestamp_millis() as u64,
            ))
        }
        Level::Hour12 => {
            let dt = DateTime::from_timestamp_millis(time as i64)
                .context("get_time_range")?
                .with_minute(0)
                .context("get_time_range")?
                .with_second(0)
                .context("get_time_range")?
                .with_nanosecond(0)
                .context("get_time_range")?;

            let start = dt - Duration::hours((dt.hour() as i32 % 12) as i64);
            let next = start + Duration::hours(12);

            Ok((
                start.timestamp_millis() as u64,
                next.timestamp_millis() as u64,
            ))
        }
        Level::Day1 => {
            let start = DateTime::from_timestamp_millis(time as i64)
                .context("get_time_range")?
                .date_naive()
                .and_hms_opt(0, 0, 0)
                .context("get_time_range")?;

            let next = start + Duration::days(1);

            Ok((
                start.and_utc().timestamp_millis() as u64,
                next.and_utc().timestamp_millis() as u64,
            ))
        }
        Level::Day3 => {
            let count = DateTime::from_timestamp_millis(time as i64)
                .context("get_time_range")?
                .date_naive()
                .and_hms_opt(0, 0, 0)
                .context("get_time_range")?
                .num_days_from_ce();

            let start = NaiveDate::from_num_days_from_ce_opt(count / 3 * 3)
                .context("get_time_range")?
                .and_hms_opt(0, 0, 0)
                .context("get_time_range")?;

            let next = start + Duration::days(3);

            Ok((
                start.and_utc().timestamp_millis() as u64,
                next.and_utc().timestamp_millis() as u64,
            ))
        }
        Level::Week1 => {
            let start = DateTime::from_timestamp_millis(time as i64)
                .context("get_time_range")?
                .date_naive()
                .week(Weekday::Mon)
                .first_day()
                .and_hms_opt(0, 0, 0)
                .context("get_time_range")?;

            let next = start + Duration::weeks(1);

            Ok((
                start.and_utc().timestamp_millis() as u64,
                next.and_utc().timestamp_millis() as u64,
            ))
        }
        Level::Month1 => {
            let start = Datelike::with_day(
                &DateTime::from_timestamp_millis(time as i64)
                    .context("get_time_range")?
                    .date_naive(),
                1,
            )
            .context("get_time_range")?
            .and_hms_opt(0, 0, 0)
            .context("get_time_range")?;

            let next = start + Months::new(1);

            Ok((
                start.and_utc().timestamp_millis() as u64,
                next.and_utc().timestamp_millis() as u64,
            ))
        }
    }
}

/// Aggregates a lower time-level k-line array into a higher time-level k-line array.
///
/// # Note
/// The target time level must be an integer multiple of the original time level, otherwise the results may be inaccurate.
/// The function does not ensure that the first and last k-lines in the resulting array will align perfectly with the specified time level;
/// they may be partial k-lines if the input data does not cover a complete period for the given level.
///
/// # Parameters
/// - `array`: The input k-line array.
/// - `level`: The target time level.
///
/// # Returns
/// The aggregated k-line array.
pub fn resample(array: &[KLine], level: Level) -> anyhow::Result<Vec<KLine>> {
    let mut result = Vec::new();

    if array.is_empty() {
        return Ok(result);
    }

    let mut start_index = 0;

    while start_index < array.len() {
        let start_k = array[start_index];

        let (start_time, next_time) = get_time_range(start_k.time, level)?;

        let next_index = array[start_index..]
            .iter()
            .position(|v| v.time >= next_time)
            .map(|v| start_index + v)
            .unwrap_or(array.len());

        let last_k = array[..next_index].last().unwrap();

        let mut result_k = KLine {
            time: start_time,
            open: start_k.open,
            high: start_k.high,
            low: start_k.low,
            close: last_k.close,
            volume: 0.0,
        };

        for v in &array[start_index..next_index] {
            result_k.high = result_k.high.max(v.high);
            result_k.low = result_k.low.min(v.low);
            result_k.volume += v.volume;
        }

        result.push(result_k);

        start_index = next_index;
    }

    Ok(result)
}

/// Resamples k-line data from a file into multiple different time levels and writes them to new files.
///
/// This function reads a k-line data file, determines the current time level from the file name,
/// and generates aggregated k-line data for each of the following higher time levels. The resampled data is written
/// to new files, with each file corresponding to one of the higher time levels.
///
/// # Parameters
/// - `path`: The path to the input k-line data file. The file name should include the time level (e.g., `BTC-USDT-SWAP-Minute1.json`).
///
/// # Returns
/// Returns a result indicating success or failure (`anyhow::Result<()>`).
pub fn resample_file(path: impl AsRef<Path>) -> anyhow::Result<()> {
    let path = path.as_ref();

    let level = Level::from_str(
        path.file_name()
            .context("resample_file")?
            .to_string_lossy()
            .split('-')
            .next_back()
            .context("resample_file")?
            .split('.')
            .next()
            .context("resample_file")?,
    )?;

    let file_stem = path
        .file_stem()
        .context("resample_file")?
        .to_string_lossy()
        .replace(&format!("-{}", level), "");

    let extension = path
        .extension()
        .context("resample_file")?
        .to_string_lossy()
        .to_string();

    let ds = DataSource::from_file(path)?;

    let level_list = [
        Level::Minute1,
        Level::Minute3,
        Level::Minute5,
        Level::Minute15,
        Level::Minute30,
        Level::Hour1,
        Level::Hour2,
        Level::Hour4,
        Level::Hour6,
        Level::Hour12,
        Level::Day1,
        Level::Day3,
        Level::Week1,
        Level::Month1,
    ];

    for v in level_list.into_iter().filter(|&v| v > level) {
        ds.resample(v)?
            .write_file(path.with_file_name(format!("{}-{}.{}", file_stem, v, extension)))?;
    }

    Ok(())
}

/// Converts the provided data sources, history positions, and history orders into an HTML string that can be rendered in a web browser.
pub fn to_html(
    data_source: impl AsRef<[DataSource]>,
    history_position: impl AsRef<[HistoryPosition]>,
    history_order: impl AsRef<[OrderMessage]>,
) -> String {
    let text = format!(
        "<script>window.dataSourceList={};window.historyPositionList={};window.historyOrderList={}</script>",
        &serde_json::to_string(data_source.as_ref()).unwrap(),
        &serde_json::to_string(history_position.as_ref()).unwrap(),
        &serde_json::to_string(history_order.as_ref()).unwrap(),
    );

    include_str!("../web/dist/index.html").replace("<!-- template -->", &text)
}

/// Summarizes history positions into the same metrics used by the web summary panel.
pub fn summarize(list: impl AsRef<[HistoryPosition]>) -> HistoryPositionSummary {
    let list = list.as_ref();
    let symbol = list.first().map(|v| v.symbol.clone()).unwrap_or_default();
    let leverage = list.first().map(|v| v.leverage).unwrap_or_default();

    let total_trades = list.len();
    let total_profit = list.iter().map(|v| v.total_profit).sum::<f64>();
    let total_fee = list.iter().map(|v| v.fee).sum::<f64>();
    let win_trades = list.iter().filter(|v| v.total_profit > 0.0).count();
    let loss_trades = list.iter().filter(|v| v.total_profit < 0.0).count();

    let win_rate = if total_trades == 0 {
        0.0
    } else {
        win_trades as f64 / total_trades as f64 * 100.0
    };

    let avg_profit = if total_trades == 0 {
        0.0
    } else {
        total_profit / total_trades as f64
    };

    let net_gross_profit = list
        .iter()
        .filter(|v| v.total_profit > 0.0)
        .map(|v| v.total_profit)
        .sum::<f64>();

    let net_gross_loss_abs = -list
        .iter()
        .filter(|v| v.total_profit < 0.0)
        .map(|v| v.total_profit)
        .sum::<f64>();

    let profit_loss_ratio = if net_gross_loss_abs == 0.0 {
        0.0
    } else {
        net_gross_profit / net_gross_loss_abs
    };

    let gross_profit = list
        .iter()
        .filter(|v| v.profit > 0.0)
        .map(|v| v.profit)
        .sum::<f64>();

    let gross_loss_abs = -list
        .iter()
        .filter(|v| v.profit < 0.0)
        .map(|v| v.profit)
        .sum::<f64>();

    let best_trade = list
        .iter()
        .map(|v| v.total_profit)
        .reduce(f64::max)
        .unwrap_or_default();

    let worst_trade = list
        .iter()
        .map(|v| v.total_profit)
        .reduce(f64::min)
        .unwrap_or_default();

    HistoryPositionSummary {
        symbol,
        leverage,
        total_trades,
        win_rate,
        win_trades,
        loss_trades,
        total_profit,
        profit_loss_ratio,
        net_gross_profit,
        net_gross_loss_abs,
        gross_profit,
        gross_loss_abs,
        total_fee,
        avg_profit,
        best_trade,
        worst_trade,
    }
}

/// Generates an HTML file from the provided data sources, history positions, and history orders, and opens it in the default web browser.
pub fn open_in_browser(
    data_source: impl AsRef<[DataSource]>,
    history_position: impl AsRef<[HistoryPosition]>,
    history_order: impl AsRef<[OrderMessage]>,
) -> anyhow::Result<()> {
    let html_content = to_html(data_source, history_position, history_order);
    let temp_file_path = std::env::temp_dir().join("trading-maid.html");

    std::fs::write(&temp_file_path, html_content)?;
    webbrowser::open(temp_file_path.to_str().context("open_in_browser")?)?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn history_position(total_profit: f64, profit: f64, fee: f64) -> HistoryPosition {
        HistoryPosition {
            symbol: "BTCUSDT".to_string(),
            leverage: 10,
            side: Side::Buy,
            open_avg_price: 100.0,
            close_avg_price: 101.0,
            max_quantity: 1.0,
            close_quantity: 1.0,
            total_profit,
            profit,
            fee,
            open_time: 1,
            close_time: 2,
            log: vec![],
        }
    }

    #[test]
    fn ticksnap_snap_eq_respects_half_tick_boundary() {
        let tick = 0.1;

        assert!(1.04_f64.snap_eq(1.0, tick));
        assert!(1.049_f64.snap_eq(1.0, tick));
        assert!(!1.051_f64.snap_eq(1.0, tick));
    }

    #[test]
    fn ticksnap_snap_lt_gt_use_strict_half_tick_gap() {
        let tick = 0.1;

        assert!(0.949_f64.snap_lt(1.0, tick));
        assert!(!0.95_f64.snap_lt(1.0, tick));

        assert!(1.051_f64.snap_gt(1.0, tick));
        assert!(!1.05_f64.snap_gt(1.0, tick));
    }

    #[test]
    fn ticksnap_snap_le_ge_are_consistent_with_eq_lt_gt() {
        let tick = 0.1;

        assert!(1.0_f64.snap_le(1.0, tick));
        assert!(1.0_f64.snap_ge(1.0, tick));

        assert!(0.94_f64.snap_le(1.0, tick));
        assert!(!0.94_f64.snap_ge(1.0, tick));

        assert!(1.06_f64.snap_ge(1.0, tick));
        assert!(!1.06_f64.snap_le(1.0, tick));
    }

    #[test]
    fn ticksnap_snap_to_tick_rounds_and_invalid_tick_passthrough() {
        assert!((1.24_f64.snap_to_tick(0.1) - 1.2).abs() < 1e-12);
        assert!((1.26_f64.snap_to_tick(0.1) - 1.3).abs() < 1e-12);

        assert_eq!(1.2345_f64.snap_to_tick(0.0), 1.2345);
        assert_eq!(1.2345_f64.snap_to_tick(-0.1), 1.2345);
    }

    #[test]
    fn ticksnap_invalid_tick_falls_back_to_raw_comparison() {
        assert!(0.0_f64.is_zero(0.0));
        assert!(!0.0001_f64.is_zero(0.0));

        assert!(1.0_f64.snap_eq(1.0, 0.0));
        assert!(!1.0_f64.snap_eq(1.0000001, 0.0));

        assert!(0.9_f64.snap_lt(1.0, 0.0));
        assert!(1.1_f64.snap_gt(1.0, 0.0));
    }

    #[test]
    fn is_tick_aligned_works_for_aligned_and_non_aligned_prices() {
        assert!(is_tick_aligned(68000.1, 0.1));
        assert!(is_tick_aligned(68000.1000000001, 0.1));
        assert!(is_tick_aligned(0.1_f64 + 0.2_f64, 0.1));
        assert!(!is_tick_aligned(68000.123, 0.1));
        assert!(!is_tick_aligned(1.0, 0.0));
    }

    #[test]
    fn summarize_matches_web_fields() {
        let data = vec![
            history_position(100.0, 120.0, 20.0),
            history_position(-40.0, -30.0, 10.0),
            history_position(0.0, 5.0, 5.0),
        ];

        let summary = summarize(&data);

        assert_eq!(summary.symbol, "BTCUSDT");
        assert_eq!(summary.leverage, 10);
        assert_eq!(summary.total_trades, 3);
        assert_eq!(summary.win_trades, 1);
        assert_eq!(summary.loss_trades, 1);
        assert!((summary.win_rate - 33.333333).abs() < 1e-6);
        assert!((summary.total_profit - 60.0).abs() < 1e-12);
        assert!((summary.total_fee - 35.0).abs() < 1e-12);
        assert!((summary.avg_profit - 20.0).abs() < 1e-12);
        assert!((summary.net_gross_profit - 100.0).abs() < 1e-12);
        assert!((summary.net_gross_loss_abs - 40.0).abs() < 1e-12);
        assert!((summary.profit_loss_ratio - 2.5).abs() < 1e-12);
        assert!((summary.gross_profit - 125.0).abs() < 1e-12);
        assert!((summary.gross_loss_abs - 30.0).abs() < 1e-12);
        assert!((summary.best_trade - 100.0).abs() < 1e-12);
        assert!((summary.worst_trade + 40.0).abs() < 1e-12);
    }

    #[test]
    fn summarize_empty_returns_zero_values() {
        let summary = summarize([]);

        assert_eq!(summary.symbol, "");
        assert_eq!(summary.leverage, 0);
        assert_eq!(summary.total_trades, 0);
        assert_eq!(summary.win_trades, 0);
        assert_eq!(summary.loss_trades, 0);
        assert_eq!(summary.win_rate, 0.0);
        assert_eq!(summary.total_profit, 0.0);
        assert_eq!(summary.total_fee, 0.0);
        assert_eq!(summary.avg_profit, 0.0);
        assert_eq!(summary.net_gross_profit, 0.0);
        assert_eq!(summary.net_gross_loss_abs, 0.0);
        assert_eq!(summary.profit_loss_ratio, 0.0);
        assert_eq!(summary.gross_profit, 0.0);
        assert_eq!(summary.gross_loss_abs, 0.0);
        assert_eq!(summary.best_trade, 0.0);
        assert_eq!(summary.worst_trade, 0.0);
    }

    macro_rules! assert_time_range {
        ($time_str:expr, $level:expr, $expected_start:expr, $expected_end:expr) => {
            let time = s2t_utc($time_str);
            let (actual_start, actual_end) = get_time_range(time, $level).unwrap();
            assert_eq!(
                (t2s_utc(actual_start), t2s_utc(actual_end)),
                ($expected_start.to_string(), $expected_end.to_string()),
                "time: {}, level: {}",
                $time_str,
                $level
            );
        };
    }

    // ============ 分钟级别测试 ============

    #[test]
    fn test_minute1_basic() {
        assert_time_range!(
            "2024/03/21 10:23:45",
            Level::Minute1,
            "2024/03/21 10:23:00",
            "2024/03/21 10:24:00"
        );
    }

    #[test]
    fn test_minute1_boundary() {
        // 整分钟边界
        assert_time_range!(
            "2024/03/21 10:23:00",
            Level::Minute1,
            "2024/03/21 10:23:00",
            "2024/03/21 10:24:00"
        );
        // 59秒
        assert_time_range!(
            "2024/03/21 10:23:59.999",
            Level::Minute1,
            "2024/03/21 10:23:00",
            "2024/03/21 10:24:00"
        );
    }

    #[test]
    fn test_minute3_various_offsets() {
        // 分钟 % 3 == 0
        assert_time_range!(
            "2024/03/21 10:21:30",
            Level::Minute3,
            "2024/03/21 10:21:00",
            "2024/03/21 10:24:00"
        );
        // 分钟 % 3 == 1
        assert_time_range!(
            "2024/03/21 10:22:15",
            Level::Minute3,
            "2024/03/21 10:21:00",
            "2024/03/21 10:24:00"
        );
        // 分钟 % 3 == 2
        assert_time_range!(
            "2024/03/21 10:23:45",
            Level::Minute3,
            "2024/03/21 10:21:00",
            "2024/03/21 10:24:00"
        );
    }

    #[test]
    fn test_minute5_various_offsets() {
        assert_time_range!(
            "2024/03/21 10:27:30",
            Level::Minute5,
            "2024/03/21 10:25:00",
            "2024/03/21 10:30:00"
        );
        assert_time_range!(
            "2024/03/21 10:30:00",
            Level::Minute5,
            "2024/03/21 10:30:00",
            "2024/03/21 10:35:00"
        );
    }

    #[test]
    fn test_minute15_cross_hour() {
        // 跨小时边界
        assert_time_range!(
            "2024/03/21 10:59:30",
            Level::Minute15,
            "2024/03/21 10:45:00",
            "2024/03/21 11:00:00"
        );
        assert_time_range!(
            "2024/03/21 11:02:00",
            Level::Minute15,
            "2024/03/21 11:00:00",
            "2024/03/21 11:15:00"
        );
    }

    #[test]
    fn test_minute30_basic() {
        assert_time_range!(
            "2024/03/21 10:45:30",
            Level::Minute30,
            "2024/03/21 10:30:00",
            "2024/03/21 11:00:00"
        );
        assert_time_range!(
            "2024/03/21 11:00:00",
            Level::Minute30,
            "2024/03/21 11:00:00",
            "2024/03/21 11:30:00"
        );
    }

    // ============ 小时级别测试 ============

    #[test]
    fn test_hour1_basic() {
        assert_time_range!(
            "2024/03/21 10:23:45",
            Level::Hour1,
            "2024/03/21 10:00:00",
            "2024/03/21 11:00:00"
        );
    }

    #[test]
    fn test_hour2_various() {
        // 偶数小时
        assert_time_range!(
            "2024/03/21 10:30:00",
            Level::Hour2,
            "2024/03/21 10:00:00",
            "2024/03/21 12:00:00"
        );
        // 奇数小时
        assert_time_range!(
            "2024/03/21 11:59:59",
            Level::Hour2,
            "2024/03/21 10:00:00",
            "2024/03/21 12:00:00"
        );
    }

    #[test]
    fn test_hour4_basic() {
        assert_time_range!(
            "2024/03/21 15:30:00",
            Level::Hour4,
            "2024/03/21 12:00:00",
            "2024/03/21 16:00:00"
        );
    }

    #[test]
    fn test_hour6_cross_day() {
        // 跨天边界: 22:00/04:00
        assert_time_range!(
            "2024/03/21 23:30:00",
            Level::Hour6,
            "2024/03/21 18:00:00",
            "2024/03/22 00:00:00"
        );
        assert_time_range!(
            "2024/03/22 01:15:00",
            Level::Hour6,
            "2024/03/22 00:00:00",
            "2024/03/22 06:00:00"
        );
    }

    #[test]
    fn test_hour12_basic() {
        assert_time_range!(
            "2024/03/21 15:30:00",
            Level::Hour12,
            "2024/03/21 12:00:00",
            "2024/03/22 00:00:00"
        );
        assert_time_range!(
            "2024/03/21 03:30:00",
            Level::Hour12,
            "2024/03/21 00:00:00",
            "2024/03/21 12:00:00"
        );
    }

    // ============ 日级别测试 ============

    #[test]
    fn test_day1_basic() {
        assert_time_range!(
            "2024/03/21 15:30:45",
            Level::Day1,
            "2024/03/21 00:00:00",
            "2024/03/22 00:00:00"
        );
    }

    #[test]
    fn test_day1_cross_month() {
        assert_time_range!(
            "2024/03/31 23:59:59",
            Level::Day1,
            "2024/03/31 00:00:00",
            "2024/04/01 00:00:00"
        );
    }

    #[test]
    fn test_day1_cross_year() {
        assert_time_range!(
            "2024/12/31 12:00:00",
            Level::Day1,
            "2024/12/31 00:00:00",
            "2025/01/01 00:00:00"
        );
    }

    #[test]
    fn test_day3_cross_month() {
        assert_time_range!(
            "2026/02/04 05:00:00",
            Level::Day3,
            "2026/02/03 00:00:00",
            "2026/02/06 00:00:00"
        );
    }

    // ============ 周级别测试 ============

    #[test]
    fn test_week1_monday_start() {
        // 2024/03/18 是周一
        assert_time_range!(
            "2024/03/18 00:00:00",
            Level::Week1,
            "2024/03/18 00:00:00",
            "2024/03/25 00:00:00"
        );
    }

    #[test]
    fn test_week1_midweek() {
        // 2024/03/21 是周四,当周周一是 03/18
        assert_time_range!(
            "2024/03/21 15:30:00",
            Level::Week1,
            "2024/03/18 00:00:00",
            "2024/03/25 00:00:00"
        );
    }

    #[test]
    fn test_week1_cross_month() {
        // 2024/03/31 是周日,当周周一是 03/25
        assert_time_range!(
            "2024/03/31 23:59:59",
            Level::Week1,
            "2024/03/25 00:00:00",
            "2024/04/01 00:00:00"
        );
    }

    #[test]
    fn test_week1_cross_year() {
        // 2023/12/31 是周日,当周周一是 12/25
        assert_time_range!(
            "2023/12/31 12:00:00",
            Level::Week1,
            "2023/12/25 00:00:00",
            "2024/01/01 00:00:00"
        );
    }

    // ============ 月级别测试 ============

    #[test]
    fn test_month1_basic() {
        assert_time_range!(
            "2024/03/21 15:30:00",
            Level::Month1,
            "2024/03/01 00:00:00",
            "2024/04/01 00:00:00"
        );
    }

    #[test]
    fn test_month1_first_day() {
        assert_time_range!(
            "2024/03/01 00:00:00",
            Level::Month1,
            "2024/03/01 00:00:00",
            "2024/04/01 00:00:00"
        );
    }

    #[test]
    fn test_month1_last_day_31() {
        assert_time_range!(
            "2024/01/31 23:59:59",
            Level::Month1,
            "2024/01/01 00:00:00",
            "2024/02/01 00:00:00"
        );
    }

    #[test]
    fn test_month1_last_day_30() {
        assert_time_range!(
            "2024/04/30 12:00:00",
            Level::Month1,
            "2024/04/01 00:00:00",
            "2024/05/01 00:00:00"
        );
    }

    #[test]
    fn test_month1_february_leap_year() {
        // 2024 是闰年
        assert_time_range!(
            "2024/02/29 12:00:00",
            Level::Month1,
            "2024/02/01 00:00:00",
            "2024/03/01 00:00:00"
        );
    }

    #[test]
    fn test_month1_february_common_year() {
        // 2023 不是闰年
        assert_time_range!(
            "2023/02/28 23:59:59",
            Level::Month1,
            "2023/02/01 00:00:00",
            "2023/03/01 00:00:00"
        );
    }

    #[test]
    fn test_month1_cross_year() {
        assert_time_range!(
            "2024/12/15 10:00:00",
            Level::Month1,
            "2024/12/01 00:00:00",
            "2025/01/01 00:00:00"
        );
    }

    // ============ 边界和错误处理测试 ============

    #[test]
    fn test_invalid_timestamp() {
        // 测试无效时间戳(负数或过大)
        let result = get_time_range(i64::MAX as u64, Level::Minute1);
        assert!(result.is_err());
    }

    #[test]
    fn test_epoch_time() {
        // 测试 Unix epoch
        assert_time_range!(
            "1970/01/01 00:00:00",
            Level::Minute1,
            "1970/01/01 00:00:00",
            "1970/01/01 00:01:00"
        );
    }

    #[test]
    fn test_dst_transition() {
        // 注意:由于使用 UTC 时间,不受夏令时影响
        // 这里验证逻辑一致性
        assert_time_range!(
            "2024/03/10 02:30:00", // 美国夏令时开始时间附近
            Level::Hour1,
            "2024/03/10 02:00:00",
            "2024/03/10 03:00:00"
        );
    }
}