change instruments list from symbols to name

This commit is contained in:
2026-02-05 12:06:18 +08:00
parent 485db3b51f
commit b673b917fa
+347 -342
View File
@@ -1,343 +1,348 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Financial Dashboard</title> <link rel="manifest" href="{{ url_for('static', filename='site.webmanifest') }}?v=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <link rel="apple-touch-icon" href="{{ url_for('static', filename='apple-touch-icon.png') }}?v=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css"> <link rel="icon" type="image/png" sizes="32x32" href="{{ url_for('static', filename='favicon-32x32.png') }}?v=1">
<style> <link rel="icon" type="image/png" sizes="16x16" href="{{ url_for('static', filename='favicon-16x16.png') }}?v=1">
:root { <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}?v=1">
--header-bg: #4a5568; <title>Financial Dashboard</title>
--text-up: #28a745; <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
--text-down: #dc3545; <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
} <style>
:root {
body { --header-bg: #4a5568;
background-color: #f4f7f6; --text-up: #28a745;
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Roboto; --text-down: #dc3545;
margin: 0; }
padding: 10px;
} body {
background-color: #f4f7f6;
.card { font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Roboto;
border: none; margin: 0;
border-radius: 12px; padding: 10px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
overflow: hidden;
} .card {
border: none;
.card-header { border-radius: 12px;
background-color: white !important; box-shadow: 0 4px 15px rgba(0,0,0,0.1);
border-bottom: 1px solid #eee; overflow: hidden;
padding: 15px; }
}
.card-header {
/* Zebra Striping Logic */ background-color: white !important;
.table-striped tbody tr:nth-of-type(odd) { border-bottom: 1px solid #eee;
background-color: rgba(0, 0, 0, 0.03); padding: 15px;
} }
/* Sticky Column for Mobile Scrolling */ /* Zebra Striping Logic */
.table-responsive { border-radius: 8px; } .table-striped tbody tr:nth-of-type(odd) {
background-color: rgba(0, 0, 0, 0.03);
.table thead th { }
background-color: var(--header-bg) !important;
color: white !important; /* Sticky Column for Mobile Scrolling */
font-size: 0.7rem; .table-responsive { border-radius: 8px; }
text-transform: uppercase;
letter-spacing: 0.5px; .table thead th {
text-align: center; background-color: var(--header-bg) !important;
white-space: nowrap; color: white !important;
padding: 12px 8px; font-size: 0.7rem;
border: none; text-transform: uppercase;
} letter-spacing: 0.5px;
text-align: center;
.table td { white-space: nowrap;
text-align: center; padding: 12px 8px;
vertical-align: middle; border: none;
font-size: 0.85rem; }
white-space: nowrap;
border-color: #f1f1f1; .table td {
} text-align: center;
vertical-align: middle;
/* Keep Instrument Name fixed on the left while swiping */ font-size: 0.85rem;
.table td:first-child, .table th:first-child { white-space: nowrap;
position: sticky; border-color: #f1f1f1;
left: 0; }
z-index: 10;
text-align: left; /* Keep Instrument Name fixed on the left while swiping */
min-width: 140px; .table td:first-child, .table th:first-child {
background-color: white; position: sticky;
font-weight: 700; left: 0;
} z-index: 10;
text-align: left;
/* Ensure zebra stripe works on sticky column */ min-width: 140px;
.table-striped tbody tr:nth-of-type(odd) td:first-child { background-color: white;
background-color: #f9f9f9; font-weight: 700;
} }
/* Custom UI Elements */ /* Ensure zebra stripe works on sticky column */
.btn-refresh { .table-striped tbody tr:nth-of-type(odd) td:first-child {
background-color: #0d6efd; background-color: #f9f9f9;
color: white !important; }
font-weight: 600;
border-radius: 6px; /* Custom UI Elements */
padding: 6px 16px; .btn-refresh {
transition: 0.2s; background-color: #0d6efd;
} color: white !important;
font-weight: 600;
.text-up { color: var(--text-up); font-weight: 600; } border-radius: 6px;
.text-down { color: var(--text-down); font-weight: 600; } padding: 6px 16px;
transition: 0.2s;
.badge-kd { }
background-color: #6c757d;
font-size: 0.75rem; .text-up { color: var(--text-up); font-weight: 600; }
padding: 5px 8px; .text-down { color: var(--text-down); font-weight: 600; }
}
.badge-kd {
#loading { display: none; margin-left: 10px; font-size: 0.8rem; color: #666; } background-color: #6c757d;
/* 1. Ensure the 52W column is wide enough for the progress bar */ font-size: 0.75rem;
.table td:nth-child(4), .table th:nth-child(4) { padding: 5px 8px;
min-width: 140px; }
text-align: left;
} #loading { display: none; margin-left: 10px; font-size: 0.8rem; color: #666; }
/* 1. Ensure the 52W column is wide enough for the progress bar */
/* 2. Narrow the EMA and K/D columns since they only have small numbers */ .table td:nth-child(4), .table th:nth-child(4) {
.table td:nth-child(n+5), .table th:nth-child(n+5) { min-width: 140px;
min-width: 75px; text-align: left;
} }
/* 3. Give the Instrument column a bit more breathing room if names are long */ /* 2. Narrow the EMA and K/D columns since they only have small numbers */
.table td:first-child, .table th:first-child { .table td:nth-child(n+5), .table th:nth-child(n+5) {
min-width: 180px; /* Increased from 140px */ min-width: 75px;
} }
</style>
</head> /* 3. Give the Instrument column a bit more breathing room if names are long */
<body> .table td:first-child, .table th:first-child {
<nav class="navbar navbar-expand-lg navbar-dark bg-dark mb-4"> min-width: 180px; /* Increased from 140px */
<div class="container-fluid"> }
<a class="navbar-brand" href="/"> </style>
<i class="bi bi-graph-up-arrow me-2"></i>Finance Suite </head>
</a> <body>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"> <nav class="navbar navbar-expand-lg navbar-dark bg-dark mb-4">
<span class="navbar-text text-light small d-none d-md-inline"> <div class="container-fluid">
System Status: <span id="statusIndicator" class="text-warning">● Connecting...</span> <a class="navbar-brand" href="/">
</span> <i class="bi bi-graph-up-arrow me-2"></i>Finance Suite
</button> </a>
<div class="collapse navbar-collapse" id="navbarNav"> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
<ul class="navbar-nav"> <span class="navbar-text text-light small d-none d-md-inline">
<li class="nav-item"> System Status: <span id="statusIndicator" class="text-warning">● Connecting...</span>
<a class="nav-link" href="/">Dashboard</a> </span>
</li> </button>
<li class="nav-item"> <div class="collapse navbar-collapse" id="navbarNav">
<a class="nav-link" href="/backtest">Backtester</a> <ul class="navbar-nav">
</li> <li class="nav-item">
</ul> <a class="nav-link" href="/">Dashboard</a>
</div> </li>
<span class="navbar-text text-light small d-none d-md-inline"> <li class="nav-item">
System Status: <span class="text-success">● Online</span> <a class="nav-link" href="/backtest">Backtester</a>
</span> </li>
</div> </ul>
</nav> </div>
<div class="container-fluid p-0"> <span class="navbar-text text-light small d-none d-md-inline">
<div class="card"> System Status: <span class="text-success">● Online</span>
<div class="card-header d-flex justify-content-between align-items-center"> </span>
<h5 class="mb-0 text-dark">Portfolio Signals</h5> </div>
<div class="d-flex align-items-center gap-2"> </nav>
<span id="loading" class="me-2" style="display:none;">Updating...</span> <div class="container-fluid p-0">
<div class="card">
<button class="btn btn-outline-secondary btn-sm" onclick="loadData()"> <div class="card-header d-flex justify-content-between align-items-center">
<i class="bi bi-arrow-clockwise"></i> Refresh Table <h5 class="mb-0 text-dark">Portfolio Signals</h5>
</button> <div class="d-flex align-items-center gap-2">
<span id="loading" class="me-2" style="display:none;">Updating...</span>
<button id="syncBtn" class="btn btn-primary btn-sm" onclick="runGlobalSync()">
<i class="bi bi-cloud-download"></i> Sync New Data <button class="btn btn-outline-secondary btn-sm" onclick="loadData()">
</button> <i class="bi bi-arrow-clockwise"></i> Refresh Table
</div> </button>
</div>
<div class="card-body p-0"> <button id="syncBtn" class="btn btn-primary btn-sm" onclick="runGlobalSync()">
<div class="table-responsive"> <i class="bi bi-cloud-download"></i> Sync New Data
<table class="table table-hover table-striped mb-0"> </button>
<thead> </div>
<tr> </div>
<th style="width: 25%;">Instrument</th> <div class="card-body p-0">
<th style="width: 10%;">Date</th> <th style="width: 10%;">Close</th> <div class="table-responsive">
<th style="width: 10%;">Chg%</th> <table class="table table-hover table-striped mb-0">
<th style="width: 10%;">52W Range</th> <th style="width: 8%;">20 EMA</th> <thead>
<th style="width: 10%;">50 EMA</th> <tr>
<th style="width: 10%;">100 EMA</th> <th style="width: 25%;">Instrument</th>
<th style="width: 10%;">200 EMA</th> <th style="width: 10%;">Date</th> <th style="width: 10%;">Close</th>
<th style="width: 5%;">K/D</th> <th style="width: 10%;">Chg%</th>
</tr> <th style="width: 10%;">52W Range</th> <th style="width: 8%;">20 EMA</th>
</thead> <th style="width: 10%;">50 EMA</th>
<tbody id="tableBody"> <th style="width: 10%;">100 EMA</th>
<tr><td colspan="9" class="p-4">Initializing data engine...</td></tr> <th style="width: 10%;">200 EMA</th>
</tbody> <th style="width: 5%;">K/D</th>
</table> </tr>
</div> </thead>
</div> <tbody id="tableBody">
</div> <tr><td colspan="9" class="p-4">Initializing data engine...</td></tr>
</div> </tbody>
<script> </table>
console.log("Script block loaded successfully."); </div>
</div>
// --- 1. Helper Function for K/D Styling --- </div>
function formatKD(val) { </div>
if (!val || val === "N/A" || !val.includes('/')) return `<span class="text-muted">N/A</span>`; <script>
const [k, d] = val.split('/').map(v => parseFloat(v)); console.log("Script block loaded successfully.");
let colorClass = 'text-dark';
if (k >= 80) colorClass = 'text-danger fw-bold'; // --- 1. Helper Function for K/D Styling ---
else if (k <= 20) colorClass = 'text-success fw-bold'; function formatKD(val) {
return `<span class="${colorClass}">${val}</span>`; if (!val || val === "N/A" || !val.includes('/')) return `<span class="text-muted">N/A</span>`;
} const [k, d] = val.split('/').map(v => parseFloat(v));
let colorClass = 'text-dark';
// --- 2. EMA Formatter --- if (k >= 80) colorClass = 'text-danger fw-bold';
const formatEma = (val) => { else if (k <= 20) colorClass = 'text-success fw-bold';
if (val === "N/A" || val === null || val === undefined) return `<span class="text-muted">N/A</span>`; return `<span class="${colorClass}">${val}</span>`;
const num = parseFloat(val); }
const sign = num > 0 ? "+" : "";
const colorClass = num >= 0 ? 'text-up' : 'text-down'; // --- 2. EMA Formatter ---
return `<span class="${colorClass}">${sign}${num.toFixed(1)}%</span>`; const formatEma = (val) => {
}; if (val === "N/A" || val === null || val === undefined) return `<span class="text-muted">N/A</span>`;
const num = parseFloat(val);
// --- 3. Load Table Data --- const sign = num > 0 ? "+" : "";
async function loadData() { const colorClass = num >= 0 ? 'text-up' : 'text-down';
console.log("Starting loadData..."); return `<span class="${colorClass}">${sign}${num.toFixed(1)}%</span>`;
const loading = document.getElementById('loading'); };
const tbody = document.getElementById('tableBody');
if (loading) loading.style.display = 'inline'; // --- 3. Load Table Data ---
async function loadData() {
try { console.log("Starting loadData...");
const response = await fetch('/api/summary'); const loading = document.getElementById('loading');
const data = await response.json(); const tbody = document.getElementById('tableBody');
if (loading) loading.style.display = 'inline';
if (!data || data.length === 0) {
tbody.innerHTML = '<tr><td colspan="10" class="p-4">No data found. Please run Sync.</td></tr>'; try {
return; const response = await fetch('/api/summary');
} const data = await response.json();
// Get today's date string (YYYY-MM-DD) to check freshness if (!data || data.length === 0) {
const todayStr = new Date().toISOString().split('T')[0]; tbody.innerHTML = '<tr><td colspan="10" class="p-4">No data found. Please run Sync.</td></tr>';
let htmlContent = ''; return;
}
data.forEach(item => {
// --- 1. Error Row Handling --- // Get today's date string (YYYY-MM-DD) to check freshness
if (item.error || !item.last_close) { const todayStr = new Date().toISOString().split('T')[0];
htmlContent += ` let htmlContent = '';
<tr>
<td>${item.symbol || 'Unknown'}</td> data.forEach(item => {
<td colspan="9" class="text-center p-3"> // --- 1. Error Row Handling ---
<span class="badge bg-warning text-dark"><i class="bi bi-exclamation-triangle"></i> Needs Sync</span> if (item.error || !item.last_close) {
<small class="text-muted ms-2">Local CSV not found or corrupted.</small> htmlContent += `
</td> <tr>
</tr>`; <td>${item.name || 'Unknown'}</td>
return; <td colspan="9" class="text-center p-3">
} <span class="badge bg-warning text-dark"><i class="bi bi-exclamation-triangle"></i> Needs Sync</span>
let displayDate = "N/A"; <small class="text-muted ms-2">Local CSV not found or corrupted.</small>
if (item.last_date && item.last_date.includes('-')) { </td>
const parts = item.last_date.split('-'); // Splits "2026-01-30" into ["2026", "01", "30"] </tr>`;
displayDate = `${parts[2]}/${parts[1]}`; // Combines into "30/01" return;
} }
// --- 2. Calculations & Styling --- let displayDate = "N/A";
const current = parseFloat(item.last_close) || 0; if (item.last_date && item.last_date.includes('-')) {
const low = parseFloat(item.low_52) || 0; const parts = item.last_date.split('-'); // Splits "2026-01-30" into ["2026", "01", "30"]
const high = parseFloat(item.high_52) || 0; displayDate = `${parts[2]}/${parts[1]}`; // Combines into "30/01"
}
// 52W Range Progress Bar // --- 2. Calculations & Styling ---
let rangePct = high > low ? Math.min(Math.max(((current - low) / (high - low)) * 100, 0), 100) : 0; const current = parseFloat(item.last_close) || 0;
const rangeColor = rangePct > 80 ? 'text-danger' : (rangePct < 20 ? 'text-success' : 'text-muted'); const low = parseFloat(item.low_52) || 0;
const high = parseFloat(item.high_52) || 0;
// Date Freshness check (highlight if date matches today)
const isFresh = item.last_date === todayStr; // 52W Range Progress Bar
const dateStyle = isFresh ? 'badge bg-success-subtle text-success border border-success-subtle' : 'text-muted'; let rangePct = high > low ? Math.min(Math.max(((current - low) / (high - low)) * 100, 0), 100) : 0;
const rangeColor = rangePct > 80 ? 'text-danger' : (rangePct < 20 ? 'text-success' : 'text-muted');
// --- 3. Construct Row ---
htmlContent += ` // Date Freshness check (highlight if date matches today)
<tr> const isFresh = item.last_date === todayStr;
<td> const dateStyle = isFresh ? 'badge bg-success-subtle text-success border border-success-subtle' : 'text-muted';
<div class="fw-bold">${item.name || item.symbol}</div>
</td> // --- 3. Construct Row ---
<td><span class="${dateStyle}" style="font-size: 0.75rem; padding: 2px 5px; border-radius: 4px;">${displayDate}</span></td> htmlContent += `
<td class="fw-bold">${item.last_close}</td> <tr>
<td class="${item.change_pct >= 0 ? 'text-up' : 'text-down'}"> <td>
${item.change_pct >= 0 ? '+' : ''}${item.change_pct}% <div class="fw-bold">${item.name || item.symbol}</div>
</td> </td>
<td class="${rangeColor} small"> <td><span class="${dateStyle}" style="font-size: 0.75rem; padding: 2px 5px; border-radius: 4px;">${displayDate}</span></td>
<div class="d-flex justify-content-between mb-1" style="min-width: 100px; font-size: 0.7rem;"> <td class="fw-bold">${item.last_close}</td>
<span>${item.low_52}</span><span>${item.high_52}</span> <td class="${item.change_pct >= 0 ? 'text-up' : 'text-down'}">
</div> ${item.change_pct >= 0 ? '+' : ''}${item.change_pct}%
<div class="progress" style="height: 4px;"> </td>
<div class="progress-bar bg-primary" style="width: ${rangePct}%"></div> <td class="${rangeColor} small">
</div> <div class="d-flex justify-content-between mb-1" style="min-width: 100px; font-size: 0.7rem;">
</td> <span>${item.low_52}</span><span>${item.high_52}</span>
<td>${formatEma(item.last_ema20)}</td> </div>
<td>${formatEma(item.last_ema50)}</td> <div class="progress" style="height: 4px;">
<td>${formatEma(item.last_ema100)}</td> <div class="progress-bar bg-primary" style="width: ${rangePct}%"></div>
<td>${formatEma(item.last_ema200)}</td> </div>
<td>${formatKD(item.kd_values)}</td> </td>
</tr>`; <td>${formatEma(item.last_ema20)}</td>
}); <td>${formatEma(item.last_ema50)}</td>
<td>${formatEma(item.last_ema100)}</td>
// Batch update the DOM once <td>${formatEma(item.last_ema200)}</td>
tbody.innerHTML = htmlContent; <td>${formatKD(item.kd_values)}</td>
</tr>`;
} catch (error) { });
console.error("Fetch error:", error);
tbody.innerHTML = '<tr><td colspan="10" class="text-danger p-4">Error loading summary. Check server logs.</td></tr>'; // Batch update the DOM once
} finally { tbody.innerHTML = htmlContent;
if (loading) loading.style.display = 'none';
} } catch (error) {
} console.error("Fetch error:", error);
tbody.innerHTML = '<tr><td colspan="10" class="text-danger p-4">Error loading summary. Check server logs.</td></tr>';
// --- 4. Global Sync --- } finally {
async function runGlobalSync() { if (loading) loading.style.display = 'none';
const syncBtn = document.getElementById('syncBtn'); }
const loading = document.getElementById('loading'); }
if (!syncBtn) return;
// --- 4. Global Sync ---
syncBtn.disabled = true; async function runGlobalSync() {
const originalText = syncBtn.innerHTML; const syncBtn = document.getElementById('syncBtn');
syncBtn.innerHTML = `<span class="spinner-border spinner-border-sm"></span> Syncing...`; const loading = document.getElementById('loading');
if (loading) loading.style.display = 'inline'; if (!syncBtn) return;
try { syncBtn.disabled = true;
const response = await fetch('/api/sync', { method: 'POST' }); const originalText = syncBtn.innerHTML;
if (!response.ok) throw new Error("Sync failed"); syncBtn.innerHTML = `<span class="spinner-border spinner-border-sm"></span> Syncing...`;
await loadData(); if (loading) loading.style.display = 'inline';
alert("Sync Complete!");
} catch (error) { try {
console.error("Sync error:", error); const response = await fetch('/api/sync', { method: 'POST' });
alert("Sync failed. Check terminal."); if (!response.ok) throw new Error("Sync failed");
} finally { await loadData();
syncBtn.disabled = false; alert("Sync Complete!");
syncBtn.innerHTML = originalText; } catch (error) {
if (loading) loading.style.display = 'none'; console.error("Sync error:", error);
} alert("Sync failed. Check terminal.");
} } finally {
// This checks if the Flask server is responding every 30 seconds syncBtn.disabled = false;
async function checkStatus() { syncBtn.innerHTML = originalText;
const indicator = document.getElementById('statusIndicator'); if (loading) loading.style.display = 'none';
try { }
const response = await fetch('/api/summary'); // Or a dedicated /health endpoint }
if (response.ok) { // This checks if the Flask server is responding every 30 seconds
indicator.innerHTML = '● Online'; async function checkStatus() {
indicator.className = 'text-success'; const indicator = document.getElementById('statusIndicator');
} else { try {
throw new Error(); const response = await fetch('/api/summary'); // Or a dedicated /health endpoint
} if (response.ok) {
} catch (e) { indicator.innerHTML = '● Online';
indicator.innerHTML = '● Offline'; indicator.className = 'text-success';
indicator.className = 'text-danger'; } else {
} throw new Error();
} }
} catch (e) {
setInterval(checkStatus, 300000); indicator.innerHTML = '● Offline';
checkStatus(); // Initial check indicator.className = 'text-danger';
}
// --- 5. Initial Load --- }
window.addEventListener('load', loadData);
</script> setInterval(checkStatus, 300000);
checkStatus(); // Initial check
</body>
// --- 5. Initial Load ---
window.addEventListener('load', loadData);
</script>
</body>
</html> </html>