Administrator
2024-06-27 f08cb61eb22a7c006c84fe57a2e857e2b23b061c
utils/init_data_util.py
@@ -1,4 +1,3 @@
# 设置收盘价
import decimal
@@ -7,9 +6,9 @@
from utils import tool
def re_set_price_pre(code):
def re_set_price_pre(code, force=False):
    codes = [code]
    re_set_price_pres(codes)
    re_set_price_pres(codes, force=force)
def re_set_price_pres(codes, force=False):
@@ -20,6 +19,7 @@
        pre_close = tool.to_price(decimal.Decimal(str(item['pre_close'])))
        gpcode_manager.CodePrePriceManager.set_price_pre(symbol, pre_close, force)
# 获取近90天的最大量与最近的量
# 获取最近一次涨停/涨停下一个交易日的最大值
def get_volumns_by_code(code, count=60) -> object:
@@ -29,20 +29,72 @@
    return datas
# 解析最大量
def parse_max_volume(datas, is_new_top=False):
    max_volume = 0
def parse_max_volume(code,  datas, is_new_or_near_top=False):
    result = __parse_max_volume(code, datas, is_new_or_near_top)
    refer_index = result[3]
    # 计算最低价
    refer_price = datas[refer_index]["high"]
    min_price = float(refer_price)
    for i in range(0, refer_index + 1):
        if min_price > datas[i]["low"]:
            min_price = datas[i]["low"]
    if (refer_price - min_price) / refer_price < 0.4:
        return result
    # 超跌
    new_datas = []
    for i in range(0, refer_index):
        # 获取涨幅
        item = datas[i]
        rate = (item["low"] - item["pre_close"]) / item["pre_close"]
        new_datas.append((i, rate))
    new_datas.sort(key=lambda x: x[1])
    refer_index = new_datas[0][0]
    # 获取当前天和后一天较大量
    if refer_index > 0:
        if datas[refer_index - 1]["volume"] > datas[refer_index]["volume"]:
            refer_index -= 1
    return datas[refer_index]["volume"], datas[refer_index]["volume"], datas[refer_index]['bob'].strftime(
        "%Y-%m-%d"), refer_index
# 返回:(60天最大量,昨日量,量参考日期,参考量据今交易日数)
def __parse_max_volume(code, datas, is_new_or_near_top=False):
    max_volume = 0
    max_volume_date = None
    if is_new_top:
    max_volume_index = None
    # 判断30天内是否有涨停
    if is_new_or_near_top:
        # 30天内是否有涨停
        latest_limit_up_index = None
        for i in range(30):
            if i >= len(datas):
                break
            item = datas[i]
            limit_up_price = float(gpcode_manager.get_limit_up_price_by_preprice(code, item["pre_close"]))
            if abs(limit_up_price - item["high"]) < 0.001:
                latest_limit_up_index = i
                break
        if latest_limit_up_index is not None:
            # 突破前高或者接近前高,30个交易日内有涨停
            if latest_limit_up_index > 0 and datas[latest_limit_up_index - 1]["volume"] > datas[latest_limit_up_index][
                "volume"]:
                return datas[latest_limit_up_index - 1]["volume"], datas[latest_limit_up_index - 1]["volume"], \
                       datas[latest_limit_up_index - 1]['bob'].strftime("%Y-%m-%d"), latest_limit_up_index - 1
            else:
                return datas[latest_limit_up_index]["volume"], datas[latest_limit_up_index]["volume"], \
                       datas[latest_limit_up_index]['bob'].strftime("%Y-%m-%d"), latest_limit_up_index
    if is_new_or_near_top:
        # 如果是突破前高就取最大量
        for item in datas:
        for i in range(len(datas)):
            item = datas[i]
            if max_volume < item["volume"]:
                max_volume = item["volume"]
                max_volume_date = item["bob"]
        return max_volume, max_volume, max_volume_date.strftime("%Y-%m-%d")
                max_volume_index = i
        return max_volume, max_volume, max_volume_date.strftime("%Y-%m-%d"), max_volume_index
    else:
        date = None
        target_volume = None
        for i in range(len(datas)):
@@ -53,8 +105,9 @@
                max_volume = volume
                max_volume_date = item['bob']
            # 是否有涨停
            limit_up_price = float(gpcode_manager.get_limit_up_price_by_preprice(item["pre_close"]))
            if abs(limit_up_price - item["high"]) < 0.01:
            limit_up_price = float(gpcode_manager.get_limit_up_price_by_preprice(code, item["pre_close"]))
            # 不看超过60天的涨停
            if abs(limit_up_price - item["high"]) < 0.001 and i <= 59:
                # 涨停
                next_volume = 0
                if i > 0:
@@ -63,30 +116,19 @@
                if volume < next_volume:
                    volume = next_volume
                    date = datas[i - 1]["bob"]
                target_volume = (volume, date)
                target_volume = (volume, date, i)
                break
        # 90个交易日无涨停,取最近30天内的最高量作为参考量
        if not target_volume:
            target_volume = (max_volume, max_volume_date)
        # --判断近60天无涨停的最大量
        max_60_volume_info = [0, None]
        # 60天内是否有涨停
        has_60_limit_up = False
        for i in range(60):
            if i >= len(datas):
                break
            item = datas[i]
            volume = item["volume"]
            if max_60_volume_info[0] < volume:
                max_60_volume_info = [volume, item["bob"]]
            limit_up_price = float(gpcode_manager.get_limit_up_price_by_preprice(item["pre_close"]))
            if abs(limit_up_price - item["high"]) < 0.01:
                has_60_limit_up = True
                break
        if not has_60_limit_up and target_volume[0] > max_60_volume_info[0] * 3:
            # 60天内无涨停,且60天内最大量小于最大量的1/3,判断为地量,返回近60个交易日的最大量
            return max_60_volume_info[0], max_60_volume_info[0], max_60_volume_info[1].strftime("%Y-%m-%d")
        else:
            return target_volume[0], target_volume[0], target_volume[1].strftime("%Y-%m-%d")
            # --判断近30天无涨停的最大量
            max_30_volume_info = [0, None]
            for i in range(30):
                if i >= len(datas):
                    break
                item = datas[i]
                volume = item["volume"]
                if max_30_volume_info[0] < volume:
                    max_30_volume_info = [volume, item["bob"], i]
            target_volume = max_30_volume_info
        return target_volume[0], target_volume[0], target_volume[1].strftime("%Y-%m-%d"), target_volume[2]