Spaces:
Running

volatilitybot-pro / components /volatility-indicator.js
Fraqim's picture
Add volatilities to the bot
ed2bd7f verified
Raw History Blame Contribute Delete
16.6 kB
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);