Explorar o código

TraceStack的一些补充

skyfffire %!s(int64=2) %!d(string=hai) anos
pai
achega
457b3cd5db

+ 1 - 1
.gitignore

@@ -2,7 +2,7 @@
 /.idea
 
 Cargo.lock
-config.toml
+config.toml*
 *.log
 *.log.*
 /logs*

+ 1 - 0
global/Cargo.toml

@@ -15,4 +15,5 @@ toml = "0.5.11"
 serde = "1.0.183"
 serde_derive = "1.0"
 serde_json = "1.0.104"
+chrono = "0.4.26"
 tokio = { version = "1.31.0", features = ["full"] }

+ 36 - 20
global/src/trace_stack.rs

@@ -1,44 +1,60 @@
 use std::fmt;
 use std::fmt::{Formatter};
+use chrono::Utc;
 
 #[derive(Debug, Clone)]
 pub struct TraceStack {
-    pub network: i64,
+    pub after_network: i64,                 // 到达网络层时间
+    pub before_quant: i64,                  // quant层执行开始时间(也是通道+锁走完后的时间)
+    pub after_quant: i64,                   // quant层执行结束时间
 
-    pub format_start: i64,
-    pub format_end: i64,
+    pub before_format: i64,                 // 开始格式化时间
+    pub after_format: i64,                  // 结束格式化时间
 
-    pub quant_start: i64,
-    pub quant_end: i64,
+    pub before_strategy: i64,               // 计算层开始时间
+    pub after_strategy: i64,                // 计算层结束时间
 
-    pub strategy_start: i64,
-    pub strategy_end: i64,
+    pub before_send_thread: i64,            // 发送指令时时间(进入协程前)
+    pub before_send: i64,                   // 发送指令时时间(进入协程后)
+    pub after_send: i64,                    // 计算层结束时间
 
-    pub source: String,
+    pub source: String,                     // 订单来源[depth|order]
+    pub order_command: String               // 订单发送时的command
+}
+
+impl TraceStack {
+    pub fn on_network(&mut self, after_network: i64) {
+        self.after_network = after_network;
+    }
 
-    pub order_command: String
+    pub fn on_before_quant(&mut self) {
+        self.before_quant = Utc::now().timestamp_micros();
+    }
 }
 
 impl fmt::Display for TraceStack {
     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
-        write!(f, "订单来源: {},数据格式化时间{}微秒, 策略执行时间{}微秒",
+        write!(f, "订单来源: {},数据格式化时间{}微秒,逻辑层{}微秒,策略执行时间{}微秒",
               self.source,
-              self.format_end - self.format_start,
-              // self.quant_end - self.quant_start,
-              self.strategy_end - self.strategy_start)
+              self.before_format - self.after_format,
+              self.before_quant - self.after_quant,
+              self.before_strategy - self.after_strategy)
     }
 }
 
 impl Default for TraceStack {
     fn default() -> Self {
         TraceStack {
-            network: 0,
-            format_start: 0,
-            format_end: 0,
-            quant_start: 0,
-            quant_end: 0,
-            strategy_start: 0,
-            strategy_end: 0,
+            after_network: 0,
+            before_format: 0,
+            after_format: 0,
+            before_quant: 0,
+            after_quant: 0,
+            before_strategy: 0,
+            after_strategy: 0,
+            before_send_thread: 0,
+            before_send: 0,
+            after_send: 0,
             source: "".to_string(),
             order_command: "".to_string(),
         }

+ 6 - 0
strategy/src/exchange_disguise.rs

@@ -15,6 +15,7 @@ use exchanges::binance_swap_ws::{BinanceSubscribeType, BinanceSwapWs, BinanceWsT
 use exchanges::gate_swap_rest::GateSwapRest;
 use exchanges::gate_swap_ws::{GateSubscribeType, GateSwapWs, GateWsType};
 use exchanges::kucoin_swap_ws::{KucoinSubscribeType, KucoinSwapWs, KucoinWsType};
+use global::trace_stack::TraceStack;
 use standard::exchange::ExchangeEnum::{BinanceSpot, BinanceSwap, GateSwap, KucoinSwap};
 use standard::SpecialTicker;
 use crate::model::{OrderInfo, OriginalTicker, OriginalTradeBa, OriginalTradeGa};
@@ -112,6 +113,11 @@ async fn gate_swap_run(bool_v1 :Arc<AtomicBool>, type_num: i8, quant_arc: Arc<Mu
             sleep(Duration::from_millis(1)).await;
             match rx.recv().await {
                 Some(data) => {
+
+                    let mut trace_stack = TraceStack::default();
+                    trace_stack.on_network(data.time);
+                    trace_stack.on_before_quant();
+
                     if data.code != "200".to_string() {
                         continue;
                     }

+ 3 - 4
strategy/src/model.rs

@@ -5,7 +5,7 @@ use serde_derive::{Deserialize, Serialize};
 use global::trace_stack::TraceStack;
 
 #[derive(Debug, Clone, PartialEq, Eq)]
-pub struct LocalPosition{
+pub struct LocalPosition {
     // 做多仓位
     pub long_pos: Decimal,
     // 做空仓位
@@ -28,7 +28,7 @@ impl LocalPosition {
 }
 
 #[derive(Debug, Clone)]
-pub struct TraderMsg{
+pub struct TraderMsg {
     pub position: LocalPosition,
     pub cash: Decimal,
     pub coin: Decimal,
@@ -58,8 +58,7 @@ impl TraderMsg {
 }
 
 #[derive(Debug, Clone)]
-pub struct OrderInfo{
-
+pub struct OrderInfo {
     pub symbol: String,
 
     pub amount: Decimal,

+ 3 - 3
strategy/src/strategy.rs

@@ -23,7 +23,7 @@ pub struct Strategy {
     pub post_open_interval: i64,                                    // 提交订单时延
     pub _check_local_orders_time: i64,                              // 上次查单时间
     pub _check_local_orders_interval: i64,                          // 查单间距,原文是秒级,这里改成毫秒级
-    pub in_cancel: HashMap<String, i64>,                            //撤单队列
+    pub in_cancel: HashMap<String, i64>,                            // 撤单队列
     pub cancel_wait_interval: i64,                                  // 取消等待时延
     pub in_check: HashMap<String, i64>,                             // 查单队列
     pub check_wait_interval: i64,                                   // 检测时延
@@ -45,7 +45,7 @@ pub struct Strategy {
     pub ref_name: Vec<String>,                                      //
     pub maker_mode: String,                                         //
     pub local_orders: HashMap<String, OrderInfo>,                   // 本地订单
-    pub pos: LocalPosition,                                              //
+    pub pos: LocalPosition,                                         //
     pub long_hold_value: Decimal,                                   //
     pub short_hold_value: Decimal,                                  //
     pub equity: Decimal,                                            //
@@ -1258,7 +1258,7 @@ mod tests {
     fn on_time_test() {
         global::log_utils::init_log_with_debug();
 
-        let params = Params::new("config.toml").unwrap();
+        let params = Params::new("config.toml.gate").unwrap();
         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)]);