Alvin3y1 commited on
Commit
0bde50b
Β·
verified Β·
1 Parent(s): 3842323

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -25
app.py CHANGED
@@ -29,7 +29,7 @@ HTML_PAGE = f"""
29
  <!DOCTYPE html>
30
  <html>
31
  <head>
32
- <title>BTC-USD Integral 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; }}
@@ -76,8 +76,8 @@ HTML_PAGE = f"""
76
  <div id="vol-chart" class="chart"></div>
77
  </div>
78
  <div class="col">
79
- <!-- Bottom Right: Normalized Integration -->
80
- <div id="integral-chart" class="chart"></div>
81
  </div>
82
  </div>
83
  </div>
@@ -86,11 +86,11 @@ HTML_PAGE = f"""
86
  const priceDiv = document.getElementById('price-chart');
87
  const imbPercentDiv = document.getElementById('imb-percent-chart');
88
  const volDiv = document.getElementById('vol-chart');
89
- const integDiv = document.getElementById('integral-chart');
90
 
91
  const statusDiv = document.getElementById('status');
92
 
93
- let initPrice = false, initImbP = false, initVol = false, initInteg = false;
94
 
95
  const commonConfig = {{ responsive: true, displayModeBar: false }};
96
  const commonLayout = {{
@@ -113,7 +113,7 @@ HTML_PAGE = f"""
113
  return;
114
  }}
115
 
116
- statusDiv.innerHTML = `Mid: <span class="${{data.mid >= data.prev_mid ? 'green' : 'red'}}">$${{data.mid.toLocaleString(undefined, {{minimumFractionDigits: 2}})}}</span> | Integral: ${{data.integral.last_val ? data.integral.last_val.toFixed(3) : 0}}`;
117
 
118
  // 1. PRICE HISTORY
119
  if (!initPrice) {{
@@ -139,24 +139,24 @@ HTML_PAGE = f"""
139
  if (!initVol) {{ Plotly.newPlot(volDiv, tracesVol, layoutVol, commonConfig); initVol = true; }}
140
  else {{ Plotly.react(volDiv, tracesVol, layoutVol, commonConfig); }}
141
 
142
- // 4. NORMALIZED INTEGRAL (-1 to +1)
143
- const layoutInteg = {{ ...commonLayout,
144
- title: '<b>Structural Bias (Normalized Integral)</b>',
145
  xaxis: {{title: 'Distance ($)', gridcolor:'#222'}},
146
  yaxis: {{title: 'Bias', range: [-1.1, 1.1], gridcolor:'#222', zeroline: true, zerolinecolor: '#fff'}}
147
  }};
148
 
149
- const traceInteg = {{
150
- x: data.integral.x,
151
- y: data.integral.y,
152
  type: 'scatter',
153
  mode: 'lines',
154
  fill: 'tozeroy',
155
  line: {{color: '#00bcd4', width: 2}} // Cyan
156
  }};
157
 
158
- if (!initInteg) {{ Plotly.newPlot(integDiv, [traceInteg], layoutInteg, commonConfig); initInteg = true; }}
159
- else {{ Plotly.react(integDiv, [traceInteg], layoutInteg, commonConfig); }}
160
 
161
  }} catch (e) {{ console.error("Fetch error:", e); }}
162
  }}
@@ -241,7 +241,7 @@ async def handle_data(request):
241
 
242
  # --- Calculations ---
243
  imb_x, imb_y = [], []
244
- integ_x, integ_y = [], []
245
  running_integral = 0
246
 
247
  if d_b_x and d_a_x:
@@ -264,24 +264,24 @@ async def handle_data(request):
264
  imb_x.append(s)
265
  imb_y.append(pct)
266
 
267
- # 3. Normalized Integration
268
- # We integrate the raw percentage.
269
  running_integral += pct
 
 
270
 
271
- # We normalize using tanh.
272
- # A cumulative pressure of 500% (e.g., 5 steps of 100% imbalance) -> tanh(500/scale)
273
- # Scale factor of 1000 means "significant bias" is accumulated over ~10 steps of strong pressure
274
- norm_integral = math.tanh(running_integral / 1000.0)
275
 
276
- integ_x.append(s)
277
- integ_y.append(norm_integral)
278
 
279
  return web.json_response({
280
  "mid": mid,
281
  "prev_mid": market_state['history'][-2]['p'] if len(market_state['history']) > 1 else mid,
282
  "imbalance_vol": { "dist_bids": d_b_x, "vol_bids": d_b_y, "dist_asks": d_a_x, "vol_asks": d_a_y },
283
  "imbalance_pct": { "x": imb_x, "y": imb_y },
284
- "integral": { "x": integ_x, "y": integ_y, "last_val": integ_y[-1] if integ_y else 0 },
285
  "history": market_state['history']
286
  })
287
 
@@ -305,7 +305,7 @@ async def main():
305
  site = web.TCPSite(runner, '0.0.0.0', PORT)
306
  await site.start()
307
 
308
- print(f"πŸš€ BTC-USD Integral Dashboard: http://localhost:{PORT}")
309
  await asyncio.Event().wait()
310
 
311
  if __name__ == "__main__":
 
29
  <!DOCTYPE html>
30
  <html>
31
  <head>
32
+ <title>BTC-USD Mean Pressure</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: Average Pressure (Mean Integral) -->
80
+ <div id="mean-chart" class="chart"></div>
81
  </div>
82
  </div>
83
  </div>
 
86
  const priceDiv = document.getElementById('price-chart');
87
  const imbPercentDiv = document.getElementById('imb-percent-chart');
88
  const volDiv = document.getElementById('vol-chart');
89
+ const meanDiv = document.getElementById('mean-chart');
90
 
91
  const statusDiv = document.getElementById('status');
92
 
93
+ let initPrice = false, initImbP = false, initVol = false, initMean = false;
94
 
95
  const commonConfig = {{ responsive: true, displayModeBar: false }};
96
  const commonLayout = {{
 
113
  return;
114
  }}
115
 
116
+ statusDiv.innerHTML = `Mid: <span class="${{data.mid >= data.prev_mid ? 'green' : 'red'}}">$${{data.mid.toLocaleString(undefined, {{minimumFractionDigits: 2}})}}</span> | Avg Bias: ${{data.mean.last_val ? data.mean.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. MEAN PRESSURE (-1 to +1)
143
+ const layoutMean = {{ ...commonLayout,
144
+ title: '<b>Average Pressure (Integral / Steps)</b>',
145
  xaxis: {{title: 'Distance ($)', gridcolor:'#222'}},
146
  yaxis: {{title: 'Bias', range: [-1.1, 1.1], gridcolor:'#222', zeroline: true, zerolinecolor: '#fff'}}
147
  }};
148
 
149
+ const traceMean = {{
150
+ x: data.mean.x,
151
+ y: data.mean.y,
152
  type: 'scatter',
153
  mode: 'lines',
154
  fill: 'tozeroy',
155
  line: {{color: '#00bcd4', width: 2}} // Cyan
156
  }};
157
 
158
+ if (!initMean) {{ Plotly.newPlot(meanDiv, [traceMean], layoutMean, commonConfig); initMean = true; }}
159
+ else {{ Plotly.react(meanDiv, [traceMean], layoutMean, commonConfig); }}
160
 
161
  }} catch (e) {{ console.error("Fetch error:", e); }}
162
  }}
 
241
 
242
  # --- Calculations ---
243
  imb_x, imb_y = [], []
244
+ mean_x, mean_y = [], []
245
  running_integral = 0
246
 
247
  if d_b_x and d_a_x:
 
264
  imb_x.append(s)
265
  imb_y.append(pct)
266
 
267
+ # 3. Mean Pressure
268
+ # Integral / Number of Steps
269
  running_integral += pct
270
+ step_count = i + 1
271
+ mean_pressure = running_integral / step_count
272
 
273
+ # Normalize to -1..1 (divide by 100%)
274
+ norm_mean = mean_pressure / 100.0
 
 
275
 
276
+ mean_x.append(s)
277
+ mean_y.append(norm_mean)
278
 
279
  return web.json_response({
280
  "mid": mid,
281
  "prev_mid": market_state['history'][-2]['p'] if len(market_state['history']) > 1 else mid,
282
  "imbalance_vol": { "dist_bids": d_b_x, "vol_bids": d_b_y, "dist_asks": d_a_x, "vol_asks": d_a_y },
283
  "imbalance_pct": { "x": imb_x, "y": imb_y },
284
+ "mean": { "x": mean_x, "y": mean_y, "last_val": mean_y[-1] if mean_y else 0 },
285
  "history": market_state['history']
286
  })
287
 
 
305
  site = web.TCPSite(runner, '0.0.0.0', PORT)
306
  await site.start()
307
 
308
+ print(f"πŸš€ BTC-USD Mean Pressure Dashboard: http://localhost:{PORT}")
309
  await asyncio.Event().wait()
310
 
311
  if __name__ == "__main__":