Эх сурвалжийг харах

一些公共接口的实现。

skyfffire 3 сар өмнө
parent
commit
9c70f407e6

+ 1 - 1
exchanges/Cargo.toml

@@ -11,7 +11,7 @@ serde_json = "1.0.104"
 #tungstenite = { git = "https://github.com/PrivateRookie/tungstenite-rs.git", rev = "1d9289276518e5ab7e5194126d40b441d8938375" }
 #tungstenite = { git = "https://github.com/PrivateRookie/tungstenite-rs.git", rev = "f368f3087d50d97658fda5337550e587bb1ba1b6" }
 
-tokio-tungstenite= { git = "https://github.com/HonestHouLiang/tokio-tungstenite.git",rev = "208fc9b09bcc2e2c8cb52e1cde5087446464fc91"  }
+tokio-tungstenite= { git = "https://github.com/skyfffire/tokio-tungstenite-proxy.git" }
 futures-util = { version = "0.3.28", default-features = false, features = ["sink", "std"] }
 futures-channel = "0.3.28"
 

+ 13 - 3
exchanges/src/mexc_swap_rest.rs

@@ -33,7 +33,7 @@ impl MexcSwapRest {
 
     pub fn new(is_colo: bool, login_param: BTreeMap<String, String>) -> MexcSwapRest
     {
-        return MexcSwapRest::new_with_tag("default-MexcSwapRest".to_string(), is_colo, login_param);
+        MexcSwapRest::new_with_tag("default-MexcSwapRest".to_string(), is_colo, login_param)
     }
     pub fn new_with_tag(tag: String, is_colo: bool, login_param: BTreeMap<String, String>) -> MexcSwapRest {
         let base_url = if is_colo {
@@ -62,7 +62,7 @@ impl MexcSwapRest {
     /*******************************************************************************************************/
     /*****************************************rest请求函数********************************************************/
     /*******************************************************************************************************/
-    //获取合约信息
+    // 获取合约信息
     pub async fn get_market(&mut self, params: Value) -> ResponseData {
         let data = self.request("GET".to_string(),
                                 "/api/v1".to_string(),
@@ -72,6 +72,17 @@ impl MexcSwapRest {
         ).await;
         data
     }
+    
+    // 获取合约行情数据
+    pub async fn get_ticker(&mut self, params: Value) -> ResponseData {
+        let data = self.request("GET".to_string(),
+                                "/api/v1".to_string(),
+                                "/contract/ticker".to_string(),
+                                false,
+                                params,
+        ).await;
+        data
+    }
 
     /*******************************************************************************************************/
     /*****************************************工具函数********************************************************/
@@ -224,7 +235,6 @@ impl MexcSwapRest {
         sign
     }
 
-    // async fn http_tool(&mut self, request_path: String, request_type: String, params: String, headers: HeaderMap) -> Result<ResponseData, reqwest::Error> {
     async fn http_tool(&mut self, request_path: String,
                        request_type: String,
                        params: String,

+ 1 - 0
global/src/public_params.rs

@@ -32,4 +32,5 @@ pub const BITGET_USDT_SPOT_LIMIT:i64 = 100;
 pub const BYBIT_USDT_SWAP_LIMIT:i64 = 10;
 pub const HTX_USDT_SWAP_LIMIT:i64 = 24;
 pub const MEXC_SPOT_LIMIT:i64 = 333;
+pub const MEXC_USDT_SWAP_LIMIT:i64 = 30;
 pub const RATIO:i64 = 4;

+ 33 - 11
standard/src/mexc_swap.rs

@@ -104,15 +104,36 @@ impl Platform for MexcSwap {
     }
     // 获取市场行情
     async fn get_ticker(&mut self) -> Result<Ticker, Error> {
-        Err(Error::new(ErrorKind::NotFound, "mexc_swap:该交易所方法未实现".to_string()))
+        self.get_ticker_symbol(self.symbol.clone()).await
     }
 
-    async fn get_record(&mut self, _interval: String) -> Result<Vec<Record>, Error> {
-        todo!("还没做呢")
+    async fn get_ticker_symbol(&mut self, symbol: String) -> Result<Ticker, Error> {
+        let params = json!({
+            "symbol": symbol,
+        });
+
+        let res_data = self.request.get_ticker(params).await;
+        if res_data.code == 200 {
+            let ticker_info = res_data.data;
+            let time = Decimal::from_i64(ticker_info["timestamp"].as_i64().unwrap()).unwrap();
+            let result = Ticker {
+                time,
+                high: Decimal::from_f64(ticker_info["high24Price"].as_f64().unwrap()).unwrap(),
+                low: Decimal::from_f64(ticker_info["lower24Price"].as_f64().unwrap()).unwrap(),
+                sell: Decimal::from_f64(ticker_info["ask1"].as_f64().unwrap()).unwrap(),
+                buy: Decimal::from_f64(ticker_info["bid1"].as_f64().unwrap()).unwrap(),
+                last: Decimal::from_f64(ticker_info["lastPrice"].as_f64().unwrap()).unwrap(),
+                volume: Decimal::from_f64(ticker_info["volume24"].as_f64().unwrap()).unwrap(),
+                open_interest: Decimal::from_f64(ticker_info["holdVol"].as_f64().unwrap()).unwrap(),
+            };
+            Ok(result)
+        } else {
+            Err(Error::new(ErrorKind::Other, res_data.to_string()))
+        }
     }
 
-    async fn get_ticker_symbol(&mut self, _symbol: String) -> Result<Ticker, Error> {
-        Err(Error::new(ErrorKind::NotFound, "mexc_swap:该交易所方法未实现".to_string()))
+    async fn get_record(&mut self, _interval: String) -> Result<Vec<Record>, Error> {
+        todo!("还没做呢")
     }
 
     async fn get_market(&mut self) -> Result<Market, Error> {
@@ -128,16 +149,17 @@ impl Platform for MexcSwap {
                     Err(Error::new(ErrorKind::Other, res_data.to_string()))
                 }
                 Some(value) => {
+                    // info!("get_market:res_data={:}", serde_json::to_string_pretty(&value)?);
                     let symbol = value["symbol"].as_str().unwrap().to_string();
                     let base_asset = value["baseCoin"].as_str().unwrap().to_string();
                     let quote_asset = value["quoteCoin"].as_str().unwrap().to_string();
-                    let tick_size = Decimal::from_i64(value["priceUnit"].as_i64().unwrap()).unwrap();
-                    let amount_size = Decimal::from_i64(value["volUnit"].as_i64().unwrap()).unwrap();
+                    let tick_size = Decimal::from_f64(value["priceUnit"].as_f64().unwrap()).unwrap();
+                    let amount_size = Decimal::from_f64(value["volUnit"].as_f64().unwrap()).unwrap();
                     let price_precision = Decimal::from_i64(value["priceScale"].as_i64().unwrap()).unwrap();
                     let amount_precision = Decimal::from_i64(value["volScale"].as_i64().unwrap()).unwrap();
                     let min_qty = Decimal::from_i64(value["minVol"].as_i64().unwrap()).unwrap();
                     let max_qty = Decimal::from_i64(value["maxVol"].as_i64().unwrap()).unwrap();
-                    let multiplier = Decimal::from_i64(value["contractSize"].as_i64().unwrap()).unwrap();
+                    let multiplier = Decimal::from_f64(value["contractSize"].as_f64().unwrap()).unwrap();
 
                     let result = Market {
                         symbol,
@@ -179,13 +201,13 @@ impl Platform for MexcSwap {
                     let symbol = value["symbol"].as_str().unwrap().to_string();
                     let base_asset = value["baseCoin"].as_str().unwrap().to_string();
                     let quote_asset = value["quoteCoin"].as_str().unwrap().to_string();
-                    let tick_size = Decimal::from_i64(value["priceUnit"].as_i64().unwrap()).unwrap();
-                    let amount_size = Decimal::from_i64(value["volUnit"].as_i64().unwrap()).unwrap();
+                    let tick_size = Decimal::from_f64(value["priceUnit"].as_f64().unwrap()).unwrap();
+                    let amount_size = Decimal::from_f64(value["volUnit"].as_f64().unwrap()).unwrap();
                     let price_precision = Decimal::from_i64(value["priceScale"].as_i64().unwrap()).unwrap();
                     let amount_precision = Decimal::from_i64(value["volScale"].as_i64().unwrap()).unwrap();
                     let min_qty = Decimal::from_i64(value["minVol"].as_i64().unwrap()).unwrap();
                     let max_qty = Decimal::from_i64(value["maxVol"].as_i64().unwrap()).unwrap();
-                    let multiplier = Decimal::from_i64(value["contractSize"].as_i64().unwrap()).unwrap();
+                    let multiplier = Decimal::from_f64(value["contractSize"].as_f64().unwrap()).unwrap();
 
                     let result = Market {
                         symbol,

+ 1 - 1
strategy/Cargo.toml

@@ -23,4 +23,4 @@ reqwest = { version = "0.11.14", features = ["json"] }
 
 futures-util = { version = "0.3.28", default-features = false, features = ["sink", "std"] }
 futures-channel = "0.3.28"
-tokio-tungstenite= { git = "https://github.com/HonestHouLiang/tokio-tungstenite.git",rev = "208fc9b09bcc2e2c8cb52e1cde5087446464fc91"  }
+tokio-tungstenite= { git = "https://github.com/skyfffire/tokio-tungstenite-proxy.git" }

+ 4 - 1
strategy/src/core.rs

@@ -20,7 +20,7 @@ use global::params::Params;
 use global::trace_stack::TraceStack;
 use standard::{Account, Depth, Market, Order, OrderCommand, Platform, Position, PositionModeEnum, Record, SpecialTicker, Ticker, Trade};
 use standard::exchange::{Exchange};
-use standard::exchange::ExchangeEnum::{BinanceSwap, BybitSwap, BitgetSwap, GateSwap};
+use standard::exchange::ExchangeEnum::{BinanceSwap, BybitSwap, BitgetSwap, GateSwap, MexcSwap};
 
 use crate::model::{LocalPosition, OrderInfo, TokenParam};
 use crate::predictor::Predictor;
@@ -224,6 +224,9 @@ impl Core {
                 // "htx_usdt_swap" => {
                 //     Exchange::new(HtxSwap, symbol, params.colo != 0i8, exchange_params, order_sender, error_sender).await
                 // }
+                "mexc_usdt_swap" => {
+                    Exchange::new(MexcSwap, symbol, params.colo != 0i8, exchange_params, order_sender, error_sender).await
+                }
                 _ => {
                     error!("203未找到对应的交易所rest枚举!");
                     panic!("203未找到对应的交易所rest枚举!");

+ 7 - 7
strategy/src/mexc_usdt_swap.rs

@@ -31,14 +31,14 @@ pub async fn reference_mexc_swap_run(is_shutdown_arc: Arc<AtomicBool>,
 
         // 读取数据
         let core_arc_clone = Arc::clone(&core_arc);
-        let mut rest = core_arc_clone.lock().await.platform_rest.clone_box();
+        let rest = core_arc_clone.lock().await.platform_rest.clone_box();
         let multiplier = rest.get_self_market().multiplier;
-        let mut records = rest.get_record("1".to_string()).await.unwrap();
-        for record in records.iter_mut() {
-            let core_arc_clone = core_arc.clone();
-
-            on_record(core_arc_clone, record).await
-        }
+        // let mut records = rest.get_record("1".to_string()).await.unwrap();
+        // for record in records.iter_mut() {
+        //     let core_arc_clone = core_arc.clone();
+        // 
+        //     on_record(core_arc_clone, record).await
+        // }
 
         let depth_asks = Arc::new(Mutex::new(Vec::new()));
         let depth_bids = Arc::new(Mutex::new(Vec::new()));

+ 5 - 1
strategy/src/utils.rs

@@ -92,7 +92,9 @@ pub fn get_limit_requests_num_per_second(exchange: String) -> i64 {
         return public_params::BYBIT_USDT_SWAP_LIMIT * public_params::RATIO;
     } else if exchange.eq("htx_usdt_swap") {
         return public_params::HTX_USDT_SWAP_LIMIT * public_params::RATIO;
-    }else {
+    } else if exchange.eq("mexc_usdt_swap") {
+        return public_params::MEXC_USDT_SWAP_LIMIT * public_params::RATIO;
+    } else {
         error!("限频规则(ratio)未找到,请检查配置!");
         panic!("限频规则(ratio)未找到,请检查配置!");
     }
@@ -126,6 +128,8 @@ pub fn get_limit_order_requests_num_per_second(exchange: String) -> i64 {
         return public_params::BYBIT_USDT_SWAP_LIMIT
     } else if exchange.eq("htx_usdt_swap") {
         return public_params::HTX_USDT_SWAP_LIMIT
+    }  else if exchange.eq("mexc_usdt_swap") {
+        return public_params::MEXC_USDT_SWAP_LIMIT
     } else {
         error!("限频规则(limit)未找到,请检查配置!");
         panic!("限频规则(limit)未找到,请检查配置!");