skyffire 1 éve
szülő
commit
7b497cc64f
2 módosított fájl, 52 hozzáadás és 6 törlés
  1. 4 0
      README.MD
  2. 48 6
      src/msv.rs

+ 4 - 0
README.MD

@@ -35,6 +35,10 @@
             ["1715653817313", "2"],         // [时间戳, 预期利润1]
             ["1715653817316", "-2"],        // [时间戳, 预期利润2]
         ],
+        "sigmas": [                         // 波动率^2                       
+            ["1715653817313", "2"],         // [时间戳, 波动率^2 1]
+            ["1715653817316", "20"],        // [时间戳, 波动率^2 1]
+        ],
         "total_size": 3,                    // 总trades条数
         "result_size": 1,                   // 指标数据条数
     },

+ 48 - 6
src/msv.rs

@@ -108,6 +108,8 @@ pub fn generate_msv_by_trades(mut trades: Vec<Trade>, mills_back: Decimal, simpl
     let mut msv_data: Vec<Vec<Decimal>> = vec![];
     // 预期利润幅度(except_profit_rate)
     let mut epr_data: Vec<Vec<Decimal>> = vec![];
+    // 波动率sigma
+    let mut sigma_data: Vec<Vec<Decimal>> = vec![];
 
     const GAMMA: Decimal = dec!(0.5);
 
@@ -120,13 +122,12 @@ pub fn generate_msv_by_trades(mut trades: Vec<Trade>, mills_back: Decimal, simpl
 
         // 该元素向前遍历range毫秒
         let mut range_index = index;
-
-        // 计算区间的预定价格
+        // 该区间的预定价格
         let mut ref_price = trade.price;
         let mut dissociation = Decimal::ZERO;
         loop {
             // 下标合法性判断
-            if range_index <= 0 {
+            if range_index == 0 {
                 break;
             }
 
@@ -164,6 +165,41 @@ pub fn generate_msv_by_trades(mut trades: Vec<Trade>, mills_back: Decimal, simpl
             future_range_index += 1;
         }
 
+        // 计算过去至多100条数据的sigma值 sigma^2 = (1 / (tn-t0))*sum((S(tk) - S(tk-1)) ^ 2)
+        let mut sigma_index = index - 1;
+        let t_last = trade.time;
+        let mut t_first = trade.time;
+        // 右值
+        let mut total_right = Decimal::ZERO;
+        loop {
+            let flag_trade = trades.get(sigma_index).unwrap();
+            let next_trade = trades.get(sigma_index + 1).unwrap();
+
+            // 下标合法性判断
+            if sigma_index == 0 || sigma_index + 100 <= index {
+                t_first = flag_trade.time;
+                break;
+            }
+
+            // 计算差值
+            let diff = Decimal::ONE - flag_trade.price / next_trade.price;
+            total_right += diff * diff;
+
+            sigma_index = sigma_index - 1;
+        }
+        let sigma = if t_first == t_last {
+            if sigma_index + 100 > index {
+                Decimal::ZERO
+            } else {
+                let time_diff = dec!(0.001);
+                (Decimal::ONE / time_diff) + total_right
+            }
+        } else {
+            let time_diff = (t_last - t_first) / Decimal::ONE_THOUSAND;
+            (Decimal::ONE / time_diff) + total_right
+        };
+        sigma_data.push(vec![trade.time, sigma]);
+
         let last_price = trade.price;
         // ==================== 波动逻辑计算 ====================
         let mut rate = Decimal::ONE_HUNDRED * (last_price - ref_price) / ref_price;
@@ -214,6 +250,7 @@ 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_epr_data: Vec<Vec<Decimal>> = vec![];
+    let mut final_sigma_data: Vec<Vec<Decimal>> = vec![];
 
     let mut depth_index = 0;
     let mut final_volume_data: Vec<Vec<Decimal>> = vec![];
@@ -225,6 +262,7 @@ pub fn generate_msv_by_trades(mut trades: Vec<Trade>, mills_back: Decimal, simpl
         let mut max_msv_data = Decimal::ZERO;
         let mut max_msv_qty_data = Decimal::ZERO;
         let mut max_epr_data = Decimal::ZERO;
+        let mut max_sigma_data = Decimal::ZERO;
 
         // ====================================== 数据生产 ===============================================
         // 获取时间范围内的波动率数据
@@ -243,15 +281,17 @@ pub fn generate_msv_by_trades(mut trades: Vec<Trade>, mills_back: Decimal, simpl
             let msv_d = msv_data[msv_index][1];
             let msv_qty_data = msv_data[msv_index][2];
             let epr_d = epr_data[msv_index][1];
+            let sigma_d = sigma_data[msv_index][1];
             // msv波动数据
             if max_msv_data.abs() < msv_d.abs() {
                 max_msv_data = msv_d;
                 max_msv_qty_data = msv_qty_data;
                 max_epr_data = epr_d;
             }
-            // // epr数据
-            // if max_epr_data.abs() < epr_d.abs() {
-            // }
+            // 波动率sigma
+            if max_sigma_data.abs() < sigma_d {
+                max_sigma_data = sigma_d;
+            }
             // 下标步近
             msv_index = msv_index + 1;
         }
@@ -318,6 +358,7 @@ pub fn generate_msv_by_trades(mut trades: Vec<Trade>, mills_back: Decimal, simpl
             final_qty.rescale(2);
             final_volume_data.push(vec![index_timestamp, final_qty]);
         }
+        final_sigma_data.push(vec![index_timestamp, max_sigma_data]);
 
         // ====================================== 时间步进处理 ======================================
         // 对时间进行步近
@@ -335,6 +376,7 @@ pub fn generate_msv_by_trades(mut trades: Vec<Trade>, mills_back: Decimal, simpl
         "msv": final_msv_data,
         "liqs": final_volume_data,
         "eprs": final_epr_data,
+        "sigmas": final_sigma_data,
         "total_size": total_size,
         "result_size": result_size,
     })