Prechádzať zdrojové kódy

a1和b1的曲线展示

skyffire 1 rok pred
rodič
commit
b316abf6ed
3 zmenil súbory, kde vykonal 74 pridanie a 21 odobranie
  1. 11 3
      README.MD
  2. 60 17
      src/msv.rs
  3. 3 1
      src/server.rs

+ 11 - 3
README.MD

@@ -27,9 +27,17 @@
             ["1715653817313", "0.03", "2"], // [时间戳, 波动幅度, 强度]
             ["1715653817316", "0.02", "7"], // [时间戳, 波动幅度, 强度]
         ],
-        "tr": [                             // 交易速率
-            ["1715653817313", "1"],         // [时间戳, 高速率]
-            ["1715653817316", "0"],         // [时间戳, 低速率]
+        "liqs": [                           // 流动性副图
+            ["1715653817313", "1"],         // [时间戳, 流动性量]
+            ["1715653817316", "0"],         // [时间戳, 流动性量]
+        ],
+        "a1s": [                            // 卖一集合
+            ["1715653817313", "1"],         // [时间戳, 卖一价]
+            ["1715653817316", "2"],         // [时间戳, 卖一价]
+        ],
+        "b1s": [                            // 买一集合                            
+            ["1715653817313", "1"],         // [时间戳, 买一价]
+            ["1715653817316", "0"],         // [时间戳, 买一价]
         ],
         "total_size": 3,                    // 总trades条数
         "result_size": 1,                   // 指标数据条数

+ 60 - 17
src/msv.rs

@@ -172,10 +172,25 @@ pub fn generate_msv_by_trades(mut trades: Vec<Trade>, mills_back: Decimal, simpl
     }
 
     // 按时间序列填充数据
+    let mut msv_index = 0;
     let mut final_msv_data: Vec<Vec<Decimal>> = vec![];
-    let mut final_depth_data: Vec<Vec<Decimal>> = vec![];
-    let mut data_index = 0;
+
     let mut depth_index = 0;
+    let mut final_depth_data: Vec<Vec<Decimal>> = vec![];
+
+    let mut final_a1s_data: Vec<Vec<Decimal>> = vec![];
+    let mut final_b1s_data: Vec<Vec<Decimal>> = vec![];
+    let a1_first = if simple_depths.len() > 0 {
+        simple_depths[0].a1
+    } else {
+        Decimal::ZERO
+    };
+    let b1_first = if simple_depths.len() > 0 {
+        simple_depths[0].b1
+    } else {
+        Decimal::ZERO
+    };
+
     let mut index_timestamp = Decimal::from_i64(start_time).unwrap();
     let last_timestamp = Decimal::from_i64(end_time).unwrap();
     let step_timestamp = dec!(1000);
@@ -186,18 +201,18 @@ pub fn generate_msv_by_trades(mut trades: Vec<Trade>, mills_back: Decimal, simpl
         // 获取时间范围内的波动率数据
         loop {
             // 下标合法性判断
-            if data_index >= msv_data.len() {
+            if msv_index >= msv_data.len() {
                 break;
             }
 
             // msv_data的指定下标数据不在时间范围内(时间范围:指的是[index_timestamp-mills_back, index_timestamp]这个范围)
-            if index_timestamp < msv_data[data_index][0] {
+            if index_timestamp < msv_data[msv_index][0] {
                 break;
             }
 
             // -------------- 大小判断,取值
-            let msv_d = msv_data[data_index][1];
-            let msv_diss_data = msv_data[data_index][2];
+            let msv_d = msv_data[msv_index][1];
+            let msv_diss_data = msv_data[msv_index][2];
             // msv波动数据
             if max_msv_data.abs() < msv_d.abs() {
                 max_msv_data = msv_d;
@@ -207,40 +222,66 @@ pub fn generate_msv_by_trades(mut trades: Vec<Trade>, mills_back: Decimal, simpl
                 max_msv_diss_data = msv_diss_data;
             }
             // 下标步近
-            data_index = data_index + 1;
+            msv_index = msv_index + 1;
         }
 
-        // 获取时间范围内的深度数据
+        // 获取时间范围内的深度数据、买一及卖一价数据
         let mut max_size = Decimal::ZERO;
         let mut min_size = Decimal::ONE_THOUSAND * Decimal::ONE_THOUSAND;
+        let mut max_spread_index = 0usize;
+        let mut max_spread = Decimal::ZERO;
         loop {
             // 下标合法性判断
             if depth_index >= simple_depths.len() {
                 break;
             }
+            let depth = &simple_depths[depth_index];
             // 时间范围合法性判断,只统计那一秒以内的深度总交易量
-            if index_timestamp < simple_depths[depth_index].time {
+            if index_timestamp < depth.time {
                 break;
             }
-            // 这一秒的深度最大值
-            max_size = max(max_size, simple_depths[depth_index].size);
-            min_size = min(min_size, simple_depths[depth_index].size);
+            // 这一秒的深度最大值、最小值
+            max_size = max(max_size, depth.size);
+            min_size = min(min_size, depth.size);
+            // 这一秒的差价最大值
+            let spread = depth.a1 - depth.b1;
+            if spread > max_spread {
+                max_spread = spread;
+                max_spread_index = depth_index;
+            }
             // 下标步近
             depth_index += 1;
         }
-        // 智能填充数据
+
+        // ====================================== 智能填充数据 ===============================================
+        // 价差及买一、卖一数据处理
+        if simple_depths.len() > 0 {
+            let depth = &simple_depths[max_spread_index];
+
+            final_a1s_data.push(vec![index_timestamp, depth.a1 - a1_first]);
+            final_b1s_data.push(vec![index_timestamp, depth.b1 - b1_first]);
+        } else {
+            final_a1s_data.push(vec![index_timestamp, Decimal::ZERO]);
+            final_b1s_data.push(vec![index_timestamp, Decimal::ZERO]);
+        }
+
+        // 流动性数据叠加
         let rst_size = if (max_size == Decimal::ZERO || min_size == Decimal::ONE_THOUSAND * Decimal::ONE_THOUSAND) && final_depth_data.len() > 0 {
             final_depth_data.last().unwrap()[1]
         } else {
             if (max_size == Decimal::ZERO || min_size == Decimal::ONE_THOUSAND * Decimal::ONE_THOUSAND) && simple_depths.len() > 0 {
                 simple_depths[0].size
             } else {
-                (max_size + min_size) / Decimal::TWO
+                if simple_depths.len() > 0 {
+                    (max_size + min_size) / Decimal::TWO
+                } else {
+                    Decimal::ZERO
+                }
             }
         };
-        // 简易的深度数据处理
         final_depth_data.push(vec![index_timestamp, rst_size]);
 
+        // 波动率数据处理
         // 如果这两个值为0,则代表这mills_back毫秒以内是没有数据的,填充0数据,使得x轴是完整的
         if max_msv_data == Decimal::ZERO {
             final_msv_data.push(vec![index_timestamp, Decimal::ZERO, Decimal::ZERO]);
@@ -250,7 +291,7 @@ pub fn generate_msv_by_trades(mut trades: Vec<Trade>, mills_back: Decimal, simpl
             final_msv_data.push(vec![index_timestamp, max_msv_data, max_msv_diss_data]);
         }
 
-        // ---------------- 最后的时间处理 -----------------
+        // ====================================== 时间步进处理 ======================================
         // 对时间进行步近
         index_timestamp = index_timestamp + step_timestamp;
         // 时间越界
@@ -264,7 +305,9 @@ pub fn generate_msv_by_trades(mut trades: Vec<Trade>, mills_back: Decimal, simpl
     let result_size = final_msv_data.len();
     json!({
         "msv": final_msv_data,
-        "tr": final_depth_data,
+        "liqs": final_depth_data,
+        "a1s": final_a1s_data,
+        "b1s": final_b1s_data,
         "total_size": total_size,
         "result_size": result_size,
     })

+ 3 - 1
src/server.rs

@@ -33,7 +33,9 @@ pub struct Trade {
 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 pub struct SimpleDepth {
     pub time: Decimal,
-    pub size: Decimal
+    pub size: Decimal,
+    pub a1: Decimal,
+    pub b1: Decimal
 }
 
 // 句柄 GET 请求