Spaces:
Running
Running
File size: 16,595 Bytes
ed2bd7f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | 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 = `
<style>
:host {
display: block;
}
.panel {
background: rgba(30, 41, 59, 0.6);
backdrop-filter: blur(12px);
border: 1px solid rgba(255, 255, 255, 0.05);
border-radius: 1rem;
padding: 1.25rem;
}
.header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 1rem;
}
.title {
font-weight: 700;
color: white;
display: flex;
align-items: center;
gap: 0.5rem;
font-size: 0.95rem;
}
.volatility-value {
font-size: 2rem;
font-weight: 800;
color: white;
font-variant-numeric: tabular-nums;
}
.volatility-label {
font-size: 0.75rem;
color: #94a3b8;
text-transform: uppercase;
letter-spacing: 0.05em;
}
.gauge-container {
position: relative;
height: 8px;
background: rgba(15, 23, 42, 0.8);
border-radius: 4px;
overflow: hidden;
margin: 1rem 0;
}
.gauge-fill {
height: 100%;
border-radius: 4px;
transition: width 0.5s ease, background-color 0.3s ease;
}
.gauge-fill.low { background: linear-gradient(90deg, #10b981, #34d399); }
.gauge-fill.medium { background: linear-gradient(90deg, #f59e0b, #fbbf24); }
.gauge-fill.high { background: linear-gradient(90deg, #f43f5e, #fb7185); }
.gauge-fill.extreme { background: linear-gradient(90deg, #8b5cf6, #a78bfa); }
.stats-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 0.75rem;
margin-top: 1rem;
}
.stat-item {
text-align: center;
padding: 0.5rem;
background: rgba(15, 23, 42, 0.5);
border-radius: 0.5rem;
}
.stat-value {
font-size: 0.875rem;
font-weight: 600;
color: white;
font-variant-numeric: tabular-nums;
}
.stat-label {
font-size: 0.65rem;
color: #64748b;
text-transform: uppercase;
margin-top: 0.25rem;
}
.trend-indicator {
display: inline-flex;
align-items: center;
gap: 0.25rem;
font-size: 0.75rem;
padding: 0.25rem 0.5rem;
border-radius: 0.25rem;
font-weight: 500;
}
.trend-rising {
background: rgba(16, 185, 129, 0.15);
color: #10b981;
}
.trend-falling {
background: rgba(244, 63, 94, 0.15);
color: #f43f5e;
}
.trend-stable {
background: rgba(99, 102, 241, 0.15);
color: #6366f1;
}
.index-selector {
display: flex;
gap: 0.5rem;
margin-bottom: 1rem;
flex-wrap: wrap;
}
.index-btn {
padding: 0.375rem 0.75rem;
border-radius: 0.375rem;
font-size: 0.75rem;
font-weight: 500;
border: 1px solid rgba(255, 255, 255, 0.1);
background: rgba(15, 23, 42, 0.5);
color: #94a3b8;
cursor: pointer;
transition: all 0.2s;
}
.index-btn:hover {
background: rgba(99, 102, 241, 0.2);
color: white;
}
.index-btn.active {
background: rgba(99, 102, 241, 0.3);
border-color: rgba(99, 102, 241, 0.5);
color: white;
}
.volatility-history {
display: flex;
gap: 2px;
height: 40px;
align-items: flex-end;
margin-top: 0.75rem;
padding: 0.5rem;
background: rgba(15, 23, 42, 0.5);
border-radius: 0.5rem;
}
.history-bar {
flex: 1;
border-radius: 1px;
transition: all 0.3s;
}
.alert-badge {
display: inline-flex;
align-items: center;
gap: 0.375rem;
font-size: 0.7rem;
padding: 0.25rem 0.5rem;
border-radius: 0.25rem;
margin-top: 0.5rem;
}
.alert-high {
background: rgba(244, 63, 94, 0.15);
color: #f43f5e;
border: 1px solid rgba(244, 63, 94, 0.2);
}
.alert-low {
background: rgba(16, 185, 129, 0.15);
color: #10b981;
border: 1px solid rgba(16, 185, 129, 0.2);
}
</style>
<div class="panel">
<div class="header">
<div class="title">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" class="text-primary-400">
<path d="M13 2L3 14h9l-1 8 10-12h-9l1-8z"></path>
</svg>
Volatility Monitor
</div>
<span class="trend-indicator trend-${this.volatilityData.trend}">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
${this.getTrendIcon()}
</svg>
${this.volatilityData.trend}
</span>
</div>
<div class="index-selector">
<button class="index-btn" data-index="10">V10</button>
<button class="index-btn" data-index="25">V25</button>
<button class="index-btn" data-index="50">V50</button>
<button class="index-btn active" data-index="75">V75</button>
<button class="index-btn" data-index="100">V100</button>
</div>
<div style="display: flex; align-items: baseline; gap: 0.5rem;">
<span class="volatility-value" id="current-vol">75.0</span>
<span class="volatility-label">Index</span>
</div>
<div class="gauge-container">
<div class="gauge-fill high" id="volatility-gauge" style="width: 75%;"></div>
</div>
<div class="volatility-history" id="vol-history">
${this.generateHistoryBars()}
</div>
<div class="stats-grid">
<div class="stat-item">
<div class="stat-value" id="avg-vol">68.2</div>
<div class="stat-label">24h Avg</div>
</div>
<div class="stat-item">
<div class="stat-value" id="high-vol">89.4</div>
<div class="stat-label">24h High</div>
</div>
<div class="stat-item">
<div class="stat-value" id="low-vol">45.1</div>
<div class="stat-label">24h Low</div>
</div>
</div>
<div id="volatility-alert"></div>
</div>
`;
this.attachEventListeners();
}
getTrendIcon() {
const icons = {
rising: '<polyline points="23 6 13.5 15.5 8.5 10.5 1 18"></polyline><polyline points="17 6 23 6 23 12"></polyline>',
falling: '<polyline points="23 18 13.5 8.5 8.5 13.5 1 6"></polyline><polyline points="17 18 23 18 23 12"></polyline>',
stable: '<line x1="5" y1="12" x2="19" y2="12"></line>'
};
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 += `<div class="history-bar" style="height: ${height}%; background: ${color}; opacity: ${0.3 + (i / 30) * 0.7};"></div>`;
}
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 = `
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
${this.getTrendIcon()}
</svg>
${this.volatilityData.trend}
`;
}
// Show alerts for extreme volatility
if (alertEl) {
if (this.volatilityData.current > 100) {
alertEl.innerHTML = `
<div class="alert-badge alert-high">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"></path>
<line x1="12" y1="9" x2="12" y2="13"></line>
<line x1="12" y1="17" x2="12.01" y2="17"></line>
</svg>
Extreme Volatility - High Risk
</div>
`;
} else if (this.volatilityData.current < 20) {
alertEl.innerHTML = `
<div class="alert-badge alert-low">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="10"></circle>
<line x1="12" y1="8" x2="12" y2="12"></line>
<line x1="12" y1="16" x2="12.01" y2="16"></line>
</svg>
Low Volatility - Reduced Opportunity
</div>
`;
} 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); |