test / app.py
Alvin3y1's picture
Update app.py
a01765b verified
raw
history blame
22.2 kB
import asyncio
import json
import logging
import time
import bisect
from aiohttp import web
import websockets
# --- Configuration ---
SYMBOL_KRAKEN = "BTC/USD"
PORT = 7860
HISTORY_LENGTH = 300
BROADCAST_RATE = 0.1 # 10Hz updates
# --- Logging ---
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
# --- In-Memory State ---
market_state = {
"bids": {},
"asks": {},
"history": [], # Price history: {t, p}
"liq_history": [], # Liquidity Trend history: {t, v}
"current_mid": 0.0,
"prev_mid": 0.0,
"ready": False
}
connected_clients = set()
# --- AI Logic Helper ---
def analyze_structure(diff_x, diff_y, current_mid):
if not diff_y or len(diff_y) < 5:
return None
# 1. Momentum Projection
net_total = diff_y[-1] # Total cumulative delta at max depth
momentum_shift = net_total * 0.2
projected_price = current_mid + momentum_shift
# 2. Find Structural Reversals
support_level = None
resistance_level = None
scan_limit = len(diff_y) // 2
for i in range(1, scan_limit):
prev_val = diff_y[i-1]
curr_val = diff_y[i]
dist = diff_x[i]
if prev_val > 0 and curr_val < 0 and resistance_level is None:
resistance_level = current_mid + dist
if prev_val < 0 and curr_val > 0 and support_level is None:
support_level = current_mid - dist
return {
"projected": projected_price,
"support": support_level,
"resistance": resistance_level,
"net_score": net_total
}
def process_market_data():
if not market_state['ready']:
return {"error": "Initializing..."}
mid = market_state['current_mid']
# Snapshot Top 300 for Depth Chart
raw_bids = sorted(market_state['bids'].items(), key=lambda x: -x[0])[:300]
raw_asks = sorted(market_state['asks'].items(), key=lambda x: x[0])[:300]
# Calculate Cumulative Volume
d_b_x, d_b_y, cum = [], [], 0
for p, q in raw_bids:
d = mid - p
if d >= 0:
cum += q
d_b_x.append(d); d_b_y.append(cum)
d_a_x, d_a_y, cum = [], [], 0
for p, q in raw_asks:
d = p - mid
if d >= 0:
cum += q
d_a_x.append(d); d_a_y.append(cum)
# Calculate Net Liquidity Curve (Depth)
diff_x, diff_y = [], []
if d_b_x and d_a_x:
max_dist = min(d_b_x[-1], d_a_x[-1])
step_size = max_dist / 100
steps = [i * step_size for i in range(1, 101)]
for s in steps:
idx_b = bisect.bisect_right(d_b_x, s)
vol_b = d_b_y[idx_b-1] if idx_b > 0 else 0
idx_a = bisect.bisect_right(d_a_x, s)
vol_a = d_a_y[idx_a-1] if idx_a > 0 else 0
diff_x.append(s)
diff_y.append(vol_b - vol_a)
analysis = analyze_structure(diff_x, diff_y, mid)
# Store Liquidity Trend for history
now = time.time()
if analysis:
# Update Trend History if needed (throttle slightly to match graph res)
if not market_state['liq_history'] or (now - market_state['liq_history'][-1]['t'] > 0.5):
market_state['liq_history'].append({'t': now, 'v': analysis['net_score']})
if len(market_state['liq_history']) > HISTORY_LENGTH:
market_state['liq_history'].pop(0)
return {
"mid": mid,
"history": market_state['history'], # Price History
"liq_history": market_state['liq_history'], # Net Liq History
"diff": { "x": diff_x, "y": diff_y }, # Depth Snapshot
"analysis": analysis
}
# --- HTML Frontend ---
HTML_PAGE = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AI Liquidity Dashboard | {SYMBOL_KRAKEN}</title>
<script src="https://unpkg.com/lightweight-charts@4.1.1/dist/lightweight-charts.standalone.production.js"></script>
<style>
:root {{
--bg-color: #0b0c10;
--panel-bg: #1f2833;
--text-main: #c5c6c7;
--accent-green: #66fcf1;
--accent-red: #ff3b3b;
--border: #2d3842;
}}
body {{ margin: 0; padding: 0; background-color: var(--bg-color); color: var(--text-main); font-family: monospace; overflow: hidden; height: 100vh; width: 100vw; }}
/* Updated Grid for 3 Rows in Main Column */
.grid-container {{
display: grid;
grid-template-columns: 3fr 1fr;
grid-template-rows: 2fr 1fr 1fr; /* Price (50%), Trend (25%), Depth (25%) */
gap: 4px;
height: 100vh;
padding: 4px;
box-sizing: border-box;
}}
.panel {{ background: #12141a; border: 1px solid var(--border); border-radius: 4px; position: relative; display: flex; flex-direction: column; overflow: hidden; }}
#p-price {{ grid-column: 1 / 2; grid-row: 1 / 2; }}
#p-trend {{ grid-column: 1 / 2; grid-row: 2 / 3; }} /* NEW PANEL */
#p-depth {{ grid-column: 1 / 2; grid-row: 3 / 4; }}
#p-stats {{ grid-column: 2 / 3; grid-row: 1 / 4; border-left: 2px solid #45a29e; }}
.panel-header {{ padding: 6px 10px; background: #0f1116; border-bottom: 1px solid var(--border); font-size: 11px; font-weight: bold; display: flex; justify-content: space-between; color: var(--accent-green); text-transform: uppercase; }}
#tv-price, #tv-trend, #tv-depth {{ flex: 1; width: 100%; position: relative; }}
.stats-content {{ padding: 15px; overflow-y: auto; flex: 1; }}
.stat-box {{ margin-bottom: 20px; padding: 10px; background: rgba(255,255,255,0.02); border-radius: 4px; }}
.stat-label {{ font-size: 11px; color: #666; display: block; margin-bottom: 4px; }}
.stat-value {{ font-size: 24px; font-weight: bold; }}
.green {{ color: var(--accent-green); }}
.red {{ color: var(--accent-red); }}
.terminal-box {{ margin-top: auto; font-size: 11px; height: 200px; display: flex; flex-direction: column; }}
.term-header {{ border-bottom: 1px dashed #444; margin-bottom: 5px; opacity: 0.7; }}
#term-logs {{ flex: 1; overflow-y: hidden; display: flex; flex-direction: column-reverse; }}
.log-line {{ margin-top: 4px; padding-left: 8px; border-left: 2px solid #333; }}
#loader {{ position: absolute; top:0; left:0; width:100%; height:100%; background: rgba(0,0,0,0.95); z-index: 999; display: flex; flex-direction: column; justify-content: center; align-items: center; color: var(--accent-green); }}
</style>
</head>
<body>
<div id="loader">
<div style="font-size: 24px;">ESTABLISHING UPLINK...</div>
<div id="loading-status">Connecting to WebSocket Stream...</div>
</div>
<div class="grid-container">
<!-- ROW 1: PRICE -->
<div id="p-price" class="panel">
<div class="panel-header"><span>BTC/USD Price Action</span><span id="live-price">---</span></div>
<div id="tv-price"></div>
</div>
<!-- ROW 2: LIQUIDITY TREND (NEW) -->
<div id="p-trend" class="panel">
<div class="panel-header"><span>Net Liquidity Trend (Time Series)</span><span id="live-trend">0.0</span></div>
<div id="tv-trend"></div>
</div>
<!-- ROW 3: DEPTH STRUCTURE -->
<div id="p-depth" class="panel">
<div class="panel-header"><span>Market Depth (Snapshot)</span><span>Range: $100</span></div>
<div id="tv-depth"></div>
</div>
<!-- COL 2: STATS -->
<div id="p-stats" class="panel">
<div class="panel-header">ANALYTICS ENGINE</div>
<div class="stats-content">
<div class="stat-box">
<span class="stat-label">NET LIQUIDITY SCORE</span>
<span id="score-val" class="stat-value">0</span>
</div>
<div class="stat-box">
<span class="stat-label">KEY STRUCTURE</span>
<div style="display:flex; justify-content:space-between;"><span>RESIST:</span><span id="res-val" class="red">---</span></div>
<div style="display:flex; justify-content:space-between;"><span>SUPPORT:</span><span id="sup-val" class="green">---</span></div>
</div>
<div class="stat-box" style="border: 1px solid #444;">
<span class="stat-label" style="color:var(--accent-green);">AI PROJECTION</span>
<span id="proj-val" class="stat-value">---</span>
</div>
<div class="terminal-box">
<div class="term-header">> SYSTEM LOGS</div>
<div id="term-logs"></div>
</div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {{
const dom = {{
loader: document.getElementById('loader'),
status: document.getElementById('loading-status'),
price: document.getElementById('live-price'),
trend: document.getElementById('live-trend'),
scoreVal: document.getElementById('score-val'),
resVal: document.getElementById('res-val'),
supVal: document.getElementById('sup-val'),
projVal: document.getElementById('proj-val'),
logs: document.getElementById('term-logs')
}};
// --- CHART INIT ---
const chartCommon = {{
layout: {{ background: {{ type: 'solid', color: '#12141a' }}, textColor: '#888' }},
grid: {{ vertLines: {{ color: '#1f2833' }}, horzLines: {{ color: '#1f2833' }} }},
rightPriceScale: {{ borderColor: '#2d3842' }},
timeScale: {{ borderColor: '#2d3842', timeVisible: true, secondsVisible: true }},
crosshair: {{ mode: 0 }}
}};
// 1. Price Chart
const priceChart = LightweightCharts.createChart(document.getElementById('tv-price'), chartCommon);
const priceSeries = priceChart.addLineSeries({{ color: '#2962FF', lineWidth: 2 }});
const predSeries = priceChart.addLineSeries({{ color: '#ff9800', lineWidth: 2, lineStyle: 2 }});
let supportLine = null, resistanceLine = null;
// 2. Trend Chart (Baseline Series)
const trendChart = LightweightCharts.createChart(document.getElementById('tv-trend'), {{
...chartCommon,
rightPriceScale: {{ scaleMargins: {{ top: 0.1, bottom: 0.1 }} }}
}});
// Baseline: Positive = Green, Negative = Red
const trendSeries = trendChart.addBaselineSeries({{
baseValue: {{ type: 'price', price: 0 }},
topLineColor: '#66fcf1', topFillColor1: 'rgba(102, 252, 241, 0.28)', topFillColor2: 'rgba(102, 252, 241, 0.05)',
bottomLineColor: '#ff3b3b', bottomFillColor1: 'rgba(255, 59, 59, 0.28)', bottomFillColor2: 'rgba(255, 59, 59, 0.05)',
}});
// 3. Depth Chart (Custom Format)
const depthChart = LightweightCharts.createChart(document.getElementById('tv-depth'), {{
...chartCommon,
timeScale: {{ tickMarkFormatter: (time) => parseFloat(time).toFixed(0) }},
localization: {{ timeFormatter: (time) => 'Dist: $' + parseFloat(time).toFixed(2) }}
}});
const bullSeries = depthChart.addAreaSeries({{ topColor: 'rgba(102, 252, 241, 0.4)', bottomColor: 'rgba(102, 252, 241, 0.0)', lineColor: '#66fcf1', lineWidth: 2 }});
const bearSeries = depthChart.addAreaSeries({{ topColor: 'rgba(255, 59, 59, 0.4)', bottomColor: 'rgba(255, 59, 59, 0.0)', lineColor: '#ff3b3b', lineWidth: 2 }});
// Auto-Resize
const resizeObserver = new ResizeObserver(entries => {{
for(let entry of entries) {{
const {{width, height}} = entry.contentRect;
if(entry.target.id === 'tv-price') priceChart.applyOptions({{width, height}});
if(entry.target.id === 'tv-trend') trendChart.applyOptions({{width, height}});
if(entry.target.id === 'tv-depth') depthChart.applyOptions({{width, height}});
}}
}});
['tv-price', 'tv-trend', 'tv-depth'].forEach(id => resizeObserver.observe(document.getElementById(id)));
// --- WEBSOCKET ---
function log(msg, type='neutral') {{
const div = document.createElement('div');
div.className = 'log-line';
div.style.borderLeftColor = type === 'bull' ? '#66fcf1' : type === 'bear' ? '#ff3b3b' : '#333';
div.innerHTML = `<span style="opacity:0.5">${{new Date().toLocaleTimeString()}}</span> ${{msg}}`;
dom.logs.prepend(div);
if (dom.logs.children.length > 15) dom.logs.removeChild(dom.logs.lastChild);
}}
function connect() {{
const proto = window.location.protocol === 'https:' ? 'wss' : 'ws';
const url = `${{proto}}://${{window.location.host}}/ws`;
const ws = new WebSocket(url);
ws.onopen = () => {{ dom.status.innerText = "Receiving Data Stream..."; }};
ws.onclose = () => {{ dom.loader.style.display = 'flex'; dom.status.innerText = "Reconnecting..."; setTimeout(connect, 3000); }};
ws.onmessage = (event) => {{
const data = JSON.parse(event.data);
if (data.error) return;
dom.loader.style.display = 'none';
// 1. Price Data
const cleanHistory = [];
const seen = new Set();
data.history.forEach(d => {{
const t = Math.floor(d.t);
if (!seen.has(t)) {{ seen.add(t); cleanHistory.push({{ time: t, value: d.p }}); }}
}});
if (cleanHistory.length) {{
priceSeries.setData(cleanHistory);
const last = cleanHistory[cleanHistory.length-1];
dom.price.innerText = last.value.toLocaleString(undefined, {{minimumFractionDigits: 2}});
// Analysis Overlays
if (data.analysis) {{
const {{ projected, support, resistance, net_score }} = data.analysis;
predSeries.setData([last, {{ time: last.time + 60, value: projected }}]);
dom.projVal.innerText = projected.toFixed(0);
// Sync Trend Chart Value
dom.trend.innerText = net_score.toFixed(1);
dom.trend.style.color = net_score >= 0 ? 'var(--accent-green)' : 'var(--accent-red)';
dom.scoreVal.innerText = net_score.toFixed(1);
dom.scoreVal.className = net_score > 0 ? "stat-value green" : "stat-value red";
// S/R Lines
if (support) {{
dom.supVal.innerText = support.toFixed(0);
if (!supportLine) supportLine = priceSeries.createPriceLine({{ price: support, color: '#00e676', title: 'SUP' }});
else supportLine.applyOptions({{ price: support }});
}} else {{
dom.supVal.innerText = '---';
if (supportLine) {{ priceSeries.removePriceLine(supportLine); supportLine = null; }}
}}
if (resistance) {{
dom.resVal.innerText = resistance.toFixed(0);
if (!resistanceLine) resistanceLine = priceSeries.createPriceLine({{ price: resistance, color: '#ff1744', title: 'RES' }});
else resistanceLine.applyOptions({{ price: resistance }});
}} else {{
dom.resVal.innerText = '---';
if (resistanceLine) {{ priceSeries.removePriceLine(resistanceLine); resistanceLine = null; }}
}}
// AI Logs
if (Math.abs(net_score) > 50 && Math.random() > 0.98) {{
log(net_score > 0 ? "Momentum: Strong Buy" : "Momentum: Strong Sell", net_score > 0 ? 'bull' : 'bear');
}}
}}
}}
// 2. Liquidity Trend Data
if (data.liq_history) {{
const trendData = [];
const seenT = new Set();
data.liq_history.forEach(d => {{
const t = Math.floor(d.t);
if(!seenT.has(t)) {{ seenT.add(t); trendData.push({{ time: t, value: d.v }}); }}
}});
if (trendData.length) trendSeries.setData(trendData);
}}
// 3. Depth Snapshot
if (data.diff && data.diff.x.length) {{
const bull = [], bear = [];
for (let i = 0; i < data.diff.x.length; i++) {{
const x = data.diff.x[i];
const y = data.diff.y[i];
if (y >= 0) {{ bull.push({{ time: x, value: y }}); bear.push({{ time: x, value: 0 }}); }}
else {{ bull.push({{ time: x, value: 0 }}); bear.push({{ time: x, value: y }}); }}
}}
bullSeries.setData(bull);
bearSeries.setData(bear);
}}
}};
}}
connect();
}});
</script>
</body>
</html>
"""
async def kraken_worker():
global market_state
while True:
try:
async with websockets.connect("wss://ws.kraken.com/v2") as ws:
logging.info(f"πŸ”Œ Connected to Kraken ({SYMBOL_KRAKEN})")
await ws.send(json.dumps({
"method": "subscribe",
"params": {"channel": "book", "symbol": [SYMBOL_KRAKEN], "depth": 500}
}))
async for message in ws:
payload = json.loads(message)
channel = payload.get("channel")
data = payload.get("data", [])
if channel == "book":
for item in data:
for bid in item.get('bids', []):
q, p = float(bid['qty']), float(bid['price'])
if q == 0: market_state['bids'].pop(p, None)
else: market_state['bids'][p] = q
for ask in item.get('asks', []):
q, p = float(ask['qty']), float(ask['price'])
if q == 0: market_state['asks'].pop(p, None)
else: market_state['asks'][p] = q
if market_state['bids'] and market_state['asks']:
best_bid = max(market_state['bids'].keys())
best_ask = min(market_state['asks'].keys())
mid = (best_bid + best_ask) / 2
market_state['prev_mid'] = market_state['current_mid']
market_state['current_mid'] = mid
market_state['ready'] = True
now = time.time()
if not market_state['history'] or (now - market_state['history'][-1]['t'] > 0.5):
market_state['history'].append({'t': now, 'p': mid})
if len(market_state['history']) > HISTORY_LENGTH:
market_state['history'].pop(0)
except Exception as e:
logging.warning(f"⚠️ Reconnecting: {e}")
await asyncio.sleep(3)
async def broadcast_worker():
while True:
if connected_clients and market_state['ready']:
payload = process_market_data()
msg = json.dumps(payload)
for ws in list(connected_clients):
try: await ws.send_str(msg)
except: pass
await asyncio.sleep(BROADCAST_RATE)
async def websocket_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
connected_clients.add(ws)
try:
async for msg in ws:
pass
finally:
connected_clients.remove(ws)
return ws
async def handle_index(request):
return web.Response(text=HTML_PAGE, content_type='text/html')
async def start_background(app):
app['kraken_task'] = asyncio.create_task(kraken_worker())
app['broadcast_task'] = asyncio.create_task(broadcast_worker())
async def cleanup_background(app):
app['kraken_task'].cancel()
app['broadcast_task'].cancel()
try: await app['kraken_task']; await app['broadcast_task']
except: pass
async def main():
app = web.Application()
app.router.add_get('/', handle_index)
app.router.add_get('/ws', websocket_handler)
app.on_startup.append(start_background)
app.on_cleanup.append(cleanup_background)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, '0.0.0.0', PORT)
await site.start()
print(f"πŸš€ AI Dashboard: http://localhost:{PORT}")
await asyncio.Event().wait()
if __name__ == "__main__":
try: asyncio.run(main())
except KeyboardInterrupt: pass