Total Hedge Cost
${formatCurrency(bestOption.cost)}
AI Strategy Recommendation
To protect against a significant market downturn ("tail event"), the AI model recommends purchasing ${bestOption.contracts} put option contracts on ${document.getElementById('marketIndex').value} with a strike price of ${formatCurrency(bestOption.strikePrice)}.
This strategy acts as insurance, establishing a "floor" below which your portfolio's value will not significantly drop from a market decline.
The total cost of this protection is ${formatCurrency(bestOption.cost)}, which represents approximately ${hedgeCostPercent.toFixed(2)}% of your total portfolio value for the specified hedge period.
Portfolio Payoff at Expiration
`;
renderPayoffChart(data);
}
function renderPayoffChart(data) {
const ctx = document.getElementById('payoffChart')?.getContext('2d');
if (!ctx) return;
if (chartInstance) chartInstance.destroy();
const { portfolioValue, S, bestOption } = data;
const labels = [];
const unhedgedPayoff = [];
const hedgedPayoff = [];
const initialPortfolioValue = portfolioValue;
for(let price = S * 0.6; price <= S * 1.1; price += S * 0.01) {
labels.push(price);
const percentChange = (price - S) / S;
// Unhedged portfolio P/L
const unhedgedFinalValue = initialPortfolioValue * (1 + percentChange);
unhedgedPayoff.push(unhedgedFinalValue - initialPortfolioValue);
// Payoff from the put options
const optionPayoff = Math.max(0, bestOption.strikePrice - price) * bestOption.contracts * 100;
// Hedged portfolio P/L
const hedgedPnl = (unhedgedFinalValue - initialPortfolioValue) + optionPayoff - bestOption.cost;
hedgedPayoff.push(hedgedPnl);
}
chartInstance = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [
{
label: 'Unhedged Portfolio P/L',
data: unhedgedPayoff,
borderColor: '#ef4444', // red-500
borderWidth: 2,
pointRadius: 0,
borderDash: [5, 5]
},
{
label: 'Hedged Portfolio P/L',
data: hedgedPayoff,
borderColor: '#2563eb', // blue-600
backgroundColor: 'rgba(37, 99, 235, 0.1)',
fill: { target: 'origin', above: 'rgba(37, 99, 235, 0.1)', below: 'rgba(239, 68, 68, 0.1)'},
borderWidth: 3,
pointRadius: 0,
tension: 0.1
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: {
title: { display: true, text: `Index Price (${document.getElementById('marketIndex').value}) at Expiration` },
ticks: { callback: value => '$' + labels[value].toFixed(2) }
},
y: {
title: { display: true, text: 'Portfolio Profit / Loss ($)' },
ticks: { callback: value => '$' + value.toLocaleString() }
}
},
plugins: {
tooltip: { callbacks: { label: c => `${c.dataset.label}: $${c.raw.toLocaleString()}` } }
}
}
});
}
// --- PDF Download ---
function downloadPDF() {
const pdfElement = document.getElementById('pdf-output');
if (!pdfElement) return alert("Could not find content to download.");
const originalBg = pdfElement.style.backgroundColor;
pdfElement.style.backgroundColor = 'white';
const { jsPDF } = window.jspdf;
html2canvas(pdfElement, { scale: 2, useCORS: true, backgroundColor: '#ffffff' })
.then(canvas => {
pdfElement.style.backgroundColor = originalBg;
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'p', unit: 'px', format: [canvas.width, canvas.height] });
pdf.addImage(imgData, 'PNG', 0, 0, canvas.width, canvas.height);
pdf.save('Tail-Hedge-Strategy-Report.pdf');
}).catch(err => {
pdfElement.style.backgroundColor = originalBg;
console.error("PDF Generation Error:", err);
alert("An error occurred while generating the PDF.");
});
}