624 lines
21 KiB
HTML
624 lines
21 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>交易记录分析系统</title>
|
|
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
font-family: 'Microsoft YaHei', 'Segoe UI', Tahoma, sans-serif;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
min-height: 100vh;
|
|
padding: 20px;
|
|
}
|
|
|
|
.container {
|
|
max-width: 1600px;
|
|
margin: 0 auto;
|
|
}
|
|
|
|
header {
|
|
background: rgba(255, 255, 255, 0.95);
|
|
backdrop-filter: blur(10px);
|
|
border-radius: 15px;
|
|
padding: 30px;
|
|
margin-bottom: 20px;
|
|
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
|
|
}
|
|
|
|
h1 {
|
|
color: #2d3748;
|
|
font-size: 32px;
|
|
margin-bottom: 15px;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 15px;
|
|
}
|
|
|
|
h1::before {
|
|
content: '📊';
|
|
font-size: 40px;
|
|
}
|
|
|
|
.strategy-selector {
|
|
display: flex;
|
|
gap: 15px;
|
|
align-items: center;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
.strategy-selector label {
|
|
font-weight: 600;
|
|
color: #4a5568;
|
|
font-size: 16px;
|
|
}
|
|
|
|
.strategy-selector select {
|
|
padding: 12px 20px;
|
|
border: 2px solid #e2e8f0;
|
|
border-radius: 8px;
|
|
font-size: 16px;
|
|
background: white;
|
|
cursor: pointer;
|
|
transition: all 0.3s;
|
|
min-width: 250px;
|
|
}
|
|
|
|
.strategy-selector select:hover {
|
|
border-color: #667eea;
|
|
}
|
|
|
|
.strategy-selector select:focus {
|
|
outline: none;
|
|
border-color: #667eea;
|
|
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
|
|
}
|
|
|
|
/* 核心指标卡片 */
|
|
.metrics-grid {
|
|
display: grid;
|
|
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
|
|
gap: 20px;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.metric-card {
|
|
background: white;
|
|
border-radius: 15px;
|
|
padding: 25px;
|
|
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
|
|
transition: transform 0.3s, box-shadow 0.3s;
|
|
position: relative;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.metric-card:hover {
|
|
transform: translateY(-5px);
|
|
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
|
|
}
|
|
|
|
.metric-card::before {
|
|
content: '';
|
|
position: absolute;
|
|
top: 0;
|
|
left: 0;
|
|
width: 100%;
|
|
height: 4px;
|
|
background: linear-gradient(90deg, #667eea, #764ba2);
|
|
}
|
|
|
|
.metric-label {
|
|
font-size: 14px;
|
|
color: #718096;
|
|
margin-bottom: 8px;
|
|
font-weight: 500;
|
|
}
|
|
|
|
.metric-value {
|
|
font-size: 32px;
|
|
font-weight: 700;
|
|
color: #2d3748;
|
|
margin-bottom: 5px;
|
|
}
|
|
|
|
.metric-value.positive {
|
|
color: #48bb78;
|
|
}
|
|
|
|
.metric-value.negative {
|
|
color: #f56565;
|
|
}
|
|
|
|
.metric-sub {
|
|
font-size: 13px;
|
|
color: #a0aec0;
|
|
}
|
|
|
|
/* 图表区域 */
|
|
.charts-grid {
|
|
display: grid;
|
|
grid-template-columns: repeat(auto-fit, minmax(500px, 1fr));
|
|
gap: 20px;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.chart-card {
|
|
background: white;
|
|
border-radius: 15px;
|
|
padding: 25px;
|
|
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
|
|
}
|
|
|
|
.chart-card.full-width {
|
|
grid-column: 1 / -1;
|
|
}
|
|
|
|
.chart-title {
|
|
font-size: 18px;
|
|
font-weight: 600;
|
|
color: #2d3748;
|
|
margin-bottom: 20px;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 10px;
|
|
}
|
|
|
|
.chart-container {
|
|
position: relative;
|
|
height: 350px;
|
|
}
|
|
|
|
/* 交易明细表 */
|
|
.table-card {
|
|
background: white;
|
|
border-radius: 15px;
|
|
padding: 25px;
|
|
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
|
|
overflow-x: auto;
|
|
}
|
|
|
|
.table-controls {
|
|
display: flex;
|
|
gap: 15px;
|
|
margin-bottom: 20px;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
.table-controls input,
|
|
.table-controls select {
|
|
padding: 10px 15px;
|
|
border: 2px solid #e2e8f0;
|
|
border-radius: 8px;
|
|
font-size: 14px;
|
|
}
|
|
|
|
table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
font-size: 14px;
|
|
}
|
|
|
|
thead {
|
|
background: #f7fafc;
|
|
position: sticky;
|
|
top: 0;
|
|
}
|
|
|
|
th {
|
|
padding: 15px 10px;
|
|
text-align: left;
|
|
font-weight: 600;
|
|
color: #4a5568;
|
|
border-bottom: 2px solid #e2e8f0;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
td {
|
|
padding: 12px 10px;
|
|
border-bottom: 1px solid #f7fafc;
|
|
color: #2d3748;
|
|
}
|
|
|
|
tbody tr:hover {
|
|
background: #f7fafc;
|
|
}
|
|
|
|
.action-buy {
|
|
color: #48bb78;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.action-sell {
|
|
color: #f56565;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.pnl-positive {
|
|
color: #48bb78;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.pnl-negative {
|
|
color: #f56565;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.loading {
|
|
text-align: center;
|
|
padding: 50px;
|
|
color: #718096;
|
|
font-size: 18px;
|
|
}
|
|
|
|
.error {
|
|
background: #fed7d7;
|
|
color: #c53030;
|
|
padding: 20px;
|
|
border-radius: 10px;
|
|
margin: 20px 0;
|
|
}
|
|
|
|
@media (max-width: 768px) {
|
|
.charts-grid {
|
|
grid-template-columns: 1fr;
|
|
}
|
|
|
|
h1 {
|
|
font-size: 24px;
|
|
}
|
|
|
|
.metric-value {
|
|
font-size: 24px;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<header>
|
|
<h1>量化交易分析系统</h1>
|
|
<div class="strategy-selector">
|
|
<label for="strategy-select">选择策略:</label>
|
|
<select id="strategy-select" onchange="loadStrategy()">
|
|
<option value="">请选择策略...</option>
|
|
{% for strategy in strategies %}
|
|
<option value="{{ strategy }}">{{ strategy }}</option>
|
|
{% endfor %}
|
|
</select>
|
|
</div>
|
|
</header>
|
|
|
|
<div id="content">
|
|
<div class="loading">请选择策略以查看分析数据</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
let currentData = null;
|
|
|
|
function loadStrategy() {
|
|
const select = document.getElementById('strategy-select');
|
|
const strategyName = select.value;
|
|
|
|
if (!strategyName) {
|
|
document.getElementById('content').innerHTML = '<div class="loading">请选择策略以查看分析数据</div>';
|
|
return;
|
|
}
|
|
|
|
document.getElementById('content').innerHTML = '<div class="loading">加载中...</div>';
|
|
|
|
fetch(`/api/strategy/${strategyName}`)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
currentData = data;
|
|
renderDashboard(data);
|
|
})
|
|
.catch(error => {
|
|
document.getElementById('content').innerHTML = `<div class="error">加载失败: ${error.message}</div>`;
|
|
});
|
|
}
|
|
|
|
function renderDashboard(data) {
|
|
const content = document.getElementById('content');
|
|
|
|
content.innerHTML = `
|
|
<!-- 核心指标 -->
|
|
<div class="metrics-grid">
|
|
<div class="metric-card">
|
|
<div class="metric-label">总收益</div>
|
|
<div class="metric-value ${data.metrics.total_pnl >= 0 ? 'positive' : 'negative'}">
|
|
¥${formatNumber(data.metrics.total_pnl)}
|
|
</div>
|
|
<div class="metric-sub">${data.metrics.total_return >= 0 ? '+' : ''}${data.metrics.total_return}%</div>
|
|
</div>
|
|
|
|
<div class="metric-card">
|
|
<div class="metric-label">交易次数</div>
|
|
<div class="metric-value">${data.metrics.total_trades}</div>
|
|
<div class="metric-sub">盈: ${data.metrics.win_count} / 亏: ${data.metrics.loss_count}</div>
|
|
</div>
|
|
|
|
<div class="metric-card">
|
|
<div class="metric-label">胜率</div>
|
|
<div class="metric-value ${data.metrics.win_rate >= 50 ? 'positive' : 'negative'}">
|
|
${data.metrics.win_rate}%
|
|
</div>
|
|
<div class="metric-sub">盈亏比: ${data.metrics.profit_factor}</div>
|
|
</div>
|
|
|
|
<div class="metric-card">
|
|
<div class="metric-label">平均盈利</div>
|
|
<div class="metric-value positive">¥${formatNumber(data.metrics.avg_win)}</div>
|
|
<div class="metric-sub">最大: ¥${formatNumber(data.metrics.max_win)}</div>
|
|
</div>
|
|
|
|
<div class="metric-card">
|
|
<div class="metric-label">平均亏损</div>
|
|
<div class="metric-value negative">¥${formatNumber(data.metrics.avg_loss)}</div>
|
|
<div class="metric-sub">最大: ¥${formatNumber(data.metrics.max_loss)}</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 图表区域 -->
|
|
<div class="charts-grid">
|
|
<div class="chart-card full-width">
|
|
<div class="chart-title">📈 权益曲线</div>
|
|
<div class="chart-container">
|
|
<canvas id="equityChart"></canvas>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="chart-card">
|
|
<div class="chart-title">📅 月度收益</div>
|
|
<div class="chart-container">
|
|
<canvas id="monthlyChart"></canvas>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="chart-card">
|
|
<div class="chart-title">🎯 股票表现</div>
|
|
<div class="chart-container">
|
|
<canvas id="stockChart"></canvas>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 交易明细 -->
|
|
<div class="table-card">
|
|
<div class="chart-title">📋 交易明细</div>
|
|
<div class="table-controls">
|
|
<input type="text" id="searchInput" placeholder="搜索股票代码..." onkeyup="filterTable()">
|
|
<select id="actionFilter" onchange="filterTable()">
|
|
<option value="">全部交易</option>
|
|
<option value="BUY">买入</option>
|
|
<option value="SELL">卖出</option>
|
|
</select>
|
|
</div>
|
|
<table id="tradesTable">
|
|
<thead>
|
|
<tr>
|
|
<th>日期</th>
|
|
<th>股票</th>
|
|
<th>操作</th>
|
|
<th>价格</th>
|
|
<th>数量</th>
|
|
<th>手续费</th>
|
|
<th>原因</th>
|
|
<th>盈亏</th>
|
|
<th>收益率</th>
|
|
<th>资金余额</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="tradesTableBody">
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
`;
|
|
|
|
// 渲染图表
|
|
renderEquityChart(data.equity_curve);
|
|
renderMonthlyChart(data.monthly_returns);
|
|
renderStockChart(data.stock_performance);
|
|
renderTradesTable(data.trades);
|
|
}
|
|
|
|
function renderEquityChart(equityCurve) {
|
|
const ctx = document.getElementById('equityChart').getContext('2d');
|
|
|
|
new Chart(ctx, {
|
|
type: 'line',
|
|
data: {
|
|
labels: equityCurve.map(d => d.date),
|
|
datasets: [{
|
|
label: '账户权益',
|
|
data: equityCurve.map(d => d.equity),
|
|
borderColor: '#667eea',
|
|
backgroundColor: 'rgba(102, 126, 234, 0.1)',
|
|
tension: 0.4,
|
|
fill: true,
|
|
pointRadius: 3,
|
|
pointHoverRadius: 6
|
|
}]
|
|
},
|
|
options: {
|
|
responsive: true,
|
|
maintainAspectRatio: false,
|
|
plugins: {
|
|
legend: {
|
|
display: true,
|
|
position: 'top'
|
|
},
|
|
tooltip: {
|
|
mode: 'index',
|
|
intersect: false,
|
|
callbacks: {
|
|
label: function(context) {
|
|
return '权益: ¥' + formatNumber(context.parsed.y);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
scales: {
|
|
y: {
|
|
beginAtZero: false,
|
|
ticks: {
|
|
callback: function(value) {
|
|
return '¥' + (value / 1000).toFixed(0) + 'K';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
function renderMonthlyChart(monthlyReturns) {
|
|
const ctx = document.getElementById('monthlyChart').getContext('2d');
|
|
|
|
new Chart(ctx, {
|
|
type: 'bar',
|
|
data: {
|
|
labels: monthlyReturns.map(d => d.year_month),
|
|
datasets: [{
|
|
label: '月度收益',
|
|
data: monthlyReturns.map(d => d.pnl),
|
|
backgroundColor: monthlyReturns.map(d => d.pnl >= 0 ? 'rgba(72, 187, 120, 0.8)' : 'rgba(245, 101, 101, 0.8)'),
|
|
borderColor: monthlyReturns.map(d => d.pnl >= 0 ? '#48bb78' : '#f56565'),
|
|
borderWidth: 2
|
|
}]
|
|
},
|
|
options: {
|
|
responsive: true,
|
|
maintainAspectRatio: false,
|
|
plugins: {
|
|
legend: {
|
|
display: false
|
|
},
|
|
tooltip: {
|
|
callbacks: {
|
|
label: function(context) {
|
|
return '收益: ¥' + formatNumber(context.parsed.y);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
scales: {
|
|
y: {
|
|
ticks: {
|
|
callback: function(value) {
|
|
return '¥' + (value / 1000).toFixed(0) + 'K';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
function renderStockChart(stockPerformance) {
|
|
const ctx = document.getElementById('stockChart').getContext('2d');
|
|
|
|
new Chart(ctx, {
|
|
type: 'doughnut',
|
|
data: {
|
|
labels: stockPerformance.map(d => d.ts_code),
|
|
datasets: [{
|
|
label: '股票收益',
|
|
data: stockPerformance.map(d => Math.abs(d.total_pnl)),
|
|
backgroundColor: [
|
|
'rgba(102, 126, 234, 0.8)',
|
|
'rgba(118, 75, 162, 0.8)',
|
|
'rgba(72, 187, 120, 0.8)',
|
|
'rgba(245, 101, 101, 0.8)',
|
|
'rgba(237, 137, 54, 0.8)',
|
|
'rgba(49, 151, 149, 0.8)'
|
|
],
|
|
borderWidth: 2,
|
|
borderColor: 'white'
|
|
}]
|
|
},
|
|
options: {
|
|
responsive: true,
|
|
maintainAspectRatio: false,
|
|
plugins: {
|
|
legend: {
|
|
position: 'right'
|
|
},
|
|
tooltip: {
|
|
callbacks: {
|
|
label: function(context) {
|
|
const stock = stockPerformance[context.dataIndex];
|
|
return [
|
|
`收益: ¥${formatNumber(stock.total_pnl)}`,
|
|
`胜率: ${stock.win_rate}%`,
|
|
`交易: ${stock.trade_count}次`
|
|
];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
function renderTradesTable(trades) {
|
|
const tbody = document.getElementById('tradesTableBody');
|
|
tbody.innerHTML = '';
|
|
|
|
trades.forEach(trade => {
|
|
const row = document.createElement('tr');
|
|
row.innerHTML = `
|
|
<td>${trade.date}</td>
|
|
<td>${trade.ts_code}</td>
|
|
<td class="${trade.action === 'BUY' ? 'action-buy' : 'action-sell'}">${trade.action}</td>
|
|
<td>¥${trade.price}</td>
|
|
<td>${trade.shares}</td>
|
|
<td>¥${trade.commission}</td>
|
|
<td>${trade.reason}</td>
|
|
<td class="${trade.pnl !== null ? (trade.pnl >= 0 ? 'pnl-positive' : 'pnl-negative') : ''}">
|
|
${trade.pnl !== null ? '¥' + formatNumber(trade.pnl) : '-'}
|
|
</td>
|
|
<td class="${trade.pnl_pct !== null ? (trade.pnl_pct >= 0 ? 'pnl-positive' : 'pnl-negative') : ''}">
|
|
${trade.pnl_pct !== null ? (trade.pnl_pct >= 0 ? '+' : '') + trade.pnl_pct + '%' : '-'}
|
|
</td>
|
|
<td style="font-weight: 600; color: #4a5568;">
|
|
${trade.balance !== null ? '¥' + formatNumber(trade.balance) : '-'}
|
|
</td>
|
|
`;
|
|
tbody.appendChild(row);
|
|
});
|
|
}
|
|
|
|
function filterTable() {
|
|
const searchValue = document.getElementById('searchInput').value.toLowerCase();
|
|
const actionFilter = document.getElementById('actionFilter').value;
|
|
const rows = document.querySelectorAll('#tradesTableBody tr');
|
|
|
|
rows.forEach(row => {
|
|
const stockCode = row.cells[1].textContent.toLowerCase();
|
|
const action = row.cells[2].textContent;
|
|
|
|
const matchSearch = stockCode.includes(searchValue);
|
|
const matchAction = !actionFilter || action === actionFilter;
|
|
|
|
row.style.display = matchSearch && matchAction ? '' : 'none';
|
|
});
|
|
}
|
|
|
|
function formatNumber(num) {
|
|
return new Intl.NumberFormat('zh-CN', {
|
|
minimumFractionDigits: 2,
|
|
maximumFractionDigits: 2
|
|
}).format(num);
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|