Browse Source

可以批量导出多个,可以设定向前回溯范围。

skyffire 1 year ago
parent
commit
bbf958b863
4 changed files with 41 additions and 14 deletions
  1. 11 4
      config_analyze.toml.sample
  2. 6 7
      src/export_analyze.rs
  3. 20 1
      src/main.rs
  4. 4 2
      src/utils/utils.rs

+ 11 - 4
config_analyze.toml.sample

@@ -2,9 +2,16 @@
 is_export = true
 # 配置代理
 proxy_address = "127.0.0.1:7890"
-# 查询币对
-symbol = "kas_USDT"
+# 查询币对列表,突发行情时查单个可以注释掉其他的
+# 注:不建议一次查太多,免得限频,小心封ip(试过6个没有事)
+symbols = [
+#    "BAKE_USDT",
+#    "OP_USDT",
+    "KAS_USDT"
+]
 # 配置交易所 目前支持["binance", "gate"]
 exchanges = ["gate"]
-#时间范围 m:分钟, H:小时, D:天, W:周, M:月(分钟和月份区分大小写,其余不区分。例:10m)
-range_interval = "4H"
+# 时间范围 m:分钟, H:小时, D:天, W:周, M:月(分钟和月份区分大小写,其余不区分。例:10m)
+range_interval = "4H"
+# 向前回溯幅度范围(ms)
+range_limit = 61

+ 6 - 7
src/export_analyze.rs

@@ -21,7 +21,7 @@ pub struct RangeInterval {
     pub interval_text: String,
 }
 
-pub async fn export_ticker(symbol: &str, exchange: &str, range_interval: &str) {
+pub async fn export_ticker(symbol: &str, exchange: &str, range_interval: &str, range_limit: Decimal) {
     // 从此刻往前算时间
     let timestamp_seconds_now = Utc::now().timestamp();
     let range_interval = parse_range_interval(range_interval);
@@ -30,7 +30,7 @@ pub async fn export_ticker(symbol: &str, exchange: &str, range_interval: &str) {
     let end_at = timestamp_seconds_now;
 
     let ticker_info = get_ticker_info(&exchange.to_uppercase(), start_at, end_at, symbol).await;
-    let amplitude_map = get_amplitude(ticker_info);
+    let amplitude_map = get_amplitude(ticker_info, range_limit);
 
     export_template::template_analyze::export_html(&range_interval.interval_text, &symbol.to_uppercase(), amplitude_map.keys().cloned().collect(), amplitude_map.values().cloned().collect())
 }
@@ -69,12 +69,11 @@ pub fn parse_range_interval(range_interval: &str) -> RangeInterval {
 }
 
 // 计算最近range毫秒的的波动
-pub fn calc_gate_ticker_amplitude(ticker_map: BTreeMap<u64, Ticker>) -> BTreeMap<Decimal, Decimal> {
-    let limit_range = dec!(100);
+pub fn calc_gate_ticker_amplitude(ticker_map: BTreeMap<u64, Ticker>, limit_range: Decimal) -> BTreeMap<Decimal, Decimal> {
     let mut amplitude_map: BTreeMap<Decimal, Decimal> = BTreeMap::new();
 
     // 每一个元素都遍历一遍
-    info!("精确幅度计算执行中");
+    info!("精确幅度计算执行中……");
     let keys: Vec<u64> = ticker_map.keys().cloned().collect();
     for (index, create_time) in keys.iter().enumerate() {
         let ticker = ticker_map.get(&create_time).unwrap();
@@ -164,9 +163,9 @@ pub async fn get_ticker_info(exchange: &str, start_at: i64, end_at: i64, symbol:
     return ticker_map;
 }
 
-pub fn get_amplitude(ticker_map: BTreeMap<u64, Ticker>) -> BTreeMap<Decimal, Decimal> {
+pub fn get_amplitude(ticker_map: BTreeMap<u64, Ticker>, range_limit: Decimal) -> BTreeMap<Decimal, Decimal> {
     // 逻辑层执行
-    let amplitude_map = calc_gate_ticker_amplitude(ticker_map);
+    let amplitude_map = calc_gate_ticker_amplitude(ticker_map, range_limit);
     let amplitude_map_len = amplitude_map.len();
     info!("逻辑层执行完毕,剩余有效波动条数:{}。", amplitude_map_len);
 

+ 20 - 1
src/main.rs

@@ -1,3 +1,5 @@
+use tokio::spawn;
+use tokio::task::JoinHandle;
 use tracing::info;
 use crate::utils::logs;
 use crate::utils::utils::{AnalyzeConfigInfo, BalanceConfigInfo, TickerConfigInfo};
@@ -56,6 +58,23 @@ async fn main() {
         if http::proxy::ParsingDetail::http_enable_proxy(config_clone.proxy_address.clone()) {
             info!("检测有代理配置,配置走代理");
         }
-        export_analyze::export_ticker(&config_clone.symbol, &config_clone.exchanges[0], &config_clone.range_interval).await;
+
+        info!("批量导出多币对。");
+        let mut handles: Vec<JoinHandle<()>> = Vec::new();
+        for symbol in &config_clone.symbols {
+            let s = symbol.clone();
+            let e = config_clone.exchanges[0].clone();
+            let ri = config_clone.range_interval.clone();
+            let rl = config_clone.range_limit;
+            let handle = spawn(async move {
+                export_analyze::export_ticker(&s, &e, &ri, rl).await;
+            });
+            handles.push(handle);
+        }
+
+        // 等待请求完毕
+        for handle in handles {
+            handle.await.expect("协程运行失败");
+        }
     }
 }

+ 4 - 2
src/utils/utils.rs

@@ -114,9 +114,10 @@ pub fn get_balance_config_info(file_path: &str) -> BalanceConfigInfo {
 pub struct AnalyzeConfigInfo {
     pub is_export: bool,
     pub proxy_address: String,
-    pub symbol: String,
+    pub symbols: Vec<String>,
     pub exchanges: Vec<String>,
     pub range_interval: String,
+    pub range_limit: Decimal,
 }
 
 impl AnalyzeConfigInfo {
@@ -124,9 +125,10 @@ impl AnalyzeConfigInfo {
         AnalyzeConfigInfo {
             is_export: false,
             proxy_address: "".to_string(),
-            symbol: "".to_string(),
+            symbols: vec![],
             exchanges: vec![],
             range_interval: "".to_string(),
+            range_limit: Default::default(),
         }
     }
 }