246 строки
9.4 KiB
HTML
246 строки
9.4 KiB
HTML
<!doctype html>
|
|
<html lang="ru">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<title>Trade Autopilot</title>
|
|
<script src="https://unpkg.com/lightweight-charts@4.2.3/dist/lightweight-charts.standalone.production.js"></script>
|
|
<style>
|
|
body { margin: 0; font-family: Arial, sans-serif; background: #0b1020; color: #e7ecff; }
|
|
header { padding: 16px 20px; display: flex; gap: 12px; align-items: center; border-bottom: 1px solid #1e2947; flex-wrap: wrap; }
|
|
input, button, select { padding: 10px 12px; border-radius: 8px; border: 1px solid #344266; background: #111936; color: #e7ecff; }
|
|
button { cursor: pointer; }
|
|
main { display: grid; grid-template-columns: 1fr 380px; gap: 16px; padding: 16px; }
|
|
#chart { height: 620px; background: #0f1730; border-radius: 14px; overflow: hidden; }
|
|
.panel { background: #0f1730; border: 1px solid #1e2947; border-radius: 14px; padding: 16px; }
|
|
.metric { margin-bottom: 14px; }
|
|
.label { color: #8fa0d0; font-size: 12px; }
|
|
.value { font-size: 20px; margin-top: 4px; }
|
|
.pill { padding: 6px 10px; border-radius: 999px; background: #162042; color: #9fb2ff; border: 1px solid #2b3a68; }
|
|
.danger { border-color: #7f1d1d; color: #fecaca; }
|
|
.ok { border-color: #14532d; color: #bbf7d0; }
|
|
.row { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
|
|
pre { white-space: pre-wrap; word-break: break-word; font-size: 12px; color: #b8c4ef; max-height: 260px; overflow: auto; }
|
|
@media (max-width: 980px) { main { grid-template-columns: 1fr; } }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<header>
|
|
<strong>Trade Autopilot</strong>
|
|
<input id="market" value="BTCUSDT" />
|
|
<button onclick="loadMarket()">Загрузить</button>
|
|
<button onclick="analyze()">AI анализ</button>
|
|
<button onclick="decide()">AI решение</button>
|
|
<button onclick="autoDemoTrade()">Авто демо-сделка</button>
|
|
<span class="pill" id="status">подключение...</span>
|
|
<span class="pill" id="source">source: —</span>
|
|
</header>
|
|
<main>
|
|
<section id="chart"></section>
|
|
<aside class="panel">
|
|
<div class="metric">
|
|
<div class="label">Live цена</div>
|
|
<div class="value" id="livePrice">—</div>
|
|
</div>
|
|
<div class="metric">
|
|
<div class="label">Демо баланс</div>
|
|
<div class="value" id="balance">—</div>
|
|
</div>
|
|
<div class="metric">
|
|
<div class="label">Equity</div>
|
|
<div class="value" id="equity">—</div>
|
|
</div>
|
|
<div class="metric">
|
|
<div class="label">Бот</div>
|
|
<div class="row">
|
|
<button onclick="setBotEnabled(true)">Вкл бота</button>
|
|
<button onclick="setBotEnabled(false)">Выкл бота</button>
|
|
<button class="danger" onclick="disableLive()">Live OFF</button>
|
|
</div>
|
|
<div class="row">
|
|
<input id="ack" placeholder="I_UNDERSTAND_LIVE_TRADING_RISK" />
|
|
<button class="danger" onclick="enableLive()">Live ON</button>
|
|
</div>
|
|
<pre id="botSettings">—</pre>
|
|
</div>
|
|
<div class="metric">
|
|
<div class="label">AI результат</div>
|
|
<pre id="aiResult">—</pre>
|
|
</div>
|
|
<div class="metric">
|
|
<div class="label">Последнее событие</div>
|
|
<pre id="ticker">—</pre>
|
|
</div>
|
|
</aside>
|
|
</main>
|
|
|
|
<script>
|
|
const chartEl = document.getElementById('chart');
|
|
const chart = LightweightCharts.createChart(chartEl, {
|
|
width: chartEl.clientWidth,
|
|
height: chartEl.clientHeight,
|
|
layout: { background: { color: '#0f1730' }, textColor: '#d7defa' },
|
|
grid: { vertLines: { color: '#1b2748' }, horzLines: { color: '#1b2748' } },
|
|
timeScale: { timeVisible: true, secondsVisible: false },
|
|
});
|
|
const candleSeries = chart.addCandlestickSeries();
|
|
let socket;
|
|
let currentCandle = null;
|
|
|
|
function normalizeKline(raw) {
|
|
const rows = raw.data || [];
|
|
const candles = rows.map(row => ({
|
|
time: Math.floor(Number(row.created_at || row[0]) / 1000),
|
|
open: Number(row.open || row[1]),
|
|
high: Number(row.high || row[3]),
|
|
low: Number(row.low || row[4]),
|
|
close: Number(row.close || row[2]),
|
|
})).filter(x => Number.isFinite(x.time) && Number.isFinite(x.close));
|
|
|
|
candles.sort((a, b) => a.time - b.time);
|
|
currentCandle = candles.length ? candles[candles.length - 1] : null;
|
|
return candles;
|
|
}
|
|
|
|
function updateLiveCandle(price, tsMs) {
|
|
if (!Number.isFinite(price)) return;
|
|
const bucket = Math.floor((tsMs || Date.now()) / 60000) * 60;
|
|
if (!currentCandle || currentCandle.time !== bucket) {
|
|
currentCandle = { time: bucket, open: price, high: price, low: price, close: price };
|
|
} else {
|
|
currentCandle.high = Math.max(currentCandle.high, price);
|
|
currentCandle.low = Math.min(currentCandle.low, price);
|
|
currentCandle.close = price;
|
|
}
|
|
candleSeries.update(currentCandle);
|
|
document.getElementById('livePrice').textContent = price.toFixed(8).replace(/0+$/, '').replace(/\.$/, '');
|
|
}
|
|
|
|
async function loadMarket() {
|
|
const market = getMarket();
|
|
document.getElementById('status').textContent = 'загрузка истории...';
|
|
const res = await fetch(`/api/v1/market/kline?market=${market}&period=1min&limit=200`);
|
|
const json = await res.json();
|
|
candleSeries.setData(normalizeKline(json));
|
|
chart.timeScale().fitContent();
|
|
await loadMarkers(market);
|
|
connectWs(market);
|
|
loadAccount();
|
|
loadBotSettings();
|
|
}
|
|
|
|
function getMarket() {
|
|
return document.getElementById('market').value.trim().toUpperCase() || 'BTCUSDT';
|
|
}
|
|
|
|
async function loadAccount() {
|
|
const res = await fetch('/api/v1/demo/account');
|
|
const acc = await res.json();
|
|
document.getElementById('balance').textContent = `${Number(acc.balance).toFixed(2)} ${acc.quote_asset}`;
|
|
document.getElementById('equity').textContent = `${Number(acc.equity).toFixed(2)} ${acc.quote_asset}`;
|
|
}
|
|
|
|
async function loadBotSettings() {
|
|
const res = await fetch('/api/v1/bot/settings');
|
|
const json = await res.json();
|
|
document.getElementById('botSettings').textContent = JSON.stringify(json, null, 2);
|
|
}
|
|
|
|
async function analyze() {
|
|
const res = await fetch(`/api/v1/bot/analyze?market=${getMarket()}`);
|
|
const json = await res.json();
|
|
document.getElementById('aiResult').textContent = JSON.stringify(json, null, 2);
|
|
}
|
|
|
|
async function decide() {
|
|
const res = await fetch(`/api/v1/bot/decide?market=${getMarket()}`, { method: 'POST' });
|
|
const json = await res.json();
|
|
document.getElementById('aiResult').textContent = JSON.stringify(json, null, 2);
|
|
}
|
|
|
|
async function autoDemoTrade() {
|
|
const res = await fetch(`/api/v1/bot/auto-demo-trade?market=${getMarket()}`, { method: 'POST' });
|
|
const json = await res.json();
|
|
document.getElementById('aiResult').textContent = JSON.stringify(json, null, 2);
|
|
await loadAccount();
|
|
await loadMarkers(getMarket());
|
|
}
|
|
|
|
async function setBotEnabled(enabled) {
|
|
await fetch(`/api/v1/bot/settings?enabled=${enabled}`, { method: 'POST' });
|
|
await loadBotSettings();
|
|
}
|
|
|
|
async function enableLive() {
|
|
const ack = encodeURIComponent(document.getElementById('ack').value.trim());
|
|
const res = await fetch(`/api/v1/bot/live/enable?ack=${ack}`, { method: 'POST' });
|
|
const json = await res.json();
|
|
document.getElementById('aiResult').textContent = JSON.stringify(json, null, 2);
|
|
await loadBotSettings();
|
|
}
|
|
|
|
async function disableLive() {
|
|
const res = await fetch('/api/v1/bot/live/disable', { method: 'POST' });
|
|
const json = await res.json();
|
|
document.getElementById('aiResult').textContent = JSON.stringify(json, null, 2);
|
|
await loadBotSettings();
|
|
}
|
|
|
|
async function loadMarkers(market) {
|
|
const res = await fetch(`/api/v1/dashboard/markers?market=${market}&limit=100`);
|
|
const json = await res.json();
|
|
const markers = (json.items || []).map(item => ({
|
|
time: item.time,
|
|
position: item.side === 'buy' ? 'belowBar' : 'aboveBar',
|
|
shape: item.side === 'buy' ? 'arrowUp' : 'arrowDown',
|
|
text: item.text,
|
|
}));
|
|
candleSeries.setMarkers(markers);
|
|
}
|
|
|
|
function connectWs(market) {
|
|
if (socket) socket.close();
|
|
const proto = location.protocol === 'https:' ? 'wss' : 'ws';
|
|
socket = new WebSocket(`${proto}://${location.host}/ws/market/${market}`);
|
|
|
|
socket.onopen = () => {
|
|
document.getElementById('status').textContent = `live: ${market}`;
|
|
};
|
|
|
|
socket.onmessage = (event) => {
|
|
const msg = JSON.parse(event.data);
|
|
document.getElementById('ticker').textContent = JSON.stringify(msg, null, 2).slice(0, 1600);
|
|
document.getElementById('source').textContent = `source: ${msg.source || msg.type}`;
|
|
|
|
if (msg.type === 'live_price') {
|
|
updateLiveCandle(Number(msg.price), Number(msg.ts));
|
|
}
|
|
if (msg.type === 'stream_warning') {
|
|
document.getElementById('status').textContent = 'fallback HTTP';
|
|
}
|
|
|
|
const acc = msg.demo_account;
|
|
if (acc) {
|
|
document.getElementById('balance').textContent = `${Number(acc.balance).toFixed(2)} ${acc.quote_asset}`;
|
|
document.getElementById('equity').textContent = `${Number(acc.equity).toFixed(2)} ${acc.quote_asset}`;
|
|
}
|
|
if (msg.bot_state) {
|
|
document.getElementById('botSettings').textContent = JSON.stringify(msg.bot_state, null, 2);
|
|
}
|
|
};
|
|
|
|
socket.onclose = () => {
|
|
document.getElementById('status').textContent = 'соединение закрыто';
|
|
};
|
|
|
|
socket.onerror = () => {
|
|
document.getElementById('status').textContent = 'ошибка ws';
|
|
};
|
|
}
|
|
|
|
window.addEventListener('resize', () => chart.applyOptions({ width: chartEl.clientWidth, height: chartEl.clientHeight }));
|
|
loadMarket();
|
|
</script>
|
|
</body>
|
|
</html>
|