Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import json
|
|
| 3 |
import logging
|
| 4 |
import time
|
| 5 |
import bisect
|
|
|
|
| 6 |
from aiohttp import web
|
| 7 |
import websockets
|
| 8 |
|
|
@@ -28,7 +29,7 @@ HTML_PAGE = f"""
|
|
| 28 |
<!DOCTYPE html>
|
| 29 |
<html>
|
| 30 |
<head>
|
| 31 |
-
<title>BTC-USD Gradient
|
| 32 |
<script src="https://cdn.plot.ly/plotly-2.24.1.min.js"></script>
|
| 33 |
<style>
|
| 34 |
body {{ margin: 0; padding: 0; background-color: #0e0e0e; color: #ccc; font-family: sans-serif; overflow: hidden; }}
|
|
@@ -75,7 +76,7 @@ HTML_PAGE = f"""
|
|
| 75 |
<div id="vol-chart" class="chart"></div>
|
| 76 |
</div>
|
| 77 |
<div class="col">
|
| 78 |
-
<!-- Bottom Right: Differentiation
|
| 79 |
<div id="deriv-chart" class="chart"></div>
|
| 80 |
</div>
|
| 81 |
</div>
|
|
@@ -112,7 +113,7 @@ HTML_PAGE = f"""
|
|
| 112 |
return;
|
| 113 |
}}
|
| 114 |
|
| 115 |
-
statusDiv.innerHTML = `Mid: <span class="${{data.mid >= data.prev_mid ? 'green' : 'red'}}">$${{data.mid.toLocaleString(undefined, {{minimumFractionDigits: 2}})}}</span> |
|
| 116 |
|
| 117 |
// 1. PRICE HISTORY
|
| 118 |
if (!initPrice) {{
|
|
@@ -138,21 +139,21 @@ HTML_PAGE = f"""
|
|
| 138 |
if (!initVol) {{ Plotly.newPlot(volDiv, tracesVol, layoutVol, commonConfig); initVol = true; }}
|
| 139 |
else {{ Plotly.react(volDiv, tracesVol, layoutVol, commonConfig); }}
|
| 140 |
|
| 141 |
-
// 4.
|
| 142 |
-
// This shows the rate of change of pressure. Large spikes = walls.
|
| 143 |
const layoutDeriv = {{ ...commonLayout,
|
| 144 |
-
title: '<b>
|
| 145 |
xaxis: {{title: 'Distance ($)', gridcolor:'#222'}},
|
| 146 |
-
yaxis: {{title: '
|
| 147 |
}};
|
| 148 |
|
|
|
|
| 149 |
const traceDeriv = {{
|
| 150 |
x: data.deriv.x,
|
| 151 |
y: data.deriv.y,
|
| 152 |
type: 'scatter',
|
| 153 |
mode: 'lines',
|
| 154 |
fill: 'tozeroy',
|
| 155 |
-
line: {{color: '#e040fb', width: 1.5}}
|
| 156 |
}};
|
| 157 |
|
| 158 |
if (!initDeriv) {{ Plotly.newPlot(derivDiv, [traceDeriv], layoutDeriv, commonConfig); initDeriv = true; }}
|
|
@@ -245,7 +246,7 @@ async def handle_data(request):
|
|
| 245 |
|
| 246 |
if d_b_x and d_a_x:
|
| 247 |
max_dist = min(d_b_x[-1], d_a_x[-1])
|
| 248 |
-
# Use 100
|
| 249 |
step_size = max_dist / 100
|
| 250 |
steps = [i * step_size for i in range(1, 101)]
|
| 251 |
|
|
@@ -262,28 +263,28 @@ async def handle_data(request):
|
|
| 262 |
# 2. Imbalance %
|
| 263 |
total_vol = vol_b + vol_a
|
| 264 |
pct = ((vol_b - vol_a) / total_vol) * 100 if total_vol > 0 else 0
|
| 265 |
-
|
| 266 |
imb_x.append(s)
|
| 267 |
imb_y.append(pct)
|
| 268 |
|
| 269 |
-
# 3.
|
| 270 |
-
#
|
| 271 |
if i > 0:
|
| 272 |
change = pct - last_pct
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
deriv_x.append(s)
|
| 274 |
-
deriv_y.append(
|
| 275 |
|
| 276 |
last_pct = pct
|
| 277 |
|
| 278 |
-
# Find max gradient for display
|
| 279 |
-
max_grad = max(deriv_y) if deriv_y else 0
|
| 280 |
-
|
| 281 |
return web.json_response({
|
| 282 |
"mid": mid,
|
| 283 |
"prev_mid": market_state['history'][-2]['p'] if len(market_state['history']) > 1 else mid,
|
| 284 |
"imbalance_vol": { "dist_bids": d_b_x, "vol_bids": d_b_y, "dist_asks": d_a_x, "vol_asks": d_a_y },
|
| 285 |
"imbalance_pct": { "x": imb_x, "y": imb_y },
|
| 286 |
-
"deriv": { "x": deriv_x, "y": deriv_y, "
|
| 287 |
"history": market_state['history']
|
| 288 |
})
|
| 289 |
|
|
@@ -307,7 +308,7 @@ async def main():
|
|
| 307 |
site = web.TCPSite(runner, '0.0.0.0', PORT)
|
| 308 |
await site.start()
|
| 309 |
|
| 310 |
-
print(f"π BTC-USD
|
| 311 |
await asyncio.Event().wait()
|
| 312 |
|
| 313 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import logging
|
| 4 |
import time
|
| 5 |
import bisect
|
| 6 |
+
import math
|
| 7 |
from aiohttp import web
|
| 8 |
import websockets
|
| 9 |
|
|
|
|
| 29 |
<!DOCTYPE html>
|
| 30 |
<html>
|
| 31 |
<head>
|
| 32 |
+
<title>BTC-USD Normalized Gradient</title>
|
| 33 |
<script src="https://cdn.plot.ly/plotly-2.24.1.min.js"></script>
|
| 34 |
<style>
|
| 35 |
body {{ margin: 0; padding: 0; background-color: #0e0e0e; color: #ccc; font-family: sans-serif; overflow: hidden; }}
|
|
|
|
| 76 |
<div id="vol-chart" class="chart"></div>
|
| 77 |
</div>
|
| 78 |
<div class="col">
|
| 79 |
+
<!-- Bottom Right: Normalized Differentiation -->
|
| 80 |
<div id="deriv-chart" class="chart"></div>
|
| 81 |
</div>
|
| 82 |
</div>
|
|
|
|
| 113 |
return;
|
| 114 |
}}
|
| 115 |
|
| 116 |
+
statusDiv.innerHTML = `Mid: <span class="${{data.mid >= data.prev_mid ? 'green' : 'red'}}">$${{data.mid.toLocaleString(undefined, {{minimumFractionDigits: 2}})}}</span> | Gradient: ${{data.deriv.last_val ? data.deriv.last_val.toFixed(3) : 0}}`;
|
| 117 |
|
| 118 |
// 1. PRICE HISTORY
|
| 119 |
if (!initPrice) {{
|
|
|
|
| 139 |
if (!initVol) {{ Plotly.newPlot(volDiv, tracesVol, layoutVol, commonConfig); initVol = true; }}
|
| 140 |
else {{ Plotly.react(volDiv, tracesVol, layoutVol, commonConfig); }}
|
| 141 |
|
| 142 |
+
// 4. NORMALIZED GRADIENT (-1 to +1)
|
|
|
|
| 143 |
const layoutDeriv = {{ ...commonLayout,
|
| 144 |
+
title: '<b>Gradient (Normalized -1 to +1)</b>',
|
| 145 |
xaxis: {{title: 'Distance ($)', gridcolor:'#222'}},
|
| 146 |
+
yaxis: {{title: 'Norm Rate', range: [-1.1, 1.1], gridcolor:'#222', zeroline: true, zerolinecolor: '#fff'}}
|
| 147 |
}};
|
| 148 |
|
| 149 |
+
// Color line based on positive/negative?
|
| 150 |
const traceDeriv = {{
|
| 151 |
x: data.deriv.x,
|
| 152 |
y: data.deriv.y,
|
| 153 |
type: 'scatter',
|
| 154 |
mode: 'lines',
|
| 155 |
fill: 'tozeroy',
|
| 156 |
+
line: {{color: '#e040fb', width: 1.5}}
|
| 157 |
}};
|
| 158 |
|
| 159 |
if (!initDeriv) {{ Plotly.newPlot(derivDiv, [traceDeriv], layoutDeriv, commonConfig); initDeriv = true; }}
|
|
|
|
| 246 |
|
| 247 |
if d_b_x and d_a_x:
|
| 248 |
max_dist = min(d_b_x[-1], d_a_x[-1])
|
| 249 |
+
# Use 100 steps
|
| 250 |
step_size = max_dist / 100
|
| 251 |
steps = [i * step_size for i in range(1, 101)]
|
| 252 |
|
|
|
|
| 263 |
# 2. Imbalance %
|
| 264 |
total_vol = vol_b + vol_a
|
| 265 |
pct = ((vol_b - vol_a) / total_vol) * 100 if total_vol > 0 else 0
|
|
|
|
| 266 |
imb_x.append(s)
|
| 267 |
imb_y.append(pct)
|
| 268 |
|
| 269 |
+
# 3. Normalized Gradient (tanh scaling)
|
| 270 |
+
# We use tanh(change / scale_factor) to fit into -1..1
|
| 271 |
if i > 0:
|
| 272 |
change = pct - last_pct
|
| 273 |
+
# Scaling factor of 2.0 provides good sensitivity for % changes
|
| 274 |
+
# A change of 2% -> 0.76 (Strong)
|
| 275 |
+
# A change of 5% -> 0.98 (Max Saturated)
|
| 276 |
+
norm_change = math.tanh(change / 2.0)
|
| 277 |
deriv_x.append(s)
|
| 278 |
+
deriv_y.append(norm_change)
|
| 279 |
|
| 280 |
last_pct = pct
|
| 281 |
|
|
|
|
|
|
|
|
|
|
| 282 |
return web.json_response({
|
| 283 |
"mid": mid,
|
| 284 |
"prev_mid": market_state['history'][-2]['p'] if len(market_state['history']) > 1 else mid,
|
| 285 |
"imbalance_vol": { "dist_bids": d_b_x, "vol_bids": d_b_y, "dist_asks": d_a_x, "vol_asks": d_a_y },
|
| 286 |
"imbalance_pct": { "x": imb_x, "y": imb_y },
|
| 287 |
+
"deriv": { "x": deriv_x, "y": deriv_y, "last_val": deriv_y[-1] if deriv_y else 0 },
|
| 288 |
"history": market_state['history']
|
| 289 |
})
|
| 290 |
|
|
|
|
| 308 |
site = web.TCPSite(runner, '0.0.0.0', PORT)
|
| 309 |
await site.start()
|
| 310 |
|
| 311 |
+
print(f"π BTC-USD Normalized Dashboard: http://localhost:{PORT}")
|
| 312 |
await asyncio.Event().wait()
|
| 313 |
|
| 314 |
if __name__ == "__main__":
|