| | |
| | | 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( |
| | | 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 += 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( |
| | | 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"] |
| | |
| | | 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, |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, i, |
| | | total_datas, |
| | | local_today_num_operate_map.get( |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | if left_count > 0: |
| | | total_num += left_count * val["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, |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, i, |
| | | total_datas, |
| | | local_today_num_operate_map.get( |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | if left_count > 0: |
| | | watch_indexes.add(i) |
| | |
| | | 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, |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, index, |
| | | total_data, |
| | | local_today_num_operate_map) |
| | | 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, |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, i, |
| | | total_data, |
| | | local_today_num_operate_map) |
| | | 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, |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, |
| | | i, |
| | | total_datas, |
| | | local_today_num_operate_map.get( |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | if left_count > 0: |
| | | watch_indexes.add(i) |
| | |
| | | # 小金额过滤 |
| | | 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, |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, i, |
| | | total_datas, |
| | | local_today_num_operate_map.get( |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | if left_count > 0: |
| | | total_num += val['num'] * left_count |
| | |
| | | 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, |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, |
| | | wi, |
| | | total_data, |
| | | _local_today_num_operate_map) |
| | | 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, |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, |
| | | wi, |
| | | total_data, |
| | | _local_today_num_operate_map) |
| | | 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, |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, index, |
| | | total_datas, |
| | | local_operate_map) |
| | | 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( |
| | | 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 |