Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import json
|
|
| 3 |
import logging
|
| 4 |
import time
|
| 5 |
import bisect
|
| 6 |
-
import math
|
| 7 |
from aiohttp import web
|
| 8 |
import websockets
|
| 9 |
|
|
@@ -29,7 +28,7 @@ HTML_PAGE = f"""
|
|
| 29 |
<!DOCTYPE html>
|
| 30 |
<html>
|
| 31 |
<head>
|
| 32 |
-
<title>BTC-USD 6-
|
| 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; }}
|
|
@@ -73,20 +72,19 @@ HTML_PAGE = f"""
|
|
| 73 |
<div id="vol-chart" class="chart"></div>
|
| 74 |
</div>
|
| 75 |
<div class="col">
|
| 76 |
-
<!-- Net Difference (Bids - Asks) -->
|
| 77 |
<div id="diff-chart" class="chart"></div>
|
| 78 |
</div>
|
| 79 |
</div>
|
| 80 |
|
| 81 |
-
<!-- ROW 3 -->
|
| 82 |
<div class="row">
|
| 83 |
<div class="col">
|
| 84 |
-
<!--
|
| 85 |
-
<div id="
|
| 86 |
</div>
|
| 87 |
<div class="col">
|
| 88 |
-
<!--
|
| 89 |
-
<div id="
|
| 90 |
</div>
|
| 91 |
</div>
|
| 92 |
</div>
|
|
@@ -96,13 +94,13 @@ HTML_PAGE = f"""
|
|
| 96 |
const imbPercentDiv = document.getElementById('imb-percent-chart');
|
| 97 |
const volDiv = document.getElementById('vol-chart');
|
| 98 |
const diffDiv = document.getElementById('diff-chart');
|
| 99 |
-
const
|
| 100 |
-
const
|
| 101 |
|
| 102 |
const statusDiv = document.getElementById('status');
|
| 103 |
|
| 104 |
let initPrice = false, initImbP = false, initVol = false;
|
| 105 |
-
let initDiff = false,
|
| 106 |
|
| 107 |
const commonConfig = {{ responsive: true, displayModeBar: false }};
|
| 108 |
const commonLayout = {{
|
|
@@ -125,18 +123,22 @@ HTML_PAGE = f"""
|
|
| 125 |
return;
|
| 126 |
}}
|
| 127 |
|
| 128 |
-
statusDiv.innerHTML = `Mid: <span class="${{data.mid >= data.prev_mid ? 'green' : 'red'}}">$${{data.mid.toLocaleString(undefined, {{minimumFractionDigits: 2}})}}</span> |
|
| 129 |
|
| 130 |
// 1. PRICE HISTORY
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
// 2. IMBALANCE %
|
| 136 |
-
const traceImbP = {{ x: data.imbalance_pct.x, y: data.imbalance_pct.y, type: 'scatter', mode: 'lines', fill: 'tozeroy', line: {{color: '#ffeb3b'}} }};
|
| 137 |
const layoutImbP = {{ ...commonLayout, title: '<b>Pressure % (-100 to 100)</b>', yaxis: {{range: [-105, 105], gridcolor:'#222'}} }};
|
| 138 |
-
if (!initImbP) {{ Plotly.newPlot(imbPercentDiv, [
|
| 139 |
-
else {{ Plotly.react(imbPercentDiv, [
|
| 140 |
|
| 141 |
// 3. VOLUME BY DISTANCE
|
| 142 |
const tracesVol = [
|
|
@@ -151,16 +153,15 @@ HTML_PAGE = f"""
|
|
| 151 |
if (!initDiff) {{ Plotly.newPlot(diffDiv, [traceDiff], {{ ...commonLayout, title: '<b>Net Liquidity (Bids - Asks)</b>' }}, commonConfig); initDiff = true; }}
|
| 152 |
else {{ Plotly.react(diffDiv, [traceDiff], {{ ...commonLayout, title: '<b>Net Liquidity (Bids - Asks)</b>' }}, commonConfig); }}
|
| 153 |
|
| 154 |
-
// 5.
|
| 155 |
-
const
|
| 156 |
-
if (!
|
| 157 |
-
else {{ Plotly.react(
|
| 158 |
|
| 159 |
-
// 6.
|
| 160 |
-
const
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
else {{ Plotly.react(meanDiv, [traceMean], layoutMean, commonConfig); }}
|
| 164 |
|
| 165 |
}} catch (e) {{ console.error("Fetch error:", e); }}
|
| 166 |
}}
|
|
@@ -246,9 +247,10 @@ async def handle_data(request):
|
|
| 246 |
# --- Calculations ---
|
| 247 |
imb_x, imb_y = [], []
|
| 248 |
diff_x, diff_y = [], []
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
|
|
|
| 252 |
|
| 253 |
if d_b_x and d_a_x:
|
| 254 |
max_dist = min(d_b_x[-1], d_a_x[-1])
|
|
@@ -273,24 +275,23 @@ async def handle_data(request):
|
|
| 273 |
diff = vol_b - vol_a
|
| 274 |
diff_x.append(s); diff_y.append(diff)
|
| 275 |
|
| 276 |
-
# 4.
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
avg_diff_x.append(s); avg_diff_y.append(avg_diff)
|
| 280 |
|
| 281 |
-
# 5.
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
|
| 286 |
return web.json_response({
|
| 287 |
"mid": mid,
|
| 288 |
"prev_mid": market_state['history'][-2]['p'] if len(market_state['history']) > 1 else mid,
|
| 289 |
"imbalance_vol": { "dist_bids": d_b_x, "vol_bids": d_b_y, "dist_asks": d_a_x, "vol_asks": d_a_y },
|
| 290 |
"imbalance_pct": { "x": imb_x, "y": imb_y },
|
| 291 |
-
"diff": { "x": diff_x, "y": diff_y
|
| 292 |
-
"
|
| 293 |
-
"
|
| 294 |
"history": market_state['history']
|
| 295 |
})
|
| 296 |
|
|
|
|
| 3 |
import logging
|
| 4 |
import time
|
| 5 |
import bisect
|
|
|
|
| 6 |
from aiohttp import web
|
| 7 |
import websockets
|
| 8 |
|
|
|
|
| 28 |
<!DOCTYPE html>
|
| 29 |
<html>
|
| 30 |
<head>
|
| 31 |
+
<title>BTC-USD 6-Layer Deep Dive</title>
|
| 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; }}
|
|
|
|
| 72 |
<div id="vol-chart" class="chart"></div>
|
| 73 |
</div>
|
| 74 |
<div class="col">
|
|
|
|
| 75 |
<div id="diff-chart" class="chart"></div>
|
| 76 |
</div>
|
| 77 |
</div>
|
| 78 |
|
| 79 |
+
<!-- ROW 3 (New) -->
|
| 80 |
<div class="row">
|
| 81 |
<div class="col">
|
| 82 |
+
<!-- Integral of Net Diff -->
|
| 83 |
+
<div id="int-diff-chart" class="chart"></div>
|
| 84 |
</div>
|
| 85 |
<div class="col">
|
| 86 |
+
<!-- Avg Integral (Integral / Steps) -->
|
| 87 |
+
<div id="avg-int-chart" class="chart"></div>
|
| 88 |
</div>
|
| 89 |
</div>
|
| 90 |
</div>
|
|
|
|
| 94 |
const imbPercentDiv = document.getElementById('imb-percent-chart');
|
| 95 |
const volDiv = document.getElementById('vol-chart');
|
| 96 |
const diffDiv = document.getElementById('diff-chart');
|
| 97 |
+
const intDiffDiv = document.getElementById('int-diff-chart');
|
| 98 |
+
const avgIntDiv = document.getElementById('avg-int-chart');
|
| 99 |
|
| 100 |
const statusDiv = document.getElementById('status');
|
| 101 |
|
| 102 |
let initPrice = false, initImbP = false, initVol = false;
|
| 103 |
+
let initDiff = false, initIntDiff = false, initAvgInt = false;
|
| 104 |
|
| 105 |
const commonConfig = {{ responsive: true, displayModeBar: false }};
|
| 106 |
const commonLayout = {{
|
|
|
|
| 123 |
return;
|
| 124 |
}}
|
| 125 |
|
| 126 |
+
statusDiv.innerHTML = `Mid: <span class="${{data.mid >= data.prev_mid ? 'green' : 'red'}}">$${{data.mid.toLocaleString(undefined, {{minimumFractionDigits: 2}})}}</span> | Avg Bias: ${{data.avg_int.last_val ? data.avg_int.last_val.toFixed(2) : 0}}`;
|
| 127 |
|
| 128 |
// 1. PRICE HISTORY
|
| 129 |
+
if (!initPrice) {{
|
| 130 |
+
Plotly.newPlot(priceDiv, [{{ x: data.history.map(d=>new Date(d.t*1000)), y: data.history.map(d=>d.p), type: 'scatter', mode:'lines', line: {{color: '#29b6f6', width: 2}} }}],
|
| 131 |
+
{{ ...commonLayout, title: '<b>Midprice</b>', xaxis: {{type:'date', gridcolor:'#222'}} }}, commonConfig);
|
| 132 |
+
initPrice = true;
|
| 133 |
+
}} else {{
|
| 134 |
+
Plotly.react(priceDiv, [{{ x: data.history.map(d=>new Date(d.t*1000)), y: data.history.map(d=>d.p), type: 'scatter', mode:'lines', line: {{color: '#29b6f6', width: 2}} }}],
|
| 135 |
+
{{ ...commonLayout, title: '<b>Midprice</b>', xaxis: {{type:'date', gridcolor:'#222'}} }}, commonConfig);
|
| 136 |
+
}}
|
| 137 |
|
| 138 |
// 2. IMBALANCE %
|
|
|
|
| 139 |
const layoutImbP = {{ ...commonLayout, title: '<b>Pressure % (-100 to 100)</b>', yaxis: {{range: [-105, 105], gridcolor:'#222'}} }};
|
| 140 |
+
if (!initImbP) {{ Plotly.newPlot(imbPercentDiv, [{{ x: data.imbalance_pct.x, y: data.imbalance_pct.y, type: 'scatter', mode: 'lines', fill: 'tozeroy', line: {{color: '#ffeb3b'}} }}], layoutImbP, commonConfig); initImbP = true; }}
|
| 141 |
+
else {{ Plotly.react(imbPercentDiv, [{{ x: data.imbalance_pct.x, y: data.imbalance_pct.y, type: 'scatter', mode: 'lines', fill: 'tozeroy', line: {{color: '#ffeb3b'}} }}], layoutImbP, commonConfig); }}
|
| 142 |
|
| 143 |
// 3. VOLUME BY DISTANCE
|
| 144 |
const tracesVol = [
|
|
|
|
| 153 |
if (!initDiff) {{ Plotly.newPlot(diffDiv, [traceDiff], {{ ...commonLayout, title: '<b>Net Liquidity (Bids - Asks)</b>' }}, commonConfig); initDiff = true; }}
|
| 154 |
else {{ Plotly.react(diffDiv, [traceDiff], {{ ...commonLayout, title: '<b>Net Liquidity (Bids - Asks)</b>' }}, commonConfig); }}
|
| 155 |
|
| 156 |
+
// 5. INTEGRAL OF NET DIFFERENCE
|
| 157 |
+
const traceIntDiff = {{ x: data.int_diff.x, y: data.int_diff.y, type: 'scatter', mode: 'lines', fill: 'tozeroy', line: {{color: '#00bcd4'}} }};
|
| 158 |
+
if (!initIntDiff) {{ Plotly.newPlot(intDiffDiv, [traceIntDiff], {{ ...commonLayout, title: '<b>Integrated Net Liquidity (Sum of Diff)</b>' }}, commonConfig); initIntDiff = true; }}
|
| 159 |
+
else {{ Plotly.react(intDiffDiv, [traceIntDiff], {{ ...commonLayout, title: '<b>Integrated Net Liquidity (Sum of Diff)</b>' }}, commonConfig); }}
|
| 160 |
|
| 161 |
+
// 6. AVERAGE INTEGRATED DIFFERENCE (Integral / Steps)
|
| 162 |
+
const traceAvgInt = {{ x: data.avg_int.x, y: data.avg_int.y, type: 'scatter', mode: 'lines', fill: 'tozeroy', line: {{color: '#ff9800'}} }};
|
| 163 |
+
if (!initAvgInt) {{ Plotly.newPlot(avgIntDiv, [traceAvgInt], {{ ...commonLayout, title: '<b>Avg Integrated Liq (Int / Steps)</b>' }}, commonConfig); initAvgInt = true; }}
|
| 164 |
+
else {{ Plotly.react(avgIntDiv, [traceAvgInt], {{ ...commonLayout, title: '<b>Avg Integrated Liq (Int / Steps)</b>' }}, commonConfig); }}
|
|
|
|
| 165 |
|
| 166 |
}} catch (e) {{ console.error("Fetch error:", e); }}
|
| 167 |
}}
|
|
|
|
| 247 |
# --- Calculations ---
|
| 248 |
imb_x, imb_y = [], []
|
| 249 |
diff_x, diff_y = [], []
|
| 250 |
+
int_diff_x, int_diff_y = [], []
|
| 251 |
+
avg_int_x, avg_int_y = [], []
|
| 252 |
+
|
| 253 |
+
running_diff_sum = 0
|
| 254 |
|
| 255 |
if d_b_x and d_a_x:
|
| 256 |
max_dist = min(d_b_x[-1], d_a_x[-1])
|
|
|
|
| 275 |
diff = vol_b - vol_a
|
| 276 |
diff_x.append(s); diff_y.append(diff)
|
| 277 |
|
| 278 |
+
# 4. Integral of Net Difference
|
| 279 |
+
running_diff_sum += diff
|
| 280 |
+
int_diff_x.append(s); int_diff_y.append(running_diff_sum)
|
|
|
|
| 281 |
|
| 282 |
+
# 5. Average Integrated Difference (Int / Steps)
|
| 283 |
+
step_count = i + 1
|
| 284 |
+
avg_int = running_diff_sum / step_count
|
| 285 |
+
avg_int_x.append(s); avg_int_y.append(avg_int)
|
| 286 |
|
| 287 |
return web.json_response({
|
| 288 |
"mid": mid,
|
| 289 |
"prev_mid": market_state['history'][-2]['p'] if len(market_state['history']) > 1 else mid,
|
| 290 |
"imbalance_vol": { "dist_bids": d_b_x, "vol_bids": d_b_y, "dist_asks": d_a_x, "vol_asks": d_a_y },
|
| 291 |
"imbalance_pct": { "x": imb_x, "y": imb_y },
|
| 292 |
+
"diff": { "x": diff_x, "y": diff_y },
|
| 293 |
+
"int_diff": { "x": int_diff_x, "y": int_diff_y },
|
| 294 |
+
"avg_int": { "x": avg_int_x, "y": avg_int_y, "last_val": avg_int_y[-1] if avg_int_y else 0 },
|
| 295 |
"history": market_state['history']
|
| 296 |
})
|
| 297 |
|