Browse Source

停机操作优化

JiahengHe 2 năm trước cách đây
mục cha
commit
3a053a9fab
1 tập tin đã thay đổi với 34 bổ sung16 xóa
  1. 34 16
      strategy/src/quant.rs

+ 34 - 16
strategy/src/quant.rs

@@ -826,7 +826,7 @@ impl Quant {
                                        self.params.account_name, draw_back, self.strategy.equity, self.strategy.max_equity);
                 warn!(exit_msg);
                 self.exit_msg = exit_msg;
-                self.exit(10).await;
+                self.stop().await;
             }
         }
         // 回撤风控2
@@ -835,14 +835,14 @@ impl Quant {
             let exit_msg = format!("{} 交易亏损,触发止损,准备停机。", self.params.account_name);
             warn!(exit_msg);
             self.exit_msg = exit_msg;
-            self.exit(10).await;
+            self.stop().await;
         }
         // 报单延迟风控,平均延迟允许上限5000ms
         if self.platform_rest.get_request_avg_delay() > dec!(5000) {
             let exit_msg = format!("{} 延迟爆表 触发风控 准备停机。", self.params.account_name);
             warn!(exit_msg);
             self.exit_msg = exit_msg;
-            self.exit(10).await;
+            self.stop().await;
         }
 
         // 仓位异常风控,只在合约模式下执行
@@ -869,7 +869,7 @@ impl Quant {
                 let exit_msg = format!("{} 合约连续检查本地仓位和推算仓位不符合,退出。", self.params.account_name);
                 warn!(exit_msg);
                 self.exit_msg = exit_msg;
-                self.exit(10).await;
+                self.stop().await;
             }
         }
 
@@ -878,7 +878,7 @@ impl Quant {
             let exit_msg = format!("{} 开仓量为0,退出。", self.params.account_name);
             warn!(exit_msg);
             self.exit_msg = exit_msg;
-            self.exit(10).await;
+            self.stop().await;
         }
 
         // 行情更新异常风控
@@ -896,7 +896,7 @@ impl Quant {
                 warn!(?now_time_millis, ?last_update_millis, ?limit);
                 warn!(exit_msg);
                 self.exit_msg = exit_msg;
-                self.exit(10).await;
+                self.stop().await;
             }
         }
         let local_orders = self.local_orders.clone();
@@ -907,7 +907,7 @@ impl Quant {
                 let exit_msg = format!("{}订单停留过长,怀疑异常,退出,cid:{}。", self.params.account_name, client_id);
                 warn!(exit_msg);
                 self.exit_msg = exit_msg;
-                self.exit(10).await;
+                self.stop().await;
             }
         }
 
@@ -918,7 +918,7 @@ impl Quant {
                     let exit_msg = format!("{} long_pos_bias: {},持仓均价异常,退出。", self.params.account_name, self.strategy.long_pos_bias);
                     warn!(exit_msg);
                     self.exit_msg = exit_msg;
-                    self.exit(10).await;
+                    self.stop().await;
                 }
             }
         }
@@ -928,7 +928,7 @@ impl Quant {
                     let exit_msg = format!("{} short_pos_bias: {},持仓均价异常,退出。", self.params.account_name, self.strategy.long_pos_bias);
                     warn!(exit_msg);
                     self.exit_msg = exit_msg;
-                    self.exit(10).await;
+                    self.stop().await;
                 }
             }
         }
@@ -940,7 +940,7 @@ impl Quant {
                 warn!(exit_msg);
                 warn!(?self.strategy.ref_price, ?self.strategy.mp);
                 self.exit_msg = exit_msg;
-                self.exit(10).await;
+                self.stop().await;
             }
         }
 
@@ -950,7 +950,7 @@ impl Quant {
             warn!(exit_msg);
             warn!(?self.strategy.ref_price, ?self.strategy.mp);
             self.exit_msg = exit_msg;
-            self.exit(10).await;
+            self.stop().await;
         }
     }
 
@@ -1020,7 +1020,8 @@ impl Quant {
         }
     }
 
-    pub async fn exit(&mut self, delay: i8){
+
+    pub async fn stop(&mut self){
         /**
          *  停机函数
          *  mode_signal 不能小于80
@@ -1030,6 +1031,23 @@ impl Quant {
         **/
         info!("进入停机流程...");
         self.mode_signal = 80;
+        sleep(Duration::from_secs(10)).await;
+        
+        info!("开始退出操作");
+        info!("为避免api失效导致遗漏仓位 建议人工复查");
+        self.check_position().await;
+        // 开启停机信号
+
+        sleep(Duration::from_secs(3)).await;
+        info!("双重检查遗漏仓位");
+        self.check_position().await;
+        info!("停机退出  停机原因: {}", self.exit_msg);
+        // 发送交易状态 await self._post_params()
+        // TODO: 向中控发送信号
+        info!("退出进程!");
+    }
+
+    pub async fn exit(&mut self, delay: i8){
         info!("预约退出操作 delay:{}", delay);
         if delay > 0i8 {
             sleep(Duration::from_secs(delay as u64)).await;
@@ -1064,7 +1082,7 @@ impl Quant {
         if start_cash.is_zero() && start_coin.is_zero() {
             self.exit_msg = format!("{}{}{}{}", "初始为零 cash: ", start_cash, " coin: ", start_coin);
             // 停止程序
-            self.exit(10).await;
+            self.stop().await;
             return false;
         }
         info!("初始cash: {start_cash} 初始coin: {start_coin}");
@@ -1072,7 +1090,7 @@ impl Quant {
         if mp <= Decimal::ZERO {
             self.exit_msg = format!("{}{}", "初始价格获取错误: ", mp);
             // 停止程序
-            self.exit(10).await;
+            self.stop().await;
             return false;
         } else {
             info!("初始价格为 {}", mp);
@@ -1097,7 +1115,7 @@ impl Quant {
         if self.strategy.step_size.is_zero() || self.strategy.tick_size.is_zero() {
             self.exit_msg = format!("{}{}{}{}", "交易精度未正常获取 step_size: ", self.strategy.step_size, " tick_size:", self.strategy.tick_size);
             // 停止程序
-            self.exit(10).await;
+            self.stop().await;
             return false;
         } else {
             info!("数量精度 {}", self.strategy.step_size);
@@ -1123,7 +1141,7 @@ impl Quant {
             (long_one_hand_value < hand_min_limit && short_one_hand_value < hand_min_limit) {
             self.exit_msg = format!("{}{}{}{}", "初始下单量太少 buy: ", long_one_hand_amount, " sell: ", short_one_hand_amount);
             // 停止程序
-            self.exit(10).await;
+            self.stop().await;
             return false;
         }
         // 初始化调度器