skyffire 1 жил өмнө
parent
commit
a85a3334be
1 өөрчлөгдсөн 31 нэмэгдсэн , 12 устгасан
  1. 31 12
      src/rank.rs

+ 31 - 12
src/rank.rs

@@ -25,6 +25,8 @@ pub struct Rank {
     is_binance: bool,
 }
 
+const ONE_QUARTER: Decimal = dec!(0.25);
+const ONE_PERCENT: Decimal = dec!(0.01);
 const TWENTY: Decimal = dec!(20);
 const SIXTY: Decimal = dec!(60);
 const TWO_HUNDRED: Decimal = dec!(200);
@@ -40,52 +42,69 @@ pub fn generate_rank_by_indicator_map(indicator_map: &MutexGuard<HashMap<String,
         // ============== msv相关数据的计算 =================
         let mut msv_abs_total = Decimal::ZERO;
         let mut msv_abs_max = Decimal::ZERO;
-        let mut effective_count = Decimal::ZERO;
-        for value in indicators.msv.iter() {
+        let mut msv_count = Decimal::ZERO;
+        let mut effective_epr_count = Decimal::ZERO;
+        for (index, value) in indicators.msv.iter().enumerate() {
             let msv_abs_value = value[1].abs();
 
             if msv_abs_value <= Decimal::ZERO {
                 continue;
             }
 
-            effective_count += Decimal::ONE;
+            msv_count += Decimal::ONE;
             msv_abs_total += msv_abs_value;
 
             if msv_abs_value > msv_abs_max {
                 msv_abs_max = msv_abs_value
             }
+
+            let epr = &indicators.eprs[index];
+            if epr[1] > msv_abs_value * ONE_QUARTER {
+                effective_epr_count += Decimal::ONE;
+            }
         }
         // 计算波动率平均值
-        let mut msv_abs_avg = if effective_count == Decimal::ZERO {
+        let mut msv_abs_avg = if msv_count == Decimal::ZERO {
             Decimal::ZERO
         } else {
-            msv_abs_total / effective_count
+            msv_abs_total / msv_count
         };
         msv_abs_avg.rescale(6);
-        // 开仓基准值
+        // 开仓基准值, 除以200是先除以2,再除以100(因为系统开仓参数是0.01代表1%)
         let mut coverted_open_base = (msv_abs_max + msv_abs_avg) / TWO_HUNDRED;
         coverted_open_base.rescale(8);
 
         // ============== epr相关数据的计算 =================
         let mut epr_total = Decimal::ZERO;
         let mut epr_max = Decimal::ZERO;
+        let mut epr_count = Decimal::ZERO;
         for value in indicators.eprs.iter() {
+            if value[1].abs() < ONE_PERCENT {
+                continue;
+            }
+
             epr_total += value[1];
+            epr_count += Decimal::ONE;
 
             if value[1] > epr_max {
                 epr_max = value[1]
             }
         }
+        let epr_avg = if epr_count == Decimal::ZERO {
+            epr_total
+        } else {
+            epr_total / epr_count
+        };
 
         // ============== liq相关数据的计算 =================
         let mut liquidity_total = Decimal::ZERO;
         for value in indicators.liqs.iter() {
             liquidity_total += value[1] * Decimal::ONE_THOUSAND;
         }
-        let mut liquidity_avg = if effective_count == Decimal::ZERO {
+        let mut liquidity_avg = if msv_count == Decimal::ZERO {
             Decimal::ZERO
         } else {
-            liquidity_total / effective_count
+            liquidity_total / msv_count
         };
         liquidity_avg.rescale(0);
 
@@ -93,9 +112,9 @@ pub fn generate_rank_by_indicator_map(indicator_map: &MutexGuard<HashMap<String,
         // msv_score计算规则
         let mut msv_score = if msv_abs_total > Decimal::ZERO && epr_total > Decimal::ZERO {
             if epr_total > msv_abs_total {
-                (epr_max * SIXTY).powd(Decimal::TWO)
+                (epr_avg * SIXTY).powd(Decimal::TWO)
             } else {
-                ((epr_max * epr_total / msv_abs_total) * SIXTY).powd(Decimal::TWO)
+                (epr_avg * SIXTY * (epr_total / msv_abs_total)).powd(Decimal::TWO)
             }
         } else {
             Decimal::ZERO
@@ -107,7 +126,7 @@ pub fn generate_rank_by_indicator_map(indicator_map: &MutexGuard<HashMap<String,
         liquidity_score.rescale(2);
 
         // frequency_score计算规则
-        let mut frequency_score = ((effective_count / Decimal::ONE_THOUSAND) * TWENTY).sqrt().unwrap();
+        let mut frequency_score = ((effective_epr_count / Decimal::ONE_THOUSAND) * TWENTY).sqrt().unwrap();
         frequency_score.rescale(2);
 
         let temp = msv_score * liquidity_score * frequency_score;
@@ -129,7 +148,7 @@ pub fn generate_rank_by_indicator_map(indicator_map: &MutexGuard<HashMap<String,
             msv_abs_avg,
             coverted_open_base,
             epr_total,
-            effective_count,
+            effective_count: msv_count,
             liquidity_avg,
             is_binance,
         };