| | |
| | | """ |
| | | L2卖管理 |
| | | """ |
| | | import json |
| | | import random |
| | | import time |
| | | |
| | | from db import redis_manager |
| | | from db.redis_manager import RedisUtils |
| | | from db import redis_manager_delegate as redis_manager |
| | | from db.redis_manager_delegate import RedisUtils |
| | | from log_module import async_log_util |
| | | from log_module.log import logger_l2_market_sell, logger_debug |
| | | from utils import tool |
| | | from utils.tool import CodeDataCacheUtil |
| | | |
| | | |
| | | # 卖行情信息 |
| | | class L2MarketSellManager: |
| | | __db = 0 |
| | | __redis_manager = redis_manager.RedisManager(0) |
| | | __instance = None |
| | | __current_total_sell_data_cache = {} |
| | | __last_total_sell_data_cache = {} |
| | | __used_refer_sell_data_cache = {} |
| | | # 总卖信息列表 |
| | | __total_sell_data_cache_list_cache = {} |
| | | |
| | | def __new__(cls, *args, **kwargs): |
| | | if not cls.__instance: |
| | | cls.__instance = super(L2MarketSellManager, cls).__new__(cls, *args, **kwargs) |
| | | time.sleep(random.randint(0, 2000) / 1000) |
| | | cls.__load_datas() |
| | | return cls.__instance |
| | | |
| | |
| | | |
| | | def clear(self): |
| | | self.__used_refer_sell_data_cache.clear() |
| | | self.__total_sell_data_cache_list_cache.clear() |
| | | keys = RedisUtils.keys(self.__get_redis(), "fast_buy_used_sell_data-*") |
| | | for k in keys: |
| | | RedisUtils.delete_async(self.__db, k) |
| | | |
| | | # 设置当前的总卖 |
| | | def set_current_total_sell_data(self, code, time_str, money): |
| | | def set_current_total_sell_data(self, code, time_str, money, volume, sell_1_info, sell_n_info): |
| | | """ |
| | | @param code: |
| | | @param time_str: |
| | | @param money: |
| | | @param volume: |
| | | @param sell_1_info: 格式(卖1价格,卖1量) |
| | | @param sell_n_info:卖挡位 |
| | | @return: |
| | | """ |
| | | # 记录日志 |
| | | async_log_util.info(logger_l2_market_sell, f"{code}: {time_str}-{money}") |
| | | if code in self.__current_total_sell_data_cache: |
| | | self.__last_total_sell_data_cache[code] = self.__current_total_sell_data_cache.get(code) |
| | | self.__current_total_sell_data_cache[code] = (time_str, round(money)) |
| | | async_log_util.info(logger_l2_market_sell, f"{code}: {time_str}-{money} {sell_1_info} {sell_n_info}") |
| | | |
| | | if code not in self.__total_sell_data_cache_list_cache: |
| | | self.__total_sell_data_cache_list_cache[code] = [] |
| | | if self.__total_sell_data_cache_list_cache[code] and self.__total_sell_data_cache_list_cache[code][-1][ |
| | | 0] == time_str: |
| | | return |
| | | |
| | | self.__total_sell_data_cache_list_cache[code].append((time_str, round(money), volume, sell_1_info, sell_n_info)) |
| | | # 保留最多10条数据 |
| | | if len(self.__total_sell_data_cache_list_cache[code]) > 10: |
| | | self.__total_sell_data_cache_list_cache[code] = self.__total_sell_data_cache_list_cache[code][-10:] |
| | | |
| | | def get_current_total_sell_data(self, code): |
| | | """ |
| | | @param code: |
| | | @return:(时间, 总买额, 总量, 卖1信息, 卖挡位信息) |
| | | """ |
| | | total_sell_data_cache_list = self.__total_sell_data_cache_list_cache.get(code) |
| | | if not total_sell_data_cache_list: |
| | | return None |
| | | return total_sell_data_cache_list[-1] |
| | | |
| | | # 获取参考卖的数据 |
| | | def get_refer_sell_data(self, code, time_str): |
| | | cuurent = self.__current_total_sell_data_cache.get(code) |
| | | if cuurent is None: |
| | | return None |
| | | if int(time_str.replace(":", "")) > int(cuurent[0].replace(":", "")): |
| | | return cuurent |
| | | last = self.__last_total_sell_data_cache.get(code) |
| | | if int(time_str.replace(":", "")) > int(last[0].replace(":", "")): |
| | | return last |
| | | """ |
| | | 获取可引用的总卖额 |
| | | @param code: |
| | | @param time_str: 当前数据截止时间 |
| | | @return: (time_str, round(money), volume, sell_1_info, sell_n_infos) |
| | | """ |
| | | total_sell_data_cache_list = self.__total_sell_data_cache_list_cache.get(code) |
| | | if total_sell_data_cache_list: |
| | | for i in range(len(total_sell_data_cache_list) - 1, -1, -1): |
| | | sell_data = total_sell_data_cache_list[i] |
| | | if int(time_str.replace(":", "")) > int(sell_data[0].replace(":", "")): |
| | | # 取刚好比当前时间早的数据 |
| | | return sell_data |
| | | return None |
| | | |
| | | |
| | | # 板上卖统计 |
| | | class L2LimitUpSellManager: |
| | | __db = 0 |
| | | __redis_manager = redis_manager.RedisManager(0) |
| | | __instance = None |
| | | __limit_up_sell_cache = {} |
| | | |
| | | def __new__(cls, *args, **kwargs): |
| | | if not cls.__instance: |
| | | cls.__instance = super(L2LimitUpSellManager, cls).__new__(cls, *args, **kwargs) |
| | | cls.__load_datas() |
| | | return cls.__instance |
| | | |
| | | @classmethod |
| | | def __get_redis(cls): |
| | | return cls.__redis_manager.getRedis() |
| | | |
| | | @classmethod |
| | | def __load_datas(cls): |
| | | __redis = cls.__get_redis() |
| | | try: |
| | | keys = RedisUtils.keys(__redis, "limit_up_sell_indexes-*") |
| | | for k in keys: |
| | | code = k.split("-")[-1] |
| | | val = RedisUtils.smembers(__redis, k) |
| | | CodeDataCacheUtil.set_cache(cls.__limit_up_sell_cache, code, val) |
| | | except: |
| | | pass |
| | | finally: |
| | | RedisUtils.realse(__redis) |
| | | |
| | | def add_limit_up_sell(self, code, index): |
| | | if code not in self.__limit_up_sell_cache: |
| | | self.__limit_up_sell_cache[code] = set() |
| | | self.__limit_up_sell_cache[code].add(index) |
| | | RedisUtils.sadd_async(self.__db, f"limit_up_sell_indexes-{code}", index) |
| | | RedisUtils.expire_async(self.__db, f"limit_up_sell_indexes-{code}", tool.get_expire()) |
| | | |
| | | def remove_limit_up_sell(self, code, index): |
| | | if code in self.__limit_up_sell_cache: |
| | | self.__limit_up_sell_cache[code].discard(index) |
| | | RedisUtils.srem_async(self.__db, f"limit_up_sell_indexes-{code}", index) |
| | | |
| | | def clear(self): |
| | | self.__limit_up_sell_cache.clear() |
| | | keys = RedisUtils.keys(self.__get_redis(), "limit_up_sell_indexes-*") |
| | | for k in keys: |
| | | RedisUtils.delete_async(self.__db, k) |
| | | |
| | | # 设置当前的总卖 |
| | | def get_limit_up_sell_indexes(self, code): |
| | | if code in self.__limit_up_sell_cache: |
| | | return self.__limit_up_sell_cache[code] |
| | | return set() |