`;
}
function addTradeLog(message, colorClass, profitText) {
const logContainer = document.getElementById('trade-log-container');
const entry = document.createElement('div');
entry.className = `flex justify-between items-center ${colorClass}`;
entry.innerHTML = `${o.time.toLocaleTimeString()} ${o.buyEx} -> ${o.sellEx} $${o.buyPrice.toFixed(3)} $${o.sellPrice.toFixed(3)} $${(o.profit*100).toFixed(2)} `).join('');
const reportHtml = `
`;
const pdfTemplate = document.getElementById('pdf-template');
pdfTemplate.innerHTML = reportHtml;
pdfTemplate.classList.remove('invisible');
try {
const { jsPDF } = window.jspdf;
const canvas = await html2canvas(pdfTemplate.querySelector('.pdf-page'), { scale: 2 });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth(), pdfHeight = (canvas.height * pdfWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
pdf.save(`Arbitrage_Report_${document.getElementById('asset-ticker').value}.pdf`);
} catch (e) { console.error('PDF Generation Error:', e); } finally {
downloadPdfBtn.disabled = false;
downloadPdfBtn.textContent = 'Download PDF Report';
pdfTemplate.classList.add('invisible');
pdfTemplate.innerHTML = '';
}
}
// --- EVENT LISTENERS ---
startBtn.addEventListener('click', startSimulation);
stopBtn.addEventListener('click', stopSimulation);
downloadPdfBtn.addEventListener('click', generatePdfReport);
// --- INITIALIZATION ---
switchTab(0);
initChart();
updateSummaryCards();
});
[${new Date().toLocaleTimeString()}] ${message}
${profitText}
`; logContainer.appendChild(entry); logContainer.scrollTop = logContainer.scrollHeight; } function initChart() { const ctx = document.getElementById('price-chart').getContext('2d'); if (priceChart) priceChart.destroy(); priceChart = new Chart(ctx, { type: 'line', data: { datasets: Object.entries(marketData).map(([label, data]) => ({ label, data: [{x: new Date(), y: data.price}], borderColor: data.color, tension: 0.1, pointRadius: 0 })) }, options: { responsive: true, maintainAspectRatio: false, scales: { x: { type: 'timeseries', time: { unit: 'second' }, ticks: { color: '#9ca3af' } }, y: { ticks: { callback: v => `$${v.toFixed(2)}`, color: '#9ca3af' } } } } }); } // --- PDF GENERATION --- async function generatePdfReport() { if (simulation.state.running) stopSimulation(); downloadPdfBtn.disabled = true; downloadPdfBtn.textContent = 'Generating...'; const { opportunities, totalProfit, tradeCount } = simulation.state; const tableRows = opportunities.map(o => `Arbitrage Scan Report
ASSET: ${document.getElementById('asset-ticker').value} | DATE: ${new Date().toLocaleDateString()}
OPPORTUNITIES FOUND
${tradeCount}
TOTAL SIMULATED PROFIT
$${totalProfit.toFixed(2)}
AVG PROFIT / TRADE
$${(totalProfit / tradeCount || 0).toFixed(2)}
Trade Execution Log
| Time | Route | Buy Price | Sell Price | Profit (100 shares) |
|---|
