Bladeren bron

更改交易量拉取时间范围以及定时任务执行时间

交易量拉取时间范围(昨日8点~今日8点)
定时任务执行时间(每日8.30)
JiahengHe 8 maanden geleden
bovenliggende
commit
d2bed05c38
2 gewijzigde bestanden met toevoegingen van 43 en 35 verwijderingen
  1. 24 22
      src/core_libs.rs
  2. 19 13
      strategy/src/core.rs

+ 24 - 22
src/core_libs.rs

@@ -6,7 +6,7 @@ use strategy::{exchange_disguise, core};
 use std::sync::Arc;
 use std::sync::atomic::{AtomicBool};
 use std::time::Duration;
-use chrono::{Local, Timelike, Utc};
+use chrono::{Datelike, Local, TimeZone, Utc};
 use tokio::sync::{mpsc, Mutex};
 use tokio::time::{sleep_until, Instant};
 use tracing::{error, info};
@@ -127,34 +127,36 @@ pub async fn init(params: Params,
     // 定时交易量更新
     let trade_volume_core_arc = core_arc.clone();
     tokio::spawn(async move {
-        info!("1周交易量统计定时任务启动(1hour)...");
+        info!("交易量统计定时任务启动(每天早上8:30)...");
         loop {
-            // 获取当前时间并计算下一个整点
             let now = Local::now();
-            let truncated_now = now
-                .with_minute(0)
-                .unwrap()
-                .with_second(0)
-                .unwrap()
-                .with_nanosecond(0)
-                .unwrap();
-
-            let next_hour = truncated_now + chrono::Duration::hours(1);
-
-            // 计算需要等待的时间
-            let wait_duration = next_hour - now;
-            let wait_secs = wait_duration.num_seconds() as u64;
-            
-            if wait_secs > 0 {
-                sleep_until(Instant::now() + Duration::from_secs(wait_secs)).await;
+
+            // 使用 and_hms_opt 方法构建今天早上8:30的时间
+            let target_time_opt = Local.with_ymd_and_hms(now.year(), now.month(), now.day(), 14, 0, 0);
+
+            // 检查时间构建是否成功
+            let mut target_time = match target_time_opt.single() {
+                Some(time) => time,
+                None => panic!("Invalid time!"), // 处理无效时间的情况
+            };
+
+            // 如果当前时间已经超过今天的8:30,则调整为明天的8:30
+            if now >= target_time {
+                target_time = target_time + chrono::Duration::days(1);
             }
-            
-            // 更新近7天的交易量统计
+
+            // 将目标时间转换为 Instant
+            let duration_to_wait = (target_time - now).to_std().unwrap();
+
+            sleep_until(Instant::now() + duration_to_wait).await;
+            // 防止过快循环,等待一分钟后再继续下一次循环
+
+            // 更新近1天(昨8点~今8点)的交易量统计
             let mut core = trade_volume_core_arc.lock().await;
             core.update_trade_volume().await;
 
             // 等待一分钟以避免过快地循环
-            sleep_until(Instant::now() + Duration::from_secs(60)).await;
+            sleep_until(Instant::now() + Duration::from_secs(5*60)).await;
         }
     });
 

+ 19 - 13
strategy/src/core.rs

@@ -5,8 +5,8 @@ use std::io::Error;
 use std::str::FromStr;
 use std::sync::{Arc};
 use std::sync::atomic::{AtomicBool, Ordering};
-use std::time::{Duration, SystemTime, UNIX_EPOCH};
-use chrono::{Utc};
+use std::time::{Duration};
+use chrono::{Local, TimeZone, Utc, Datelike};
 use rust_decimal::{Decimal, MathematicalOps};
 use rust_decimal::prelude::{ToPrimitive};
 use rust_decimal_macros::dec;
@@ -577,22 +577,28 @@ impl Core {
     }
 
     pub async fn update_trade_volume(&mut self) {
-        // 获取当前时间戳(毫秒)
-        let end_time = SystemTime::now()
-            .duration_since(UNIX_EPOCH)
-            .expect("Time went backwards")
-            .as_millis() as i64;
-
-        // 计算7天前的毫秒数
-        let seven_days_millis = 7 * 24 * 60 * 60 * 1000;
+        // 获取当前本地时间
+        let now = Local::now();
+
+        // 获取今天早上8点
+        let target_time_opt = Local.with_ymd_and_hms(now.year(), now.month(), now.day(), 8, 0, 0);
+        // 检查时间构建是否成功
+        let today_eight_am = match target_time_opt.single() {
+            Some(time) => time,
+            None => panic!("Invalid time!"), // 处理无效时间的情况
+        };
 
-        // 计算开始时间戳
-        let start_time = end_time - seven_days_millis;
+        // 获取昨天早上8点
+        let yesterday_eight_am = today_eight_am - chrono::Duration::days(1);
+        // 获取时间戳
+        let end_time = today_eight_am.timestamp() * 1000;
+        let start_time = yesterday_eight_am.timestamp() * 1000;
+        info!("获取交易量时间范围: {} 至 {}", start_time, end_time);
         let trade_volume = self.platform_rest.get_trade_amount(start_time, end_time).await;
         let mut cci = self.cci_arc.lock().await;
         match trade_volume {
             Ok(volume) => {
-                info!("交易量定时更新成功: {}", volume);
+                info!("交易量更新成功: {}", volume);
                 self.trading_volume = volume;
                 cci.trading_volume = volume;
             },