Browse Source

尝试解决撤单失败的后续处理问题

skyfffire 2 years ago
parent
commit
cecca905b9
4 changed files with 29 additions and 27 deletions
  1. 9 7
      standard/src/gate_swap.rs
  2. 9 7
      standard/src/kucoin_swap.rs
  3. 9 11
      strategy/src/quant.rs
  4. 2 2
      strategy/src/strategy.rs

+ 9 - 7
standard/src/gate_swap.rs

@@ -362,13 +362,15 @@ impl Platform for GateSwap {
                         result_sd.send(result).await.unwrap();
                     }
                     Err(error) => {
-                        if error.to_string() == "ORDER_NOT_FOUND" {
-                            let mut err_order = Order::new();
-                            err_order.id = order_id;
-                            err_order.custom_id = custom_id;
-                            err_order.status = "REMOVE".to_string();
-
-                            result_sd.send(err_order).await.unwrap();
+                        // 取消失败去查订单。
+                        let query_rst = self_clone.get_order_detail(&order_id, &custom_id).await;
+                        match query_rst {
+                            Ok(order) => {
+                                result_sd.send(order).await.unwrap();
+                            }
+                            Err(_) => {
+                                panic!("撤单失败,而且查单也失败了,gate_io_swap,oid={}, cid={}。", order_id.clone(), custom_id.clone());
+                            }
                         }
                         err_sd.send(error).await.unwrap();
                     }

+ 9 - 7
standard/src/kucoin_swap.rs

@@ -357,13 +357,15 @@ impl Platform for KucoinSwap {
                             result_sd.send(result).await.unwrap();
                         }
                         Err(error) => {
-                            if error.to_string() == "The order cannot be canceled." {
-                                let mut err_order = Order::new();
-                                err_order.id = order_id;
-                                err_order.custom_id = custom_id;
-                                err_order.status = "REMOVE".to_string();
-
-                                result_sd.send(err_order).await.unwrap();
+                            // 取消失败去查订单。
+                            let query_rst = self_clone.get_order_detail(&order_id, &custom_id).await;
+                            match query_rst {
+                                Ok(order) => {
+                                    result_sd.send(order).await.unwrap();
+                                }
+                                Err(_) => {
+                                    panic!("撤单失败,而且查单也失败了,kucoin_swap,oid={}, cid={}。", order_id.clone(), custom_id.clone());
+                                }
                             }
                             err_sd.send(error).await.unwrap();
                         }

+ 9 - 11
strategy/src/quant.rs

@@ -311,6 +311,9 @@ impl Quant {
     }
 
     pub fn update_local_order(&mut self, data: OrderInfo) {
+        if data.filled != Decimal::ZERO {
+            info!("接收到订单信息①:{:?}", data);
+        }
         /*
          更新订单
             首先直接复写本地订单
@@ -368,9 +371,10 @@ impl Quant {
                     // 只有开仓成交才触发onPosition
                     // 如果漏推送 rest补充的订单查询信息过来 可能会导致 kd kk 推送出现计算分母为0的情况
                     if filled > Decimal::ZERO {
-                        let filled_order = data.clone();
-                        info!("移除本地订单:{:?}", filled_order);
+                        let mut filled_order = data.clone();
+                        filled_order.side = side.clone();
 
+                        info!("移除本地订单:{:?}, local_by_orders: {:?}", filled_order, self.local_position_by_orders);
                         if self.exchange.contains("spot") { // 如果是现货交易 还需要修改equity
                             // 现货必须考虑fee 买入fee单位为币 卖出fee单位为u
                             let fee = data.fee;
@@ -472,7 +476,7 @@ impl Quant {
                                     self.local_position_by_orders.short_avg = Decimal::ZERO;
                                 }
                             } else {
-                                info!("错误的仓位方向{}", side);
+                                error!("错误的仓位方向{}", side);
                             }
                             // 统计合约交易手续费 正fee为扣手续费 负fee为返佣
                             if data.fee > Decimal::ZERO {
@@ -1021,8 +1025,7 @@ impl Quant {
                             }
                             match self.platform_rest.take_order("t-123", side, price, position.amount.abs()).await {
                                 Ok(order)=>{
-                                    info!("清仓下单!");
-                                    info!(?order);
+                                    info!("清仓下单,{:?}", order);
                                     // 执行完当前币对  结束循环
                                     break;
                                 },
@@ -1069,6 +1072,7 @@ impl Quant {
         info!("停机退出  停机原因: {}", self.exit_msg);
         // 发送交易状态 await self._post_params()
         // TODO: 向中控发送信号
+        self.running.store(false, Ordering::Relaxed);
         info!("退出进程!");
     }
 
@@ -1293,15 +1297,9 @@ pub fn on_timer(quant_arc: Arc<Mutex<Quant>>) -> JoinHandle<()> {
 
 #[cfg(test)]
 mod tests {
-    use std::collections::BTreeMap;
-    use std::io::Error;
     use rust_decimal::Decimal;
     use rust_decimal_macros::dec;
-    use tokio::sync::mpsc::{channel, Receiver, Sender};
     use tracing::info;
-    use standard::Order;
-
-    use crate::quant::Quant;
 
     #[tokio::test]
     async fn test_new_exchange() {

+ 2 - 2
strategy/src/strategy.rs

@@ -212,7 +212,7 @@ impl Strategy {
         strategy._start_time = now.timestamp_millis();
         // 检查订单的时间戳
         strategy._check_local_orders_time = now.timestamp_millis();
-        strategy._check_local_orders_interval = 10 * 1000;
+        strategy._check_local_orders_interval = 5 * 1000;
         // 下单的相关限制处理
         strategy.place_order_limit = params.place_order_limit;
         strategy.request_limit_check_time = now.timestamp_millis();
@@ -1154,7 +1154,7 @@ mod tests {
     use rust_decimal_macros::dec;
     use tracing::{debug};
     use crate::model::{OrderInfo, TraderMsg};
-    use crate::params::Params;
+    use global::params::Params;
     use crate::strategy::Strategy;
 
     #[test]