"""
|
成交量管理
|
"""
|
|
# 设置历史量
|
# max60 60天最大量
|
# yesterday 昨天的量
|
import json
|
|
import global_util
|
import gpcode_manager
|
from db import redis_manager
|
import tool
|
from log import logger_day_volumn
|
|
__redis_manager = redis_manager.RedisManager(0)
|
|
|
# 设置历史量
|
def set_histry_volumn(code, max60, yesterday, max60_day=''):
|
redis = __redis_manager.getRedis()
|
global_util.max60_volumn[code] = max60
|
global_util.yesterday_volumn[code] = yesterday
|
redis.setex("volumn_max60-{}".format(code), tool.get_expire(), json.dumps((max60,max60_day)))
|
redis.setex("volumn_yes-{}".format(code), tool.get_expire(), yesterday)
|
|
|
# 获取历史量
|
def get_histry_volumn(code):
|
max60 = global_util.max60_volumn.get(code)
|
yesterday = global_util.yesterday_volumn.get(code)
|
redis = __redis_manager.getRedis()
|
if max60 is None:
|
max60 = redis.get("volumn_max60-{}".format(code))
|
if max60:
|
max60 = json.loads(max60)
|
if yesterday is None:
|
yesterday = redis.get("volumn_yes-{}".format(code))
|
return max60, yesterday
|
|
|
# 设置今日量
|
def set_today_volumn(code, volumn):
|
logger_day_volumn.info("code:{} volumn:{}".format(code, volumn))
|
redis = __redis_manager.getRedis()
|
global_util.today_volumn[code] = volumn
|
redis.setex("volumn_today-{}".format(code), tool.get_expire(), volumn)
|
|
|
# 获取今日量
|
def get_today_volumn(code):
|
_volumn = global_util.today_volumn.get(code)
|
redis = __redis_manager.getRedis()
|
if _volumn is None:
|
_volumn = redis.get("volumn_today-{}".format(code))
|
return _volumn
|
|
|
# 获取量比(今日量/max(60天最大量,昨日量))
|
def get_volume_rate(code, with_info=False):
|
today = get_today_volumn(code)
|
max60, yesterday = get_histry_volumn(code)
|
if today is None or max60 is None or yesterday is None:
|
raise Exception("获取量失败")
|
rate = round(int(today) / max(int(max60[0]), int(yesterday)), 2)
|
if not with_info:
|
return rate
|
return rate, (today, max(int(max60[0]), int(yesterday)))
|
|
|
# 获取量比索引
|
def get_volume_rate_index(volume_rate):
|
rates = [0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6]
|
for index in range(0, len(rates)):
|
if volume_rate <= rates[index]:
|
return index
|
return len(rates) - 1
|
|
|
# 保存今日量
|
def save_today_volumn(code, volumn, volumnUnit):
|
_volumn = None
|
if volumnUnit == 0:
|
_volumn = round(float(volumn) * 100000000)
|
elif volumnUnit == 1:
|
_volumn = round(float(volumn) * 10000)
|
elif volumnUnit == 2:
|
_volumn = int(volumn)
|
if _volumn is not None:
|
set_today_volumn(code, _volumn * 100)
|
|
|
# 将量从数据库加入内存
|
def load():
|
redis = __redis_manager.getRedis()
|
keys = redis.keys("volumn_max60-*")
|
if keys is not None:
|
for k in keys:
|
code = k.split("-")[1]
|
max60_volumn = redis.get(k)
|
if max60_volumn:
|
max60_volumn = json.loads(max60_volumn)
|
global_util.max60_volumn[code] = max60_volumn
|
keys = redis.keys("volumn_yes-*")
|
if keys is not None:
|
for k in keys:
|
code = k.split("-")[1]
|
global_util.yesterday_volumn[code] = redis.get(k)
|
|
|
if __name__ == "__main__":
|
print(get_histry_volumn("603717"))
|