| | |
| | | ########华鑫配置######## |
| | | if not is_windows() or True: |
| | | # 下单1手 |
| | | BUY_MONEY_PER_CODE = 500 if constant.IS_A else 20000 |
| | | BUY_MONEY_PER_CODE = 500 if constant.IS_A else 5000 |
| | | L2_SOURCE_TYPE = L2_SOURCE_TYPE_HUAXIN |
| | | JUEJIN_LOCAL_API = False |
| | | TRADE_WAY = TRADE_WAY_HUAXIN |
| | |
| | | cls.__order_no_set_dict[code].discard(order_no) |
| | | cls.__delegating_sell_num_dict[code] -= volume |
| | | async_log_util.l2_data_log.info(hx_logger_l2_sell_delegate, f"{code}-卖撤-{order_no, price, volume}") |
| | | |
| | | else: |
| | | if d[3] != '1': |
| | | # 卖 |
| | |
| | | async_log_util.l2_data_log.info(hx_logger_l2_sell_delegate, f"{code}-卖-{order_no, price, volume}") |
| | | except: |
| | | pass |
| | | |
| | | @classmethod |
| | | def get_delegating_sell_num(cls, code): |
| | | """ |
| | | 获取处于委托状态的卖单总手数 |
| | | @param code: |
| | | @return: |
| | | """ |
| | | return cls.__delegating_sell_num_dict.get(code) |
| | | |
| | | @classmethod |
| | | def set_deal_datas(cls, code, datas): |
| | |
| | | try: |
| | | limit_up_price = gpcode_manager.get_limit_up_price_as_num(code) |
| | | has_limit_up_active_buy = False |
| | | order_no_set = cls.__order_no_set_dict.get(code) |
| | | if order_no_set is None: |
| | | order_no_set = set() |
| | | for d in datas: |
| | | if d[1] != limit_up_price: |
| | | continue |
| | | # 是否有涨停主动买成交 |
| | | if d[6] < d[7]: |
| | | continue |
| | | if d[1] != limit_up_price: |
| | | continue |
| | | has_limit_up_active_buy = True |
| | | break |
| | | |
| | | if code in cls.__delegating_sell_num_dict: |
| | | for d in datas: |
| | | # 减去 |
| | | if d[7] in order_no_set: |
| | | cls.__delegating_sell_num_dict[code] -= d[2] |
| | | |
| | | if has_limit_up_active_buy: |
| | | # 打印日志 |
| | | async_log_util.l2_data_log.info(hx_logger_l2_sell_deal, f"有涨停主动卖:{code}-{datas[-1][3]}-{ cls.__delegating_sell_num_dict.get(code)}") |
| | | except: |
| | | pass |
| | | |
| | | @classmethod |
| | | def clear_data(cls, code): |
| | | """ |
| | | 清除数据:当出现主动卖的时候就可以清除数据 |
| | | @param code: |
| | | @return: |
| | | """ |
| | | if code in cls.__order_no_set_dict: |
| | | cls.__order_no_set_dict.pop(code) |
| | | |
| | | if code in cls.__order_no_data_map_dict: |
| | | cls.__order_no_data_map_dict.pop(code) |
| | | |
| | | if code in cls.__delegating_sell_num_dict: |
| | | cls.__delegating_sell_num_dict.pop(code) |
| | | |
| | | |
| | |
| | | total_sell_info[1] = big_sell_orders |
| | | # ----------------统计涨停主动买----------------- |
| | | try: |
| | | limit_up_active_buy_datas = [] |
| | | for d in datas: |
| | | if is_active_sell(d[7], d[6]): |
| | | # 被动买 |
| | |
| | | # 是否是涨停 |
| | | if d[1] == limit_up_price: |
| | | # 有涨停主动买 |
| | | L2TradeSingleDataManager.set_limit_up_active_buy(code, datas[-1][3], datas[-1][6]) |
| | | break |
| | | limit_up_active_buy_datas.append(d) |
| | | L2TradeSingleDataManager.set_limit_up_active_buy(code, limit_up_active_buy_datas) |
| | | except: |
| | | pass |
| | | |
| | |
| | | current_price_process_manager.set_latest_not_limit_up_time(code, |
| | | l2_huaxin_util.convert_time( |
| | | d[3], with_ms=True)) |
| | | else: |
| | | # 主动卖(板上) |
| | | if d[1] == limit_up_price: |
| | | L2LimitUpSellDataManager.clear_data(code) |
| | | break |
| | | except: |
| | | pass |
| | | |
| | |
| | | @return: |
| | | """ |
| | | |
| | | def OnLimitUpActiveBuy(self, code, huaxin_timestamp, buy_no): |
| | | def OnLimitUpActiveBuy(self, code, transaction_data): |
| | | """ |
| | | 涨停主动买触发 |
| | | @param code: |
| | | @param huaxin_timestamp: |
| | | @param buy_no: |
| | | @param transaction_data: 成交数据 |
| | | @return: |
| | | """ |
| | | |
| | |
| | | # cls.__callback.OnTradeSingle(code, 0, cls.TYPE_ACTIVE, cls.__latest_sell_active_deal_data_dict[code]) |
| | | |
| | | @classmethod |
| | | def set_limit_up_active_buy(cls, code, huaxin_timestamp, buy_no): |
| | | cls.__callback.OnLimitUpActiveBuy(code, huaxin_timestamp, buy_no) |
| | | def set_limit_up_active_buy(cls, code, transaction_datas): |
| | | if transaction_datas: |
| | | cls.__callback.OnLimitUpActiveBuy(code, transaction_datas) |
| | | |
| | | @classmethod |
| | | def get_valid_trade_single(cls, code, latest_time_with_ms): |
| | |
| | | TargetCodePlateKeyManager |
| | | from third_data.history_k_data_util import JueJinApi |
| | | from trade import trade_manager, l2_trade_util, \ |
| | | trade_data_manager, trade_constant |
| | | trade_data_manager, trade_constant, radical_buy_strategy |
| | | |
| | | from trade.huaxin import huaxin_trade_api as trade_api, huaxin_trade_api, huaxin_trade_data_update, \ |
| | | huaxin_trade_record_manager, huaxin_sell_util |
| | |
| | | except Exception as e: |
| | | logger_debug.exception(e) |
| | | |
| | | def OnLimitUpActiveBuy(self, code, huaxin_timestamp, buy_no): |
| | | def OnLimitUpActiveBuy(self, code, transaction_datas): |
| | | try: |
| | | # 判断最近60个交易日有无涨停 |
| | | # 判断昨日是否涨停过 |
| | | async_log_util.info(logger_l2_radical_buy, f"涨停主动买:{code}-{huaxin_timestamp}") |
| | | async_log_util.info(logger_l2_radical_buy, f"涨停主动买:{code}-{transaction_datas[-1]}") |
| | | deal_codes = RadicalBuyDealCodesManager().get_deal_codes() |
| | | if len(deal_codes) >= 2: |
| | | async_log_util.info(logger_l2_radical_buy, f"成交代码个数大于2个:{code}-{deal_codes}") |
| | |
| | | except Exception as e: |
| | | logger_debug.exception(e) |
| | | |
| | | # 有可以扫的板块 |
| | | # 判断最近的主动买占了总买额的多少 |
| | | refer_sell_data = L2MarketSellManager().get_refer_sell_data(code, l2_huaxin_util.convert_time( |
| | | huaxin_timestamp)) |
| | | async_log_util.info(logger_l2_radical_buy, f"参考总卖额:{code}-{refer_sell_data}") |
| | | if refer_sell_data: |
| | | # 如果总卖额大于500w,成交到一半就直接扫 |
| | | if refer_sell_data[1] >= 500 * 1e4: |
| | | THRESH_HOLD_VOLUME = refer_sell_data[2] |
| | | if refer_sell_data[4] and len(refer_sell_data[4]) < 10: |
| | | # 小于10挡,最后一档才是涨停价 |
| | | if refer_sell_data[4][-1][1] > 0: |
| | | THRESH_HOLD_VOLUME = refer_sell_data[4][-1][1] |
| | | |
| | | refer_sell_time = refer_sell_data[0] |
| | | # 获取最近的主动买成交量 |
| | | limit_up_deal_infos = HuaXinSellOrderStatisticManager.get_latest_6s_active_buy_deal_volumes( |
| | | code) |
| | | async_log_util.info(logger_l2_radical_buy, f"最近涨停主动买成交:{code}-{limit_up_deal_infos}") |
| | | # 总卖额要大于100w |
| | | deal_volume = 0 |
| | | for i in range(0, len(limit_up_deal_infos)): |
| | | # >=统计到的总卖 |
| | | if int(refer_sell_time.replace(":", "")) > int( |
| | | limit_up_deal_infos[i][0].replace(":", "")): |
| | | break |
| | | deal_volume += limit_up_deal_infos[i][1] |
| | | async_log_util.info(logger_l2_radical_buy, f"成交量:{deal_volume}/{THRESH_HOLD_VOLUME}") |
| | | deal_rate = round(deal_volume / THRESH_HOLD_VOLUME, 2) |
| | | if deal_rate > 0.5: |
| | | # 判断参考时间之后是否有大单成交 |
| | | # big_deal_money_list = BigOrderDealManager().get_total_buy_money_list(code) |
| | | # 成交比例大于50% |
| | | total_datas = l2_data_util.local_today_datas.get(code) |
| | | buy_single_index, buy_exec_index = total_datas[-1]["index"], total_datas[-1]["index"] |
| | | buy_volume_rate = L2TradeDataProcessor.volume_rate_info[code][0] |
| | | sell_info = (refer_sell_data[0], refer_sell_data[0]) |
| | | threshold_money = 0 |
| | | order_begin_pos_info = OrderBeginPosInfo(buy_single_index=buy_single_index, |
| | | buy_exec_index=buy_exec_index, |
| | | buy_compute_index=buy_exec_index, |
| | | num=deal_volume // 100, count=1, |
| | | max_num_set=set(), |
| | | buy_volume_rate=buy_volume_rate, |
| | | mode=OrderBeginPosInfo.MODE_RADICAL, |
| | | mode_desc="激进买入", |
| | | sell_info=sell_info, |
| | | threshold_money=threshold_money) |
| | | L2TradeDataProcessor.save_order_begin_data(code, order_begin_pos_info) |
| | | L2TradeDataProcessor.start_buy(code, total_datas[-1], total_datas[-1]["index"], True) |
| | | # -----根据成交比例判断是否可买------ |
| | | result_by_volume = radical_buy_strategy.limit_up_active_buy_deal(code, transaction_datas) |
| | | if result_by_volume[0] != radical_buy_strategy.BUY_MODE_NONE: |
| | | if result_by_volume[0] == radical_buy_strategy.BUY_MODE_DIRECT: |
| | | refer_sell_data = L2MarketSellManager().get_refer_sell_data(code, |
| | | l2_huaxin_util.convert_time( |
| | | transaction_datas[-1][3])) |
| | | total_datas = l2_data_util.local_today_datas.get(code) |
| | | buy_single_index, buy_exec_index = total_datas[-1]["index"], total_datas[-1]["index"] |
| | | buy_volume_rate = L2TradeDataProcessor.volume_rate_info[code][0] |
| | | sell_info = (0, 0) |
| | | if refer_sell_data: |
| | | sell_info = (refer_sell_data[0],refer_sell_data[1]) |
| | | threshold_money = 0 |
| | | order_begin_pos_info = OrderBeginPosInfo(buy_single_index=buy_single_index, |
| | | buy_exec_index=buy_exec_index, |
| | | buy_compute_index=buy_exec_index, |
| | | num=1, count=1, |
| | | max_num_set=set(), |
| | | buy_volume_rate=buy_volume_rate, |
| | | mode=OrderBeginPosInfo.MODE_RADICAL, |
| | | mode_desc="激进买入", |
| | | sell_info=sell_info, |
| | | threshold_money=threshold_money) |
| | | L2TradeDataProcessor.save_order_begin_data(code, order_begin_pos_info) |
| | | L2TradeDataProcessor.start_buy(code, total_datas[-1], total_datas[-1]["index"], True) |
| | | else: |
| | | # L2委托有大单就扫 |
| | | # 记录买入信号, 3s内有效 |
| | | RadicalBuyDealCodesManager.buy_by_l2_delegate_expire_time_dict[code] = ( |
| | | time.time() + 3, buy_no) |
| | | time.time() + 3, transaction_datas[-1][6]) |
| | | else: |
| | | async_log_util.info(logger_l2_radical_buy, f"不能下单:{code}-{result_by_volume}") |
| | | else: |
| | | volume_rate = code_volumn_manager.get_volume_rate(code) |
| | | async_log_util.info(logger_l2_radical_buy, f"没有可扫入的板块:{code},量比:{volume_rate}") |
New file |
| | |
| | | """ |
| | | 激进买策略管理 |
| | | """ |
| | | # 上一个50W的起始时间:{code:"09:30:00.120"} |
| | | from l2.huaxin import l2_huaxin_util |
| | | from l2.l2_limitup_sell_data_manager import L2LimitUpSellDataManager |
| | | from l2.l2_sell_manager import L2MarketSellManager |
| | | from l2.l2_transaction_data_manager import HuaXinSellOrderStatisticManager |
| | | from log_module import async_log_util |
| | | from log_module.log import logger_l2_radical_buy, hx_logger_l2_transaction |
| | | from utils import tool |
| | | |
| | | # 不买入 |
| | | BUY_MODE_NONE = 0 |
| | | # 直接买入 |
| | | BUY_MODE_DIRECT = 1 |
| | | # 根据L2买入 |
| | | BUY_MODE_BY_L2 = 2 |
| | | |
| | | # 开始计时时间 |
| | | __start_time_dict = {} |
| | | # 转换时间缓存 |
| | | __huaxin_timestamp_convert_temp = {} |
| | | # 主动成交的累计金额 |
| | | __deal_active_buy_total_money = {} |
| | | # 最近成交的买单信息{code:[订单号,量,金额]} |
| | | __latest_deal_active_buy_order = {} |
| | | |
| | | |
| | | def __get_deal_rate_by(code, huaxin_timestamp): |
| | | """ |
| | | |
| | | @param code: 代码 |
| | | @param huaxin_timestamp: 华鑫成交时间戳 |
| | | @return:买入方式, 消息 |
| | | """ |
| | | """ |
| | | 获取备用成交比例 |
| | | @param code: |
| | | @return: |
| | | """ |
| | | refer_sell_data = L2MarketSellManager().get_refer_sell_data(code, l2_huaxin_util.convert_time( |
| | | huaxin_timestamp)) |
| | | async_log_util.info(logger_l2_radical_buy, f"参考总卖额(备用):{code}-{refer_sell_data}") |
| | | if refer_sell_data: |
| | | # 如果总卖额大于500w,成交到一半就直接扫 |
| | | if refer_sell_data[1] >= 500 * 1e4: |
| | | THRESH_HOLD_VOLUME = refer_sell_data[2] |
| | | if refer_sell_data[4] and len(refer_sell_data[4]) < 10: |
| | | # 小于10挡,最后一档才是涨停价 |
| | | if refer_sell_data[4][-1][1] > 0: |
| | | THRESH_HOLD_VOLUME = refer_sell_data[4][-1][1] |
| | | |
| | | refer_sell_time = refer_sell_data[0] |
| | | # 获取最近的主动买成交量 |
| | | limit_up_deal_infos = HuaXinSellOrderStatisticManager.get_latest_6s_active_buy_deal_volumes( |
| | | code) |
| | | async_log_util.info(logger_l2_radical_buy, f"最近涨停主动买成交(备用):{code}-{limit_up_deal_infos}") |
| | | deal_volume = 0 |
| | | for i in range(0, len(limit_up_deal_infos)): |
| | | # >=统计到的总卖 |
| | | if int(refer_sell_time.replace(":", "")) > int( |
| | | limit_up_deal_infos[i][0].replace(":", "")): |
| | | break |
| | | deal_volume += limit_up_deal_infos[i][1] |
| | | async_log_util.info(logger_l2_radical_buy, f"成交量(备用):{deal_volume}/{THRESH_HOLD_VOLUME}") |
| | | deal_rate = round(deal_volume / THRESH_HOLD_VOLUME, 2) |
| | | if deal_rate >= 0.5: |
| | | return BUY_MODE_DIRECT, f"达到买入条件(备用):比例-{deal_rate}" |
| | | else: |
| | | return BUY_MODE_NONE, f"尚未达到买入比例(备用):{deal_rate}" |
| | | else: |
| | | return BUY_MODE_BY_L2, f"总卖额小于500w(备用)({refer_sell_data[1]})" |
| | | else: |
| | | return BUY_MODE_NONE, "尚未获取到总卖额(备用)" |
| | | |
| | | |
| | | def limit_up_active_buy_deal(code, transaction_datas): |
| | | async_log_util.info(hx_logger_l2_transaction, f"板上成交:{code}-{transaction_datas}") |
| | | """ |
| | | 涨停主动买成交 |
| | | @param code: |
| | | @param transaction_datas: # q.append((data['SecurityID'], data['TradePrice'], data['TradeVolume'], |
| | | # data['OrderTime'], data['MainSeq'], data['SubSeq'], data['BuyNo'], |
| | | # data['SellNo'], data['ExecType'])) |
| | | @return: 买入方式, 消息 |
| | | """ |
| | | # ---------初始化------------- |
| | | if code not in __huaxin_timestamp_convert_temp: |
| | | __huaxin_timestamp_convert_temp[code] = {} |
| | | if code not in __deal_active_buy_total_money: |
| | | __deal_active_buy_total_money[code] = 0 |
| | | if code not in __latest_deal_active_buy_order: |
| | | __latest_deal_active_buy_order[code] = ['', 0, 0] |
| | | for transaction_data in transaction_datas: |
| | | # 统计金额 |
| | | money = transaction_data[1] * transaction_data[2] |
| | | price = transaction_data[1] |
| | | __deal_active_buy_total_money[code] += money |
| | | if __latest_deal_active_buy_order[code][0] == transaction_data[6]: |
| | | __latest_deal_active_buy_order[code][1] += transaction_data[2] |
| | | __latest_deal_active_buy_order[code][2] += money |
| | | else: |
| | | __latest_deal_active_buy_order[code] = [transaction_data[6], transaction_data[2], money] |
| | | if code not in __start_time_dict: |
| | | # 判断买单成交的金额,如果金额大于50w就作为计时起始点 |
| | | if __latest_deal_active_buy_order[code][2] >= 500000: |
| | | # 设置计时起点 |
| | | __start_time_dict[code] = l2_huaxin_util.convert_time(transaction_data[3], with_ms=True) |
| | | |
| | | transaction_data = transaction_datas[-1] |
| | | huaxin_timestamp = transaction_data[3] |
| | | price = transaction_data[1] |
| | | time_str_with_ms = __huaxin_timestamp_convert_temp[code].get(huaxin_timestamp) |
| | | if not time_str_with_ms: |
| | | __huaxin_timestamp_convert_temp[code].clear() |
| | | time_str_with_ms = l2_huaxin_util.convert_time(huaxin_timestamp, with_ms=True) |
| | | __huaxin_timestamp_convert_temp[code][huaxin_timestamp] = time_str_with_ms |
| | | # 涨停主动买成交 |
| | | if code in __start_time_dict: |
| | | # 判断与当前时间的差值 |
| | | THRESHOLD_TIME = 1000 if tool.is_sh_code(code) else 500 |
| | | if tool.trade_time_sub_with_ms(time_str_with_ms, __start_time_dict[code]) > THRESHOLD_TIME: |
| | | # 距离上个较大单成交已经过去一定时间 |
| | | __start_time_dict.pop(code) |
| | | # 清除成交金额 |
| | | __deal_active_buy_total_money[code] = 0 |
| | | return BUY_MODE_NONE, f"距离上个大单成交超过{THRESHOLD_TIME}ms" |
| | | |
| | | selling_num = L2LimitUpSellDataManager.get_delegating_sell_num(code) |
| | | if selling_num: |
| | | total_sell = __deal_active_buy_total_money[code] + selling_num * price |
| | | rate = round(__deal_active_buy_total_money[code] / total_sell, 2) |
| | | # 成交的比例 |
| | | if total_sell > 500 * 1e4: |
| | | if rate > 0.5: |
| | | return BUY_MODE_DIRECT, f"剩余涨停总卖额-{selling_num * price},原涨停总卖-{total_sell},已成交额-{__deal_active_buy_total_money[code]},成交比例-{rate} " |
| | | else: |
| | | return BUY_MODE_NONE, f"被动卖成交比例未达到:剩余涨停总卖额-{selling_num * price},原涨停总卖-{total_sell},已成交额-{__deal_active_buy_total_money[code]},成交比例-{rate}" |
| | | else: |
| | | return BUY_MODE_BY_L2, f"剩余涨停总卖额小于500w-{selling_num * price},原涨停总卖-{total_sell},已成交额-{__deal_active_buy_total_money[code]},成交比例-{rate} " |
| | | else: |
| | | return __get_deal_rate_by(code, huaxin_timestamp) |
| | | else: |
| | | return BUY_MODE_NONE, "没有50w以上的买单成交" |
| | | |