Browse Source

支持none展示

skyffire 9 months ago
parent
commit
e35014693c
1 changed files with 35 additions and 21 deletions
  1. 35 21
      src/msv.rs

+ 35 - 21
src/msv.rs

@@ -490,7 +490,7 @@ pub async fn generate_fot(query_value: Value) -> HttpResponse {
         // force_orders处理
         let force_orders = parse_json_to_force_orders(force_orders_response.data);
 
-        let indicator = generate_fot_by_trades(processed_trades, force_orders, value_ln);
+        let indicator = generate_fot_by_trades(processed_trades, force_orders, value_ln, Decimal::from(end_time));
 
         // 返回数据
         let response = Response {
@@ -508,8 +508,8 @@ pub async fn generate_fot(query_value: Value) -> HttpResponse {
     }
 }
 
-pub fn generate_fot_by_trades(processed_trades: HashMap<Decimal, Decimal>, force_orders: Vec<ForceOrder>, value_limit_ln: Decimal) -> Value {
-    let mut profits_rst: Vec<Vec<Decimal>> = vec![];
+pub fn generate_fot_by_trades(processed_trades: HashMap<Decimal, Decimal>, force_orders: Vec<ForceOrder>, value_limit_ln: Decimal, end_time: Decimal) -> Value {
+    let mut profits_rst: Vec<Vec<Option<Decimal>>> = vec![];
     // 外层遍历信号源
     for force_order in force_orders {
         let force_value = if force_order.value > Decimal::ONE {
@@ -539,30 +539,37 @@ pub fn generate_fot_by_trades(processed_trades: HashMap<Decimal, Decimal>, force
         let mut prev_calc_price = first_trade_price;
 
         // 开始统计每一秒的收益率
-        let mut profit_list: Vec<Decimal> = vec![];
+        let mut profit_list: Vec<Option<Decimal>> = vec![];
         for next_time_int in 1..3601 {
             let next_time = Decimal::from(next_time_int);
 
-            let calc_price = match processed_trades.get(&(trade_second + next_time)) {
-                None => {
-                    prev_calc_price
-                }
-                Some(trade_price) => {
-                    trade_price.clone()
+            // 已计算到终点的,显示null,不然前端看起来很难看
+            let profit = if (trade_second + next_time) * Decimal::ONE_THOUSAND <= end_time {
+                let calc_price = match processed_trades.get(&(trade_second + next_time)) {
+                    None => {
+                        prev_calc_price
+                    }
+                    Some(trade_price) => {
+                        trade_price.clone()
+                    }
+                };
+
+                // 求收益率
+                let mut p = (calc_price / first_trade_price) - Decimal::ONE;
+                p.rescale(4);
+                // 负数另考虑
+                if force_value < Decimal::ZERO {
+                    p = -p;
                 }
-            };
 
-            // 求收益率
-            let mut profit = (calc_price / first_trade_price) - Decimal::ONE;
-            profit.rescale(4);
-            // 负数另考虑
-            if force_value < Decimal::ZERO {
-                profit = -profit;
-            }
+                prev_calc_price = calc_price;
 
-            profit_list.push(profit);
+                Some(p)
+            } else {
+                None
+            };
 
-            prev_calc_price = calc_price;
+            profit_list.push(profit);
         }
 
         profits_rst.push(profit_list)
@@ -574,7 +581,14 @@ pub fn generate_fot_by_trades(processed_trades: HashMap<Decimal, Decimal>, force
         let mut total_profit = Decimal::ZERO;
 
         for profit_list in &profits_rst {
-            total_profit += profit_list[index]
+            total_profit += match profit_list[index] {
+                None => {
+                    Decimal::ZERO
+                }
+                Some(p) => {
+                    p
+                }
+            }
         }
 
         profit_total_list.push(total_profit)