Alvin3y1 commited on
Commit
0eee686
·
verified ·
1 Parent(s): 69a5bc2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -41
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-Way Analysis</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; }}
@@ -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
- <!-- Avg Diff (Diff / Steps) -->
85
- <div id="avg-diff-chart" class="chart"></div>
86
  </div>
87
  <div class="col">
88
- <!-- Mean Pressure (Previous) -->
89
- <div id="mean-chart" class="chart"></div>
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 avgDiffDiv = document.getElementById('avg-diff-chart');
100
- const meanDiv = document.getElementById('mean-chart');
101
 
102
  const statusDiv = document.getElementById('status');
103
 
104
  let initPrice = false, initImbP = false, initVol = false;
105
- let initDiff = false, initAvgDiff = false, initMean = 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> | Net Vol: ${{data.diff.last_val ? data.diff.last_val.toFixed(2) : 0}}`;
129
 
130
  // 1. PRICE HISTORY
131
- const tracePrice = {{ 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}} }};
132
- if (!initPrice) {{ Plotly.newPlot(priceDiv, [tracePrice], {{ ...commonLayout, title: '<b>Midprice</b>', xaxis: {{type:'date', gridcolor:'#222'}} }}, commonConfig); initPrice = true; }}
133
- else {{ Plotly.react(priceDiv, [tracePrice], {{ ...commonLayout, title: '<b>Midprice</b>', xaxis: {{type:'date', gridcolor:'#222'}} }}, commonConfig); }}
 
 
 
 
 
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, [traceImbP], layoutImbP, commonConfig); initImbP = true; }}
139
- else {{ Plotly.react(imbPercentDiv, [traceImbP], layoutImbP, commonConfig); }}
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. AVG NET DIFFERENCE (Diff / Steps)
155
- const traceAvgDiff = {{ x: data.avg_diff.x, y: data.avg_diff.y, type: 'scatter', mode: 'lines', fill: 'tozeroy', line: {{color: '#ff9800'}} }};
156
- if (!initAvgDiff) {{ Plotly.newPlot(avgDiffDiv, [traceAvgDiff], {{ ...commonLayout, title: '<b>Density (Net Vol / Step)</b>' }}, commonConfig); initAvgDiff = true; }}
157
- else {{ Plotly.react(avgDiffDiv, [traceAvgDiff], {{ ...commonLayout, title: '<b>Density (Net Vol / Step)</b>' }}, commonConfig); }}
158
 
159
- // 6. MEAN PRESSURE (-1 to +1)
160
- const traceMean = {{ x: data.mean.x, y: data.mean.y, type: 'scatter', mode: 'lines', fill: 'tozeroy', line: {{color: '#00bcd4'}} }};
161
- const layoutMean = {{ ...commonLayout, title: '<b>Avg Pressure % (-1 to 1)</b>', yaxis: {{range: [-1.1, 1.1], gridcolor:'#222'}} }};
162
- if (!initMean) {{ Plotly.newPlot(meanDiv, [traceMean], layoutMean, commonConfig); initMean = true; }}
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
- avg_diff_x, avg_diff_y = [], []
250
- mean_x, mean_y = [], []
251
- running_integral_pct = 0
 
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. Avg Diff (Diff / Steps) -> Density
277
- step_count = i + 1
278
- avg_diff = diff / step_count
279
- avg_diff_x.append(s); avg_diff_y.append(avg_diff)
280
 
281
- # 5. Mean Pressure (Integral Pct / Steps)
282
- running_integral_pct += pct
283
- mean_pressure = (running_integral_pct / step_count) / 100.0 # Normalize -1 to 1
284
- mean_x.append(s); mean_y.append(mean_pressure)
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, "last_val": diff_y[-1] if diff_y else 0 },
292
- "avg_diff": { "x": avg_diff_x, "y": avg_diff_y },
293
- "mean": { "x": mean_x, "y": mean_y },
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