From 275a72f4237a79cd0aa270eb2dc81ebd5172830f Mon Sep 17 00:00:00 2001
From: admin <admin@example.com>
Date: 星期三, 21 五月 2025 14:42:44 +0800
Subject: [PATCH] 新增买入代码管理

---
 strategy/order_methods.py |   99 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 97 insertions(+), 2 deletions(-)

diff --git a/strategy/order_methods.py b/strategy/order_methods.py
index 550774f..3ed4808 100644
--- a/strategy/order_methods.py
+++ b/strategy/order_methods.py
@@ -5,9 +5,12 @@
 import multiprocessing
 import threading
 
+from db import redis_manager_delegate as redis_manager
+from db.redis_manager_delegate import RedisUtils
+from log_module import async_log_util
 from strategy import data_cache, account_management
 import data_server
-from log_module.log import logger_debug, logger_common
+from log_module.log import logger_debug, logger_common, logger_trade
 from strategy.trade_setting import TradeSetting
 
 from trade import huaxin_trade_api, huaxin_trade_data_update, middle_api_protocol
@@ -20,6 +23,10 @@
 
 # 涓嬪崟涔板叆鍑芥暟锛堟寜閲戦锛屼互闄愪环涔帮級銆愭寜閲戦涔� 鍩虹鐗堛��
 def buy_order_by_value(symbol, buy_order_value, sec_name, current_price):
+    if symbol[-6:] in TodayBuyCodeManager().get_buy_codes():
+        logger.info(f"{symbol}宸茬粡涓嬭繃鍗�")
+        return
+
     # 鑷姩涔� 寮�鍏崇洃鍚柟娉�
     if not TradeSetting().get_auto_buy():
         # 鏆傚仠鑷姩涔�
@@ -40,11 +47,13 @@
     logger.info(f"current_price===={current_price}    order_price===={order_price}")
     logger.info(f"buy_order===={buy_order}")
     orderStatusMsg = buy_order['data'].get('orderStatusMsg', None)
+    orderRef = buy_order['data'].get('orderRef', None)
     statusMsg = buy_order['data'].get('statusMsg', None)
     logger.info(f"orderStatusMsg==={orderStatusMsg}")
     logger.info(f"statusMsg==={statusMsg}")
     # orderStatusMsg 涓嶅湪buy_order鐨勪笅鍗曞洖璋冧腑锛岄偅涔堟墠璁や负涓嬪崟鎴愬姛
     if statusMsg is not None and statusMsg == '':
+        TodayBuyCodeManager().place_order(symbol[-6:], orderRef)
         logger.info(f"涔扮エ 涓嬪崟鎴愬姛锛氥�恵sec_name}銆�")
         # 姣忎竴娆℃垚鍔熶笅鍗曞悗瑕佹洿鏂颁竴涓� 缂撳瓨 鐨勬寔浠撴暟鎹�
         account_management.position_management()
@@ -185,8 +194,11 @@
             :return:
             """
             print(f"鏀跺埌璁㈠崟鍥炶皟锛歿order_info}")
-            logger_debug.info(f"鏀跺埌璁㈠崟鍥炶皟锛歿order_info}")
+            async_log_util.info(logger_debug, f"鏀跺埌璁㈠崟鍥炶皟锛歿order_info}")
             if huaxin_util.is_deal(order_info['orderStatus']):
+                if order_info["direction"] == '0':
+                    # 涔板叆鎴愪氦
+                    TodayBuyCodeManager().add_deal_code(order_info["securityID"], order_info.get("orderRef"))
                 # 鎴愪氦锛岄渶瑕佹洿鏂版寔浠�/濮旀墭/鎴愪氦
                 huaxin_trade_data_update.add_position_list()
                 huaxin_trade_data_update.add_delegate_list("鎴愪氦")
@@ -201,3 +213,86 @@
     huaxin_trade_api.run_trade(queue_strategy_r_trade_w, MyTradeCallback(), queue_strategy_w_trade_r,
                                queue_strategy_w_trade_for_query_r)
     threading.Thread(target=data_server.run, daemon=True).start()
+
+
+@tool.singleton
+class TodayBuyCodeManager:
+    """
+    浠婃棩涔板叆浠g爜绠$悊绫�
+    """
+    __db = 0
+    redisManager = redis_manager.RedisManager(0)
+
+    def __init__(self):
+        # 鎸傚崟涓殑浠g爜
+        self.delegating_codes_info = {}
+        self.deal_codes = set()
+        self.__load_data()
+
+    @classmethod
+    def __get_redis(cls):
+        return cls.redisManager.getRedis()
+
+    def __load_data(self):
+        """
+        鍔犺浇鏁版嵁
+        :return:
+        """
+        codes = RedisUtils.smembers(self.__get_redis(), "buy_deal_codes")
+        if codes:
+            self.deal_codes = set(codes)
+
+    def add_deal_code(self, code, order_ref=None):
+        """
+        娣诲姞涔板叆鎴愪氦鐨勪唬鐮�
+        :param order_ref:
+        :param code:
+        :return:
+        """
+        if code in self.deal_codes:
+            return
+        async_log_util.info(logger_trade, f"涔板叆鎴愪氦锛歿code}")
+        self.deal_codes.add(code)
+        if order_ref and order_ref in self.delegating_codes_info:
+            del self.delegating_codes_info[order_ref]
+        RedisUtils.sadd_async(self.__db, "buy_deal_codes", code)
+        RedisUtils.expire_async(self.__db, "buy_deal_codes", tool.get_expire())
+
+    def place_order(self, code, order_ref):
+        """
+        涓嬪崟
+        :param code: 浠g爜
+        :param order_ref: 绱㈠紩
+        :return:
+        """
+        async_log_util.info(logger_trade, f"涓嬪崟锛歿code}-{order_ref}")
+        self.delegating_codes_info[order_ref] = code
+
+    def get_buy_codes(self):
+        """
+        鑾峰彇涔板叆鐨勪唬鐮侊細鎴愪氦浠g爜+濮旀墭
+        :return:
+        """
+        codes = set()
+        if self.deal_codes:
+            codes |= self.deal_codes
+        if self.delegating_codes_info:
+            codes |= set(self.delegating_codes_info.values())
+        return codes
+
+    def buy_fail(self, order_ref):
+        """
+        涔板叆澶辫触
+        :param order_ref:
+        :return:
+        """
+        async_log_util.info(logger_trade, f"涓嬪崟澶辫触锛歿order_ref}")
+        if order_ref in self.delegating_codes_info:
+            del self.delegating_codes_info[order_ref]
+
+
+if __name__ == '__main__':
+    # 娴嬭瘯浠g爜
+    TodayBuyCodeManager().place_order("000001", 1)
+    TodayBuyCodeManager().buy_fail(1)
+    print(TodayBuyCodeManager().get_buy_codes())

--
Gitblit v1.8.0