Administrator
2025-05-12 82e8474625d9af933d6ab5825b43f6a248005010
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# 设置收盘价
import decimal
 
from code_attribute import gpcode_manager
from log_module.log import logger_debug
from third_data.history_k_data_util import HistoryKDatasUtils
from utils import tool
 
 
def re_set_price_pre(code, force=False):
    codes = [code]
    re_set_price_pres(codes, force=force)
 
 
def re_set_price_pres(codes, force=False):
    # 通过历史数据缓存获取
    result = HistoryKDatasUtils.get_gp_latest_info(codes)
    for item in result:
        symbol = item['symbol']
        symbol = symbol.split(".")[1]
        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):
    datas = HistoryKDatasUtils.get_history_tick_n(code, count, "open,high,low,close,volume,pre_close,bob,amount")
    if not datas:
        return None
    # 计算
    datas.sort(key=lambda x: x["bob"], reverse=True)
    return datas
 
 
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
 
 
def parse_max_volume_new(code, datas):
    """
    计算远高量
    @param code:
    @param datas:
    @return: [高量,高量,高量日期,高量索引]
    """
 
    def __is_limited_up(item):
        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:
            return True
        return False
 
    # 取最近60个交易日
    datas = datas[:60]
 
    # 判断是否涨停过
    target_index = None
    for i in range(len(datas)):
        data = datas[i]
        if __is_limited_up(data):
            next_data = None
            if i > 0:
                next_data = datas[i - 1]
            # max(涨停这一天, 后一天)的量
            if next_data and next_data['volume'] > data['volume']:
                target_index = i - 1
            else:
                target_index = i
            break
    if target_index is None:
        # 60天未涨停,获取60天内的最高量
        for i in range(len(datas)):
            data = datas[i]
            if target_index is None:
                target_index = i
            if data['volume'] > datas[target_index]['volume']:
                target_index = i
    return datas[target_index]['volume'], datas[target_index]['volume'], datas[target_index]['bob'].strftime(
        "%Y-%m-%d"), target_index
 
 
def parse_max_volume_in_days(datas, max_day):
    """
    解析最近几天最大的量
    @param datas:
    @param max_day:
    @return:
    """
    # 解析最近几天的最大量
    datas = datas[:max_day]
    max_volume_info = None
    for d in datas:
        if max_volume_info is None:
            max_volume_info = (d["volume"], d)
        if d["volume"] > max_volume_info[0]:
            max_volume_info = (d["volume"], d)
    if max_volume_info:
        return max_volume_info[0]
    return None
 
 
# 返回:(60天最大量,昨日量,量参考日期,参考量据今交易日数)
def __parse_max_volume(code, datas, is_new_or_near_top=False):
    max_volume = 0
    max_volume_date = None
    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 i in range(len(datas)):
            item = datas[i]
            if max_volume < item["volume"]:
                max_volume = item["volume"]
                max_volume_date = item["bob"]
                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)):
            # 查询涨停
            item = datas[i]
            volume = item["volume"]
            if max_volume < volume:
                max_volume = volume
                max_volume_date = item['bob']
            # 是否有涨停
            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:
                    next_volume = datas[i - 1]["volume"]
                date = datas[i]["bob"]
                if volume < next_volume:
                    volume = next_volume
                    date = datas[i - 1]["bob"]
                target_volume = (volume, date, i)
                break
 
        # 90个交易日无涨停,取最近30天内的最高量作为参考量
        if not target_volume:
            # --判断近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]