Browse Source

on_timer搞完。

skyfffire 2 years ago
parent
commit
e437f5aebe
3 changed files with 50 additions and 8 deletions
  1. 47 5
      strategy/src/attach_.rs
  2. 1 1
      strategy/src/lib.rs
  3. 2 2
      strategy/src/strategy.rs

+ 47 - 5
strategy/src/risk.rs → strategy/src/attach_.rs

@@ -1,12 +1,14 @@
 use std::cmp::max;
+use std::sync::Arc;
+use std::time::Duration;
 use chrono::Utc;
 use rust_decimal::Decimal;
 use rust_decimal_macros::dec;
-use tracing::warn;
+use tokio::sync::Mutex;
+use tokio::task::JoinHandle;
+use tracing::{info, warn};
 use crate::quant::Quant;
 
-impl Quant {}
-
 pub fn check_risk(mut _self: Quant) {
     // 参数检查的风控
     if _self.strategy.start_cash == Decimal::ZERO {
@@ -44,8 +46,8 @@ pub fn check_risk(mut _self: Quant) {
         // _self.stop()
     }
     // 报单延迟风控,平均延迟允许上限5000ms
-    // TODO quant.rest不存在
-    // if _self.rest.avg_delay > 5000 {
+    // TODO quant.platform_rest.avg_delay不存在
+    // if _self.platform_rest.avg_delay > 5000 {
     //     let exit_msg = format!("{} 延迟爆表 触发风控 准备停机。", _self.params.account_name);
     //     warn!(exit_msg);
     //     _self.exit_msg = exit_msg;
@@ -169,3 +171,43 @@ pub fn check_risk(mut _self: Quant) {
         // _self.stop()
     }
 }
+
+// 定期触发的系统逻辑
+pub fn on_timer(quant_arc: Arc<Mutex<Quant>>) -> JoinHandle<()> {
+    let quant_arc_clone = quant_arc.clone();
+
+    return tokio::spawn(async move {
+        tokio::time::sleep(Duration::from_secs(20)).await;
+
+        loop {
+            tokio::time::sleep(Duration::from_secs(10)).await;
+
+            let mut quant = quant_arc_clone.lock().await;
+            {
+                // 检查风控
+                // quant.check_risk();
+
+                // 线程停止信号
+                if quant.mode_signal == 1 {
+                    return
+                }
+
+                // 计算预估成交额
+                let total_trade_value = quant.local_buy_value + quant.local_sell_value;
+                let time_diff = Decimal::from(Utc::now().timestamp_millis() - quant.start_time);
+                let trade_vol_24h = ((total_trade_value / time_diff) * dec!(86400));
+                quant.strategy.trade_vol_24h_w = (trade_vol_24h / dec!(10000));
+                quant.strategy.trade_vol_24h_w.rescale(2);
+
+                // 打印各类信息
+                quant.strategy._print_summary();
+                // TODO quant没有rest
+                // info!("Rest报单平均延迟{}ms", quant.rest.avg_delay);
+                // info!("Rest报单最高延迟{}ms", quant.rest.max_delay);
+                for (name, interval) in &quant.market_update_interval {
+                    info!("WS盘口{}行情平均更新间隔{}ms。", name, interval);
+                }
+            }
+        }
+    });
+}

+ 1 - 1
strategy/src/lib.rs

@@ -4,4 +4,4 @@ mod model;
 mod strategy;
 mod predictor;
 mod utils;
-mod risk;
+mod attach_;

+ 2 - 2
strategy/src/strategy.rs

@@ -100,7 +100,7 @@ pub struct Strategy {
     pub predict: Decimal,                                           //
     pub predict_alpha: Decimal,                                     //
     pub post_side: i64,                                             // 交易方向
-    pub trade_vol_24h: Decimal,                                     //
+    pub trade_vol_24h_w: Decimal,                                   // 24小时成交额(单位:万)
     pub grid: Decimal,                                              // 网格数量
 }
 
@@ -190,7 +190,7 @@ impl Strategy {
             predict: Default::default(),
             predict_alpha: Default::default(),
             post_side: 0,
-            trade_vol_24h: Default::default(),
+            trade_vol_24h_w: Default::default(),
             grid: Decimal::from(params.grid),
         };