Răsfoiți Sursa

bybit强平数据加入

skyffire 9 luni în urmă
părinte
comite
678ee9c996

+ 7 - 0
exchanges/src/bybit_swap_ws.rs

@@ -27,6 +27,7 @@ pub enum BybitSwapSubscribeType {
     PuOrderBook50,
     PuTrade,
     PuTickers,
+    PuLiquidation,
     PuKline(String),
 
     PrPosition,
@@ -120,6 +121,7 @@ impl BybitSwapWs {
                 BybitSwapSubscribeType::PuOrderBook50 => false,
                 BybitSwapSubscribeType::PuTrade => false,
                 BybitSwapSubscribeType::PuTickers => false,
+                BybitSwapSubscribeType::PuLiquidation => false,
                 BybitSwapSubscribeType::PuKline(_) => false,
 
                 BybitSwapSubscribeType::PrPosition => true,
@@ -150,6 +152,9 @@ impl BybitSwapWs {
             BybitSwapSubscribeType::PuTickers => {
                 format!("tickers.{}", symbol)
             }
+            BybitSwapSubscribeType::PuLiquidation => {
+                format!("liquidation.{}", symbol)
+            }
             BybitSwapSubscribeType::PuKline(t) => {
                 format!("kline.{}.{}", t, symbol)
             }
@@ -330,6 +335,8 @@ impl BybitSwapWs {
                 res_data.data["ts"] = json_value["ts"].clone();
             } else if channel.contains("kline") {
                 res_data.channel = "kline".to_string();
+            } else if channel.contains("liquidation") {
+                res_data.channel = "liquidation".to_string();
             } else if channel.contains("position") {
                 res_data.channel = "position".to_string();
             } else if channel.contains("execution") {

+ 23 - 1
standard/src/bybit_swap_handle.rs

@@ -10,7 +10,7 @@ use tokio::time::Instant;
 use tracing::{error};
 use exchanges::response_base::ResponseData;
 use global::trace_stack::TraceStack;
-use crate::{Account, OrderBook, Order, Position, PositionModeEnum, SpecialOrder, Depth, Trade, Ticker, Record};
+use crate::{Account, OrderBook, Order, Position, PositionModeEnum, SpecialOrder, Depth, Trade, Ticker, Record, ForceOrder};
 
 // 处理账号信息
 pub fn handle_account_info(res_data: &ResponseData, symbol: &String) -> Account {
@@ -102,6 +102,28 @@ pub fn format_position_item(position: &Value, ct_val: &Decimal) -> Position {
     }
 }
 
+pub fn handle_force_order(res_data: &ResponseData) -> ForceOrder {
+    let json_value = &res_data.data;
+
+    let time = Decimal::from_i64(json_value["updatedTime"].as_i64().unwrap()).unwrap();
+    let symbol = json_value["symbol"].as_str().unwrap().replace("USDT", "_USDT");
+    let side = json_value["side"].as_str().unwrap().to_string();
+    let price = Decimal::from_str(json_value["price"].as_str().unwrap()).unwrap();
+    let mut amount = Decimal::from_str(json_value["size"].as_str().unwrap()).unwrap();
+    if side == "Sell" {
+        amount = -amount
+    }
+    let value = price * amount;
+
+    ForceOrder {
+        time,
+        symbol,
+        price,
+        amount,
+        value
+    }
+}
+
 // 处理order信息
 pub fn handle_order(res_data: &ResponseData, ct_val: &Decimal) -> SpecialOrder {
     let res_data_json: Vec<Value> = res_data.data.as_array().unwrap().clone();

+ 3 - 0
standard/src/exchange_struct_handler.rs

@@ -319,6 +319,9 @@ impl ExchangeStructHandler {
             ExchangeEnum::BinanceSwap => {
                 binance_swap_handle::handle_force_order(res_data)
             }
+            ExchangeEnum::BybitSwap => {
+                bybit_swap_handle::handle_force_order(res_data)
+            }
             _ => {
                 error!("暂未提供此交易所方法!force_order_handle:{:?}", exchange);
                 panic!("暂未提供此交易所方法!force_order_handle:{:?}", exchange);

+ 9 - 1
strategy/src/bybit_usdt_swap.rs

@@ -29,7 +29,8 @@ pub(crate) async fn reference_bybit_swap_run(is_shutdown_arc: Arc<AtomicBool>,
         ws.set_subscribe(vec![
             BybitSwapSubscribeType::PuTrade,
             BybitSwapSubscribeType::PuOrderBook1,
-            BybitSwapSubscribeType::PuKline("1".to_string()),
+            BybitSwapSubscribeType::PuLiquidation,
+            // BybitSwapSubscribeType::PuKline("1".to_string())
             // BybitSwapSubscribeType::PuTickers
         ]);
 
@@ -200,6 +201,13 @@ async fn on_public_data(core_arc: Arc<Mutex<Core>>, mul: &Decimal, response: &Re
                 on_record(core_arc_clone, record).await
             }
         },
+        // 强平数据
+        "liquidation" => {
+            let force_order = ExchangeStructHandler::force_order_handle(BybitSwap, &response);
+
+            let mut core = core_arc.lock().await;
+            core.update_force_order(force_order).await;
+        }
         _ => {
             error!("未知推送类型");
             error!(?response);