From 0deaa8930569222d8f4787d97a70a0a89496b44a Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期日, 26 十一月 2023 14:29:52 +0800
Subject: [PATCH] 卖提交之后2s内没有成交的需要撤单

---
 trade/huaxin/huaxin_trade_order_processor.py |   19 +++++++++++++++++--
 trade/huaxin/huaxin_trade_server.py          |   30 ++++++++++++++++++++++++++----
 trade/huaxin/huaxin_trade_api.py             |    3 ++-
 trade/huaxin/huaxin_trade_data_update.py     |    2 +-
 4 files changed, 46 insertions(+), 8 deletions(-)

diff --git a/trade/huaxin/huaxin_trade_api.py b/trade/huaxin/huaxin_trade_api.py
index 3f0ad42..97d5baf 100644
--- a/trade/huaxin/huaxin_trade_api.py
+++ b/trade/huaxin/huaxin_trade_api.py
@@ -95,7 +95,8 @@
                                                           insertDate=insertDate, direction=direction,
                                                           is_shadow_order=is_shadow_order)
                                 try:
-                                    TradeResultProcessor.process_order(order)
+                                    TradeResultProcessor.process_buy_order(order)
+                                    TradeResultProcessor.process_sell_order(order)
                                 finally:
                                     try:
                                         # 鍔犲叆2娆★紝澧炲ぇ鍔犲叆鎴愬姛鐜�
diff --git a/trade/huaxin/huaxin_trade_data_update.py b/trade/huaxin/huaxin_trade_data_update.py
index dab931b..c511bf3 100644
--- a/trade/huaxin/huaxin_trade_data_update.py
+++ b/trade/huaxin/huaxin_trade_data_update.py
@@ -67,7 +67,7 @@
                                                               insertTime=insertTime, acceptTime=acceptTime,
                                                               insertDate=insertDate, direction=direction,
                                                               is_shadow_order=is_shadow_order)
-                                    TradeResultProcessor.process_order(order)
+                                    TradeResultProcessor.process_buy_order(order)
                                     if huaxin_util.is_can_cancel(orderStatus):
                                         codes.append(code)
                                 if codes:
diff --git a/trade/huaxin/huaxin_trade_order_processor.py b/trade/huaxin/huaxin_trade_order_processor.py
index 7fd89eb..d1db965 100644
--- a/trade/huaxin/huaxin_trade_order_processor.py
+++ b/trade/huaxin/huaxin_trade_order_processor.py
@@ -111,9 +111,12 @@
     __TradeOrderIdManager = TradeOrderIdManager()
     __processed_keys = set()
     __thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=50)
+    # 璁㈠崟绱㈠紩鍙峰瓧鍏�
+    order_ref_dict = {}
 
+    # 澶勭悊涔板崟
     @classmethod
-    def process_order(cls, order: HuaxinOrderEntity):
+    def process_buy_order(cls, order: HuaxinOrderEntity):
         # 澶勭悊涓嬪崟鎴愬姛
         def process_order_success(order_: HuaxinOrderEntity, delay_s=0.0):
             if delay_s > 0:
@@ -168,6 +171,17 @@
             async_log_util.exception(hx_logger_trade_debug, e)
 
     @classmethod
+    def process_sell_order(cls, order: HuaxinOrderEntity):
+        # 澶勭悊鍗栧崟
+        if order.direction != str(huaxin_util.TORA_TSTP_D_Sell):
+            return
+        cls.order_ref_dict[order.orderRef] = order
+
+    @classmethod
+    def get_huaxin_order_by_order_ref(cls, order_ref) -> HuaxinOrderEntity:
+        return cls.order_ref_dict.get(order_ref)
+
+    @classmethod
     def order_success(cls, order: HuaxinOrderEntity):
         async_log_util.info(hx_logger_trade_debug, f"澶勭悊鍗庨懌璁㈠崟涓嬪崟鎴愬姛锛歿order.code}, {order.orderRef}, {order.orderSysID}")
         # 鍔犲叆绯荤粺璁㈠崟鍙�
@@ -185,7 +199,8 @@
                         # 4s鍐呮墠浼氭牎楠�
                         volume = total_datas[place_index]["val"]["num"]
                         for i in range(place_index + 1, len(total_datas)):
-                            if total_datas[i]["val"]["num"] == volume and order.acceptTime == total_datas[i]["val"]["time"]:
+                            if total_datas[i]["val"]["num"] == volume and order.acceptTime == total_datas[i]["val"][
+                                "time"]:
                                 huaxin_delegate_postion_manager.set_place_order_position(order.code, i)
                                 async_log_util.info(hx_logger_trade_debug, "{}鏍¢獙鐪熷疄涓嬪崟鎴愬姛锛寋}->{}", order.code, place_index,
                                                     i)
diff --git a/trade/huaxin/huaxin_trade_server.py b/trade/huaxin/huaxin_trade_server.py
index ef960c8..5ab900a 100644
--- a/trade/huaxin/huaxin_trade_server.py
+++ b/trade/huaxin/huaxin_trade_server.py
@@ -57,7 +57,7 @@
 from trade.deal_big_money_manager import DealOrderNoManager
 
 from trade.huaxin import huaxin_trade_api as trade_api, huaxin_trade_api, huaxin_trade_data_update, \
-    huaxin_trade_record_manager
+    huaxin_trade_record_manager, huaxin_trade_order_processor
 from trade.huaxin.huaxin_trade_record_manager import PositionManager
 from trade.l2_trade_factor import L2PlaceOrderParamsManager
 from trade.sell_rule_manager import SellRuleManager, SellRule
@@ -469,6 +469,8 @@
 
 
 class OutsideApiCommandCallback(outside_api_command_manager.ActionCallback):
+    __cancel_sell_thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=8)
+
     @classmethod
     def __send_response(cls, data_bytes):
         sk = SendResponseSkManager.create_send_response_sk(addr=huaxin_client.constant.SERVER_IP,
@@ -494,6 +496,20 @@
             except Exception as e1:
                 logging.exception(e1)
 
+    # 鎾ゅ崠鍗�
+    def __cancel_sell_order(self, code, order_ref):
+        for i in range(0,10):
+            time.sleep(0.2)
+            order_entity = huaxin_trade_order_processor.TradeResultProcessor.get_huaxin_order_by_order_ref(order_ref)
+            if order_entity:
+                if order_entity.orderStatus == huaxin_util.TORA_TSTP_OST_AllTraded:
+                    # 鎴愪氦鐨勫氨涓嶉渶瑕佹挙鍗曚簡
+                    return
+        try:
+            result = huaxin_trade_api.cancel_order(2, code, None, orderRef=order_ref)
+        except Exception as e:
+            logger_trade.exception(e)
+
     # 浜ゆ槗
     def OnTrade(self, client_id, request_id, data):
         try:
@@ -505,9 +521,9 @@
                 price_type = data["price_type"]
                 price = data["price"]
                 sinfo = data["sinfo"]
-                if direction == huaxin_util.TORA_TSTP_D_Sell:
-                    pass
+                if direction == 2:
                     # price_type: 0-浠锋牸绗煎瓙 1-璺屽仠浠�  2-娑ㄥ仠浠� 3-鐜颁环
+                    async_log_util.info(logger_trade, f"API鍗�: 鎺ユ敹鏁版嵁-{data}")
                     # 鑾峰彇鐜颁环
                     if price_type == 0:
                         current_price = TradeServerProcessor.get_l1_current_price(code)
@@ -529,8 +545,14 @@
                         if not current_price:
                             raise Exception("娌℃湁鑾峰彇鍒癓1鐜颁环")
                         price = current_price
+                    async_log_util.info(logger_trade, f"API鍗�:  鍗曚环-{price}")
                     result = huaxin_trade_api.order(direction, code, volume, price, sinfo=sinfo,
-                                                    blocking=True, request_id=request_id)
+                                                    blocking=False, request_id=request_id)
+                    order_ref = result["order_ref"]
+                    # 濡傛灉鏄湪姝e父浜ゆ槗鏃堕棿鎻愪氦鐨�2s涔嬪唴杩樻湭鎴愪氦鐨勯渶瑕佹挙鍗�
+                    if int("092958") <= int(tool.get_now_time_str().replace(":", "")) <= int("150000"):
+                        self.__cancel_sell_thread_pool.submit(lambda: self.__cancel_sell_order(code, order_ref))
+
                     self.send_response({"code": 0, "data": result}, client_id, request_id)
                 else:
                     result = huaxin_trade_api.order(direction, code, volume, price, price_type=price_type, sinfo=sinfo,

--
Gitblit v1.8.0