Kaynağa Gözat

币安的改造完了,与老版本对比测试看看效果。

skyffire 1 yıl önce
ebeveyn
işleme
36f14c8d52

+ 3 - 4
exchanges/src/binance_swap_ws.rs

@@ -223,7 +223,7 @@ impl BinanceSwapWs {
         // trace!("原始数据");
         // trace!(?text);
         let mut res_data = ResponseData::new("".to_string(), "200".to_string(), "success".to_string(), Value::Null);
-        let json_value: serde_json::Value = serde_json::from_str(&text).unwrap();
+        let json_value: Value = serde_json::from_str(&text).unwrap();
 
         if json_value.get("result").is_some() && json_value.get("id").is_some() &&
             json_value.get("id").unwrap() == 1
@@ -234,11 +234,10 @@ impl BinanceSwapWs {
             res_data.code = json_value["error"]["code"].to_string();
             res_data.message = json_value["error"]["msg"].to_string();
         } else if json_value.get("stream").is_some() {//订阅返回
-            info!("{}", json_value.get("data").as_ref().unwrap());
-            // res_data.data = format!("{}", json_value.get("data").as_ref().unwrap());
+            res_data.data = json_value["data"].clone();
             res_data.code = "200".to_string();
 
-            let channel = format!("{}", json_value.get("stream").as_ref().unwrap());
+            let channel = json_value["stream"].as_str().unwrap();
             if channel.contains("@aggTrade") {
                 res_data.channel = "aggTrade".to_string();
             } else if channel.contains("@depth20@100ms") {

+ 11 - 15
standard/src/binance_swap_handle.rs

@@ -1,6 +1,7 @@
 use std::str::FromStr;
 use rust_decimal::Decimal;
 use rust_decimal_macros::dec;
+use serde_json::Value;
 use exchanges::response_base::ResponseData;
 use crate::{MarketOrder, SpecialDepth, SpecialTicker};
 use crate::exchange::ExchangeEnum;
@@ -8,24 +9,19 @@ use crate::handle_info::HandleSwapInfo;
 
 
 // 处理特殊Ticker信息
-pub fn handle_special_ticker(res_data: ResponseData) -> SpecialDepth {
-    let res_data_json = res_data.data;
-    format_special_ticker(res_data_json, res_data.label)
-}
-
-pub fn format_special_ticker(data: serde_json::Value, label: String) -> SpecialDepth {
-    let bp = Decimal::from_str(data["b"].as_str().unwrap()).unwrap();
-    let bq = Decimal::from_str(data["B"].as_str().unwrap()).unwrap();
-    let ap = Decimal::from_str(data["a"].as_str().unwrap()).unwrap();
-    let aq = Decimal::from_str(data["A"].as_str().unwrap()).unwrap();
+pub fn handle_special_ticker(res_data: &ResponseData) -> SpecialDepth {
+    let bp = Decimal::from_str((*res_data).data["b"].as_str().unwrap()).unwrap();
+    let bq = Decimal::from_str((*res_data).data["B"].as_str().unwrap()).unwrap();
+    let ap = Decimal::from_str((*res_data).data["a"].as_str().unwrap()).unwrap();
+    let aq = Decimal::from_str((*res_data).data["A"].as_str().unwrap()).unwrap();
     let mp = (bp + ap) * dec!(0.5);
-    let t = Decimal::from_str(&data["u"].to_string()).unwrap();
-    let create_at = data["E"].as_i64().unwrap() * 1000;
+    let t = Decimal::from_str(&(*res_data).data["u"].to_string()).unwrap();
+    let create_at = (*res_data).data["E"].as_i64().unwrap() * 1000;
 
     let ticker_info = SpecialTicker { sell: ap, buy: bp, mid_price: mp, t, create_at };
     let depth_info = vec![bp, bq, ap, aq];
     SpecialDepth {
-        name: label,
+        name: (*res_data).label.clone(),
         depth: depth_info,
         ticker: ticker_info,
         t,
@@ -39,7 +35,7 @@ pub fn handle_special_depth(res_data: ResponseData) -> SpecialDepth {
 }
 
 // 格式化深度信息
-pub fn format_depth_items(value: serde_json::Value) -> Vec<MarketOrder> {
+pub fn format_depth_items(value: Value) -> Vec<MarketOrder> {
     let mut depth_items: Vec<MarketOrder> = vec![];
     for value in value.as_array().unwrap() {
         depth_items.push(MarketOrder {
@@ -48,4 +44,4 @@ pub fn format_depth_items(value: serde_json::Value) -> Vec<MarketOrder> {
         })
     }
     return depth_items;
-}
+}

+ 7 - 15
standard/src/gate_handle.rs → standard/src/gate_swap_handle.rs

@@ -4,7 +4,7 @@ use rust_decimal::prelude::FromPrimitive;
 use rust_decimal_macros::dec;
 use serde_json::Value;
 use tokio::time::Instant;
-use tracing::{debug, error};
+use tracing::{error};
 use exchanges::response_base::ResponseData;
 use global::trace_stack::TraceStack;
 use crate::{Account, MarketOrder, Order, Position, PositionModeEnum, SpecialDepth, SpecialOrder, SpecialTicker};
@@ -96,8 +96,6 @@ pub fn handle_order(res_data: ResponseData, ct_val: Decimal) -> SpecialOrder {
 }
 
 pub fn format_order_item(order: serde_json::Value, ct_val: Decimal) -> Order {
-    debug!("format-order-start, gate_handle");
-    debug!(?order);
     let status = order["status"].as_str().unwrap_or("");
     let text = order["text"].as_str().unwrap_or("");
     let size = Decimal::from_f64(order["size"].as_f64().unwrap()).unwrap();
@@ -121,20 +119,14 @@ pub fn format_order_item(order: serde_json::Value, ct_val: Decimal) -> Order {
         trace_stack: TraceStack::new(0, Instant::now()).on_special("120 gate_handle".to_string()),
     };
 
-    debug!(?rst_order);
-    debug!("format-order-end, gate_handle");
     return rst_order;
 }
 // 处理特殊Ticket信息
-pub fn handle_special_ticker(res_data: ResponseData) -> SpecialDepth {
-    format_special_ticker(res_data.data, res_data.label)
-}
-
-pub fn format_special_ticker(data: Value, label: String) -> SpecialDepth {
-    let depth_asks = format_depth_items(data["asks"].clone());
-    let depth_bids = format_depth_items(data["bids"].clone());
-    let t = Decimal::from_str(&data["t"].to_string()).unwrap();
-    let create_at = data["t"].as_i64().unwrap() * 1000;
+pub fn handle_special_ticker(res_data: &ResponseData) -> SpecialDepth {
+    let depth_asks = format_depth_items(res_data.data["asks"].clone());
+    let depth_bids = format_depth_items(res_data.data["bids"].clone());
+    let t = Decimal::from_str(&res_data.data["t"].to_string()).unwrap();
+    let create_at = res_data.data["t"].as_i64().unwrap() * 1000;
 
     let ap = depth_asks[0].price;
     let bp = depth_bids[0].price;
@@ -144,7 +136,7 @@ pub fn format_special_ticker(data: Value, label: String) -> SpecialDepth {
     let ticker_info = SpecialTicker { sell: ap, buy: bp, mid_price: mp, t, create_at };
     let depth_info = vec![bp, bq, ap, aq];
     SpecialDepth {
-        name: label,
+        name: (*res_data).label.clone(),
         depth: depth_info,
         ticker: ticker_info,
         t,

+ 10 - 11
standard/src/handle_info.rs

@@ -7,7 +7,7 @@ use tracing::{error};
 use exchanges::response_base::ResponseData;
 use global::public_params;
 use crate::exchange::ExchangeEnum;
-use crate::{Account, binance_swap_handle, gate_handle, MarketOrder, Position, SpecialDepth, SpecialOrder, SpecialTicker};
+use crate::{Account, binance_swap_handle, gate_swap_handle, MarketOrder, Position, SpecialDepth, SpecialOrder, SpecialTicker};
 
 #[allow(dead_code)]
 pub struct HandleSwapInfo;
@@ -30,7 +30,7 @@ impl HandleSwapInfo {
             //     panic!("暂未提供此交易所方法!handle_account_info:{:?}", exchange);
             // }
             ExchangeEnum::GateSwap => {
-                gate_handle::handle_account_info(res_data, symbol)
+                gate_swap_handle::handle_account_info(res_data, symbol)
             }
             // ExchangeEnum::KucoinSwap => {
             //     kucoin_handle::handle_account_info(res_data, symbol)
@@ -54,7 +54,7 @@ impl HandleSwapInfo {
         }
     }
     // 处理特殊Ticket信息
-    pub fn handle_special_ticker(exchange: ExchangeEnum, res_data: ResponseData) -> SpecialDepth {
+    pub fn handle_special_ticker(exchange: ExchangeEnum, res_data: &ResponseData) -> SpecialDepth {
         match exchange {
             // ExchangeEnum::BinanceSpot => {
             //     binance_spot_handle::handle_special_ticker(res_data)
@@ -63,7 +63,7 @@ impl HandleSwapInfo {
                 binance_swap_handle::handle_special_ticker(res_data)
             }
             ExchangeEnum::GateSwap => {
-                gate_handle::handle_special_ticker(res_data)
+                gate_swap_handle::handle_special_ticker(res_data)
             }
             // ExchangeEnum::KucoinSwap => {
             //     kucoin_handle::handle_special_ticker(res_data)
@@ -90,7 +90,7 @@ impl HandleSwapInfo {
                 panic!("暂未提供此交易所方法!handle_position:{:?}", exchange);
             }
             ExchangeEnum::GateSwap => {
-                gate_handle::handle_position(res_data, ct_val)
+                gate_swap_handle::handle_position(res_data, ct_val)
             }
             // ExchangeEnum::KucoinSwap => {
             //     kucoin_handle::handle_position(res_data, ct_val)
@@ -119,7 +119,7 @@ impl HandleSwapInfo {
                 panic!("暂未提供此交易所方法!handle_order:{:?}", exchange);
             }
             ExchangeEnum::GateSwap => {
-                gate_handle::handle_order(res_data, ct_val)
+                gate_swap_handle::handle_order(res_data, ct_val)
             }
             // ExchangeEnum::KucoinSwap => {
             //     kucoin_handle::handle_order(res_data, ct_val)
@@ -141,13 +141,12 @@ impl HandleSwapInfo {
 
     // 处理深度信息
     pub fn handle_special_depth(exchange: ExchangeEnum, res_data: ResponseData) -> SpecialDepth {
-        let lable = res_data.label.clone();
+        let label = res_data.label.clone();
         // 格式化
         let mut format_depth = format_depth(exchange, res_data);
         // 运算、组装
-        make_special_depth(lable, &mut format_depth.depth_asks, &mut format_depth.depth_bids, format_depth.t, format_depth.create_at)
+        make_special_depth(label, &mut format_depth.depth_asks, &mut format_depth.depth_bids, format_depth.t, format_depth.create_at)
     }
-
 }
 
 
@@ -235,8 +234,8 @@ pub fn format_depth(exchange: ExchangeEnum, res_data: ResponseData) -> DepthPara
             create_at = res_data_json["E"].as_i64().unwrap() * 1000;
         }
         ExchangeEnum::GateSwap => {
-            depth_asks = gate_handle::format_depth_items(res_data_json["asks"].clone());
-            depth_bids = gate_handle::format_depth_items(res_data_json["bids"].clone());
+            depth_asks = gate_swap_handle::format_depth_items(res_data_json["asks"].clone());
+            depth_bids = gate_swap_handle::format_depth_items(res_data_json["bids"].clone());
             // todo! 有id可以取 保证与py一致
             t = Decimal::from_str(&res_data_json["t"].to_string()).unwrap();
             create_at = res_data_json["t"].as_i64().unwrap() * 1000;

+ 1 - 1
standard/src/lib.rs

@@ -22,7 +22,7 @@ pub mod binance_spot_handle;
 // 引入gate模块
 mod gate_swap;
 mod gate_spot;
-pub mod gate_handle;
+pub mod gate_swap_handle;
 mod kucoin_swap;
 pub mod kucoin_handle;
 mod okx_swap;

+ 1 - 1
strategy/src/binance_usdt_swap.rs

@@ -83,7 +83,7 @@ async fn on_data(core_arc_clone: Arc<Mutex<Core>>,
         // 将ticker数据转换为模拟深度
         let label = data.label.clone();
         let ins = data.ins;
-        let special_depth = standard::handle_info::HandleSwapInfo::handle_special_ticker(BinanceSwap, data.clone());
+        let special_depth = standard::handle_info::HandleSwapInfo::handle_special_ticker(BinanceSwap, &data);
 
         TraceStack::show_delay(ins);