`;
const resultsOutput = document.getElementById('results-output');
if (resultsOutput) {
resultsOutput.innerHTML = tableHtml;
}
// Switch to results tab
openTab('results-tab');
};
// Error function for PoP calculation
function erf(x) {
const a1 = 0.254829592, a2 = -0.284496736, a3 = 1.421413741, a4 = -1.453152027, a5 = 1.061405429;
const p = 0.3275911;
const sign = x < 0 ? -1 : 1;
x = Math.abs(x);
const t = 1.0 / (1.0 + p * x);
const y = ((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t * Math.exp(-x * x);
return sign * (1 - y);
}
// Tab Navigation
window.openTab = function (tabId) {
const tabs = document.querySelectorAll('.tab-content');
const tabLinks = document.querySelectorAll('.tab-link');
tabs.forEach(tab => tab.classList.remove('active'));
tabLinks.forEach(link => link.classList.remove('active'));
const activeTab = document.getElementById(tabId);
if (activeTab) {
activeTab.classList.add('active');
const activeLink = document.querySelector(`.tab-link[onclick="openTab('${tabId}')"]`);
if (activeLink) activeLink.classList.add('active');
}
};
// Download PDF
window.downloadPDF = function () {
const resultsOutput = document.getElementById('results-output');
const pdfContent = resultsOutput.querySelector('.pdf-content');
if (!resultsOutput || !pdfContent || !pdfContent.querySelector('.results-table')) {
alert('No results available to download. Please calculate simulation first.');
return;
}
// Check if pdfMake is available
if (typeof pdfMake === 'undefined') {
alert('PDF generation library failed to load. Please check your internet connection and try again.');
console.error('pdfMake is not defined. Library may have failed to load.');
return;
}
try {
// Extract table data
const rows = [];
const headers = ['Underlying Asset', 'Max Loss (USD)', 'Breakeven Lower (USD)', 'Breakeven Upper (USD)', 'Probability of Profit (%)', 'ESG Score'];
rows.push(headers.map(header => ({ text: header, style: 'tableHeader' })));
const tableRow = pdfContent.querySelector('.results-table tbody tr');
if (tableRow) {
const cells = tableRow.querySelectorAll('td');
const rowData = Array.from(cells).map(cell => ({ text: cell.textContent }));
rows.push(rowData);
} else {
throw new Error('No table row found in results.');
}
// Extract summary and recommendations
const strategyType = pdfContent.querySelector('.strategy-summary p:nth-child(1)')?.textContent || 'N/A';
const totalPremiums = pdfContent.querySelector('.strategy-summary p:nth-child(2)')?.textContent || 'N/A';
const pop = pdfContent.querySelector('.strategy-summary p:nth-child(3)')?.textContent || 'N/A';
const recommendations = pdfContent.querySelector('.strategy-summary p:nth-child(5)')?.textContent || 'N/A';
// Define PDF document structure
const docDefinition = {
content: [
{ text: 'Straddle & Strangle Profit Simulator Report', style: 'header' },
{ text: '\n' },
{
table: {
headerRows: 1,
widths: ['*', 'auto', 'auto', 'auto', 'auto', 'auto'],
body: rows
},
layout: {
fillColor: function (rowIndex) {
return (rowIndex % 2 === 0 && rowIndex > 0) ? '#f2f2f2' : null;
},
hLineColor: '#ddd',
vLineColor: '#ddd'
}
},
{ text: '\n' },
{ text: 'Strategy Summary', style: 'subheader' },
{ text: strategyType, style: 'body' },
{ text: totalPremiums, style: 'body' },
{ text: pop, style: 'body' },
{ text: '\n' },
{ text: 'Recommendations', style: 'subheader' },
{ text: recommendations, style: 'body' }
],
styles: {
header: {
fontSize: 18,
bold: true,
alignment: 'center',
color: '#333'
},
subheader: {
fontSize: 14,
bold: true,
color: '#333'
},
tableHeader: {
bold: true,
fontSize: 12,
color: 'white',
fillColor: '#4CAF50'
},
body: {
fontSize: 10,
color: '#333'
}
},
defaultStyle: {
fontSize: 10,
color: '#333'
},
pageMargins: [40, 60, 40, 60]
};
// Generate and download PDF
pdfMake.createPdf(docDefinition).download('Straddle_Strangle_Profit_Report.pdf');
} catch (err) {
console.error('PDF generation failed:', err.message);
alert('Failed to generate PDF due to an internal error. Please ensure results are valid and try again.');
}
};
});