Butterfly Spread Profitability Tool

Enter inputs to see results.

Net Cost: $${data.netCost.toFixed(2)} | Expiration Date: ${data.expirationDate}

Maximum Profit: $${data.maxProfit.toFixed(2)} | Maximum Loss: $${data.maxLoss.toFixed(2)}

Lower Breakeven: $${data.lowerBreakeven.toFixed(2)} | Upper Breakeven: $${data.upperBreakeven.toFixed(2)}

Payoff Table at Expiration

${data.payoffData.map(row => ` `).join('')}
Stock Price (USD) Payout (USD)
$${row.price} $${row.payout}

Recommendations:

${data.recommendations}

`; resultsTable.innerHTML = tableHtml; } // Sort Table window.sortTable = function (n) { const table = document.querySelector('.results-table'); if (!table) return; let rows, switching = true, i, x, y, shouldSwitch, dir = 'asc', switchcount = 0; while (switching) { switching = false; rows = table.rows; for (i = 1; i < (rows.length - 1); i++) { shouldSwitch = false; x = rows[i].getElementsByTagName('TD')[n]; y = rows[i + 1].getElementsByTagName('TD')[n]; let xVal = parseFloat(x.innerHTML.replace('$', '')) || 0; let yVal = parseFloat(y.innerHTML.replace('$', '')) || 0; if (dir === 'asc') { if (xVal > yVal) { shouldSwitch = true; break; } } else if (dir === 'desc') { if (xVal < yVal) { shouldSwitch = true; break; } } } if (shouldSwitch) { rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); switching = true; switchcount++; } else if (switchcount === 0 && dir === 'asc') { dir = 'desc'; switching = true; } } }; // 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 resultsTable = document.getElementById('results-table'); const pdfContent = resultsTable.querySelector('.pdf-content'); if (!resultsTable || !pdfContent || !pdfContent.querySelector('.results-table')) { alert('No results available to download. Please calculate results 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 = ['Stock Price (USD)', 'Payout (USD)']; rows.push(headers.map(header => ({ text: header, style: 'tableHeader' }))); const tableRows = pdfContent.querySelectorAll('.results-table tbody tr'); if (tableRows.length === 0) { throw new Error('No table rows found in results.'); } tableRows.forEach(row => { const cells = row.querySelectorAll('td'); const rowData = Array.from(cells).map(cell => ({ text: cell.textContent })); rows.push(rowData); }); // Extract summary data const summaryData = Array.from(pdfContent.querySelectorAll('p')).map(p => p.textContent); const recommendations = pdfContent.querySelector('.summary p')?.textContent || 'N/A'; // Define PDF document structure const docDefinition = { content: [ { text: 'Butterfly Spread Profitability Report', style: 'header' }, { text: '\n' }, { text: summaryData.join('\n'), style: 'body' }, { text: '\n' }, { text: 'Payoff Table at Expiration', style: 'subheader' }, { table: { headerRows: 1, widths: ['*', '*'], body: rows }, layout: { fillColor: function (rowIndex) { return (rowIndex % 2 === 0 && rowIndex > 0) ? '#f2f2f2' : null; }, hLineColor: '#ddd', vLineColor: '#ddd' } }, { 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: '#007BFF' }, body: { fontSize: 10, color: '#333' } }, defaultStyle: { fontSize: 10, color: '#333' }, pageMargins: [40, 60, 40, 60] }; // Generate and download PDF pdfMake.createPdf(docDefinition).download('Butterfly_Spread_Profitability_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.'); } }; });
Scroll to Top