Administrator
2024-07-12 936ac7f6a724cfd166f8fa6eee483a7e2a20d98f
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
import datetime
import fileinput
import hashlib
import json
import logging
import os
import shutil
import time
 
from code_attribute import gpcode_manager
from utils import tool, constant
 
 
class LogUtil:
    @classmethod
    def extract_log_from_key(cls, key, path, target_path):
        fw = open(target_path, mode='w', encoding="utf-8")
        try:
            with open(path, 'r', encoding="utf-8") as f:
                lines = f.readlines()
                for line in lines:
                    if line.find("{}".format(key)) > 0:
                        fw.write(line)
        finally:
            fw.close()
 
 
# 获取日志时间
def __get_log_time(line):
    time_ = line.split("|")[0].split(" ")[1].split(".")[0]
    return time_
 
 
def __get_async_log_time(line):
    line = line.split(" - ")[1]
    time_str = line[line.find("[") + 1:line.find("[") + 9]
    return time_str
 
 
__log_file_contents = {}
 
 
# 加载文件内容
def __load_file_content(path_str, expire_timespace=20):
    md5 = hashlib.md5(path_str.encode(encoding='utf-8')).hexdigest()
    if md5 in __log_file_contents and time.time() - __log_file_contents[md5][0] < expire_timespace:
        return __log_file_contents[md5][1]
    contents = []
    if os.path.exists(path_str):
        with open(path_str, 'r', encoding="utf-8") as f:
            lines = f.readlines()
            for line in lines:
                contents.append(line)
    __log_file_contents[md5] = (time.time(), contents)
    return contents
 
 
# 加载买入得分记录
def load_latest_market_info(date=tool.get_now_date_str()):
    path = f"{constant.get_path_prefix()}/{constant.LOG_DIR}/huaxin_local/l2/market.{date}.log"
    fdatas = {}
    MAX_LINE = 5000
    with open(path, 'r') as file:
        file.seek(0, 2)
        end_position = file.tell()
        lines_to_read = MAX_LINE + 1  # 包括最后一行的换行符
 
        lines = []
        while len(lines) < lines_to_read and end_position > 0:
            # 向前移动一个字符
            end_position -= 1
            file.seek(end_position)
            # 如果字符是换行符,说明到达一行的末尾
            if file.read(1) == '\n':
                line = file.readline()
                if line:
                    lines.append(line)
 
    lines.reverse()
    for line in lines:
        start_index = line.find("]")
        line = line[start_index + 1:].strip()
        data = eval(line)
        fdatas[data[0]] = data
    return fdatas
 
 
# 加载买入得分记录
def load_market_info(date=tool.get_now_date_str()):
    path = f"{constant.get_path_prefix()}/{constant.LOG_DIR}/huaxin_local/l2/market.{date}.log"
    fdatas = {}
    with open(path, 'r') as f:
        lines = f.readlines()
        for line in lines:
            start_index = line.find("]")
            line = line[start_index + 1:].strip()
            data = eval(line)
            code = data[0]
            if code not in fdatas:
                fdatas[code] = []
            fdatas[code].append(data)
    return fdatas
 
 
def load_transactions(date=tool.get_now_date_str()):
    path = f"{constant.get_path_prefix()}/{constant.LOG_DIR}/huaxin_local/l2/transaction.{date}.log"
    fdatas = []
    with open(path, 'r') as file:
        lines = file.readlines()
        for line in lines:
            start_index = line.find("]")
            if start_index < 0:
                continue
            line = line[start_index + 1:].strip()
            data = eval(line)
            fdatas.append(data)
    return fdatas
 
 
def load_big_buy_order(date=tool.get_now_date_str()):
    """
    获取大买单
    :param date:
    :return:
    """
    path = f"{constant.get_path_prefix()}/{constant.LOG_DIR}/huaxin/l2/transaction_big_buy.{date}.log"
    fdatas = {}
    with open(path, 'r') as file:
        lines = file.readlines()
        for line in lines:
            start_index = line.find("]")
            if start_index < 0:
                continue
            line = line[start_index + 1:].strip()
            ds = line.split("#")
            code = ds[0].strip()
            data = eval(ds[1])
            if code not in fdatas:
                fdatas[code] = []
            fdatas[code].append(data)
    return fdatas
 
 
def load_kpl_blocks(date=tool.get_now_date_str()):
    fdatas = {}
    path = f"{constant.get_path_prefix()}/{constant.LOG_DIR}/gp/kpl/kpl_blocks.{date}.log"
    with open(path, 'r', encoding='utf-8') as file:
        lines = file.readlines()
        for line in lines:
            content = line[line.find(']') + 1:]
            ds = content.split("#")
            code = ds[0].strip()
            data_str = ds[1].strip()
            blocks = eval(data_str)
            fdatas[code] = blocks
    return fdatas
 
 
def load_order_ref_strategy(date=tool.get_now_date_str()):
    """
    加载下单的order_ref对应的策略
    :param date:
    :return:
    """
    fdatas = {}
    path = f"{constant.get_path_prefix()}/{constant.LOG_DIR}/gp/trade/trade.{date}.log"
    with open(path, 'r', encoding='utf-8') as file:
        lines = file.readlines()
        for line in lines:
            if line.find("可转载策略下单结果") > -1:
                line = line.split("可转载策略下单结果:")[1]
                data = eval(line)
                fdatas[data[0]] = data[1]
    return fdatas
 
 
def load_first_limit_up_time(date=tool.get_now_date_str()):
    """
    加载下单的order_ref对应的策略
    :param date:
    :return:
    """
    fdatas = {}
    path = f"{constant.get_path_prefix()}/{constant.LOG_DIR}/gp/market/first_limit_up_time.{date}.log"
    with open(path, 'r', encoding='utf-8') as file:
        lines = file.readlines()
        for line in lines:
            if line.find("]") > -1:
                line = line[line.find("]") + 1:]
                data = eval(line)
                fdatas[data[0]] = data[1]
    return fdatas
 
 
if __name__ == "__main__":
    fdatas = load_order_ref_strategy()
    print(fdatas)
 
    pass