| | |
| | | import json |
| | | import logging |
| | | import time |
| | | import l2.l2_data_util |
| | | |
| | | import constant |
| | | from code_attribute import big_money_num_manager, gpcode_manager, code_volumn_manager |
| | | from code_attribute import big_money_num_manager, gpcode_manager |
| | | import l2_data_util |
| | | from db import redis_manager_delegate as redis_manager |
| | | from db.redis_manager_delegate import RedisUtils |
| | | from trade.l2_trade_factor import L2PlaceOrderParamsManager |
| | | from utils import tool |
| | | from l2.safe_count_manager import BuyL2SafeCountManager |
| | | from l2.transaction_progress import TradeBuyQueue |
| | | from output import kp_client_msg_manager |
| | | from trade import trade_queue_manager, l2_trade_factor |
| | | from l2 import l2_log, l2_data_source_util |
| | | from l2.l2_data_util import L2DataUtil, local_today_num_operate_map, local_today_datas |
| | | from log_module.log import logger_buy_1_volumn, logger_l2_d_cancel, logger_l2_l_cancel |
| | | from l2.l2_data_util import L2DataUtil, local_today_num_operate_map, local_today_datas, local_today_buyno_map, \ |
| | | local_today_canceled_buyno_map |
| | | from log_module.log import logger_buy_1_volumn, logger_l2_l_cancel |
| | | from utils.tool import CodeDataCacheUtil |
| | | |
| | | |
| | |
| | | left_big_num += val["num"] * data["re"] |
| | | elif L2DataUtil.is_limit_up_price_buy_cancel(val): |
| | | # 查询买入位置 |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data(code, data, |
| | | local_today_num_operate_map.get( |
| | | code)) |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data_v2(data, |
| | | local_today_buyno_map.get( |
| | | code)) |
| | | if buy_index is not None and start_index <= buy_index <= end_index: |
| | | if buy_index - buy_single_index < fire_count: |
| | | left_big_num -= 0 |
| | |
| | | left_big_num += val["num"] * data["re"] |
| | | elif L2DataUtil.is_limit_up_price_buy_cancel(val): |
| | | # 查询买入位置 |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data(code, data, |
| | | local_today_num_operate_map.get( |
| | | code)) |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data_v2(data, |
| | | local_today_buyno_map.get( |
| | | code)) |
| | | if buy_index is not None and transaction_index + 1 <= buy_index <= real_place_order_index and i < start_index: |
| | | left_big_num -= val["num"] * data["re"] |
| | | l2_log.s_cancel_debug(code, "S撤初始化结果,left_big_num:{}", left_big_num) |
| | |
| | | if val["num"] * float(val["price"]) <= constant.S_CANCEL_MIN_MONEY * 100: |
| | | continue |
| | | # 查询买入位置 |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data(code, data, |
| | | local_today_num_operate_map.get( |
| | | code)) |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data_v2(data, |
| | | local_today_buyno_map.get(code)) |
| | | if buy_index is not None and transaction_index < buy_index < real_place_order_index: |
| | | cancel_num += total_data[buy_index]["re"] * int(total_data[buy_index]["val"]["num"]) |
| | | if need_cancel: |
| | |
| | | buy_num += data["re"] * int(val["num"]) |
| | | elif L2DataUtil.is_limit_up_price_buy_cancel(val): |
| | | # 查询买入位置 |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data(code, data, |
| | | local_today_num_operate_map.get( |
| | | code)) |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data_v2(data, |
| | | local_today_buyno_map.get(code)) |
| | | if buy_index is not None and buy_single_index <= buy_index: |
| | | if place_order_index and place_order_index >= buy_index: |
| | | cancel_num += total_data[buy_index]["re"] * int(total_data[buy_index]["val"]["num"]) |
| | |
| | | val = data['val'] |
| | | if not L2DataUtil.is_limit_up_price_buy(val): |
| | | continue |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count(code, i, |
| | | total_datas, |
| | | local_today_num_operate_map.get( |
| | | code)) |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, i, |
| | | total_datas, |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | if left_count > 0: |
| | | total_num += left_count * val["num"] |
| | | if total_num > thresh_hold_num: |
| | |
| | | # 小金额过滤 |
| | | if float(val['price']) * val['num'] < min_money * 100: |
| | | continue |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count(code, i, |
| | | total_datas, |
| | | local_today_num_operate_map.get( |
| | | code)) |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, i, |
| | | total_datas, |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | if left_count > 0: |
| | | watch_indexes.add(i) |
| | | if len(watch_indexes) >= 5: |
| | |
| | | self.__real_place_order_index_dict[code] = index |
| | | |
| | | def need_cancel(self, code, buy_single_index, buy_exec_index, start_index, end_index, total_data, |
| | | local_today_num_operate_map, |
| | | buy_volume_index, volume_index, |
| | | is_first_code): |
| | | if buy_single_index is None or buy_exec_index is None: |
| | |
| | | val = data['val'] |
| | | total_num += val['num'] * data['re'] |
| | | # 判断是否撤单 |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count(code, index, |
| | | total_data, |
| | | local_today_num_operate_map) |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, index, |
| | | total_data, |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | |
| | | cancel_num += val['num'] * (data['re'] - left_count) |
| | | rate = round(cancel_num / total_num, 4) |
| | |
| | | return False, None |
| | | |
| | | # 下单成功 |
| | | def place_order_success(self, code, buy_single_index, buy_exec_index, total_data, local_today_num_operate_map): |
| | | def place_order_success(self, code, buy_single_index, buy_exec_index, total_data): |
| | | self.__clear_data(code) |
| | | |
| | | # 获取H撤监听的数据索引范围 |
| | |
| | | self.__del_real_order_index(code) |
| | | |
| | | # 设置成交位 |
| | | def set_trade_progress(self, code, index, buy_exec_index, total_data, local_today_num_operate_map, m_base_value, |
| | | def set_trade_progress(self, code, index, buy_exec_index, total_data, m_base_value, |
| | | limit_up_price): |
| | | # 离下单执行位2分钟内的有效 |
| | | sub_time = tool.trade_time_sub(total_data[-1]['val']['time'], total_data[buy_exec_index]['val']['time']) |
| | |
| | | if val['num'] * val['price'] < 5900: |
| | | continue |
| | | |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count(code, i, |
| | | total_data, |
| | | local_today_num_operate_map) |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, i, |
| | | total_data, |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | left_num += val['num'] * left_count |
| | | # 剩下的不足动态M值的1/2 |
| | | rate = round(float(limit_up_price) * left_num * 100 / m_base_value, 3) |
| | |
| | | temp_rate = 3 |
| | | deal_rate = round((temp_rate * 3.5 - 2.5) / 100, 4) |
| | | |
| | | |
| | | base_rate += block_rate |
| | | base_rate += deal_rate |
| | | return round(base_rate, 2), block_rate, deal_rate |
| | | return round(base_rate, 2) |
| | | |
| | | # 设置板块涨停数量(除开自己) |
| | | @classmethod |
| | |
| | | # 小金额过滤 |
| | | if float(val['price']) * val['num'] < min_money * 100: |
| | | continue |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count(code, i, |
| | | total_datas, |
| | | local_today_num_operate_map.get( |
| | | code)) |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, |
| | | i, |
| | | total_datas, |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | if left_count > 0: |
| | | watch_indexes.add(i) |
| | | if len(watch_indexes) >= MAX_COUNT: |
| | |
| | | # 小金额过滤 |
| | | if float(val['price']) * val['num'] < MIN_MONEY: |
| | | continue |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count(code, i, |
| | | total_datas, |
| | | local_today_num_operate_map.get( |
| | | code)) |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, i, |
| | | total_datas, |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | if left_count > 0: |
| | | total_num += val['num'] * left_count |
| | | watch_indexes.add(i) |
| | |
| | | self.compute_watch_index(code, self.__last_trade_progress_dict.get(code), |
| | | self.__real_place_order_index_dict.get(code)) |
| | | |
| | | def __compute_need_cancel(self, code, buy_exec_index, start_index, end_index, total_data, |
| | | _local_today_num_operate_map, is_first_code): |
| | | def __compute_need_cancel(self, code, buy_exec_index, start_index, end_index, total_data, is_first_code): |
| | | watch_indexes = self.__get_watch_indexes_cache(code) |
| | | if not watch_indexes: |
| | | return False, None |
| | |
| | | val = data["val"] |
| | | if L2DataUtil.is_limit_up_price_buy_cancel(val): |
| | | # 查询买入位置 |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data(code, data, |
| | | _local_today_num_operate_map) |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data_v2(data, |
| | | local_today_buyno_map.get( |
| | | code)) |
| | | if buy_index is not None and buy_index in watch_indexes: |
| | | need_compute = True |
| | | break |
| | |
| | | watch_indexes_list.sort() |
| | | canceled_num = 0 |
| | | for wi in watch_indexes: |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count(code, |
| | | wi, |
| | | total_data, |
| | | _local_today_num_operate_map) |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, |
| | | wi, |
| | | total_data, |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | canceled_num += (total_data[wi]["re"] - left_count) * total_data[wi]["val"]["num"] |
| | | # if wi == watch_indexes_list[-1] and left_count == 0: |
| | | # # 离下单位置最近的一个撤单,必须触发撤单 |
| | |
| | | |
| | | return False, None |
| | | |
| | | def __compute_near_by_trade_progress_need_cancel(self, code, buy_exec_index, start_index, end_index, total_data, |
| | | _local_today_num_operate_map, is_first_code): |
| | | def __compute_near_by_trade_progress_need_cancel(self, code, buy_exec_index, start_index, end_index, total_data, is_first_code): |
| | | watch_indexes = self.__get_near_by_trade_progress_indexes_cache(code) |
| | | if not watch_indexes: |
| | | return False, None |
| | |
| | | val = data["val"] |
| | | if L2DataUtil.is_limit_up_price_buy_cancel(val): |
| | | # 查询买入位置 |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data(code, data, |
| | | _local_today_num_operate_map) |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data_v2(data, |
| | | local_today_buyno_map.get( |
| | | code)) |
| | | if buy_index is not None and buy_index in watch_indexes: |
| | | need_compute = True |
| | | break |
| | |
| | | # 计算撤单比例 |
| | | canceled_count = 0 |
| | | for wi in watch_indexes: |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count(code, |
| | | wi, |
| | | total_data, |
| | | _local_today_num_operate_map) |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, |
| | | wi, |
| | | total_data, |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | canceled_count += total_data[wi]["re"] - left_count |
| | | rate = round(canceled_count / total_count, 3) |
| | | l2_log.l_cancel_debug(code, f"计算范围:{start_index}-{end_index},成交位临近已撤单比例:{rate}") |
| | |
| | | |
| | | return False, None |
| | | |
| | | def need_cancel(self, code, buy_exec_index, start_index, end_index, total_data, _local_today_num_operate_map, |
| | | is_first_code): |
| | | def need_cancel(self, code, buy_exec_index, start_index, end_index, total_data, is_first_code): |
| | | if buy_exec_index is None: |
| | | return False, "尚未找到下单位置" |
| | | time_space = tool.trade_time_sub(total_data[start_index]["val"]["time"], |
| | |
| | | return False, None |
| | | # 下单位临近撤 |
| | | can_cancel, cancel_data = self.__compute_need_cancel(code, buy_exec_index, start_index, end_index, total_data, |
| | | _local_today_num_operate_map, is_first_code) |
| | | is_first_code) |
| | | if not can_cancel: |
| | | # 成交位临近撤 |
| | | can_cancel, cancel_data = self.__compute_near_by_trade_progress_need_cancel(code, buy_exec_index, |
| | | start_index, end_index, |
| | | total_data, |
| | | _local_today_num_operate_map, |
| | | is_first_code) |
| | | return can_cancel, cancel_data |
| | | |
| | |
| | | __SecondCancelBigNumComputer = SecondCancelBigNumComputer() |
| | | |
| | | # 开始撤单 |
| | | def start_cancel(self, code, buy_no, total_datas, buy_order_no_map, local_operate_map, m_val_num): |
| | | def start_cancel(self, code, buy_no, total_datas, m_val_num): |
| | | # TODO 暂时注释掉G撤 |
| | | return False, "暂时不执行G撤" |
| | | thresh_num = int(m_val_num * 1) |
| | |
| | | data = total_datas[index] |
| | | if L2DataUtil.is_limit_up_price_buy(data["val"]): |
| | | # 获取是否在买入执行信号周围2s |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count(code, index, |
| | | total_datas, |
| | | local_operate_map) |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, index, |
| | | total_datas, |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | if left_count > 0: |
| | | buy_nums += left_count * data["val"]["num"] |
| | | if buy_nums > thresh_num: |
| | |
| | | if L2DataUtil.is_limit_up_price_buy_cancel(data["val"]): |
| | | cancel_big_num_count += int(data["re"]) |
| | | # 获取是否在买入执行信号周围2s |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data(code, data, |
| | | local_today_num_operate_map.get( |
| | | code)) |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data_v2(data, |
| | | local_today_buyno_map.get( |
| | | code)) |
| | | if buy_index is not None: |
| | | # 相差1s |
| | | buy_time = total_datas[buy_index]["val"]["time"] |
| | |
| | | LCancelBigNumComputer |
| | | from l2.l2_data_manager import L2DataException |
| | | from l2.l2_data_util import local_today_datas, L2DataUtil, local_today_num_operate_map, local_today_buyno_map, \ |
| | | local_latest_datas |
| | | local_latest_datas, local_today_canceled_buyno_map |
| | | import l2.l2_data_util |
| | | from log_module.log import logger_l2_trade_buy, logger_l2_process, logger_l2_error, logger_profile, logger_debug |
| | | |
| | |
| | | # 如果是涨停买撤信号需要看数据位置是否比开始处理时间早 |
| | | if L2DataUtil.is_limit_up_price_buy_cancel(data["val"]): |
| | | # 获取买入信号 |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data(code, total_datas[i], |
| | | local_today_num_operate_map.get( |
| | | code)) |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data_v2(total_datas[i], |
| | | local_today_buyno_map.get( |
| | | code)) |
| | | if buy_index is not None and buy_index < begin_pos: |
| | | continue |
| | | |
| | |
| | | b_need_cancel, b_cancel_data = cls.__HourCancelBigNumComputer.need_cancel(code, _buy_single_index, |
| | | _buy_exec_index, start_index, |
| | | end_index, total_data, |
| | | local_today_num_operate_map.get( |
| | | code), |
| | | code_volumn_manager.get_volume_rate_index( |
| | | buy_volume_rate), |
| | | cls.volume_rate_info[code][1], |
| | |
| | | b_need_cancel, b_cancel_data = cls.__LCancelBigNumComputer.need_cancel(code, |
| | | _buy_exec_index, start_index, |
| | | end_index, total_data, |
| | | local_today_num_operate_map.get( |
| | | code), is_first_code) |
| | | is_first_code) |
| | | if b_need_cancel and b_cancel_data: |
| | | return b_cancel_data, "L撤销比例触发阈值" |
| | | except Exception as e: |
| | |
| | | val = data["val"] |
| | | if not L2DataUtil.is_limit_up_price_buy(val): |
| | | continue |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count(code, |
| | | data["index"], |
| | | total_data, |
| | | num_operate_map) |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, |
| | | data["index"], |
| | | total_data, |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | total_num += left_count * val["num"] |
| | | m_base_val = l2_trade_factor.L2PlaceOrderParamsManager.get_base_m_val(code) |
| | | thresh_hold_num = m_base_val // (float(gpcode_manager.get_limit_up_price(code)) * 100) |
| | |
| | | num_operate_map = local_today_num_operate_map.get(code) |
| | | for i in range(trade_index + 1, total_data[-1]["index"] + 1): |
| | | if L2DataUtil.is_limit_up_price_buy(total_data[i]["val"]): |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count(code, |
| | | total_data[ |
| | | i][ |
| | | "index"], |
| | | total_data, |
| | | num_operate_map) |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, |
| | | total_data[i]["index"], |
| | | total_data, |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | if left_count > 0: |
| | | not_cancel_num += total_data[i]["val"]["num"] |
| | | m_base_val = l2_trade_factor.L2PlaceOrderParamsManager.get_base_m_val(code) |
| | |
| | | return |
| | | |
| | | unique_key = f"{code}-{compute_start_index}-{compute_end_index}" |
| | | if cls.__latest_process_not_order_unique_keys_count.get(unique_key) and cls.__latest_process_not_order_unique_keys_count.get(unique_key) > 2: |
| | | if cls.__latest_process_not_order_unique_keys_count.get( |
| | | unique_key) and cls.__latest_process_not_order_unique_keys_count.get(unique_key) > 2: |
| | | async_log_util.error(logger_l2_error, |
| | | f"重复处理数据:code-{code} start_index-{compute_start_index} end_index-{compute_end_index}") |
| | | return |
| | |
| | | # 只统计59万以上的金额 |
| | | # 涨停买撤 |
| | | # 判断买入位置是否在买入信号之前 |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data(code, |
| | | total_datas[i], |
| | | local_today_num_operate_map.get( |
| | | code)) |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data_v2(total_datas[i], |
| | | local_today_buyno_map.get( |
| | | code)) |
| | | if buy_index is not None: |
| | | # 找到买撤数据的买入点 |
| | | if buy_index >= buy_single_index: |
| | |
| | | |
| | | return cls.__get_buy_index_with_cancel_data(code, cancel_data, local_today_num_operate_map) |
| | | |
| | | # 根据买撤数据计算买入数据(华鑫原生L2数据适用) |
| | | @classmethod |
| | | def get_buy_index_with_cancel_data_v2(cls, cancel_data, buyno_map): |
| | | order_no = str(cancel_data["val"]["orderNo"]) |
| | | buy_data = buyno_map.get(order_no) |
| | | if buy_data: |
| | | return buy_data["index"] |
| | | return None |
| | | |
| | | # 获取没撤的笔数 |
| | | @classmethod |
| | | def get_limit_up_buy_no_canceled_count(cls, code, index, total_data, local_today_num_operate_map): |
| | |
| | | return count |
| | | return 0 |
| | | |
| | | # 获取没撤的笔数 |
| | | |
| | | # 获取涨停买没有撤单的数量 |
| | | @classmethod |
| | | def get_limit_up_buy_no_canceled_count_v2(cls, code, index, total_data, canceled_buyno_map): |
| | | data = None |
| | | try: |
| | | data = total_data[index] |
| | | except Exception as e: |
| | | logger_l2_error.error( |
| | | f"未找到买入索引对应的数据:index-{index} total_data长度-{len(total_data) if total_data else 0} 错误原因:{str(e)}") |
| | | val = data["val"] |
| | | order_no = str(val["orderNo"]) |
| | | canceled_data = canceled_buyno_map.get(order_no) |
| | | if canceled_data: |
| | | return data["re"] - canceled_data["re"] |
| | | else: |
| | | return data["re"] |
| | | |
| | | |
| | | # if __name__ == "__main__": |
| | | # code = "000925" |
| | |
| | | # 买入订单号映射,只有原生的L2数据才有 |
| | | local_today_buyno_map = {} |
| | | |
| | | # 已经撤单的订单号 |
| | | local_today_canceled_buyno_map = {} |
| | | |
| | | |
| | | def load_l2_data(code, load_latest=True, force=False): |
| | | # 加载最近的l2数据 |
| | |
| | | # 根据今日数据加载 |
| | | load_num_operate_map(local_today_num_operate_map, code, local_today_datas.get(code), force) |
| | | load_buy_no_map(local_today_buyno_map, code, local_today_datas.get(code), force) |
| | | load_canceled_buy_no_map(local_today_canceled_buyno_map, code, local_today_datas.get(code), force) |
| | | return data_normal |
| | | return True |
| | | |
| | |
| | | key = "{}".format(data["val"]["orderNo"]) |
| | | if local_today_buyno_map[code].get(key) is None: |
| | | local_today_buyno_map[code].setdefault(key, data) |
| | | |
| | | |
| | | # 将数据根据orderNo分类已撤订单,原生数据才有 |
| | | def load_canceled_buy_no_map(local_today_canceled_buyno_map, code, source_datas, clear=False): |
| | | # 只有原生L2数据才会有此操作 |
| | | if constant.L2_SOURCE_TYPE != constant.L2_SOURCE_TYPE_HUAXIN: |
| | | return |
| | | if local_today_canceled_buyno_map.get(code) is None: |
| | | local_today_canceled_buyno_map[code] = {} |
| | | if clear: |
| | | local_today_canceled_buyno_map[code] = {} |
| | | |
| | | for data in source_datas: |
| | | # 只留下买撤 |
| | | if data["val"]["operateType"] != 1: |
| | | continue |
| | | # 只填充买入数据 |
| | | key = "{}".format(data["val"]["orderNo"]) |
| | | if local_today_canceled_buyno_map[code].get(key) is None: |
| | | local_today_canceled_buyno_map[code].setdefault(key, data) |
| | | |
| | | |
| | | @tool.async_call |
| | |
| | | # 是否有买撤数据 |
| | | if cancel_datas: |
| | | for cancel_data in cancel_datas: |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data(code, cancel_data, |
| | | local_today_num_operate_map) |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data_v2(cancel_data, |
| | | local_today_buyno_map.get( |
| | | code)) |
| | | if buy_index == data["index"]: |
| | | return True |
| | | return False |
| | |
| | | from trade import l2_trade_factor |
| | | from db import redis_manager_delegate as redis_manager |
| | | from utils import tool |
| | | from l2.l2_data_util import L2DataUtil |
| | | from l2.l2_data_util import L2DataUtil, local_today_buyno_map |
| | | |
| | | |
| | | class BuyL2SafeCountManager(object): |
| | |
| | | buy_num += int(val["num"]) * data["re"] |
| | | elif L2DataUtil.is_limit_up_price_buy_cancel(val): |
| | | # 获取买入信息 |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data(code, data, |
| | | local_today_num_operate_map) |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data_v2(data, local_today_buyno_map.get(code)) |
| | | if buy_index is not None: |
| | | if last_buy_single_index <= buy_index <= end_index: |
| | | cancel_num += int(val["num"]) * data["re"] |
| | |
| | | logger_l2_trade_buy_queue.info("获取成交位置成功: code-{} index-{} 数据-{}", code, |
| | | buy_progress_index, |
| | | json.dumps(buy_queue_result_list)) |
| | | # 计算大单成交额 |
| | | deal_big_money_manager.DealComputeProgressManager().set_trade_progress(code, |
| | | buy_progress_index, |
| | | l2.l2_data_util.local_today_datas.get( |
| | | code), |
| | | l2.l2_data_util.local_today_num_operate_map.get( |
| | | code)) |
| | | |
| | | else: |
| | | raise Exception("暂未获取到交易进度") |
| | | msg += "计算成交进度:" + f"{time.time() - _start_time} " |
| | |
| | | # @unittest.skip("跳过此单元测试") |
| | | def test_trade(self): |
| | | threading.Thread(target=async_log_util.run_sync,daemon=True).start() |
| | | code = "002640" |
| | | code = "603178" |
| | | clear_trade_data(code) |
| | | l2.l2_data_util.load_l2_data(code) |
| | | total_datas = deepcopy(l2.l2_data_util.local_today_datas[code]) |
| | |
| | | buy_progress_index, |
| | | buy_exec_index, |
| | | total_datas, |
| | | num_operate_map, |
| | | m_base_val, |
| | | limit_up_price) |
| | | if need_cancel: |
| | |
| | | try: |
| | | need_cancel, msg = cls.__GCancelBigNumComputer.start_cancel(code, f"{order_no}", |
| | | l2_data_util.local_today_datas.get( |
| | | code), |
| | | l2_data_util.local_today_buyno_map.get( |
| | | code), |
| | | l2_data_util.local_today_num_operate_map.get( |
| | | code), m_val_num) |
| | | if need_cancel: |
| | | # 需要撤单 |
| | |
| | | def h_cancel(code, buy_single_index, buy_exec_index): |
| | | try: |
| | | HourCancelBigNumComputer().place_order_success(code, buy_single_index, buy_exec_index, |
| | | local_today_datas.get(code), |
| | | local_today_num_operate_map.get(code)) |
| | | local_today_datas.get(code)) |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | logger_l2_error.exception(e) |
| | |
| | | # 数据预处理 |
| | | num_operate_map = {} |
| | | l2.l2_data_util.load_num_operate_map(num_operate_map, code, datas) |
| | | num_dict = {} |
| | | for data in datas: |
| | | if data["val"]["num"] not in num_dict: |
| | | num_dict[data["val"]["num"]] = [] |
| | | num_dict[data["val"]["num"]].append(data) |
| | | buy_no_map = {} |
| | | l2.l2_data_util.load_buy_no_map(buy_no_map, code, datas) |
| | | |
| | | l2.l2_data_util.load_canceled_buy_no_map(l2.l2_data_util.local_today_canceled_buyno_map, code, datas) |
| | | |
| | | # num_dict = {} |
| | | # for data in datas: |
| | | # if data["val"]["num"] not in num_dict: |
| | | # num_dict[data["val"]["num"]] = [] |
| | | # num_dict[data["val"]["num"]].append(data) |
| | | |
| | | local_time = time.strftime("%Y%m%dT%H%M%S", time.localtime(time.time())) |
| | | file_name = "{}/{}_{}.xls".format(dest_dir, code, local_time) |
| | |
| | | ws.write(index, 9, data["val"].get("orderNo"), style) |
| | | # 查询是否撤单 |
| | | if int(data["val"]["operateType"]) == 0: |
| | | cancel = False |
| | | cancel_data = l2.l2_data_util.local_today_canceled_buyno_map.get(code).get(str(data["val"]["orderNo"])) |
| | | # 买 |
| | | for d in num_dict[data["val"]["num"]]: |
| | | if int(d["val"]["operateType"]) == 1: |
| | | buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data(code, d, |
| | | num_operate_map[ |
| | | code]) |
| | | if buy_index == data["index"]: |
| | | try: |
| | | ws.write(index, 8, "{}-{}".format(d["index"], d["val"]["time"]), cancel_style) |
| | | break |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | if cancel_data: |
| | | if cancel_data["index"] == 137: |
| | | print("进入断点") |
| | | try: |
| | | ws.write(index, 8, "{}-{}".format(cancel_data["index"], cancel_data["val"]["time"]), cancel_style) |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | |
| | | ws.write(index, 3, "{}万".format(round(int(data["val"]["num"]) * float(data["val"]["price"]) / 100, 2)), style) |
| | | wb.save(file_name) |
| | |
| | | datas = local_today_datas[code] |
| | | num_operate_map = {} |
| | | l2.l2_data_util.load_num_operate_map(num_operate_map, code, datas) |
| | | for progress in progresses: |
| | | deal_big_money_manager.DealComputeProgressManager().set_trade_progress(code, progress, datas, |
| | | num_operate_map[code]) |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | try: |
| | | export_l2_excel("002375") |
| | | export_l2_excel("600476") |
| | | except Exception as e: |
| | | logging.exception(e) |