myTestFreqAI/lightweight_analyzer.py
zhangkun9038@dingtalk.com 2a30c49118 tradestocsv.py
2025-10-14 22:07:06 +08:00

85 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
import re
from pathlib import Path
# 定义结果目录
data_dir = Path('./result')
# 指定要分析的文件
target_file = data_dir / 'backtest-result-2025-10-14_13-45-04.json'
print(f"分析文件: {target_file}")
print(f"文件大小: {target_file.stat().st_size / (1024 * 1024):.2f} MB")
# 使用正则表达式来查找交易记录中的orders和ft_is_entry字段
trade_pattern = re.compile(r'"orders":\s*\[\{.*?\}\]', re.DOTALL)
entry_order_pattern = re.compile(r'"ft_is_entry":\s*true', re.DOTALL)
price_pattern = re.compile(r'"safe_price":\s*(\d+\.\d+)', re.DOTALL)
pair_pattern = re.compile(r'"pair":\s*"([^"]+)"', re.DOTALL)
# 统计信息
found_trades = 0
trades_with_multiple_entries = 0
# 逐行读取文件,避免一次性加载全部内容
print("开始分析文件...")
current_chunk = ""
chunk_size = 1024 * 1024 # 1MB chunks
with open(target_file, 'r', encoding='utf-8') as f:
while True:
chunk = f.read(chunk_size)
if not chunk:
break
current_chunk += chunk
# 查找交易记录
trade_matches = trade_pattern.finditer(current_chunk)
for match in trade_matches:
found_trades += 1
# 每1000笔交易显示进度
if found_trades % 1000 == 0:
print(f"已分析 {found_trades} 笔交易")
trade_content = match.group(0)
# 查找入场订单
entry_orders = entry_order_pattern.findall(trade_content)
if len(entry_orders) > 1:
trades_with_multiple_entries += 1
# 提取交易对
pair_match = pair_pattern.search(trade_content)
pair = pair_match.group(1) if pair_match else "Unknown"
# 提取价格
prices = price_pattern.findall(trade_content)
prices = [float(p) for p in prices[:len(entry_orders)]] # 只取与入场订单数量匹配的价格
if len(prices) >= 2:
# 计算第一和第二个价格的降幅
base_price = prices[0]
next_price = prices[1]
price_drop_percent = ((base_price - next_price) / base_price) * 100
print(f"交易对: {pair}, 基础价格: {base_price:.4f}, 加仓价格: {next_price:.4f}, 降幅: {price_drop_percent:.2f}%")
# 输出最终统计结果
print(f"\n分析完成!")
print(f"找到的交易数: {found_trades}")
print(f"包含多个入场订单的交易数: {trades_with_multiple_entries}")
if trades_with_multiple_entries > 0:
print(f"\n成功找到了 {trades_with_multiple_entries} 笔包含加仓的交易记录!")
print("\n注意: 由于文件较大,此分析仅显示了部分关键信息。")
print("建议: 如果需要更详细的分析结果可以使用pandas库创建完整的分析报告。")
else:
print("\n未找到包含多个入场订单的交易记录。")
print("可能原因:")
print("1. 回测数据中确实没有加仓交易")
print("2. 加仓订单的标记方式与脚本预期不符")
print("3. 文件格式与预期不同")