Credit Spread Risk-Reward Calculator

Enter inputs and calculate spread to see results.

Net Credit Received: $${netCredit.toFixed(2)}

Probability of Profit: ${pop}%

Recommendations:

${recommendations}

`; 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 spread first.'); return; } // Extract table data const rows = []; const headers = ['Underlying Asset', 'Max Profit (USD)', 'Max Loss (USD)', 'Breakeven Price (USD)', 'Probability of Profit (%)', 'Risk-Reward Ratio', '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); } // Extract summary and recommendations const strategyType = pdfContent.querySelector('.strategy-summary p:nth-child(1)')?.textContent || ''; const netCredit = pdfContent.querySelector('.strategy-summary p:nth-child(2)')?.textContent || ''; const pop = pdfContent.querySelector('.strategy-summary p:nth-child(3)')?.textContent || ''; const recommendations = pdfContent.querySelector('.strategy-summary p:nth-child(5)')?.textContent || ''; // Define PDF document structure const docDefinition = { content: [ { text: 'Credit Spread Risk-Reward Calculator Report', style: 'header' }, { text: '\n' }, { table: { headerRows: 1, widths: ['*', 'auto', '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: netCredit, 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] }; try { // Generate and download PDF pdfMake.createPdf(docDefinition).download('Credit_Spread_Risk_Reward_Report.pdf'); } catch (err) { console.error('PDF generation failed:', err); alert('Failed to generate PDF. Please try again.'); } }; });
Scroll to Top