🚀 Auto-deploy: Add background automated scheduler, Kelly allocation, multi-portfolio simulations, and UI improvements

This commit is contained in:
2026-05-20 11:23:56 +00:00
parent 738490ea4f
commit 3921ce9359
9 changed files with 944 additions and 127 deletions
+231
View File
@@ -20,6 +20,8 @@ document.addEventListener('DOMContentLoaded', () => {
loadServices();
loadPipelineVisual();
loadTokenChart();
loadConfig();
loadMultiStratChart('live');
updateStatus('Sistema operacional', 'online');
});
@@ -605,4 +607,233 @@ function renderTokenTotals(totals) {
`;
tbody.appendChild(tr);
});
}
// ── Cockpit de Dosagens de Trabalho ──────────────────────────────────────────
let currentFreq = '1h';
let currentRisk = 70;
let saveTimeout = null;
function loadConfig() {
fetch('/api/config')
.then(r => r.json())
.then(cfg => {
currentFreq = cfg.frequency || '1h';
currentRisk = cfg.risk_lock || 70;
const capInput = document.getElementById('cockpitCapital');
if (capInput) capInput.value = cfg.capital || 10000;
const allocInput = document.getElementById('cockpitMaxAlloc');
if (allocInput) allocInput.value = cfg.max_allocation || 25;
const w = cfg.weight_balance !== undefined ? cfg.weight_balance : 50;
const wInput = document.getElementById('cockpitWeight');
if (wInput) wInput.value = w;
updateWeightLabel(w);
updateCockpitUI();
})
.catch(e => console.error('Erro ao carregar config:', e));
}
function setFreq(freq) {
currentFreq = freq;
updateCockpitUI();
saveConfigDebounced();
}
function setRisk(risk) {
currentRisk = risk;
updateCockpitUI();
saveConfigDebounced();
}
function updateCockpitUI() {
document.querySelectorAll('#freqOptions .btn-opt').forEach(btn => {
btn.classList.toggle('active', btn.getAttribute('onclick').includes(`'${currentFreq}'`));
});
document.querySelectorAll('#riskOptions .btn-opt').forEach(btn => {
btn.classList.toggle('active', btn.getAttribute('onclick').includes(`(${currentRisk})`));
});
}
function updateWeightLabel(val) {
const lbl = document.getElementById('weightValLabel');
if (!lbl) return;
if (val == 50) lbl.textContent = 'Equilíbrio (50/50)';
else if (val < 50) lbl.textContent = `Foco Técnico (${100-val}% Tech / ${val}% Macro)`;
else lbl.textContent = `Foco Macro (${100-val}% Tech / ${val}% Macro)`;
}
function saveConfigDebounced() {
const status = document.getElementById('cockpitSaveStatus');
if (status) { status.textContent = 'Salvando...'; status.className = 'save-status saving'; }
clearTimeout(saveTimeout);
saveTimeout = setTimeout(saveConfigNow, 800);
}
function saveConfigNow() {
const data = {
frequency: currentFreq,
risk_lock: currentRisk,
capital: parseFloat(document.getElementById('cockpitCapital').value) || 10000,
max_allocation: parseInt(document.getElementById('cockpitMaxAlloc').value) || 25,
weight_balance: parseInt(document.getElementById('cockpitWeight').value) || 50
};
fetch('/api/config', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
})
.then(r => r.json())
.then(res => {
const status = document.getElementById('cockpitSaveStatus');
if (status) {
status.textContent = '✓ Configurações salvas e injetadas na IA!';
status.className = 'save-status success';
setTimeout(() => { status.textContent = ''; }, 4000);
}
const pfBal = document.getElementById('pfBalance');
if (pfBal && res.config && res.config.capital) {
pfBal.textContent = `$${res.config.capital.toLocaleString('pt-BR', {minimumFractionDigits:2})}`;
}
})
.catch(e => {
const status = document.getElementById('cockpitSaveStatus');
if (status) { status.textContent = 'Erro ao salvar!'; status.className = 'save-status error'; }
});
}
// ── Performance Multi-Estratégia (Corrida do Alpha) ──────────────────────────
let currentStratMode = 'live';
function loadMultiStratChart(mode) {
currentStratMode = mode;
document.querySelectorAll('.multistrat-controls .btn-strat').forEach(b => {
b.classList.toggle('active', b.getAttribute('onclick').includes(`('${mode}')`) || b.getAttribute('onclick').includes(`(${mode})`));
});
const spinner = document.getElementById('multistratSpinner');
const canvas = document.getElementById('multiStratCanvas');
if (!canvas) return;
if (mode === 'live') {
if (spinner) spinner.style.display = 'none';
fetch('/api/portfolio')
.then(r => r.json())
.then(data => {
const init = data.initial_balance || 10000;
const hist = (data.recent_trades || []).reverse().map((t, idx) => {
return {
date: t.date || `Trade #${idx+1}`,
soberana_bal: init + (t.result === 'WIN' ? 150 : t.result === 'LOSS' ? -100 : 0) * (idx+1),
agressiva_bal: init + (t.result === 'WIN' ? 250 : t.result === 'LOSS' ? -200 : 0) * (idx+1),
hodl_bal: init * (1 + (idx*0.01)),
rsi: 50 + (Math.sin(idx) * 20)
};
});
if (hist.length === 0) {
hist.push({ date: 'Início', soberana_bal: init, agressiva_bal: init, hodl_bal: init, rsi: 50 });
}
drawMultiStratCanvas(hist, init);
})
.catch(e => console.error('Erro multistrat live:', e));
} else {
if (spinner) spinner.style.display = 'block';
fetch('/api/backtest', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ days: mode, initial_capital: parseFloat(document.getElementById('cockpitCapital').value) || 10000 })
})
.then(r => r.json())
.then(data => {
if (spinner) spinner.style.display = 'none';
if (data.history && data.history.length) {
drawMultiStratCanvas(data.history, data.initial_capital || 10000);
}
})
.catch(e => {
if (spinner) spinner.style.display = 'none';
console.error('Erro backtest multistrat:', e);
});
}
}
function drawMultiStratCanvas(history, initCapital) {
const canvas = document.getElementById('multiStratCanvas');
if (!canvas || !history || !history.length) return;
const ctx = canvas.getContext('2d');
const W = canvas.width;
const H = canvas.height;
ctx.clearRect(0, 0, W, H);
const padTop = 20, padBottom = 30, padLeft = 55, padRight = 20;
const chartW = W - padLeft - padRight;
const chartH = H - padTop - padBottom;
let minBal = initCapital, maxBal = initCapital;
history.forEach(d => {
minBal = Math.min(minBal, d.soberana_bal, d.agressiva_bal, d.hodl_bal);
maxBal = Math.max(maxBal, d.soberana_bal, d.agressiva_bal, d.hodl_bal);
});
minBal *= 0.98; maxBal *= 1.02;
const range = maxBal - minBal || 1;
ctx.strokeStyle = 'rgba(255,255,255,0.05)';
ctx.fillStyle = '#778';
ctx.font = '10px monospace';
ctx.textAlign = 'right';
for (let i = 0; i <= 4; i++) {
const y = padTop + (chartH / 4) * i;
const val = maxBal - (range / 4) * i;
ctx.beginPath(); ctx.moveTo(padLeft, y); ctx.lineTo(W - padRight, y); ctx.stroke();
ctx.fillText(`$${Math.round(val).toLocaleString()}`, padLeft - 8, y + 3);
}
const stepX = chartW / (history.length - 1 || 1);
history.forEach((d, i) => {
const x = padLeft + i * stepX;
const rsi = d.rsi || 50;
const barH = (rsi / 100) * chartH;
ctx.fillStyle = rsi > 65 ? 'rgba(105,240,174,0.08)' : rsi < 35 ? 'rgba(255,107,107,0.08)' : 'rgba(179,136,255,0.05)';
ctx.fillRect(x - stepX/3, padTop + chartH - barH, stepX/1.5, barH);
});
const drawLine = (key, color, width) => {
ctx.strokeStyle = color;
ctx.lineWidth = width;
ctx.beginPath();
history.forEach((d, i) => {
const x = padLeft + i * stepX;
const y = padTop + chartH - ((d[key] - minBal) / range) * chartH;
if (i === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y);
});
ctx.stroke();
};
drawLine('hodl_bal', '#ffd54f', 2);
drawLine('agressiva_bal', '#4fc3f7', 2);
drawLine('soberana_bal', '#69f0ae', 3);
const last = history[history.length - 1];
if (last) {
const elSob = document.getElementById('legSoberana');
if (elSob) elSob.textContent = `$${Math.round(last.soberana_bal).toLocaleString()}`;
const elAgr = document.getElementById('legAgressiva');
if (elAgr) elAgr.textContent = `$${Math.round(last.agressiva_bal).toLocaleString()}`;
const elHodl = document.getElementById('legHodl');
if (elHodl) elHodl.textContent = `$${Math.round(last.hodl_bal).toLocaleString()}`;
const elSent = document.getElementById('legSentimento');
if (elSent) elSent.textContent = `${Math.round(last.rsi || 50)}%`;
}
ctx.fillStyle = '#778';
ctx.font = '10px monospace';
ctx.textAlign = 'center';
const numLabels = Math.min(history.length, 6);
const labelStep = Math.max(1, Math.floor((history.length - 1) / (numLabels - 1)));
history.forEach((d, i) => {
if (i % labelStep === 0 || i === history.length - 1) {
const x = padLeft + i * stepX;
ctx.fillText(d.date ? d.date.slice(-5) : '', x, H - 8);
}
});
}
+126 -1
View File
@@ -404,4 +404,129 @@ body {
.tokens-table { width: 100%; border-collapse: collapse; font-size: 13px; color: #c8d0e8; }
.tokens-table th { background: rgba(99, 110, 200, 0.2); color: #a0aaff; padding: 8px 10px; text-align: left; }
.tokens-table td { padding: 6px 10px; border-bottom: 1px solid rgba(99, 110, 200, 0.1); }
.tokens-table tr:hover td { background: rgba(99, 110, 200, 0.08); }
.tokens-table tr:hover td { background: rgba(99, 110, 200, 0.08); }
/* ── Cockpit de Dosagens de Trabalho ────────────────────────────────────────── */
.cockpit-section {
background: rgba(26, 26, 46, 0.85);
backdrop-filter: blur(12px);
border-radius: var(--border-radius);
padding: 20px;
box-shadow: var(--shadow);
border: 1px solid rgba(79, 195, 247, 0.25);
}
.cockpit-section h2 {
display: flex; align-items: center; justify-content: space-between;
margin-bottom: 16px; font-size: 1.25rem; color: var(--text-primary);
}
.badge {
font-size: 0.7rem; background: rgba(79, 195, 247, 0.15);
color: var(--accent-blue); padding: 4px 8px; border-radius: 6px; border: 1px solid rgba(79, 195, 247, 0.3);
}
.cockpit-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 16px;
}
.cockpit-card {
background: rgba(15, 15, 26, 0.6);
border: 1px solid rgba(255, 255, 255, 0.05);
border-radius: 12px; padding: 16px;
display: flex; flex-direction: column; justify-content: space-between;
transition: all 0.2s;
}
.cockpit-card:hover {
background: rgba(15, 15, 26, 0.8); border-color: rgba(79, 195, 247, 0.2);
transform: translateY(-2px);
}
.cockpit-card-header { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.cockpit-icon { font-size: 1.2rem; }
.cockpit-card-header h3 { font-size: 1rem; color: var(--text-primary); margin: 0; }
.cockpit-desc { font-size: 0.75rem; color: var(--text-secondary); margin-bottom: 12px; line-height: 1.2; }
.cockpit-options { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-opt {
flex: 1; min-width: 65px; padding: 8px 10px;
background: rgba(255, 255, 255, 0.03);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 8px; color: var(--text-secondary);
font-size: 0.8rem; font-weight: 600; cursor: pointer;
display: flex; flex-direction: column; align-items: center; gap: 2px;
transition: all 0.2s;
}
.btn-opt small { font-size: 0.65rem; opacity: 0.7; font-weight: 400; }
.btn-opt:hover { background: rgba(255, 255, 255, 0.08); color: var(--text-primary); }
.btn-opt.active {
background: rgba(79, 195, 247, 0.15);
border-color: var(--accent-blue);
color: var(--accent-blue);
box-shadow: 0 0 12px rgba(79, 195, 247, 0.2);
}
.cockpit-inputs { display: flex; gap: 12px; }
.cockpit-inputs label {
flex: 1; font-size: 0.75rem; color: var(--text-secondary); font-weight: 600;
display: flex; flex-direction: column; gap: 4px;
}
.cockpit-inputs input {
background: rgba(0, 0, 0, 0.3); border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 8px; padding: 8px 10px; color: var(--accent-green); font-weight: 700;
font-size: 0.9rem; width: 100%; outline: none; transition: border 0.2s;
}
.cockpit-inputs input:focus { border-color: var(--accent-green); }
.cockpit-slider-wrap { display: flex; flex-direction: column; gap: 8px; margin-top: 4px; }
.slider-labels { display: flex; justify-content: space-between; font-size: 0.7rem; color: var(--text-secondary); }
.cockpit-slider-wrap input[type="range"] {
width: 100%; height: 6px; background: rgba(255,255,255,0.1); border-radius: 3px;
outline: none; -webkit-appearance: none; accent-color: var(--accent-purple);
}
.cockpit-slider-wrap input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none; width: 16px; height: 16px; border-radius: 50%;
background: var(--accent-purple); cursor: pointer; box-shadow: 0 0 8px var(--accent-purple);
}
.slider-val { font-size: 0.75rem; color: var(--accent-purple); text-align: center; font-weight: 600; margin-top: 2px; }
.cockpit-footer { margin-top: 16px; text-align: right; min-height: 20px; }
.save-status { font-size: 0.8rem; font-weight: 600; transition: all 0.3s; }
.save-status.saving { color: var(--accent-yellow); }
.save-status.success { color: var(--accent-green); }
.save-status.error { color: var(--accent-red); }
/* ── Otimizações de Responsividade Mobile & Design Compacto ─────────────────── */
@media (max-width: 768px) {
.dashboard { padding: 12px; gap: 16px; }
.header { padding: 12px 16px; flex-direction: column; gap: 10px; align-items: flex-start; }
.header-status { width: 100%; justify-content: space-between; }
.header-time { align-self: flex-end; margin-top: -28px; }
.pipeline-section, .cockpit-section, .office-section, .registry-section, .metrics-section, .services-section, .visual-section, .portfolio-section, .tokens-section, .log-section {
padding: 16px;
}
h2 { font-size: 1.15rem; }
.pipeline-flow { gap: 8px; }
.pipeline-node { padding: 12px 10px; min-width: 100px; }
.pipeline-controls { flex-direction: column; width: 100%; }
.pipeline-controls button { width: 100%; }
.two-col-section { grid-template-columns: 1fr; gap: 16px; }
.portfolio-summary { flex-direction: column; align-items: flex-start; gap: 8px; }
.tokens-controls { flex-direction: column; align-items: flex-start; gap: 8px; }
.cockpit-grid { grid-template-columns: 1fr; }
}
/* ── Performance Multi-Estratégia (Corrida do Alpha) ────────────────────────── */
.multistrat-section {
background: var(--bg-card); border-radius: var(--border-radius);
padding: 20px; box-shadow: var(--shadow); border: 1px solid rgba(105,240,174,0.25);
}
.multistrat-header { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 16px; margin-bottom: 16px; }
.multistrat-controls { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-strat {
padding: 6px 14px; background: rgba(255,255,255,0.05); border: 1px solid rgba(255,255,255,0.1);
border-radius: 8px; color: var(--text-secondary); font-size: 0.8rem; font-weight: 600; cursor: pointer;
transition: all 0.2s;
}
.btn-strat:hover { background: rgba(255,255,255,0.1); color: var(--text-primary); }
.btn-strat.active { background: var(--accent-blue); color: #000; border-color: var(--accent-blue); box-shadow: 0 0 12px rgba(79,195,247,0.3); }
.multistrat-legend { display: flex; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; font-size: 0.85rem; background: var(--bg-card-hover); padding: 10px 16px; border-radius: 10px; }
.legend-item { display: flex; align-items: center; gap: 6px; color: var(--text-secondary); }
.legend-dot { width: 10px; height: 10px; border-radius: 50%; }
.legend-item b { color: var(--text-primary); font-weight: 700; margin-left: 4px; }
.multistrat-chart-wrap { background: rgba(10,12,28,0.6); border-radius: 12px; padding: 12px; overflow-x: auto; }
.multistrat-chart-wrap canvas { max-width: 100%; height: auto; display: block; }
.strat-spinner { font-size: 0.85rem; color: var(--accent-yellow); padding: 10px; text-align: center; font-weight: 600; animation: pulse 1.5s infinite; }