Administrator
2024-11-01 e1953accc467913165515b3569539da782beee72
L2 Market数据驱动卖
1个文件已修改
102 ■■■■■ 已修改文件
trade/trade_strategy.py 102 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/trade_strategy.py
@@ -415,6 +415,57 @@
        cls.process_for_sell(datas)
    @classmethod
    def excute_sell_rule(cls, code, buy1_volume, buy1_price, source="l1"):
        rules = TradeRuleManager().list_can_excut_rules_cache(types=[TradeRuleManager.TYPE_SELL])
        if not rules:
            return
        excuted_rule_ids = set()
        if buy1_volume is not None:
            for r in rules:
                if r.code != code:
                    continue
                # --------判断是否可以执行--------
                can_excute = False
                if round(float(buy1_price), 2) <= round(float(r.buy1_price), 2):
                    # 价格已经触发
                    if r.buy1_volume:
                        if r.buy1_volume >= buy1_volume:
                            # 量价触发
                            can_excute = True
                            async_log_util.info(logger_trade,
                                                f"触发卖规则({code}-{(buy1_price, buy1_volume, source)}):量触发{buy1_volume}/{r.buy1_volume}")
                    else:
                        can_excute = True
                        async_log_util.info(logger_trade,
                                            f"触发卖规则({code}-{(buy1_price, buy1_volume, source)}):价格触发{buy1_price}/{r.buy1_price}")
                        # 价格触发
                    # 获取价格类型
                    if not can_excute:
                        continue
                    # 请求卖出锁
                    TradeRuleManager().require_sell_lock(r.id_)
                    try:
                        if r.id_ in excuted_rule_ids:
                            continue
                        excuted_rule_ids.add(r.id_)
                        # 获取最新的执行状况
                        r = TradeRuleManager().get_by_id(r.id_)
                        if r.excuted:
                            continue
                        # 提交卖
                        limit_down_price = gpcode_manager.get_limit_down_price(code)
                        limit_up_price = gpcode_manager.get_limit_up_price(code)
                        huaxin_sell_util.start_sell(code, r.sell_volume, r.sell_price_type, limit_up_price,
                                                    limit_down_price,
                                                    buy1_price)
                        TradeRuleManager().excuted(r.id_)
                    except Exception as e:
                        logger_debug.exception(e)
                    finally:
                        TradeRuleManager().release_sell_lock(r.id_)
    @classmethod
    def process_for_sell(cls, datas):
        rules = TradeRuleManager().list_can_excut_rules_cache(types=[TradeRuleManager.TYPE_SELL])
        excuted_rule_ids = set()
@@ -425,50 +476,7 @@
                # 格式 (代码,现价,涨幅,量,更新时间,买1价格,买1量)
                buy1_volume = d[6]
                buy1_price = d[5]
                if buy1_volume:
                    for r in rules:
                        # 生效时间
                        if r.code == code:
                            # --------判断是否可以执行--------
                            can_excute = False
                            if round(float(buy1_price), 2) <= round(float(r.buy1_price), 2):
                                # 价格已经触发
                                if r.buy1_volume:
                                    if r.buy1_volume >= buy1_volume:
                                        # 量价触发
                                        can_excute = True
                                        async_log_util.info(logger_trade,
                                                            f"触发卖规则({code}-{d}):量触发{buy1_volume}/{r.buy1_volume}")
                                else:
                                    can_excute = True
                                    async_log_util.info(logger_trade,
                                                        f"触发卖规则({code}-{d}):价格触发{buy1_price}/{r.buy1_price}")
                                    # 价格触发
                                # 获取价格类型
                                if not can_excute:
                                    continue
                                # 请求卖出锁
                                TradeRuleManager().require_sell_lock(r.id_)
                                try:
                                    if r.id_ in excuted_rule_ids:
                                        continue
                                    excuted_rule_ids.add(r.id_)
                                    # 获取最新的执行状况
                                    r = TradeRuleManager().get_by_id(r.id_)
                                    if r.excuted:
                                        continue
                                    # 提交卖
                                    limit_down_price = gpcode_manager.get_limit_down_price(code)
                                    limit_up_price = gpcode_manager.get_limit_up_price(code)
                                    huaxin_sell_util.start_sell(code, r.sell_volume, r.sell_price_type, limit_up_price,
                                                                limit_down_price,
                                                                buy1_price)
                                    TradeRuleManager().excuted(r.id_)
                                except Exception as e:
                                    logger_debug.exception(e)
                                finally:
                                    TradeRuleManager().release_sell_lock(r.id_)
                cls.excute_sell_rule(code, buy1_volume, buy1_price)
# 读取L1的数据
@@ -545,7 +553,11 @@
                logger_debug.error(f"集合竞价卖撤处理出错:{code} - {str(e)}")
    def OnMarketData(self, code, datas):
        logger_debug.info(f"收到L2Market数据:{datas}")
        # logger_debug.info(f"收到L2Market数据:{datas}")
        for d in datas:
            code = d["securityID"]
            buy1 = d["buy"][0]
            L1DataProcessor.excute_sell_rule(code, buy1[1], buy1[0], "l2")
# 做一些初始化的操作