Spaces:
Running
Running
File size: 9,202 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 | class CustomVolatilityComparison extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.indices = [
{ name: 'V10', value: 12.5, change: 2.3, volume: 45200 },
{ name: 'V25', value: 28.3, change: -1.5, volume: 38700 },
{ name: 'V50', value: 52.1, change: 5.7, volume: 52300 },
{ name: 'V75', value: 75.8, change: 8.2, volume: 61200 },
{ name: 'V100', value: 98.4, change: -3.1, volume: 48900 }
];
}
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;
}
.index-list {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.index-item {
display: grid;
grid-template-columns: 50px 1fr auto auto;
align-items: center;
gap: 1rem;
padding: 0.75rem;
background: rgba(15, 23, 42, 0.5);
border-radius: 0.5rem;
transition: all 0.2s;
cursor: pointer;
}
.index-item:hover {
background: rgba(99, 102, 241, 0.1);
transform: translateX(4px);
}
.index-name {
font-weight: 600;
color: white;
font-size: 0.875rem;
}
.volatility-bar-container {
position: relative;
height: 6px;
background: rgba(255, 255, 255, 0.05);
border-radius: 3px;
overflow: hidden;
}
.volatility-bar {
height: 100%;
border-radius: 3px;
transition: width 0.5s ease;
}
.volatility-bar.v10 { background: linear-gradient(90deg, #10b981, #34d399); }
.volatility-bar.v25 { background: linear-gradient(90deg, #3b82f6, #60a5fa); }
.volatility-bar.v50 { background: linear-gradient(90deg, #f59e0b, #fbbf24); }
.volatility-bar.v75 { background: linear-gradient(90deg, #f43f5e, #fb7185); }
.volatility-bar.v100 { background: linear-gradient(90deg, #8b5cf6, #a78bfa); }
.index-value {
font-weight: 700;
color: white;
font-size: 0.875rem;
font-variant-numeric: tabular-nums;
min-width: 50px;
text-align: right;
}
.index-change {
font-size: 0.75rem;
font-weight: 600;
padding: 0.25rem 0.5rem;
border-radius: 0.25rem;
min-width: 60px;
text-align: center;
}
.change-positive {
background: rgba(16, 185, 129, 0.15);
color: #10b981;
}
.change-negative {
background: rgba(244, 63, 94, 0.15);
color: #f43f5e;
}
.sparkline {
width: 60px;
height: 24px;
}
.volume-indicator {
display: flex;
align-items: center;
gap: 0.25rem;
font-size: 0.65rem;
color: #64748b;
margin-top: 0.25rem;
}
.recommendation {
margin-top: 1rem;
padding: 0.75rem;
background: rgba(16, 185, 129, 0.1);
border: 1px solid rgba(16, 185, 129, 0.2);
border-radius: 0.5rem;
}
.recommendation-title {
font-size: 0.75rem;
font-weight: 600;
color: #10b981;
margin-bottom: 0.25rem;
}
.recommendation-text {
font-size: 0.75rem;
color: #94a3b8;
}
</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">
<line x1="18" y1="20" x2="18" y2="10"></line>
<line x1="12" y1="20" x2="12" y2="4"></line>
<line x1="6" y1="20" x2="6" y2="14"></line>
</svg>
Index Comparison
</div>
<span style="font-size: 0.75rem; color: #64748b;">Live</span>
</div>
<div class="index-list" id="index-list">
${this.indices.map(idx => this.renderIndexItem(idx)).join('')}
</div>
<div class="recommendation">
<div class="recommendation-title">Trading Recommendation</div>
<div class="recommendation-text" id="recommendation-text">
V75 shows strong momentum with rising volatility. Consider trend-following strategies.
</div>
</div>
</div>
`;
}
renderIndexItem(idx) {
const changeClass = idx.change >= 0 ? 'change-positive' : 'change-negative';
const changeSign = idx.change >= 0 ? '+' : '';
const barClass = idx.name.toLowerCase();
const barWidth = Math.min(100, (idx.value / 120) * 100);
return `
<div class="index-item" data-index="${idx.name}">
<span class="index-name">${idx.name}</span>
<div>
<div class="volatility-bar-container">
<div class="volatility-bar ${barClass}" style="width: ${barWidth}%;"></div>
</div>
<div class="volume-indicator">
<svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="22 12 18 12 15 21 9 3 6 12 2 12"></polyline>
</svg>
Vol: ${(idx.volume / 1000).toFixed(1)}K
</div>
</div>
<span class="index-value">${idx.value.toFixed(1)}</span>
<span class="index-change ${changeClass}">${changeSign}${idx.change.toFixed(1)}%</span>
</div>
`;
}
startSimulation() {
setInterval(() => {
this.indices.forEach(idx => {
// Simulate small changes
const change = (Math.random() - 0.5) * 2;
idx.value = Math.max(5, idx.value + change);
idx.change += (Math.random() - 0.5) * 0.5;
idx.volume += Math.floor((Math.random() - 0.5) * 1000);
});
this.updateDisplay();
}, 3000);
}
updateDisplay() {
const list = this.shadowRoot.getElementById('index-list');
if (list) {
list.innerHTML = this.indices.map(idx => this.renderIndexItem(idx)).join('');
}
// Update recommendation based on highest volatility change
const hottest = this.indices.reduce((a, b) => Math.abs(a.change) > Math.abs(b.change) ? a : b);
const recText = this.shadowRoot.getElementById('recommendation-text');
if (recText) {
const direction = hottest.change > 0 ? 'rising' : 'falling';
recText.textContent = `${hottest.name} shows ${direction} volatility momentum. ${hottest.change > 5 ? 'Consider momentum strategies.' : 'Suitable for range trading.'}`;
}
}
}
customElements.define('custom-volatility-comparison', CustomVolatilityComparison); |