fix fetch data in signal page and the backtest export excel issue

This commit is contained in:
2026-02-02 21:30:19 +08:00
parent 50f9734dcd
commit 891f0ea2b0
21 changed files with 8328 additions and 57803 deletions
+44 -3
View File
@@ -244,6 +244,8 @@
if (!res.ok) {
throw new Error(data.error || `Server returned ${res.status}`);
}
// SAVE DATA GLOBALLY FOR EXCEL EXPORTER
window.currentBacktestData = data;
// Update UI
document.getElementById('kpiArea')?.classList.remove('d-none');
@@ -454,10 +456,49 @@ function updateSyncBadge(dateString) {
}
function exportToExcel() {
const table = document.getElementById("ledgerTable");
const wb = XLSX.utils.table_to_book(table);
XLSX.writeFile(wb, "Investment_Backtest_Results.xlsx");
// 1. Verify the global data exists
if (!window.currentBacktestData || window.currentBacktestData.length === 0) {
alert("No data available to export. Please run a backtest first.");
return;
}
const excelRows = window.currentBacktestData.map(row => {
// Calculate returns using the same logic as your renderTable
const vaAnnRet = row.va_invested > 0
? ((row.va_value - row.va_invested) / row.va_invested)
: 0;
const dcaAnnRet = row.dca_invested > 0
? ((row.dca_value - row.dca_invested) / row.dca_invested)
: 0;
return {
"Date": row.date,
"Price": row.price,
// DVA / Value Averaging Columns (Separated)
"DVA Investment ($)": row.va_diff,
"DVA Shares Change": row.va_shares_trans,
"DVA Total Shares": row.va_shares_total,
"DVA Target Value": row.va_target_value,
"DVA Portfolio Value": row.va_value,
// DCA Columns (Separated)
"DCA Investment ($)": row.dca_invested,
"DCA Shares Change": row.dca_shares_trans,
"DCA Total Shares": row.dca_shares_total,
"DCA Portfolio Value": row.dca_value,
// Performance
"DVA Return (%)": (vaAnnRet * 100).toFixed(2) + "%",
"DCA Return (%)": (dcaAnnRet * 100).toFixed(2) + "%"
}
});
// 3. Convert JSON to Worksheet
const worksheet = XLSX.utils.json_to_sheet(excelRows);
// 4. Create Workbook and Download
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Backtest Results");
XLSX.writeFile(workbook, "Investment_Backtest_Results.xlsx");
}
// This checks if the Flask server is responding every 30 seconds
async function checkStatus() {
const indicator = document.getElementById('statusIndicator');