fix fetch data in signal page and the backtest export excel issue
This commit is contained in:
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user