Explorar o código

一些细节逻辑验证

skyfffire %!s(int64=2) %!d(string=hai) anos
pai
achega
4ed3d4cd4d
Modificáronse 3 ficheiros con 46 adicións e 32 borrados
  1. 1 1
      src/main.rs
  2. 14 14
      strategy/src/strategy.rs
  3. 31 17
      tests/order_command_test.rs

+ 1 - 1
src/main.rs

@@ -90,7 +90,7 @@ async fn main() {
                 Some(error) => {
                     let quant = error_handler_quant_arc.lock().await;
 
-                    error!("出现错误{:?}", error);
+                    error!("main: 订单出现错误{:?}", error);
                 },
                 None => {
                     error!("Error channel has been closed!");

+ 14 - 14
strategy/src/strategy.rs

@@ -233,7 +233,7 @@ impl Strategy {
     }
 
     // 更新当前strategy的各类信息
-    #[instrument(skip(self, trader_msg), level="DEBUG")]
+    #[instrument(skip(self, trader_msg), level="TRACE")]
     pub fn _update_data(&mut self, trader_msg: &TraderMsg) -> bool {
         debug!(?self);
         debug!(?trader_msg);
@@ -348,7 +348,7 @@ impl Strategy {
     }
 
     // 取消目标方向订单,原文是_cancel_targit_side_orders
-    #[instrument(skip(self, command), level="DEBUG")]
+    #[instrument(skip(self, command), level="TRACE")]
     pub fn _cancel_target_side_orders(&self, command: &mut OrderCommand) {
         // 要取消的目标方向
         let target_side = vec![
@@ -376,7 +376,7 @@ impl Strategy {
     }
 
     // 生成各类挂单价格,原文是gen_dist
-    #[instrument(skip(self), level="DEBUG")]
+    #[instrument(skip(self), level="TRACE")]
     pub fn generate_dist(&mut self) {
         let open = self.trade_open_dist;
         let close = self.trade_close_dist;
@@ -442,7 +442,7 @@ impl Strategy {
     }
 
     // 统计请求次数
-    #[instrument(skip(self, command), level="DEBUG")]
+    #[instrument(skip(self, command), level="TRACE")]
     pub fn _update_request_num(&mut self, command: &OrderCommand) {
         debug!(?command);
         debug!(?self.request_order_count, ?self.request_count);
@@ -457,7 +457,7 @@ impl Strategy {
     }
 
     // 根据平均请求次数限制开仓下单
-    #[instrument(skip(self, command), level="DEBUG")]
+    #[instrument(skip(self, command), level="TRACE")]
     pub fn _check_request_limit(&mut self, command: &mut OrderCommand) {
         debug!(?command);
         // 如果当前请求数超过限制
@@ -493,7 +493,7 @@ impl Strategy {
     }
 
     // 新增正在撤单、检查撤单队列,释放过时限制
-    #[instrument(skip(self), level="DEBUG")]
+    #[instrument(skip(self), level="TRACE")]
     pub fn _update_in_cancel(&mut self, command: &mut OrderCommand) {
         let mut new_cancel: HashMap<String, Vec<String>> = HashMap::new();
 
@@ -529,7 +529,7 @@ impl Strategy {
     }
 
     // 维护查单队列,检查是否在撤单
-    #[instrument(skip(self), level="DEBUG")]
+    #[instrument(skip(self), level="TRACE")]
     pub fn _release_in_check(&mut self) {
         debug!(?self.in_check);
         // 为什么要移出来:Rust不允许边循环边修改map
@@ -555,7 +555,7 @@ impl Strategy {
     }
 
     // 检查是否正在撤单
-    #[instrument(skip(self), level="DEBUG")]
+    #[instrument(skip(self), level="TRACE")]
     pub fn _release_in_cancel(&mut self) {
         debug!(?self.in_cancel);
         // 为什么要移出来:Rust不允许边循环边修改map
@@ -593,7 +593,7 @@ impl Strategy {
     }
 
     // 刷新持仓比例
-    #[instrument(skip(self), level="DEBUG")]
+    #[instrument(skip(self), level="TRACE")]
     pub fn _pos_rate(&mut self) {
         debug!(?self);
 
@@ -655,7 +655,7 @@ impl Strategy {
     }
 
     // 清空所有挂单和仓位保持休眠状态
-    #[instrument(skip(self, command), level="DEBUG")]
+    #[instrument(skip(self, command), level="TRACE")]
     pub fn _close_all(&self, command: &mut OrderCommand) {
         // 撤掉全部挂单
         let mut pd_amount = Decimal::ZERO;
@@ -750,7 +750,7 @@ impl Strategy {
     }
 
     // 平仓订单处理命令
-    #[instrument(skip(self, command), level="DEBUG")]
+    #[instrument(skip(self, command), level="TRACE")]
     pub fn _post_close(&self, command: &mut OrderCommand) {
         debug!(?command);
 
@@ -895,7 +895,7 @@ impl Strategy {
     }
 
     // 生成取消订单的指令
-    #[instrument(skip(self, command), level="DEBUG")]
+    #[instrument(skip(self, command), level="TRACE")]
     pub fn _cancel_open(&self, command: &mut OrderCommand) {
         debug!(?command);
         // 挂单范围
@@ -932,7 +932,7 @@ impl Strategy {
     }
 
     // 超时触发查单信号
-    #[instrument(skip(self, command), level="DEBUG")]
+    #[instrument(skip(self, command), level="TRACE")]
     pub fn _check_local_orders(&mut self, command: &mut OrderCommand) {
         debug!(?command);
         // 超时检测
@@ -974,7 +974,7 @@ impl Strategy {
     }
 
     // 开单指令生成逻辑
-    #[instrument(skip(self, command), level="DEBUG")]
+    #[instrument(skip(self, command), level="TRACE")]
     pub fn _post_open(&mut self, command: &mut OrderCommand) {
         debug!(?command);
         // 开仓逻辑检测,主要是检测整点开仓逻辑

+ 31 - 17
tests/order_command_test.rs

@@ -1,7 +1,8 @@
 use std::collections::{BTreeMap};
 use std::{env};
-use tokio::sync::mpsc;
+use tokio::sync::{mpsc, Mutex};
 use std::io::Error;
+use std::sync::Arc;
 use std::time::Duration;
 use tracing::{error, info};
 use exchanges::proxy;
@@ -24,11 +25,14 @@ async fn main() {
 
     global::log_utils::init_log_with_info();
 
-    let exchange = Exchange::new(ExchangeEnum::GateSwap, "BTC_USDT".to_string(), false,
-                                 params, order_sender.clone(), error_sender.clone()).await;
+    let mut exchange = Exchange::new(ExchangeEnum::GateSwap, "BTC_USDT".to_string(), false,
+                                     params, order_sender.clone(), error_sender.clone()).await;
+    let exchange_arc = Arc::new(Mutex::new(exchange));
     let main_thread = tokio::spawn(async move {
         let client_id_0 = "123425678".to_string();
         let client_id_1 = "123452679".to_string();
+        let client_id_2 = "123452677".to_string();
+        let client_id_3 = "123452676".to_string();
 
         loop {
             // 下单
@@ -36,30 +40,42 @@ async fn main() {
             let mut command = OrderCommand::new();
             command.limits_open.insert("BTC_USDT1".to_string(), vec!["0.0001".to_string(), "kd".to_string(), "24000".to_string(), client_id_0.clone()]);
             command.limits_open.insert("BTC_USDT2".to_string(), vec!["0.0001".to_string(), "kk".to_string(), "28000".to_string(), client_id_1.clone()]);
-            exchange.command_order(command.clone()).await;
-            tokio::time::sleep(Duration::from_secs(5)).await;
+            command.limits_open.insert("BTC_USDT3".to_string(), vec!["0.0001".to_string(), "kd".to_string(), "23000".to_string(), client_id_2.clone()]);
+            command.limits_open.insert("BTC_USDT4".to_string(), vec!["0.0001".to_string(), "kk".to_string(), "29000".to_string(), client_id_3.clone()]);
+            info!(?command);
+            let e0 = exchange_arc.clone();
+            tokio::spawn(async move {
+                e0.lock().await.command_order(command.clone()).await;
+            });
+            info!("下单委托完毕");
+            tokio::time::sleep(Duration::from_millis(500)).await;
 
             // 查单
-            info!("查单");
-            let mut command = OrderCommand::new();
-            command.check.insert("BTC_USDT1".to_string(), vec![client_id_0.clone(), "".to_string()]);
-            command.check.insert("BTC_USDT2".to_string(), vec![client_id_1.clone(), "".to_string()]);
-            exchange.command_order(command.clone()).await;
-            tokio::time::sleep(Duration::from_secs(5)).await;
+            // info!("查单");
+            // let mut command = OrderCommand::new();
+            // command.check.insert("BTC_USDT1".to_string(), vec![client_id_0.clone(), "".to_string()]);
+            // command.check.insert("BTC_USDT2".to_string(), vec![client_id_1.clone(), "".to_string()]);
+            // exchange.command_order(command.clone()).await;
+            // tokio::time::sleep(Duration::from_secs(5)).await;
 
             // 撤单
             info!("撤单");
             let mut command = OrderCommand::new();
             command.cancel.insert("BTC_USDT1".to_string(), vec![client_id_0.clone(), "".to_string()]);
             command.cancel.insert("BTC_USDT2".to_string(), vec![client_id_1.clone(), "".to_string()]);
-            exchange.command_order(command.clone()).await;
-            tokio::time::sleep(Duration::from_secs(10)).await;
+            command.cancel.insert("BTC_USDT3".to_string(), vec![client_id_2.clone(), "".to_string()]);
+            command.cancel.insert("BTC_USDT4".to_string(), vec![client_id_3.clone(), "".to_string()]);
+            info!(?command);
+            let e1 = exchange_arc.clone();
+            tokio::spawn(async move {
+                e1.lock().await.command_order(command.clone()).await;
+            });
+            info!("撤单委托完毕");
+            tokio::time::sleep(Duration::from_secs(30)).await;
         }
     });
 
     let handler_thread = tokio::spawn(async move {
-        info!("handler_thread被创建");
-
         loop {
             match order_receiver.recv().await {
                 Some(order) => {
@@ -74,8 +90,6 @@ async fn main() {
     });
 
     let error_handler_thread = tokio::spawn(async move {
-        info!("error_handler_thread被创建");
-
         loop {
             match error_receiver.recv().await {
                 Some(error) => error!(?error),