skyffire 9 сар өмнө
parent
commit
5c84d0d782
1 өөрчлөгдсөн 44 нэмэгдсэн , 3 устгасан
  1. 44 3
      src/msv.rs

+ 44 - 3
src/msv.rs

@@ -576,30 +576,71 @@ pub fn generate_fot_by_trades(processed_trades: HashMap<Decimal, Decimal>, force
     }
 
 
+    let mut others: Vec<Vec<Decimal>> = vec![vec![], vec![]];
+
     let mut profit_total_list: Vec<Decimal> = vec![];
     for index in 0usize..3600usize {
         let mut total_profit = Decimal::ZERO;
 
+        // 本列所有的利润(本秒平仓时利润的合计)
+        let mut p_list_row: Vec<Decimal> = vec![];
         for profit_list in &profits_rst {
-            total_profit += match profit_list[index] {
+            let p = match profit_list[index] {
                 None => {
                     Decimal::ZERO
                 }
                 Some(p) => {
                     p
                 }
-            }
+            };
+
+            p_list_row.push(p);
+            total_profit += p;
         }
 
+        let (mean, median) = calculate_mean_and_median(&p_list_row);
+        others[0].push(mean);
+        others[1].push(median);
+
         profit_total_list.push(total_profit)
     }
 
     json!({
-        "profits": profits_rst,
+        "profits": others,
         "profit_total_list": profit_total_list,
     })
 }
 
+fn calculate_mean_and_median(numbers: &Vec<Decimal>) -> (Decimal, Decimal) {
+    // 过滤掉0
+    let filtered: Vec<Decimal> = numbers.iter().cloned().filter(|x| *x != Decimal::new(0, 0)).collect();
+
+    // 如果没有有效的数字,返回0和None
+    if filtered.is_empty() {
+        return (Decimal::new(0, 0), Decimal::new(0, 0));
+    }
+
+    // 计算平均数
+    let sum: Decimal = filtered.iter().sum();
+    let count = Decimal::from(filtered.len() as i64);
+    let mean = sum / count;
+
+    // 计算中位数
+    let mut sorted = filtered.clone();
+    sorted.sort_unstable();
+
+    let median = if sorted.len() % 2 == 0 {
+        // 偶数个元素
+        let mid_index = sorted.len() / 2;
+        (sorted[mid_index - 1] + sorted[mid_index]) / Decimal::new(2, 0)
+    } else {
+        // 奇数个元素
+        sorted[sorted.len() / 2]
+    };
+
+    (mean, median)
+}
+
 // 将json转换为trades
 pub fn parse_json_to_trades(trades_json: Value) -> Vec<Trade> {
     let mut rst = vec![];