Browse Source

v1.0.16: binance强平订单识别

skyffire 9 months ago
parent
commit
f32e1d243e

+ 10 - 0
exchanges/src/binance_swap_ws.rs

@@ -29,6 +29,7 @@ pub enum BinanceSwapSubscribeType {
     PuBookTicker,
     PuAggTrade,
     PuDepth20levels100ms,
+    ForceOrder,
 
     PrAccount,
     PrBalance,
@@ -122,6 +123,7 @@ impl BinanceSwapWs {
                 BinanceSwapSubscribeType::PuBookTicker => false,
                 BinanceSwapSubscribeType::PuAggTrade => false,
                 BinanceSwapSubscribeType::PuDepth20levels100ms => false,
+                BinanceSwapSubscribeType::ForceOrder => false,
 
                 BinanceSwapSubscribeType::PrAccount => { true }
                 BinanceSwapSubscribeType::PrBalance => { true }
@@ -147,6 +149,9 @@ impl BinanceSwapWs {
             BinanceSwapSubscribeType::PuBookTicker => {
                 format!("{}@bookTicker", symbol)
             }
+            BinanceSwapSubscribeType::ForceOrder => {
+                format!("{}@forceOrder", symbol)
+            }
 
             BinanceSwapSubscribeType::PrAccount => {
                 "".to_string()
@@ -170,6 +175,9 @@ impl BinanceSwapWs {
             BinanceSwapSubscribeType::PuBookTicker => {
                 "".to_string()
             }
+            BinanceSwapSubscribeType::ForceOrder => {
+                "".to_string()
+            }
             BinanceSwapSubscribeType::PrAccount => {
                 "@account".to_string()
             }
@@ -439,6 +447,8 @@ impl BinanceSwapWs {
                 res_data.channel = "depth".to_string();
             } else if channel.contains("@bookTicker") {
                 res_data.channel = "bookTicker".to_string();
+            } else if channel.contains("@forceOrder") {
+                res_data.channel = "forceOrder".to_string();
             } else {
                 res_data.code = -1;
                 res_data.channel = "未知的频道".to_string();

+ 25 - 1
standard/src/binance_swap_handle.rs

@@ -7,7 +7,7 @@ use tokio::time::Instant;
 use tracing::error;
 use exchanges::response_base::ResponseData;
 use global::trace_stack::TraceStack;
-use crate::{Account, Depth, Order, OrderBook, Position, PositionModeEnum, Record, SpecialOrder, Trade, utils};
+use crate::{Account, Depth, Order, OrderBook, Position, PositionModeEnum, Record, SpecialOrder, Trade, utils, ForceOrder};
 
 
 // 处理账号信息
@@ -78,6 +78,30 @@ 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 o = &json_value["o"];
+
+    let time = Decimal::from_i64(o["T"].as_i64().unwrap()).unwrap();
+    let symbol = o["s"].as_str().unwrap().replace("USDT", "_USDT");
+    let side = o["S"].as_str().unwrap().to_string();
+    let price = Decimal::from_str(o["p"].as_str().unwrap()).unwrap();
+    let mut amount = Decimal::from_str(o["z"].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 = res_data.data["o"].clone();

+ 13 - 1
standard/src/exchange_struct_handler.rs

@@ -4,7 +4,7 @@ use rust_decimal::prelude::FromPrimitive;
 use tracing::error;
 use exchanges::response_base::ResponseData;
 use crate::exchange::ExchangeEnum;
-use crate::{binance_swap_handle, bitget_swap_handle, bybit_swap_handle, coinex_swap_handle, gate_swap_handle, Ticker};
+use crate::{binance_swap_handle, bitget_swap_handle, bybit_swap_handle, coinex_swap_handle, gate_swap_handle, ForceOrder, Ticker};
 use crate::{Record, Trade, Depth};
 use crate::{Account, OrderBook, Position, SpecialOrder};
 
@@ -313,4 +313,16 @@ impl ExchangeStructHandler {
             }
         }
     }
+    // 处理爆仓信息
+    pub fn force_order_handle(exchange: ExchangeEnum, res_data: &ResponseData) -> ForceOrder {
+        match exchange {
+            ExchangeEnum::BinanceSwap => {
+                binance_swap_handle::handle_force_order(res_data)
+            }
+            _ => {
+                error!("暂未提供此交易所方法!force_order_handle:{:?}", exchange);
+                panic!("暂未提供此交易所方法!force_order_handle:{:?}", exchange);
+            }
+        }
+    }
 }

+ 9 - 0
standard/src/lib.rs

@@ -143,6 +143,15 @@ impl Account {
     }
 }
 
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ForceOrder {
+    pub time: Decimal,
+    pub symbol: String,
+    pub price: Decimal,
+    pub amount: Decimal,
+    pub value: Decimal,
+}
+
 /// 交易结构体(订单流)
 /// - `id(String)`: id
 /// - `time(Decimal)`: 交易更新时间戳(ms)

+ 10 - 2
strategy/src/binance_usdt_swap.rs

@@ -4,7 +4,7 @@ use std::sync::atomic::AtomicBool;
 use rust_decimal::Decimal;
 use tokio::sync::Mutex;
 use tokio_tungstenite::tungstenite::Message;
-use tracing::{error};
+use tracing::{error, info};
 use exchanges::response_base::ResponseData;
 use global::trace_stack::{TraceStack};
 use crate::core::Core;
@@ -26,7 +26,8 @@ pub(crate) async fn reference_binance_swap_run(is_shutdown_arc: Arc<AtomicBool>,
         let mut ws = BinanceSwapWs::new_label(name, is_colo, None, BinanceSwapWsType::Public).await;
         ws.set_subscribe(vec![
             BinanceSwapSubscribeType::PuBookTicker,
-            BinanceSwapSubscribeType::PuAggTrade
+            BinanceSwapSubscribeType::PuAggTrade,
+            BinanceSwapSubscribeType::ForceOrder
         ]);
 
         // 读取数据
@@ -150,6 +151,13 @@ async fn on_data(core_arc: Arc<Mutex<Core>>, multiplier: &Decimal, run_symbol: &
             let mut core = core_arc.lock().await;
             core.update_order(order_infos, trace_stack).await;
         }
+        "forceOrder" => {
+            info!("{}", response.data.to_string());
+
+            let force_order = ExchangeStructHandler::force_order_handle(ExchangeEnum::BinanceSwap, &response);
+
+            info!(?force_order);
+        }
         _ => {
             error!("未知推送类型");
             error!(?response);

+ 1 - 1
strategy/src/strategy.rs

@@ -1095,7 +1095,7 @@ impl Strategy {
 
         self._cancel_open(&mut command, local_orders, predictor);   // 撤单命令处理
         self._post_close(&mut command, local_orders, predictor);    // 平仓单命令处理
-        self._post_open(&mut command, local_orders, predictor);     // 限价单命令处理
+        // self._post_open(&mut command, local_orders, predictor);     // 限价单命令处理
         self._check_local_orders(&mut command, local_orders);       // 固定时间检查超时订单
         self._update_in_cancel(&mut command, local_orders);         // 更新撤单队列,是一个filter
         self._check_request_limit(&mut command);                    // 限制频率,移除不合规则之订单,是一个filter