Browse Source

1.修改回撤计算
2.添加双模导出

gepangpang 1 year ago
parent
commit
faee6c252d
4 changed files with 70 additions and 52 deletions
  1. 2 0
      config_balance.toml.sample
  2. 31 17
      src/export_balance.rs
  3. 35 35
      src/main.rs
  4. 2 0
      src/utils/utils.rs

+ 2 - 0
config_balance.toml.sample

@@ -10,6 +10,8 @@ exchanges = ["gate"]
 range_time = ["2023-12-20 18:00:00", "2023-12-26 10:00:00"]
 # 配置查询时间(格式:2023-12-6 15:00:00)
 count_range_time = ["2023-12-25 10:00:00", "2023-12-26 10:00:00"]
+# 配置导出表格模式(1:按小时导出,2:按余额变动导出)
+export_mode = 1
 # 导出路径(不填则为默认路径"./")
 export_path = ""
 # 导出文件名字(不填则为默认名字"export")

+ 31 - 17
src/export_balance.rs

@@ -1,9 +1,10 @@
+use std::cmp::min;
 use std::collections::{BTreeMap};
 use std::str::FromStr;
 use chrono::NaiveDateTime;
 use rust_decimal::Decimal;
 use rust_decimal_macros::dec;
-use tracing::info;
+use tracing::{error, info};
 use crate::{export_template};
 use crate::swap_gate::gate_swap_rest_utils::GateSwapRest;
 use crate::utils::utils::BalanceConfigInfo;
@@ -128,18 +129,22 @@ pub async fn export_balance(config_info: BalanceConfigInfo) {
 
     // 根据小时填充
     let mut last_timestamp: i64 = -1;
-    let mut export_time_list: Vec<i64> = vec![];
+    let mut hour_time_list: Vec<i64> = vec![];
     for item in start_time..end_time {
         if item / 3600 != last_timestamp / 3600 {
             last_timestamp = item;
-            export_time_list.push(last_timestamp);
+            hour_time_list.push(last_timestamp);
         }
     }
-    export_time_list.push(end_time);
-    let all_balance_info: Vec<Vec<String>> = supply_balance(all_account_balance_info.clone(), export_time_list.clone());
+    hour_time_list.push(end_time);
+    let all_balance_info: Vec<Vec<String>> = supply_balance(all_account_balance_info.clone(), hour_time_list.clone());
 
     account_name_list.push("total_balance".to_string());
-    export_template::template_balance::export_html(config_info_clone.clone(), &account_name_list, &export_time_list, &all_balance_info, statistic_result);
+    match config_info_clone.export_mode {
+        1 => export_template::template_balance::export_html(config_info, &account_name_list, &hour_time_list, &all_balance_info, statistic_result),
+        2 => export_template::template_balance::export_html(config_info, &account_name_list, &count_time_list, &count_balance_info, statistic_result),
+        _ => error!("导出模式错误,请检查导出模式!")
+    };
 }
 
 
@@ -155,25 +160,34 @@ pub fn statistic_balance(balance_info: Vec<Vec<String>>, count_start_time: i64,
         balance_info_map.insert(info[0].clone(), default_info);
     }
     for (account_name, balance_info) in balance_info_map.clone() {
+        let mut max_price = dec!(0);
+        let mut min_price = dec!(0);
+        let mut withdrawal = dec!(0);
         for (index, info) in balance_info.iter().enumerate() {
-            if index == 0 { continue; }
-            let present = Decimal::from_str(&info[4]).unwrap();
-            let past = Decimal::from_str(&balance_info[index - 1][4]).unwrap();
-            let withdrawal = ((Decimal::ONE - (present / past)) * dec!(100)).round_dp(2);
-            if account_name != "total_balance" {
-                if max_withdrawal < withdrawal { max_withdrawal = withdrawal }
-            } else {
-                if max_total_withdrawal < withdrawal { max_total_withdrawal = withdrawal }
+            let present_price = Decimal::from_str(&info[4]).unwrap();
+            if index == 0 || max_price < present_price {
+                max_price = present_price;
+                min_price = present_price;
+                continue;
+            }
+            let present_withdrawal = ((Decimal::ONE - (min_price / max_price)) * dec!(100)).round_dp(2);
+            if present_withdrawal > withdrawal { withdrawal = present_withdrawal }
+
+            if max_price > present_price && present_price < min_price {
+                min_price = present_price;
             }
         }
-        if account_name == "total_balance" {
+        if account_name != "total_balance" {
+            if max_withdrawal < withdrawal { max_withdrawal = withdrawal }
+        } else {
+            max_total_withdrawal = withdrawal;
             let present = Decimal::from_str(&balance_info[0][4]).unwrap();
             let past = Decimal::from_str(&balance_info[balance_info.len() - 1][4]).unwrap();
             total_income = (((past / present) - Decimal::ONE) * dec!(100)).round_dp(2);
         }
     }
-    let start_time = NaiveDateTime::from_timestamp_millis((count_start_time + 8 * 3600) * 1000).unwrap().format("%Y-%m-%d日%H点").to_string();
-    let end_time = NaiveDateTime::from_timestamp_millis((end_start_time + 8 * 3600) * 1000).unwrap().format("%Y-%m-%d日%H点").to_string();
+    let start_time = NaiveDateTime::from_timestamp_millis((count_start_time + 8 * 3600) * 1000).unwrap().format("%d日%H点").to_string();
+    let end_time = NaiveDateTime::from_timestamp_millis((end_start_time + 8 * 3600) * 1000).unwrap().format("%d日%H点").to_string();
     format!("{}到{},总收益约为{}%,最大回撤约为{}%,其中单个账号最大回撤约为{}%。", start_time, end_time, total_income, max_total_withdrawal, max_withdrawal)
 }
 

+ 35 - 35
src/main.rs

@@ -29,45 +29,45 @@ async fn main() {
         balance_info: balance_config,
     };
 
-    // if config_obj.ticker_info.is_export {
-    //     info!("----------正在导出Ticker信息----------");
-    //     let config_clone = config_obj.ticker_info.clone();
-    //     if http::proxy::ParsingDetail::http_enable_proxy(config_clone.proxy_address.clone()) {
-    //         info!("检测有代理配置,配置走代理");
-    //     }
-    //     export_ticker::export_ticker(config_clone).await;
-    // }
-    // if config_obj.balance_info.is_export {
-    //     info!("----------正在导出Balance信息----------");
-    //     let config_clone = config_obj.balance_info.clone();
-    //     if http::proxy::ParsingDetail::http_enable_proxy(config_clone.proxy_address.clone()) {
-    //         info!("检测有代理配置,配置走代理");
-    //     }
-    //     export_balance::export_balance(config_clone).await;
-    // }
-
-    if true {
+    if config_obj.ticker_info.is_export {
         info!("----------正在导出Ticker信息----------");
         let config_clone = config_obj.ticker_info.clone();
         if http::proxy::ParsingDetail::http_enable_proxy(config_clone.proxy_address.clone()) {
             info!("检测有代理配置,配置走代理");
         }
-
-        // "10M" => (1000 * 60 * 10),
-        // "20M" => (1000 * 60 * 20),
-        // "30M" => (1000 * 60 * 30),
-        // "1H" => (1000 * 60 * 60 * 1),
-        // "10H" => (1000 * 60 * 60 * 10),
-        // "1D" => (1000 * 60 * 60 * 24),
-        // "2D" => (1000 * 60 * 60 * 24 * 2),
-        // "3D" => (1000 * 60 * 60 * 24 * 3),
-        // "5D" => (1000 * 60 * 60 * 24 * 5),
-        // "1Z" => (1000 * 60 * 60 * 24 * 7),
-        // "2Z" => (1000 * 60 * 60 * 24 * 7 * 2),
-        // "4Z" => (1000 * 60 * 60 * 24 * 7 * 4),
-        // 提示: 1. 代码颗粒度为秒级,100毫秒与999毫秒都归纳于1秒之内,
-        //       2. 不同币对成交量可能存在差异,当前查询是1000条已经是最大值,但是有点币对交易量只有几百,但是有的可能好几千,这个需要酌情调整
-        //time_interval -数据请求的时间间隔 毫秒级
-        hl_pr_utile::export_ticker("WLD_USDT", "GATE", "10M", (1 * 60)).await;
+        export_ticker::export_ticker(config_clone).await;
     }
+    if config_obj.balance_info.is_export {
+        info!("----------正在导出Balance信息----------");
+        let config_clone = config_obj.balance_info.clone();
+        if http::proxy::ParsingDetail::http_enable_proxy(config_clone.proxy_address.clone()) {
+            info!("检测有代理配置,配置走代理");
+        }
+        export_balance::export_balance(config_clone).await;
+    }
+
+    // if true {
+    //     info!("----------正在导出Ticker信息----------");
+    //     let config_clone = config_obj.ticker_info.clone();
+    //     if http::proxy::ParsingDetail::http_enable_proxy(config_clone.proxy_address.clone()) {
+    //         info!("检测有代理配置,配置走代理");
+    //     }
+    //
+    //     // "10M" => (1000 * 60 * 10),
+    //     // "20M" => (1000 * 60 * 20),
+    //     // "30M" => (1000 * 60 * 30),
+    //     // "1H" => (1000 * 60 * 60 * 1),
+    //     // "10H" => (1000 * 60 * 60 * 10),
+    //     // "1D" => (1000 * 60 * 60 * 24),
+    //     // "2D" => (1000 * 60 * 60 * 24 * 2),
+    //     // "3D" => (1000 * 60 * 60 * 24 * 3),
+    //     // "5D" => (1000 * 60 * 60 * 24 * 5),
+    //     // "1Z" => (1000 * 60 * 60 * 24 * 7),
+    //     // "2Z" => (1000 * 60 * 60 * 24 * 7 * 2),
+    //     // "4Z" => (1000 * 60 * 60 * 24 * 7 * 4),
+    //     // 提示: 1. 代码颗粒度为秒级,100毫秒与999毫秒都归纳于1秒之内,
+    //     //       2. 不同币对成交量可能存在差异,当前查询是1000条已经是最大值,但是有点币对交易量只有几百,但是有的可能好几千,这个需要酌情调整
+    //     //time_interval -数据请求的时间间隔 毫秒级
+    //     hl_pr_utile::export_ticker("WLD_USDT", "GATE", "10M", (1 * 60)).await;
+    // }
 }

+ 2 - 0
src/utils/utils.rs

@@ -72,6 +72,7 @@ pub struct BalanceConfigInfo {
     pub exchanges: Vec<String>,
     pub range_time: Vec<String>,
     pub count_range_time: Vec<String>,
+    pub export_mode: u64,
     pub export_path: String,
     pub export_name: String,
 }
@@ -85,6 +86,7 @@ impl BalanceConfigInfo {
             exchanges: vec![],
             range_time: vec![],
             count_range_time: vec![],
+            export_mode: 0,
             export_path: "".to_string(),
             export_name: "".to_string(),
         }