Browse Source

加入日志级别配置,修改策略日志等级

skyfffire 2 years ago
parent
commit
d9285f9c09
2 changed files with 24 additions and 29 deletions
  1. 2 1
      strategy/config.toml
  2. 22 28
      strategy/src/strategy.rs

+ 2 - 1
strategy/config.toml

@@ -20,4 +20,5 @@ stop_loss = 0.02
 gamma = 0.999
 grid = 1
 place_order_limit = 0
-colo = 0
+colo = 0
+log_level = "debug"

+ 22 - 28
strategy/src/strategy.rs

@@ -8,7 +8,7 @@ use rust_decimal_macros::dec;
 use crate::model::{LocalPosition, OrderCommand, OrderInfo, TraderMsg};
 use crate::params::Params;
 use crate::utils;
-use tracing::{info, instrument, subscriber, error, debug};
+use tracing::{info, instrument, error, debug};
 use tracing_subscriber;
 
 #[derive(Debug)]
@@ -105,10 +105,6 @@ pub struct Strategy {
 
 impl Strategy {
     pub fn new(params: &Params, is_print: bool) -> Self {
-        return Strategy::new_with_log_level(params, is_print, tracing::Level::INFO);
-    }
-
-    pub fn new_with_log_level(params: &Params, is_print: bool, log_level: tracing::Level) -> Self {
         if params.ref_exchange.len() != params.ref_pair.len() {
             panic!("参考盘口数不等于参考品种数,退出,请检查配置!")
         }
@@ -228,20 +224,13 @@ impl Strategy {
         strategy.post_open_time = now.timestamp_millis();
         let post_open_interval_per_second = Decimal::ONE.div(Decimal::from_i64(utils::get_limit_order_requests_num_per_second(params.exchange.clone(), 0)).unwrap());
         strategy.post_open_interval = dec!(1000).mul(post_open_interval_per_second).to_i64().unwrap();
-        // 初始化日志订阅者
-        let sub = tracing_subscriber::fmt()
-            .with_max_level(log_level)
-            .with_span_events(tracing_subscriber::fmt::format::FmtSpan::FULL)
-            .finish();
-        subscriber::set_global_default(sub).expect("策略模块日志初始化错误");
-        info!("策略模块日志初始化完毕!");
         info!("策略模块初始化完成!");
 
         return strategy;
     }
 
     // 更新当前strategy的各类信息
-    #[instrument(skip(self, trader_msg), level="TRACE")]
+    #[instrument(skip(self, trader_msg), level="DEBUG")]
     pub fn _update_data(&mut self, trader_msg: &TraderMsg) -> bool {
         debug!(?self);
         debug!(?trader_msg);
@@ -356,7 +345,7 @@ impl Strategy {
     }
 
     // 取消目标方向订单,原文是_cancel_targit_side_orders
-    #[instrument(skip(self, command), level="TRACE")]
+    #[instrument(skip(self, command), level="DEBUG")]
     pub fn _cancel_target_side_orders(&self, command: &mut OrderCommand) {
         // 要取消的目标方向
         let target_side = vec![
@@ -384,7 +373,7 @@ impl Strategy {
     }
 
     // 生成各类挂单价格,原文是gen_dist
-    #[instrument(skip(self), level="TRACE")]
+    #[instrument(skip(self), level="DEBUG")]
     pub fn generate_dist(&mut self) {
         let open = self.trade_open_dist;
         let close = self.trade_close_dist;
@@ -450,7 +439,7 @@ impl Strategy {
     }
 
     // 统计请求次数
-    #[instrument(skip(self, command), level="TRACE")]
+    #[instrument(skip(self, command), level="DEBUG")]
     pub fn _update_request_num(&mut self, command: &OrderCommand) {
         debug!(?command);
         debug!(?self.request_order_count, ?self.request_count);
@@ -465,7 +454,7 @@ impl Strategy {
     }
 
     // 根据平均请求次数限制开仓下单
-    #[instrument(skip(self, command), level="TRACE")]
+    #[instrument(skip(self, command), level="DEBUG")]
     pub fn _check_request_limit(&mut self, command: &mut OrderCommand) {
         debug!(?command);
         // 如果当前请求数超过限制
@@ -502,7 +491,7 @@ impl Strategy {
     }
 
     // 新增正在撤单、检查撤单队列,释放过时限制
-    #[instrument(skip(self), level="TRACE")]
+    #[instrument(skip(self), level="DEBUG")]
     pub fn _update_in_cancel(&mut self, command: &mut OrderCommand) {
         let mut new_cancel: HashMap<String, Vec<String>> = HashMap::new();
 
@@ -538,7 +527,7 @@ impl Strategy {
     }
 
     // 维护查单队列,检查是否在撤单
-    #[instrument(skip(self), level="TRACE")]
+    #[instrument(skip(self), level="DEBUG")]
     pub fn _release_in_check(&mut self) {
         debug!(?self.in_check);
         // 为什么要移出来:Rust不允许边循环边修改map
@@ -564,7 +553,7 @@ impl Strategy {
     }
 
     // 检查是否正在撤单
-    #[instrument(skip(self), level="TRACE")]
+    #[instrument(skip(self), level="DEBUG")]
     pub fn _release_in_cancel(&mut self) {
         debug!(?self.in_cancel);
         // 为什么要移出来:Rust不允许边循环边修改map
@@ -602,7 +591,7 @@ impl Strategy {
     }
 
     // 刷新持仓比例
-    #[instrument(skip(self), level="TRACE")]
+    #[instrument(skip(self), level="DEBUG")]
     pub fn _pos_rate(&mut self) {
         debug!(?self);
 
@@ -664,7 +653,7 @@ impl Strategy {
     }
 
     // 清空所有挂单和仓位保持休眠状态
-    #[instrument(skip(self, command), level="TRACE")]
+    #[instrument(skip(self, command), level="DEBUG")]
     pub fn _close_all(&self, command: &mut OrderCommand) {
         // 撤掉全部挂单
         let mut pd_amount = Decimal::ZERO;
@@ -759,7 +748,7 @@ impl Strategy {
     }
 
     // 平仓订单处理命令
-    #[instrument(skip(self, command), level="TRACE")]
+    #[instrument(skip(self, command), level="DEBUG")]
     pub fn _post_close(&self, command: &mut OrderCommand) {
         debug!(?command);
 
@@ -904,7 +893,7 @@ impl Strategy {
     }
 
     // 生成取消订单的指令
-    #[instrument(skip(self, command), level="TRACE")]
+    #[instrument(skip(self, command), level="DEBUG")]
     pub fn _cancel_open(&self, command: &mut OrderCommand) {
         debug!(?command);
         // 挂单范围
@@ -941,7 +930,7 @@ impl Strategy {
     }
 
     // 超时触发查单信号
-    #[instrument(skip(self, command), level="TRACE")]
+    #[instrument(skip(self, command), level="DEBUG")]
     pub fn _check_local_orders(&mut self, command: &mut OrderCommand) {
         debug!(?command);
         // 超时检测
@@ -983,7 +972,7 @@ impl Strategy {
     }
 
     // 开单指令生成逻辑
-    #[instrument(skip(self, command), level="TRACE")]
+    #[instrument(skip(self, command), level="DEBUG")]
     pub fn _post_open(&mut self, command: &mut OrderCommand) {
         debug!(?command);
         // 开仓逻辑检测,主要是检测整点开仓逻辑
@@ -1156,7 +1145,7 @@ impl Strategy {
 mod tests {
     use rust_decimal::Decimal;
     use rust_decimal_macros::dec;
-    use tracing::debug;
+    use tracing::{debug, subscriber};
     use crate::model::{OrderInfo, TraderMsg};
     use crate::params::Params;
     use crate::strategy::Strategy;
@@ -1164,7 +1153,12 @@ mod tests {
     #[test]
     fn on_time_test() {
         let params = Params::new("config.toml").unwrap();
-        let mut strategy = Strategy::new_with_log_level(&params, true, tracing::Level::TRACE);
+        let sub = tracing_subscriber::fmt()
+            .with_max_level(tracing::Level::DEBUG)
+            .with_span_events(tracing_subscriber::fmt::format::FmtSpan::FULL)
+            .finish();
+        subscriber::set_global_default(sub).expect("策略模块日志初始化错误");
+        let mut strategy = Strategy::new(&params, true);
         let mut trader_msg = TraderMsg::new();
         trader_msg.market.append(&mut vec![dec!(0.92), dec!(1.0), dec!(0.89), dec!(0.79), dec!(0.99), dec!(1.0), dec!(0.89), dec!(0.79), dec!(0.89), dec!(0.79), dec!(0.99), dec!(1.0), dec!(0.89), dec!(0.79)]);
         trader_msg.position.long_pos = dec!(100);