Browse Source

calc_volatility_div_price_diff

skyffire 1 year ago
parent
commit
e3bd666494
1 changed files with 38 additions and 2 deletions
  1. 38 2
      src/symbol_filter.rs

+ 38 - 2
src/symbol_filter.rs

@@ -29,6 +29,7 @@ fn get_public_symbols(symbols_map: &Value) -> Vec<String> {
     common_symbols.unwrap_or_default().into_iter().collect::<Vec<String>>()
 }
 
+// 计算总交易量
 fn calc_total_volume(records: Value) -> Decimal {
     let mut total_volume = Decimal::ZERO;
     for record in records.as_array().unwrap() {
@@ -41,10 +42,11 @@ fn calc_total_volume(records: Value) -> Decimal {
     total_volume
 }
 
+// 计算上涨幅度
 fn calc_rise_percentage(records: Value) -> Decimal {
     let records_array = records.as_array().unwrap();
 
-    if records_array.len() == 0 {
+    if records_array.len() < 2 {
         return Decimal::ZERO
     }
 
@@ -59,11 +61,40 @@ fn calc_rise_percentage(records: Value) -> Decimal {
     return rst
 }
 
+// 计算 波动 / 价格变化值
+fn calc_volatility_div_price_diff(records: Value) -> Decimal {
+    let records_array = records.as_array().unwrap();
+
+    if records_array.len() < 2 {
+        return Decimal::ZERO
+    }
+
+    let first_record = records_array[records_array.len() - 1].clone();
+    let last_record = records_array[0].clone();
+    let open = Decimal::from_str(first_record["open"].as_str().unwrap().to_string().as_str()).unwrap();
+    let close = Decimal::from_str(last_record["close"].as_str().unwrap().to_string().as_str()).unwrap();
+
+    let price_diff = (Decimal::ONE_HUNDRED * (close - open) / open).abs();
+    let mut volatility = Decimal::ZERO;
+    for record in records_array {
+        let record_high = Decimal::from_str(record["high"].as_str().unwrap().to_string().as_str()).unwrap();
+        let record_low = Decimal::from_str(record["low"].as_str().unwrap().to_string().as_str()).unwrap();
+        let mid = (record_high + record_low) / Decimal::TWO;
+
+        volatility += Decimal::ONE_HUNDRED * (record_high - record_low) / mid;
+    }
+
+    let mut vdpd = volatility / price_diff;
+    vdpd.rescale(6);
+
+    return vdpd;
+}
+
 // 计算振幅
 fn calc_amplification_percentage(records: Value) -> Decimal {
     let records_array = records.as_array().unwrap();
 
-    if records_array.len() == 0 {
+    if records_array.len() < 2 {
         return Decimal::ZERO
     }
 
@@ -195,6 +226,7 @@ pub async fn get_final_symbols(mode: &str, exchanges: &Vec<String>, minute_time_
         let mut amp = json!({});
         let mut volume = json!({});
         let mut tc = json!({});
+        let mut vdpd = json!({});
         let mut total_volume_by_symbol = Decimal::ZERO;
         let mut total_count_by_symbol = Decimal::ZERO;
         for exchange in exchanges {
@@ -206,6 +238,9 @@ pub async fn get_final_symbols(mode: &str, exchanges: &Vec<String>, minute_time_
             // 上涨幅度处理
             rise[exchange] = json!(calc_rise_percentage(records_map[exchange][symbol].clone()));
 
+            // 波动 / 价格变化
+            vdpd[exchange] = json!(calc_volatility_div_price_diff(records_map[exchange][symbol].clone()));
+
             // 振幅处理
             amp[exchange] = json!(calc_amplification_percentage(records_map[exchange][symbol].clone()));
 
@@ -220,6 +255,7 @@ pub async fn get_final_symbols(mode: &str, exchanges: &Vec<String>, minute_time_
         let value = json!({
             "symbol": symbol.clone(),
             "rise": rise,
+            "vdpd": vdpd,
             "amp": amp,
             "volume": volume,
             "tc": tc