From c285883d71ef8a362b012983dadc7ce4256b40f6 Mon Sep 17 00:00:00 2001 From: Administrator <admin@example.com> Date: 星期五, 23 五月 2025 01:52:07 +0800 Subject: [PATCH] bug修复 --- l2_data_parser.py | 137 +++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 130 insertions(+), 7 deletions(-) diff --git a/l2_data_parser.py b/l2_data_parser.py index abdbe66..e96b4ed 100644 --- a/l2_data_parser.py +++ b/l2_data_parser.py @@ -10,6 +10,7 @@ from db import mysql_data_delegate as mysql_data from huaxin_client.l2_client_test import L2TransactionDataManager from log_module import log_export +from utils import tool def __get_target_codes(day): @@ -50,13 +51,6 @@ fdatas = [] df = pd.read_csv(f"{base_path}/Transaction.csv") category_revenue = df.groupby('BuyNo')['TradeVolume'].sum() - total_count = 0 - for chunk in chunks: - total_count += len(chunk) - for chunk in chunks: - total_count += len(chunk) - - with open(f"{base_path}/Transaction.csv", 'r', encoding='utf-8') as file: csv_reader = csv.reader(file) @@ -231,6 +225,131 @@ writer.writerow(row) +def pre_process_transactions(csv_path="E:/娴嬭瘯鏁版嵁/Transaction_Test.csv"): + def str_to_float(s): + try: + # 绉婚櫎鍗曚綅骞惰浆鎹� + return round(float(s.split("@")[0]), 2) + except: + return float("nan") + + def code_format(s): + try: + code = "{0:0>6}".format(s) + return code + except: + return '' + + # [ExchangeID, SecurityID, TradeTime, TradePrice, TradeVolume, ExecType, MainSeq, SubSeq, BuyNo, SellNo, Info1, Info2, + # Info3, TradeBSFlag, BizIndex, LocalTimeStamp] + # transaction_data = { + # "SecurityID": ['300920', '300920', '300920', '300920'], + # "TradeTime": [91500040, 91500041, 91500042, 92000040], + # "TradePrice": [15.0, 16.2, 15.2, 16.3], + # "TradeVolume": [100, 100, 200, 300], + # "BuyNo": [0, 1, 1, 1] + # } + # 瀹氫箟鑱氬悎鍑芥暟 + def first_last(group): + return pd.Series({ + 'TotalAmount': group['TradeAmount'].sum(), + 'TotalVolume': group['TradeVolume'].sum(), + 'StartTime': group['TradeTime'].iloc[0], + 'StartPrice': group['TradePrice'].iloc[0], + 'EndTime': group['TradeTime'].iloc[-1], + 'EndPrice': group['TradePrice'].iloc[-1] + }) + + dtype = { + 'SecurityID': 'category', # 浣庡熀鏁板垎绫绘暟鎹� + } + chunk_size = 100000 + # 鍒涘缓DataFrame + chunks = pd.read_csv(csv_path, chunksize=chunk_size) + result_list = [] + index = 0 + for df in chunks: + index += 1 + child_path = csv_path.replace(".csv", f"_{index}.csv") + if os.path.exists(child_path): + continue + print(f"澶勭悊绗瑊index}鎵规") + df["TradePrice"] = df["TradePrice"].apply(str_to_float) + df["SecurityID"] = df["SecurityID"].apply(code_format) + df = df[df["SecurityID"].str.startswith(("30", "00", "60"), na=False)] + # 璁$畻鎴愪氦閲戦 + df['TradeAmount'] = df['TradePrice'] * df['TradeVolume'] + + + # 鎸塖ecurityID鍜孊uyNo鍒嗙粍 + grouped = df.groupby(['SecurityID', 'BuyNo']) + + # 搴旂敤鑱氬悎鍑芥暟 + chunk_result = grouped.apply(first_last).reset_index() + + chunk_result.to_csv(child_path, index=False) + print(f"澶勭悊瀹屾瘯锛屾�诲叡{index}鎵�") + + +def pre_process_ngtstick(csv_path="E:/娴嬭瘯鏁版嵁/NGTSTick_Test.csv"): + def str_to_float(s): + try: + # 绉婚櫎鍗曚綅骞惰浆鎹� + return round(float(s.split("@")[0]), 2) + except: + return float("nan") + + def code_format(s): + try: + code = "{0:0>6}".format(s) + return code + except: + return '' + + # 瀹氫箟鑱氬悎鍑芥暟 + def first_last(group): + return pd.Series({ + 'TotalAmount': group['TradeMoney'].sum(), + 'TotalVolume': group['Volume'].sum(), + 'StartTime': group['TickTime'].iloc[0], + 'StartPrice': group['Price'].iloc[0], + 'EndTime': group['TickTime'].iloc[-1], + 'EndPrice': group['Price'].iloc[-1] + }) + + # [ExchangeID,SecurityID,MainSeq,SubSeq,TickTime,TickType,BuyNo,SellNo,Price,Volume,TradeMoney,Side,TradeBSFlag,MDSecurityStat,Info1,Info2,Info3,LocalTimeStamp] + + chunk_size = 200000 + # 鍒涘缓DataFrame + chunks = pd.read_csv(csv_path, chunksize=chunk_size) + result_list = [] + index = 0 + for df in chunks: + index += 1 + child_path = csv_path.replace(".csv", f"_{index}.csv") + if os.path.exists(child_path): + continue + print(f"澶勭悊绗瑊index}鎵规") + df = df[df["TickType"] == 'T'] + df["Price"] = df["Price"].apply(str_to_float) + df["SecurityID"] = df["SecurityID"].apply(code_format) + + df = df[df["SecurityID"].str.startswith(("30", "00", "60"), na=False)] + + # 璁$畻鎴愪氦閲戦 + df['TradeMoney'] = df["TradeMoney"].apply(str_to_float) + # 鎸塖ecurityID鍜孊uyNo鍒嗙粍 + grouped = df.groupby(['SecurityID', 'BuyNo']) + # 搴旂敤鑱氬悎鍑芥暟 + chunk_result = grouped.apply(first_last).reset_index() + chunk_result.to_csv(child_path, index=False) + print(f"澶勭悊瀹屾瘯锛屾�诲叡{index}鎵�") + + +if __name__ == '__main__1': + # df = pd.read_csv(f"E:/娴嬭瘯鏁版嵁/Transaction_Test.csv") + pre_process_ngtstick() + # 鍛戒护妯″紡 /home/userzjj/app/gp-server/l2_data_parser Transaction 2025-05-08 # 瑙f瀽澶у崟锛� /home/userzjj/app/gp-server/l2_data_parser ExtractDealBigOrder 2025-05-09 /home/userzjj/鏈�缁堟垚浜ゆ暟鎹�20250509.txt 000555 if __name__ == '__main__': @@ -251,6 +370,10 @@ parse_ngtstick(day) elif _type == 'MarketData': parse_market_data(day) + elif _type == 'Transaction_New': + pre_process_transactions(f"/home/userzjj/ftp/{day}/Transaction.csv") + elif _type == 'NGTSTick_New': + pre_process_ngtstick(f"/home/userzjj/ftp/{day}/NGTSTick.csv") elif _type == 'ExtractDealBigOrder': # 鎻愬彇鎵�鏈夋垚浜ょ殑澶у崟 if len(params) > 2: -- Gitblit v1.8.0