| | |
| | | DCancelBigNumComputer().set_real_order_index(code, index) |
| | | SecondCancelBigNumComputer().set_real_place_order_index(code, index) |
| | | LCancelBigNumComputer().set_real_place_order_index(code, index, buy_single_index=buy_single_index) |
| | | HourCancelBigNumComputer().set_real_place_order_index(code, index, buy_single_index) |
| | | |
| | | |
| | | class SecondCancelBigNumComputer: |
| | |
| | | __db = 0 |
| | | __redis_manager = redis_manager.RedisManager(0) |
| | | __tradeBuyQueue = TradeBuyQueue() |
| | | __hCancelParamsManager = l2_trade_factor.HCancelParamsManager() |
| | | __SecondCancelBigNumComputer = SecondCancelBigNumComputer() |
| | | |
| | | # 计算触发位置 |
| | | __start_compute_index_dict = {} |
| | | |
| | | # 成交位置 |
| | | __transaction_progress_index_dict = {} |
| | | # 缓存 |
| | | __cancel_watch_indexs_cache = {} |
| | | __real_place_order_index_dict = {} |
| | | |
| | | __instance = None |
| | | |
| | |
| | | val = RedisUtils.get(__redis, k) |
| | | val = json.loads(val) |
| | | CodeDataCacheUtil.set_cache(cls.__cancel_watch_indexs_cache, code, val) |
| | | |
| | | keys = RedisUtils.keys(__redis, "h_cancel_transaction_index-*") |
| | | for k in keys: |
| | | code = k.split("-")[-1] |
| | | val = RedisUtils.get(__redis, k) |
| | | val = int(val) |
| | | CodeDataCacheUtil.set_cache(cls.__transaction_progress_index_dict, code, val) |
| | | finally: |
| | | RedisUtils.realse(__redis) |
| | | |
| | |
| | | return cache_result[1] |
| | | return None, -1, False |
| | | |
| | | def __save_transaction_index(self, code, index): |
| | | CodeDataCacheUtil.set_cache(self.__transaction_progress_index_dict, code, index) |
| | | key = f"h_cancel_transaction_index-{code}" |
| | | RedisUtils.setex_async(self.__db, key, tool.get_expire(), index) |
| | | |
| | | def __clear_data(self, code): |
| | | CodeDataCacheUtil.clear_cache(self.__cancel_watch_indexs_cache, code) |
| | | ks = [f"h_cancel_watch_indexs-{code}"] |
| | | CodeDataCacheUtil.clear_cache(self.__transaction_progress_index_dict, code) |
| | | CodeDataCacheUtil.clear_cache(self.__start_compute_index_dict, code) |
| | | ks = [f"h_cancel_watch_indexs-{code}", f"h_cancel_transaction_index-{code}"] |
| | | for key in ks: |
| | | RedisUtils.delete_async(self.__db, key) |
| | | |
| | | # 计算观察索引,倒序计算 |
| | | |
| | | def compute_watch_index(self, code): |
| | | def __compute_watch_index(self, code): |
| | | if self.__cancel_watch_indexs_cache.get(code): |
| | | return |
| | | real_place_order_index = self.__SecondCancelBigNumComputer.get_real_place_order_index_cache(code) |
| | | if not real_place_order_index: |
| | | l2_log.h_cancel_debug(code, "尚未找到真实下单位置") |
| | | return |
| | | start_compute_index = self.__start_compute_index_dict.get(code) |
| | | if not start_compute_index: |
| | | l2_log.h_cancel_debug(code, "尚未找到开始计算位置") |
| | | return |
| | | |
| | | total_datas = local_today_datas.get(code) |
| | | |
| | | # -----------------计算H上------------------- |
| | | watch_indexes_up = set() |
| | | for i in range(start_compute_index, real_place_order_index): |
| | | data = total_datas[i] |
| | | val = data['val'] |
| | | if not L2DataUtil.is_limit_up_price_buy(val): |
| | | continue |
| | | # 小金额过滤 |
| | | if float(val['price']) * val['num'] < 50 * 100: |
| | | continue |
| | | 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_up.add(i) |
| | | |
| | | # ------------------计算H下----------------------- |
| | | # 计算结束位置 |
| | | total_num = 0 |
| | | # 获取m值数据 |
| | | thresh_hold_money = l2_trade_factor.L2PlaceOrderParamsManager.get_base_m_val(code) |
| | | thresh_hold_money = thresh_hold_money * 2 |
| | | thresh_hold_money = thresh_hold_money |
| | | thresh_hold_num = thresh_hold_money // (float(gpcode_manager.get_limit_up_price(code)) * 100) |
| | | end_index = real_place_order_index + 1 |
| | | for i in range(real_place_order_index + 1, total_datas[-1]["index"]): |
| | |
| | | end_index = i |
| | | break |
| | | MIN_MONEYS = [300, 200, 100, 50] |
| | | watch_indexes = set() |
| | | for min_money in MIN_MONEYS: |
| | | watch_indexes = set() |
| | | for i in range(real_place_order_index + 1, end_index + 1): |
| | | # 看是否撤单 |
| | | data = total_datas[i] |
| | |
| | | watch_indexes.add(i) |
| | | if len(watch_indexes) >= 5: |
| | | break |
| | | if watch_indexes: |
| | | self.__save_watch_index_set(code, watch_indexes) |
| | | |
| | | l2_log.h_cancel_debug(code, f"设置监听范围, 数据范围:{real_place_order_index}-{end_index} 监听范围-{watch_indexes}") |
| | | if len(watch_indexes) >= 5: |
| | | break |
| | | |
| | | if watch_indexes or watch_indexes_up: |
| | | watch_indexes |= watch_indexes_up |
| | | self.__save_watch_index_set(code, watch_indexes) |
| | | l2_log.h_cancel_debug(code, f"设置监听范围, 数据范围:{real_place_order_index}-{end_index} 监听范围-{watch_indexes}") |
| | | # 设置真实下单位置 |
| | | |
| | | def set_real_place_order_index(self, code, index): |
| | | self.__real_place_order_index_dict[code] = index |
| | | def __need_compute_watch_indexes(self, code, transaction_index): |
| | | start_compute_index = self.__start_compute_index_dict.get(code) |
| | | if start_compute_index is None: |
| | | return False |
| | | # 成交位到开始计算位置没有买单之后 |
| | | total_datas = local_today_datas.get(code) |
| | | for index in range(transaction_index + 1, start_compute_index): |
| | | data = total_datas[index] |
| | | val = data['val'] |
| | | if not L2DataUtil.is_limit_up_price_buy(index): |
| | | continue |
| | | if float(val['price']) * val['num'] < 5000: |
| | | continue |
| | | 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: |
| | | # 中间还有未撤的买单 |
| | | return False |
| | | return True |
| | | |
| | | # 设置成交进度 |
| | | def set_transaction_index(self, code, index): |
| | | if index == self.__transaction_progress_index_dict.get(code): |
| | | return |
| | | self.__transaction_progress_index_dict[code] = index |
| | | if self.__need_compute_watch_indexes(code, index): |
| | | self.__compute_watch_index(code) |
| | | |
| | | # 设置真实下单位置 |
| | | def set_real_place_order_index(self, code, index, buy_single_index): |
| | | # 计算触发位置 |
| | | min_num = int(5000 / (float(gpcode_manager.get_limit_up_price(code)))) |
| | | # 统计净涨停买的数量 |
| | | not_cancel_indexes = [] |
| | | total_datas = local_today_datas.get(code) |
| | | for j in range(buy_single_index, index): |
| | | data = total_datas[j] |
| | | val = data['val'] |
| | | if not L2DataUtil.is_limit_up_price_buy(val): |
| | | continue |
| | | if val["num"] < min_num: |
| | | continue |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, |
| | | j, |
| | | total_datas, |
| | | local_today_canceled_buyno_map.get( |
| | | code)) |
| | | if left_count > 0: |
| | | not_cancel_indexes.append(j) |
| | | if not_cancel_indexes: |
| | | temp_count = len(not_cancel_indexes) |
| | | # 取后1/3的数据 |
| | | temp_index = int(temp_count * 9 / 10) |
| | | self.__start_compute_index_dict[code] = not_cancel_indexes[temp_index] |
| | | |
| | | def need_cancel(self, code, buy_single_index, buy_exec_index, start_index, end_index, total_data, |
| | | buy_volume_index, volume_index, |
| | | is_first_code): |
| | | if buy_single_index is None or buy_exec_index is None: |
| | | return False, "尚未找到下单位置" |
| | | time_space = tool.trade_time_sub(total_data[start_index]["val"]["time"], |
| | | total_data[buy_exec_index]["val"]["time"]) |
| | | if time_space >= constant.H_CANCEL_START_TIME - 1: |
| | | # 开始计算需要监控的单 |
| | | self.compute_watch_index(code) |
| | | |
| | | # 守护30s以外的数据 |
| | | if time_space <= constant.H_CANCEL_START_TIME: |
| | | return False, None |
| | | |
| | | if int(tool.get_now_time_str().replace(":", "")) > int("145000"): |
| | | return False, None |
| | | |
| | | l2_log.cancel_debug(code, "H级是否需要撤单,数据范围:{}-{} ", start_index, end_index) |
| | | # 监听的数据 |
| | | # 设置成交进度 |
| | | if code not in self.__transaction_progress_index_dict: |
| | | return False, "没找到成交进度" |
| | | watch_index_set = self.__get_watch_index_set_cache(code) |
| | | if not watch_index_set: |
| | | # 是否有涨停买撤 |
| | | need_compute = False |
| | | # 有涨停买撤才会计算位置 |
| | | for i in range(start_index, end_index + 1): |
| | | data = total_data[i] |
| | | val = data['val'] |
| | | if L2DataUtil.is_limit_up_price_buy_cancel(val): |
| | | need_compute = True |
| | | break |
| | | if need_compute: |
| | | if self.__need_compute_watch_indexes(code, self.__transaction_progress_index_dict.get(code)): |
| | | self.__compute_watch_index(code) |
| | | watch_index_set = self.__get_watch_index_set_cache(code) |
| | | if not watch_index_set: |
| | | return False, "没有监听索引" |
| | | l2_log.cancel_debug(code, "H级是否需要撤单,数据范围:{}-{} ", start_index, end_index) |
| | | if watch_index_set: |
| | | cancel_num = 0 |
| | | total_num = 0 |