hl 1 năm trước cách đây
mục cha
commit
41db425186

+ 4 - 4
config.toml

@@ -1,11 +1,11 @@
 # 配置代理
 proxy_address = "127.0.0.1:7890"
 # 配置币对
-symbol = "bigtime_usdt"
-# 配置交易所 目前支持["binance", "gate"]
-exchanges = ["binance", "gate"]
+symbol = "doge_usdt"
+# 配置交易所 目前支持["binance", "gate""okx",]
+exchanges = ["binance", "gate", "okx", ]
 # 配置查询时间(格式:2023-12-6 15:00:00)
-range_time = ["2023-12-5 15:00:01", "2023-12-5 15:00:10"]
+range_time = ["2023-12-5 15:00:01", "2023-12-5 15:20:58"]
 # 导出路径(不填则为默认路径"./")
 export_path = ""
 # 导出文件名字(不填则为默认名字"export")

+ 71 - 1
src/handle_ticker.rs

@@ -1,9 +1,13 @@
 use std::str::FromStr;
+use std::time::Duration;
+
 use rust_decimal::Decimal;
 use rust_decimal_macros::dec;
 use tracing::{error, info};
+
 use crate::binance_swap::binance_swap_standard;
 use crate::gate_swap::gate_swap_standard;
+use crate::okx_swap::okx_swap_standard;
 use crate::struct_standard::{AggTrades, Trades};
 
 pub async fn get_gate_ticker_info(symbol: &str, start_at: &str, end_at: &str) -> Vec<Trades> {
@@ -84,9 +88,75 @@ pub async fn get_binance_ticker_info(symbol: &str, start_at: &str, end_at: &str)
         //         symbol: agg_trades.symbol,
         //         create_time: agg_trades.create_time,
         //         size: agg_trades.size,
-        //         price: agg_trades.price,
+        //         price: agg_trades.price,z
         //     })
         // }
     }
     ticker_info_list
+}
+
+pub(crate) async fn get_okx_ticker_info(symbol: &str, start_at: &str, end_at: &str) -> Vec<Trades> {
+    // let mut start_at_d = Decimal::from_str(start_at).unwrap();
+    let  end_at_d = Decimal::from_str(end_at).unwrap();
+
+    // let end_time = if end_at_d - start_at_d > dec!(3600) { (start_at_d + dec!(3600)).to_string() } else { end_at_d.to_string() };
+    // start_at_d = if start_at != "" { format!("{}{}", start_at, "000") } else { "".to_string() }.parse().unwrap();
+    let end_at_d_str = if end_at != "" { format!("{}{}", end_at_d.to_string(), "000") } else { "".to_string() };
+
+
+    info!("正在查询 okx 信息,请稍后!");
+    info!("当前时间---{:?}---{:?}",start_at,end_at);
+
+    let mut list_array: Vec<Trades> = vec![];
+    let ticker_one = okx_swap_standard::standard_history_candles(symbol, "", end_at_d_str.as_str()).await.unwrap();
+    if ticker_one.len() > 0 {
+        let ticker_0 = ticker_one.get(0).cloned();
+        let ticker_size = ticker_one.get(ticker_one.len() - 1).cloned();
+        if ticker_one.len() > 0 {
+            let mut create_time_0 = ticker_0.unwrap().create_time;
+            let mut create_time_size = ticker_size.unwrap().create_time;
+            info!("第一次查询范畴--{:?}---{:?}",create_time_0,create_time_size);
+            list_array.extend(ticker_one.clone());
+
+            let mut i = 1;
+            loop {
+                tokio::time::sleep(Duration::from_millis(500)).await;
+                i = i + 1;
+                let start_at_str = format!("{}000", start_at);
+                // info!("比较:{:?}-{:?}",start_at_str , create_time_size);
+                if start_at_str < create_time_size {
+                    let ticker_one2 = okx_swap_standard::standard_history_candles(symbol, "", create_time_size.as_ref()).await.unwrap();
+
+                    let ticker_0_2 = ticker_one2.get(0).cloned();
+                    let ticker_size_2 = ticker_one2.get(ticker_one.len() - 1).cloned();
+
+                    create_time_0 = ticker_0_2.unwrap().create_time;
+                    create_time_size = ticker_size_2.unwrap().create_time;
+                    info!("第{:?}次查询范畴--{:?}---{:?}",i,create_time_0,create_time_size);
+                    list_array.extend(ticker_one2.clone());
+                } else {
+                    info!("查询完毕!");
+                    break;
+                }
+            }
+        }
+    } else {
+        info!("没有数据,无法拿到时间戳")
+    }
+
+    let mut set = std::collections::HashSet::new();
+    list_array.clone().into_iter().filter(|e| set.insert(e.clone())).collect()
+    //
+    // list_array = list_array.iter().filter(|item| item.create_time <= end_at.to_string()).cloned().collect();
+    // let mut ticker_info_list: Vec<Trades> = vec![];
+    // for agg_trades in list_array.clone() {
+    //     ticker_info_list.push(Trades {
+    //         id: agg_trades.start_id,
+    //         symbol: agg_trades.symbol,
+    //         create_time: agg_trades.create_time,
+    //         size: agg_trades.size,
+    //         price: agg_trades.price,
+    //     })
+    // }
+    // ticker_info_list
 }

+ 2 - 1
src/http/request.rs

@@ -1,5 +1,5 @@
 use reqwest::header::HeaderMap;
-use tracing::error;
+use tracing::{error, info};
 
 #[derive(Debug, Clone)]
 pub struct Response {
@@ -35,6 +35,7 @@ pub async fn get(url: &str, params: String, headers: Option<HeaderMap>) -> Respo
     }
     params_str = if !params_str.is_empty() { params_str[..params_str.len() - 1].to_string() } else { params_str.to_string() };
     let last_url = format!("{}?{}", url, params_str);
+    info!("请求:{:?}",last_url);
 
     let client = reqwest::Client::new();
     let header_info = headers.unwrap_or_default();

+ 8 - 0
src/main.rs

@@ -10,6 +10,7 @@ pub mod http;
 pub mod struct_standard;
 pub mod export;
 pub mod handle_ticker;
+pub mod okx_swap;
 
 #[tokio::main]
 async fn main() {
@@ -45,6 +46,13 @@ async fn main() {
                     ticker_info,
                 }
             }
+            "OKX" => {
+                let ticker_info = handle_ticker::get_okx_ticker_info(&symbol, &start_at, &end_at).await;
+                ExportExchangeTickerInfo {
+                    name: exchange.to_string(),
+                    ticker_info,
+                }
+            }
             _ => {
                 error!("交易所输入错误!");
                 panic!("交易所输入错误!")

+ 2 - 0
src/okx_swap/mod.rs

@@ -0,0 +1,2 @@
+pub mod okx_swap_rest;
+pub mod okx_swap_standard;

+ 17 - 0
src/okx_swap/okx_swap_rest.rs

@@ -0,0 +1,17 @@
+use crate::http::request::{get, Response};
+
+pub async fn get_history_trades(symbol: &str,start_at: &str, end_at: &str) -> Response {
+    let url = "https://www.okx.com/api/v5/market/history-trades";
+    let mut params = serde_json::json!({
+        "instId":symbol,
+        "type":"2"
+    });
+    if end_at.len()>0{
+        params["after"] = serde_json::Value::from(end_at);
+    }
+    if start_at.len()>0{
+        params["before"] = serde_json::Value::from(start_at);
+    }
+    get(url, params.to_string(), None).await
+}
+

+ 77 - 0
src/okx_swap/okx_swap_standard.rs

@@ -0,0 +1,77 @@
+use std::io::{Error, ErrorKind};
+
+use serde::{Deserialize, Serialize};
+
+use crate::okx_swap::okx_swap_rest::get_history_trades;
+use crate::struct_standard::Trades;
+
+#[derive(Debug, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct SwapTrades {
+    inst_id: String,
+    //	产品ID
+    trade_id: String,
+    //	成交ID
+    px: String,
+    //	成交价格
+    sz: String,
+    //成交数量
+    side: String,
+    //成交方向buy:,//买sell:,//卖
+    ts: String,//成交时间,Unix时间戳的毫秒数格式, 如1597026383085
+}
+
+pub(crate) async fn standard_history_candles(symbol: &str, start_at: &str, end_at: &str) -> Result<Vec<Trades>, Error> {
+    let mut symbol_fmt = symbol.replace("_", "-").clone();
+    symbol_fmt = symbol_fmt.to_uppercase();
+
+
+    let res_data = get_history_trades(&symbol_fmt, &start_at, &end_at).await;
+    if res_data.code == "200" {
+        let res_data_str = res_data.data;
+        let json_value: serde_json::Value = serde_json::from_str(&res_data_str).unwrap();
+
+        let result = if json_value.get("code").is_some() && json_value["code"] == "0" {
+            let data = json_value.get("data").unwrap();
+            // info!("data:{:?}",data.to_string());
+
+            let ticker_list: Vec<SwapTrades> = serde_json::from_str(data.to_string().as_str()).unwrap();
+            ticker_list.iter().map(|item| {
+                Trades {
+                    id: item.trade_id.to_string(),
+                    symbol:symbol.to_string(),
+                    create_time:  item.ts.to_string(),
+                    size: item.sz.clone(),
+                    price:item.px.clone(),
+                }
+               // Trades {
+               //      id: "".to_string(),
+               //      start_id: item.ts.to_string(),
+               //      end_id: "".to_string(),
+               //      symbol: symbol.to_string(),
+               //      create_time: item.ts.to_string(),
+               //      size: item.sz.clone(),
+               //      price: item.px.clone(),
+               //  }
+            }).collect()
+        } else {
+            vec![]
+        };
+        // let result = trades_list.iter().map(|item| {
+        //     //
+        //     // ite
+        //     // AggTrades {
+        //     //     id: item.a.to_string(),
+        //     //     start_id: item.f.to_string(),
+        //     //     end_id: item.l.to_string(),
+        //     //     symbol: symbol.to_string(),
+        //     //     create_time: item.t.to_string(),
+        //     //     size: item.q.to_string(),
+        //     //     price: item.p.clone(),
+        //     // }
+        // }).collect();
+        Ok(result)
+    } else {
+        Err(Error::new(ErrorKind::Other, res_data.to_string()))
+    }
+}