Administrator
2024-05-29 c18a3c3d1e319f6ba3f3ab6a99bea7d1d7030130
获取最近的市场行情
1个文件已添加
2个文件已修改
120 ■■■■■ 已修改文件
code_attribute/code_market_manager.py 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
log_module/log_export.py 93 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
main.py 23 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
code_attribute/code_market_manager.py
@@ -20,12 +20,14 @@
        self.pre_close_price = pre_close_price
def set_market_info(data):
def set_market_info(data, with_log=True):
    """
    设置行情信息
    :param with_log: 是否写入日志
    :param data: (代码, 最近的价格, 涨幅, 买1价, 买1量, 成交总量, 买入量, 卖出量, 昨日收盘价)
    :return:
    """
    if with_log:
    async_log_util.info(logger_local_huaxin_l2_market, f"{data}")
    __market_info_dict[data[0]] = MarketInfo(data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8])
log_module/log_export.py
New file
@@ -0,0 +1,93 @@
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
if __name__ == "__main__":
    fdatas = load_latest_market_info()
    print(fdatas)
    pass
main.py
@@ -10,7 +10,7 @@
from code_attribute import target_codes_manager, gpcode_manager, code_market_manager, history_k_data_util
from huaxin_client import l2_client_for_cb, trade_client_for_cb
from huaxin_client.client_network import SendResponseSkManager
from log_module import async_log_util
from log_module import async_log_util, log_export
from records import huaxin_trade_record_manager
from trade import huaxin_trade_api, huaxin_trade_data_update, huaxin_sell_util
from utils import middle_api_protocol, outside_api_command_manager, constant, tool, huaxin_util, socket_util, sell_util, \
@@ -171,8 +171,10 @@
            # 获取买点与卖点
            buys = huaxin_trade_record_manager.DealRecordManager().list_buy_by_code_cache(cb_code)
            sells = huaxin_trade_record_manager.DealRecordManager().list_sell_by_code_cache(cb_code)
            r["buy_list"] = [{"price": str(x["price"]), "tradeTime": x["tradeTime"], "volume": x["volume"]} for x in buys]
            r["sell_list"] = [{"price": str(x["price"]), "tradeTime": x["tradeTime"], "volume": x["volume"]} for x in sells]
            r["buy_list"] = [{"price": str(x["price"]), "tradeTime": x["tradeTime"], "volume": x["volume"]} for x in
                             buys]
            r["sell_list"] = [{"price": str(x["price"]), "tradeTime": x["tradeTime"], "volume": x["volume"]} for x in
                              sells]
        send_response({"code": 0, "data": results}, client_id, request_id)
    elif type_ == "refresh_trade_data":
        # 刷新交易数据
@@ -251,6 +253,19 @@
            pass
def __init_data():
    """
    初始化参数
    :return:
    """
    try:
        market_dict = log_export.load_latest_market_info()
        for k in market_dict:
            code_market_manager.set_market_info(market_dict[k])
    except Exception as e:
        logger_debug.exception(e)
if __name__ == '__main__':
    # ===========初始化数据==========
    try:
@@ -258,6 +273,8 @@
    except Exception as e:
        logger_debug.exception(e)
    __init_data()
    trade_call_back_queue = multiprocessing.Queue()
    # 华鑫交易数据更新