Affichage graphique

This commit is contained in:
Jérôme Delacotte
2025-05-16 17:47:05 +02:00
parent c5632a0710
commit b317e382f6
2 changed files with 139 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
import pandas as pd
import plotly.graph_objects as go
from jinja2 import Template
import json
import datetime
# === CONFIGURATION ===
SUMMARY_FILE = "user_data/backtest_results/result.json" # chemin vers ton JSON
BALANCE_HISTORY_FILE = "user_data/backtest_results/backtest_result.json" # ou fichier contenant le portefeuille
# === CHARGEMENT DES DONNÉES ===
with open(SUMMARY_FILE, "r") as f:
summary = json.load(f)
df_summary = pd.DataFrame([summary])
start = summary.get("backtest_start", "")
end = summary.get("backtest_end", "")
final_balance = summary.get("final_balance", 0)
# === AFFICHAGE DES ÉVOLUTIONS DE BALANCE ===
result = pd.read_json(BALANCE_HISTORY_FILE)
result["date"] = pd.to_datetime(result["date"])
balance_trace = go.Scatter(
x=result["date"],
y=result["balance"],
mode="lines",
name="Balance",
line=dict(color="green")
)
layout = go.Layout(
title="Évolution du portefeuille",
xaxis_title="Date",
yaxis_title="Balance (USDT)",
template="plotly_white"
)
fig = go.Figure(data=[balance_trace], layout=layout)
fig.write_html("balance_plot.html", include_plotlyjs="cdn")
# === GÉNÉRATION DU RAPPORT HTML ===
template_str = """
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Rapport de Backtest</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
table { border-collapse: collapse; width: 80%; margin-bottom: 40px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
h1, h2 { color: #333; }
</style>
</head>
<body>
<h1>Rapport de Backtest</h1>
<h2>Période : {{ start }} → {{ end }}</h2>
<table>
<tr><th>Métrique</th><th>Valeur</th></tr>
{% for key, value in metrics.items() %}
<tr><td>{{ key }}</td><td>{{ value }}</td></tr>
{% endfor %}
</table>
<h2>Évolution de la balance</h2>
<iframe src="balance_plot.html" width="1000" height="600" frameborder="0"></iframe>
</body>
</html>
"""
metrics = {
"Trading Mode": summary.get("strategy", ""),
"Total Trades": summary.get("total_trades", 0),
"Final Balance": f"{final_balance:.2f} USDT",
"Profit (%)": f"{summary.get('profit_total_pct', 0):.2f}%",
"CAGR": f"{summary.get('cagr', 0):.2f}%",
"Sharpe": summary.get("sharpe", ""),
"Sortino": summary.get("sortino", ""),
"SQN": summary.get("sqn", ""),
"Calmar": summary.get("calmar", ""),
"Best Pair": summary.get("best_pair", ""),
"Worst Pair": summary.get("worst_pair", ""),
"Max Drawdown": summary.get("max_drawdown", ""),
}
template = Template(template_str)
html_content = template.render(start=start, end=end, metrics=metrics)
with open("backtest_report.html", "w") as f:
f.write(html_content)
print("✅ Rapport généré : backtest_report.html")

View File

@@ -0,0 +1,47 @@
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
# Labels
x_labels = ['B5', 'B4', 'B3', 'B2', 'B1', 'N0', 'H1', 'H2', 'H3', 'H4', 'H5']
y_labels = ['B5', 'B4', 'B3', 'B2', 'B1', 'N0', 'H1', 'H2', 'H3', 'H4', 'H5']
# Data
data = np.array([
[40.3, 52.1, 60.2, 68.6, 86.3, 76.5, 75.1, 83.5, 88.7, 96.3, 91.6],
[26.6, 39.4, 48.1, 57.0, 76.7, 82.4, 79.6, 82.4, 91.8, 86.6, 87.8],
[21.5, 27.7, 42.7, 53.2, 70.9, 76.6, 80.8, 79.4, 88.3, 88.0, 87.8],
[15.1, 20.8, 32.9, 46.9, 59.1, 79.6, 82.5, 79.6, 80.8, 87.0, 85.5],
[15.7, 15.4, 21.9, 29.4, 48.3, 66.6, 76.4, 77.8, 80.8, 83.5, 81.4],
[15.0, 10.5, 20.1, 24.5, 36.9, 59.9, 68.8, 74.1, 77.7, 83.0, 75.7],
[14.8, 10.7, 15.1, 21.0, 30.1, 47.3, 59.2, 70.4, 76.1, 82.7, 82.6],
[7.9, 8.6, 13.6, 20.6, 27.0, 39.5, 55.2, 68.9, 69.0, 78.4, 83.4],
[9.2, 6.2, 12.6, 21.7, 23.6, 33.1, 42.3, 57.8, 66.0, 71.9, 81.9],
[4.8, 13.1, 16.3, 14.5, 19.5, 26.4, 35.6, 49.2, 63.2, 68.2, 71.6],
[17.9, 25.7, 20.8, 17.8, 8.7, 18.5, 32.3, 37.7, 49.3, 59.8, 61.7]
])
# Meshgrid for 3D plotting
x = np.arange(len(x_labels))
y = np.arange(len(y_labels))
x, y = np.meshgrid(x, y)
# Plot
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(x, y, data, cmap='viridis', edgecolor='k')
# Axis formatting
ax.set_xticks(np.arange(len(x_labels)))
ax.set_yticks(np.arange(len(y_labels)))
ax.set_xticklabels(x_labels)
ax.set_yticklabels(y_labels)
ax.set_xlabel("mid_smooth_deriv1_144_bin")
ax.set_ylabel("sma144_diff_bin")
ax.set_zlabel("Probabilité de hausse (%)")
ax.set_title("Probabilité de hausse pour futur_percent_3h en 3D")
fig.colorbar(surf, shrink=0.5, aspect=10)
plt.tight_layout()
plt.show()