Drawdown Risk Analyzer
Individual Drawdown Events
| # | Peak Index | Peak Value ($) | Trough Index | Trough Value ($) | Drawdown % | Duration to Trough (periods) | Recovery Index | Recovery Duration (periods) | Recovered? |
|---|
No drawdown events to display yet, or none met the minimum threshold. Please analyze data.
Drawdown Summary & Analysis
Summary Statistics:
Maximum Drawdown: N/A
Average Drawdown Depth (of listed events): N/A
Average Duration to Trough (of listed events): N/A
Average Recovery Duration (of recovered events): N/A
Number of Drawdowns Listed: 0
Risk Insights:
Analysis will appear here after calculation.
No recovered drawdowns were found in the provided data.
`; } const largestListedDrawdown = allDrawdowns.reduce((max, dd) => dd.maxDrawdownPercent > max.maxDrawdownPercent ? dd : max, {maxDrawdownPercent: 0}); if (largestListedDrawdown.maxDrawdownPercent > 0) { analysisHtml += `The largest *listed* individual drawdown was **${largestListedDrawdown.maxDrawdownPercent.toFixed(2)}%**, which occurred from index ${largestListedDrawdown.peakIndex} to ${largestListedDrawdown.troughIndex}.
`; } } else { analysisHtml += `While an overall maximum drawdown was identified, no individual drawdown events met your specified minimum threshold of ${minDrawdownThreshold * 100}%.
`; } if (maxOverallDrawdown > 0.20) { // Example threshold for significant drawdown analysisHtml += `Considerable Risk: An overall maximum drawdown of ${maxOverallDrawdown.toFixed(2) * 100}% indicates a significant potential for capital loss. Investors should be prepared for such fluctuations.
`; } else if (maxOverallDrawdown > 0.05) { analysisHtml += `Moderate Risk: An overall maximum drawdown of ${maxOverallDrawdown.toFixed(2) * 100}% suggests a moderate level of risk. Understanding the typical recovery periods is crucial.
`; } else { analysisHtml += `Lower Risk: An overall maximum drawdown of ${maxOverallDrawdown.toFixed(2) * 100}% points to relatively lower drawdown risk historically.
`; } } drawdownAnalysisOutput.innerHTML = analysisHtml; } window.resetForm = function() { priceDataInput.value = ''; minDrawdownThresholdInput.value = '1'; drawdownEventsTableBody.innerHTML = ''; noDrawdownEventsMessage.style.display = 'block'; drawdownSummaryOutput.innerHTML = `Summary Statistics:
Maximum Drawdown: N/A
Average Drawdown Depth (of listed events): N/A
Average Duration to Trough (of listed events): N/A
Average Recovery Duration (of recovered events): N/A
Number of Drawdowns Listed: 0
`; drawdownAnalysisOutput.innerHTML = 'Analysis will appear here after calculation.
'; allDrawdowns = []; drawdownEventsTabButton.disabled = true; summaryAnalysisTabButton.disabled = true; drawdownEventsTabButton.classList.add('drawdown-btn-disabled'); summaryAnalysisTabButton.classList.add('drawdown-btn-disabled'); activateTab('input'); // Return to input tab } window.downloadPdf = function() { const element = document.getElementById('drawdownTool'); if (!element) { alert('Tool container not found for PDF generation.'); return; } // Temporarily activate all tabs to ensure all content is rendered for PDF const previouslyActiveTabContent = document.querySelector('.drawdown-tab-content.active'); const previouslyActiveTabButton = document.querySelector('.drawdown-tab-button.active'); tabContents.forEach(content => content.classList.add('active')); tabButtons.forEach(button => button.classList.remove('active')); // Deactivate all buttons document.getElementById('drawdownInputTab').classList.add('active'); // Ensure input is active if not already document.getElementById('drawdownDrawdownEventsTab').classList.add('active'); // Ensure events is active document.getElementById('drawdownSummaryAnalysisTab').classList.add('active'); // Ensure summary is active // Hide buttons and tabs for PDF export const elementsToHide = document.querySelectorAll('.drawdown-tabs, .drawdown-btn-group, .drawdown-pdf-download-btn'); const originalDisplays = []; elementsToHide.forEach(el => { originalDisplays.push(el.style.display); el.style.display = 'none'; }); html2pdf(element, { margin: 10, filename: 'Drawdown_Risk_Analysis.pdf', image: { type: 'jpeg', quality: 0.98 }, html2canvas: { scale: 2, logging: true, dpi: 192, letterRendering: true }, jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' } }).then(() => { // Restore original state after PDF generation let i = 0; elementsToHide.forEach(el => { el.style.display = originalDisplays[i++]; }); tabContents.forEach(content => content.classList.remove('active')); if (previouslyActiveTabContent) { previouslyActiveTabContent.classList.add('active'); } if (previouslyActiveTabButton) { previouslyActiveTabButton.classList.add('active'); } else { // Fallback to default if no active button was found document.querySelector('.drawdown-tab-button[data-tab="input"]').classList.add('active'); document.getElementById('drawdownInputTab').classList.add('active'); } }).catch(error => { console.error('Error generating PDF:', error); alert('Failed to generate PDF. Please try again.'); // Ensure elements are restored even on error let i = 0; elementsToHide.forEach(el => { el.style.display = originalDisplays[i++]; }); tabContents.forEach(content => content.classList.remove('active')); if (previouslyActiveTabContent) { previouslyActiveTabContent.classList.add('active'); } if (previouslyActiveTabButton) { previouslyActiveTabButton.classList.add('active'); } else { document.querySelector('.drawdown-tab-button[data-tab="input"]').classList.add('active'); document.getElementById('drawdownInputTab').classList.add('active'); } }); } // Initialize UI state resetForm(); });