|
# 市场行情等级管理
|
from db import redis_manager_delegate as redis_manager
|
from db.redis_manager_delegate import RedisUtils
|
from utils import tool
|
|
|
class MarketSituationManager:
|
# 差
|
SITUATION_BAD = 0
|
# 一般
|
SITUATION_COMMON = 1
|
# 好
|
SITUATION_GOOD = 2
|
|
__db = 2
|
redisManager = redis_manager.RedisManager(2)
|
__situation_cache = SITUATION_BAD
|
__instance = None
|
|
def __new__(cls, *args, **kwargs):
|
if not cls.__instance:
|
cls.__instance = super(MarketSituationManager, cls).__new__(cls, *args, **kwargs)
|
# 初始化设置
|
# 获取交易窗口的锁
|
cls.__instance.__situation_cache = cls.get_situation()
|
return cls.__instance
|
|
@classmethod
|
def __get_redis(cls):
|
return cls.redisManager.getRedis()
|
|
def set_situation(self, situation):
|
if situation not in [self.SITUATION_BAD, self.SITUATION_COMMON, self.SITUATION_GOOD]:
|
raise Exception("situation参数值错误")
|
self.__situation_cache = situation
|
RedisUtils.setex_async(self.__db, "market_situation", tool.get_expire(), situation)
|
|
# 是否可以下单
|
@classmethod
|
def get_situation(cls):
|
# 默认设置为可交易
|
val = RedisUtils.get(cls.__get_redis(), "market_situation")
|
if val is None:
|
return cls.SITUATION_BAD
|
return int(val)
|
|
def get_situation_cache(self):
|
return self.__situation_cache
|
|
|
class TradeBlockBuyModeManager:
|
# 第一位:保留 第二位:保留 第三位:买独苗 第四位:传统
|
# 0bxxxx
|
# 传统模式
|
MODE_TRADITIONAL = 0b0001
|
# 独苗模式
|
MODE_UNIQUE_BLOCK = 0b0010
|
|
__REDIS_KEY = "trade_block_buy_mode"
|
__mode = int(MODE_TRADITIONAL)
|
|
"""
|
板块买入模式管理
|
"""
|
__instance = None
|
__db = 2
|
redisManager = redis_manager.RedisManager(2)
|
|
def __new__(cls, *args, **kwargs):
|
if not cls.__instance:
|
cls.__instance = super(TradeBlockBuyModeManager, cls).__new__(cls, *args, **kwargs)
|
return cls.__instance
|
|
@classmethod
|
def __load_data(cls):
|
val = cls.__get_redis().get(cls.__REDIS_KEY)
|
if val is not None:
|
cls.__mode = int(val)
|
|
@classmethod
|
def __get_redis(cls):
|
return cls.redisManager.getRedis()
|
|
def __set_mode(self, mode: int):
|
self.__mode = mode
|
RedisUtils.setex_async(self.__db, self.__REDIS_KEY, tool.get_expire(), mode)
|
|
def get_mode(self):
|
return self.__mode
|
|
def add_unique_block(self):
|
mode = int(self.__mode | self.MODE_UNIQUE_BLOCK)
|
self.__set_mode(mode)
|
|
def remove_unique_block(self):
|
mode = int(self.__mode & ~self.MODE_UNIQUE_BLOCK)
|
self.__set_mode(mode)
|
|
def can_buy_unique_block(self):
|
r = self.__mode & self.MODE_UNIQUE_BLOCK > 1
|
return r > 0
|