"""
|
股票数据工具
|
"""
|
|
# 股票代码相关的参数
|
import decimal
|
import time
|
|
from code_attribute import gpcode_manager
|
from db import mysql_data_delegate as mysql_data, redis_manager_delegate as redis_manager
|
from db.mysql_data_delegate import Mysqldb
|
from db.redis_manager_delegate import RedisUtils
|
from utils import tool
|
from utils.tool import async_call
|
|
__db = 0
|
_redisManager = redis_manager.RedisManager(0)
|
|
|
# 代码对应的价格是否正确
|
def is_same_code_with_price(code, price):
|
# 昨日收盘价
|
price_close = gpcode_manager.CodePrePriceManager.get_price_pre_cache(code)
|
max_price = tool.to_price(decimal.Decimal(str(price_close)) * decimal.Decimal(tool.get_limit_up_rate(code)))
|
min_price = tool.to_price(decimal.Decimal(str(price_close)) * decimal.Decimal(tool.get_limit_down_rate(code)))
|
if min_price <= decimal.Decimal(str(price)) <= max_price:
|
return True
|
return False
|
|
|
# 自由流通股本工具类
|
class ZYLTGBUtil:
|
__db = 0
|
__mysql = Mysqldb()
|
|
@classmethod
|
def save(cls, code, val, unit):
|
RedisUtils.setex(_redisManager.getRedis(), "zyltgb-{}".format(code), tool.get_expire(),
|
round(float(val) * 100000000) if int(unit) == 0 else round(
|
float(val) * 10000))
|
|
@classmethod
|
def save_async(cls, code, val, price):
|
"""
|
异步保存自由流通股本
|
@param code:
|
@param val:
|
@param price:
|
@return:
|
"""
|
RedisUtils.setex_async(cls.__db, "zyltgb-{}".format(code), tool.get_expire(),
|
val)
|
if price > 0:
|
zylt_volume = int(int(val) / float(price))
|
cls.save_volume(code, zylt_volume)
|
|
@classmethod
|
def save_volume(cls, code, zylt_volume):
|
result = cls.__mysql.select_one(f"select * from kpl_zylt_volume where code = {code}")
|
if result:
|
cls.__mysql.execute(
|
f"update kpl_zylt_volume set zylt_volume = {zylt_volume}, update_time=now() where code = {code}")
|
else:
|
cls.__mysql.execute(
|
f"insert into kpl_zylt_volume(code, zylt_volume, create_time, update_time) values ('{code}',{zylt_volume},now(),now())")
|
|
@classmethod
|
def get_today_updated_volume_codes(cls):
|
"""
|
获取今日已经更新量的代码
|
@return:
|
"""
|
fresults = cls.__mysql.select_all(f"select code from kpl_zylt_volume where update_time >= '{tool.get_now_date_str()}'")
|
if fresults:
|
return [x[0] for x in fresults]
|
return []
|
|
@classmethod
|
def count_today_updated_volume_codes(cls):
|
"""
|
查询今日已经更新量的个数
|
@return:
|
"""
|
fresults = cls.__mysql.select_one(f"select count(code) from kpl_zylt_volume where update_time >= '{tool.get_now_date_str()}'")
|
if fresults:
|
return fresults[0]
|
return 0
|
|
|
|
@classmethod
|
def get(cls, code):
|
val = RedisUtils.get(_redisManager.getRedis(), "zyltgb-{}".format(code))
|
if val is not None:
|
return int(val)
|
return None
|
|
|
if __name__ == "__main__":
|
#ZYLTGBUtil.save_async("000333", 256222112, 65.54)
|
print(ZYLTGBUtil.count_today_updated_volume_codes())
|