Total Estimated Cost
${formatCurrency(bestHedge.hedgeCost)}
New Portfolio Delta
${formatNum(bestHedge.greeks.delta)}
AI Strategy Recommendation
The AI optimizer recommends purchasing ${bestHedge.numContracts} put option contracts with a strike price of ${formatCurrency(bestHedge.strikePrice)}. This strategy is designed to significantly reduce downside risk (neutralize delta) while balancing the cost of the hedge. The total premium for this protection is estimated at ${formatCurrency(bestHedge.hedgeCost)}. This hedge effectively acts as an insurance policy against a sharp drop in ${stockTicker}'s price over the next ${data.hedgePeriod} days.
Risk Analysis (Portfolio Greeks)
Before Hedge
- Delta (Directional Risk): ${formatNum(numShares)}
- Gamma (Acceleration Risk): 0.00
- Vega (Volatility Risk): 0.00
- Theta (Time Decay): 0.00
After Hedge
- Delta (Directional Risk): ${formatNum(bestHedge.greeks.delta)}
- Gamma (Acceleration Risk): ${formatNum(bestHedge.greeks.gamma)}
- Vega (Volatility Risk): ${formatCurrency(bestHedge.greeks.vega)}
- Theta (Time Decay): ${formatCurrency(bestHedge.greeks.theta)}
Portfolio Payoff Diagram at Expiration
`;
renderPayoffChart(data);
}
function renderPayoffChart(data) {
const ctx = document.getElementById('payoffChart')?.getContext('2d');
if (!ctx) return;
if (chartInstance) chartInstance.destroy();
const { stockPrice, numShares, bestHedge } = data;
const labels = [];
const stockPayoff = [];
const hedgePayoff = [];
const combinedPayoff = [];
for(let price = stockPrice * 0.7; price <= stockPrice * 1.3; price += stockPrice * 0.01) {
labels.push(price);
const sPay = (price - stockPrice) * numShares;
// Payoff of a long put option at expiration
const hPay = (Math.max(0, bestHedge.strikePrice - price) * bestHedge.numContracts * 100) - bestHedge.hedgeCost;
stockPayoff.push(sPay);
hedgePayoff.push(hPay);
combinedPayoff.push(sPay + hPay);
}
chartInstance = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [
{
label: 'Unhedged Stock Position',
data: stockPayoff,
borderColor: '#9ca3af', // gray-400
borderWidth: 2,
pointRadius: 0,
borderDash: [5, 5]
},
{
label: 'Hedged Portfolio (Stock + Puts)',
data: combinedPayoff,
borderColor: '#2563eb', // blue-600
backgroundColor: 'rgba(37, 99, 235, 0.1)',
fill: true,
borderWidth: 3,
pointRadius: 0,
tension: 0.1
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: {
title: { display: true, text: 'Stock Price at Expiration ($)' },
ticks: { callback: value => '$' + labels[value].toFixed(2) }
},
y: {
title: { display: true, text: 'Profit / Loss ($)' },
ticks: { callback: value => '$' + value.toLocaleString() }
}
},
plugins: {
tooltip: {
callbacks: {
label: context => `${context.dataset.label}: $${context.parsed.y.toLocaleString()}`
}
}
}
}
});
}
// --- PDF Download Functionality ---
function downloadPDF() {
const pdfElement = document.getElementById('pdf-output');
const stockTicker = document.getElementById('stockTicker')?.value.toUpperCase() || 'Hedge';
if (!pdfElement) {
alert("Could not find content to download.");
return;
}
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; // Restore background
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({
orientation: 'portrait',
unit: 'px',
format: [canvas.width, canvas.height]
});
pdf.addImage(imgData, 'PNG', 0, 0, canvas.width, canvas.height);
pdf.save(`${stockTicker}-Hedging-Strategy.pdf`);
}).catch(err => {
console.error("PDF Generation Error:", err);
pdfElement.style.backgroundColor = originalBg;
alert("An error occurred while generating the PDF.");
});
}