|
@@ -7,6 +7,10 @@ import json
|
|
|
import re
|
|
import re
|
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
+# 配置matplotlib中文字体显示
|
|
|
|
|
+plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans'] # 用来正常显示中文标签
|
|
|
|
|
+plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
|
|
|
|
|
+
|
|
|
def extract_timestamp(timestamp_str):
|
|
def extract_timestamp(timestamp_str):
|
|
|
"""从时间戳字符串中提取日期时间"""
|
|
"""从时间戳字符串中提取日期时间"""
|
|
|
if isinstance(timestamp_str, tuple):
|
|
if isinstance(timestamp_str, tuple):
|
|
@@ -149,14 +153,10 @@ def analyze_data(df):
|
|
|
|
|
|
|
|
results = {}
|
|
results = {}
|
|
|
|
|
|
|
|
- # 基础统计分析
|
|
|
|
|
|
|
+ # 数值列定义(用于其他分析)
|
|
|
numeric_columns = ['pct', 'openLimit', 'closeLimit', 'cexPrice', 'dexPrice',
|
|
numeric_columns = ['pct', 'openLimit', 'closeLimit', 'cexPrice', 'dexPrice',
|
|
|
'exchangeOutAmount', 'profit', 'price_diff_pct', 'trade_value']
|
|
'exchangeOutAmount', 'profit', 'price_diff_pct', 'trade_value']
|
|
|
|
|
|
|
|
- # 计算基础统计量
|
|
|
|
|
- stats = df[numeric_columns].describe()
|
|
|
|
|
- results['basic_stats'] = stats
|
|
|
|
|
-
|
|
|
|
|
# 按交易对分组统计
|
|
# 按交易对分组统计
|
|
|
if 'symbol' in df.columns:
|
|
if 'symbol' in df.columns:
|
|
|
symbol_stats = df.groupby('symbol')['profit'].agg(['count', 'sum', 'mean', 'std']).reset_index()
|
|
symbol_stats = df.groupby('symbol')['profit'].agg(['count', 'sum', 'mean', 'std']).reset_index()
|
|
@@ -186,11 +186,6 @@ def analyze_data(df):
|
|
|
hourly_stats = df.groupby('hour')['profit'].agg(['count', 'sum', 'mean']).reset_index()
|
|
hourly_stats = df.groupby('hour')['profit'].agg(['count', 'sum', 'mean']).reset_index()
|
|
|
results['hourly_stats'] = hourly_stats
|
|
results['hourly_stats'] = hourly_stats
|
|
|
|
|
|
|
|
- # 相关性分析
|
|
|
|
|
- if len(numeric_columns) > 1:
|
|
|
|
|
- correlation = df[numeric_columns].corr()
|
|
|
|
|
- results['correlation'] = correlation
|
|
|
|
|
-
|
|
|
|
|
return results
|
|
return results
|
|
|
|
|
|
|
|
def visualize_data(df, results, output_dir='analysis_results'):
|
|
def visualize_data(df, results, output_dir='analysis_results'):
|
|
@@ -251,27 +246,6 @@ def visualize_data(df, results, output_dir='analysis_results'):
|
|
|
plt.savefig(os.path.join(output_dir, 'symbol_profit.png'))
|
|
plt.savefig(os.path.join(output_dir, 'symbol_profit.png'))
|
|
|
plt.close()
|
|
plt.close()
|
|
|
|
|
|
|
|
- # 5. 相关性热图
|
|
|
|
|
- if 'correlation' in results:
|
|
|
|
|
- correlation = results['correlation']
|
|
|
|
|
- plt.figure(figsize=(12, 10))
|
|
|
|
|
- plt.imshow(correlation, cmap='coolwarm', vmin=-1, vmax=1)
|
|
|
|
|
- plt.colorbar()
|
|
|
|
|
- plt.title('特征相关性热图')
|
|
|
|
|
- plt.xticks(range(len(correlation.columns)), correlation.columns, rotation=90)
|
|
|
|
|
- plt.yticks(range(len(correlation.columns)), correlation.columns)
|
|
|
|
|
-
|
|
|
|
|
- # 在热图上添加相关系数值
|
|
|
|
|
- for i in range(len(correlation.columns)):
|
|
|
|
|
- for j in range(len(correlation.columns)):
|
|
|
|
|
- plt.text(j, i, f'{correlation.iloc[i, j]:.2f}',
|
|
|
|
|
- ha='center', va='center',
|
|
|
|
|
- color='white' if abs(correlation.iloc[i, j]) > 0.5 else 'black')
|
|
|
|
|
-
|
|
|
|
|
- plt.tight_layout()
|
|
|
|
|
- plt.savefig(os.path.join(output_dir, 'correlation_heatmap.png'))
|
|
|
|
|
- plt.close()
|
|
|
|
|
-
|
|
|
|
|
# 6. 按小时的平均利润
|
|
# 6. 按小时的平均利润
|
|
|
if 'hourly_stats' in results:
|
|
if 'hourly_stats' in results:
|
|
|
hourly_stats = results['hourly_stats']
|
|
hourly_stats = results['hourly_stats']
|
|
@@ -317,28 +291,6 @@ def generate_report(results, output_dir='analysis_results'):
|
|
|
<p>生成时间: """ + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + """</p>
|
|
<p>生成时间: """ + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + """</p>
|
|
|
"""
|
|
"""
|
|
|
|
|
|
|
|
- # 添加基础统计信息
|
|
|
|
|
- if 'basic_stats' in results:
|
|
|
|
|
- html_content += """
|
|
|
|
|
- <h2>基础统计信息</h2>
|
|
|
|
|
- <table>
|
|
|
|
|
- <tr>
|
|
|
|
|
- <th>指标</th>
|
|
|
|
|
- """
|
|
|
|
|
-
|
|
|
|
|
- for col in results['basic_stats'].columns:
|
|
|
|
|
- html_content += f"<th>{col}</th>"
|
|
|
|
|
-
|
|
|
|
|
- html_content += "</tr>"
|
|
|
|
|
-
|
|
|
|
|
- for idx, row in results['basic_stats'].iterrows():
|
|
|
|
|
- html_content += f"<tr><td>{idx}</td>"
|
|
|
|
|
- for col in results['basic_stats'].columns:
|
|
|
|
|
- html_content += f"<td>{row[col]:.4f}</td>"
|
|
|
|
|
- html_content += "</tr>"
|
|
|
|
|
-
|
|
|
|
|
- html_content += "</table>"
|
|
|
|
|
-
|
|
|
|
|
# 添加交易对统计
|
|
# 添加交易对统计
|
|
|
if 'symbol_stats' in results:
|
|
if 'symbol_stats' in results:
|
|
|
html_content += """
|
|
html_content += """
|
|
@@ -467,11 +419,6 @@ def generate_report(results, output_dir='analysis_results'):
|
|
|
<img src="symbol_profit.png" alt="各交易对总利润">
|
|
<img src="symbol_profit.png" alt="各交易对总利润">
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
- <div class="image-container">
|
|
|
|
|
- <h3>特征相关性热图</h3>
|
|
|
|
|
- <img src="correlation_heatmap.png" alt="特征相关性热图">
|
|
|
|
|
- </div>
|
|
|
|
|
-
|
|
|
|
|
<div class="image-container">
|
|
<div class="image-container">
|
|
|
<h3>各小时平均利润</h3>
|
|
<h3>各小时平均利润</h3>
|
|
|
<img src="hourly_avg_profit.png" alt="各小时平均利润">
|
|
<img src="hourly_avg_profit.png" alt="各小时平均利润">
|