class CustomVolatilityIndicator extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }); this.volatilityData = { current: 75, average: 68, high: 89, low: 45, trend: 'rising' }; } connectedCallback() { this.render(); this.startSimulation(); } render() { this.shadowRoot.innerHTML = `
Volatility Monitor
${this.getTrendIcon()} ${this.volatilityData.trend}
75.0 Index
${this.generateHistoryBars()}
68.2
24h Avg
89.4
24h High
45.1
24h Low
`; this.attachEventListeners(); } getTrendIcon() { const icons = { rising: '', falling: '', stable: '' }; return icons[this.volatilityData.trend] || icons.stable; } generateHistoryBars() { let bars = ''; for (let i = 0; i < 30; i++) { const height = 20 + Math.random() * 80; const color = height > 70 ? '#f43f5e' : height > 50 ? '#f59e0b' : '#10b981'; bars += `
`; } return bars; } attachEventListeners() { const buttons = this.shadowRoot.querySelectorAll('.index-btn'); buttons.forEach(btn => { btn.addEventListener('click', (e) => { buttons.forEach(b => b.classList.remove('active')); e.target.classList.add('active'); this.switchIndex(parseInt(e.target.dataset.index)); }); }); } switchIndex(index) { // Simulate different volatility levels for different indices const baseVolatilities = { 10: { current: 12, average: 10, high: 18, low: 6 }, 25: { current: 28, average: 25, high: 38, low: 15 }, 50: { current: 52, average: 48, high: 65, low: 35 }, 75: { current: 75, average: 68, high: 89, low: 45 }, 100: { current: 98, average: 95, high: 120, low: 75 } }; const data = baseVolatilities[index]; this.volatilityData = { ...this.volatilityData, ...data }; this.updateDisplay(); // Dispatch event for chart update const event = new CustomEvent('index-change', { detail: { index: `R_${index}`, volatility: data.current }, bubbles: true, composed: true }); this.dispatchEvent(event); } startSimulation() { setInterval(() => { // Simulate volatility changes const change = (Math.random() - 0.5) * 4; this.volatilityData.current = Math.max(10, Math.min(150, this.volatilityData.current + change)); // Update trend if (change > 1) this.volatilityData.trend = 'rising'; else if (change < -1) this.volatilityData.trend = 'falling'; else this.volatilityData.trend = 'stable'; // Update high/low this.volatilityData.high = Math.max(this.volatilityData.high, this.volatilityData.current); this.volatilityData.low = Math.min(this.volatilityData.low, this.volatilityData.current); this.updateDisplay(); this.updateHistory(); }, 2000); } updateDisplay() { const currentEl = this.shadowRoot.getElementById('current-vol'); const gaugeEl = this.shadowRoot.getElementById('volatility-gauge'); const avgEl = this.shadowRoot.getElementById('avg-vol'); const highEl = this.shadowRoot.getElementById('high-vol'); const lowEl = this.shadowRoot.getElementById('low-vol'); const trendEl = this.shadowRoot.querySelector('.trend-indicator'); const alertEl = this.shadowRoot.getElementById('volatility-alert'); if (currentEl) currentEl.textContent = this.volatilityData.current.toFixed(1); if (avgEl) avgEl.textContent = this.volatilityData.average.toFixed(1); if (highEl) highEl.textContent = this.volatilityData.high.toFixed(1); if (lowEl) lowEl.textContent = this.volatilityData.low.toFixed(1); if (gaugeEl) { const percentage = Math.min(100, (this.volatilityData.current / 150) * 100); gaugeEl.style.width = `${percentage}%`; // Update color based on level gaugeEl.className = 'gauge-fill'; if (this.volatilityData.current < 30) gaugeEl.classList.add('low'); else if (this.volatilityData.current < 60) gaugeEl.classList.add('medium'); else if (this.volatilityData.current < 90) gaugeEl.classList.add('high'); else gaugeEl.classList.add('extreme'); } if (trendEl) { trendEl.className = `trend-indicator trend-${this.volatilityData.trend}`; trendEl.innerHTML = ` ${this.getTrendIcon()} ${this.volatilityData.trend} `; } // Show alerts for extreme volatility if (alertEl) { if (this.volatilityData.current > 100) { alertEl.innerHTML = `
Extreme Volatility - High Risk
`; } else if (this.volatilityData.current < 20) { alertEl.innerHTML = `
Low Volatility - Reduced Opportunity
`; } else { alertEl.innerHTML = ''; } } } updateHistory() { const historyEl = this.shadowRoot.getElementById('vol-history'); if (!historyEl) return; const bars = historyEl.querySelectorAll('.history-bar'); const newHeight = 20 + (this.volatilityData.current / 150) * 80; const newColor = this.volatilityData.current > 70 ? '#f43f5e' : this.volatilityData.current > 50 ? '#f59e0b' : '#10b981'; // Shift bars and add new one for (let i = 0; i < bars.length - 1; i++) { bars[i].style.height = bars[i + 1].style.height; bars[i].style.background = bars[i + 1].style.background; } bars[bars.length - 1].style.height = `${newHeight}%`; bars[bars.length - 1].style.background = newColor; } } customElements.define('custom-volatility-indicator', CustomVolatilityIndicator);