Alvin3y1 commited on
Commit
bbe99a8
·
verified ·
1 Parent(s): 58b7790

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -75
app.py CHANGED
@@ -19,7 +19,8 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
19
  market_state = {
20
  "bids": {},
21
  "asks": {},
22
- "history": [],
 
23
  "current_mid": 0.0,
24
  "prev_mid": 0.0,
25
  "ready": False
@@ -53,24 +54,8 @@ def analyze_structure(diff_x, diff_y, current_mid):
53
 
54
  projected_price = current_mid + impact
55
 
56
- support_level = None
57
- resistance_level = None
58
- scan_limit = len(diff_y) // 2
59
-
60
- for i in range(1, scan_limit):
61
- prev_val = diff_y[i-1]
62
- curr_val = diff_y[i]
63
- dist = diff_x[i]
64
-
65
- if prev_val > 0 and curr_val < 0 and resistance_level is None:
66
- resistance_level = current_mid + dist
67
- if prev_val < 0 and curr_val > 0 and support_level is None:
68
- support_level = current_mid - dist
69
-
70
  return {
71
  "projected": projected_price,
72
- "support": support_level,
73
- "resistance": resistance_level,
74
  "net_score": weighted_imbalance
75
  }
76
 
@@ -118,10 +103,18 @@ def process_market_data():
118
  chart_asks.append(vol_a)
119
 
120
  analysis = analyze_structure(diff_x, diff_y, mid)
 
 
 
 
 
 
 
121
 
122
  return {
123
  "mid": mid,
124
  "history": market_state['history'],
 
125
  "depth_x": diff_x,
126
  "depth_net": diff_y,
127
  "depth_bids": chart_bids,
@@ -176,11 +169,6 @@ HTML_PAGE = f"""
176
  .green {{ color: var(--accent-green); }}
177
  .red {{ color: var(--accent-red); }}
178
 
179
- .terminal-box {{ margin-top: auto; font-size: 11px; height: 250px; display: flex; flex-direction: column; }}
180
- .term-header {{ border-bottom: 1px dashed #444; margin-bottom: 5px; opacity: 0.7; }}
181
- #term-logs {{ flex: 1; overflow-y: hidden; display: flex; flex-direction: column-reverse; }}
182
- .log-line {{ margin-top: 4px; padding-left: 8px; border-left: 2px solid #333; }}
183
-
184
  #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); }}
185
  </style>
186
  </head>
@@ -215,19 +203,10 @@ HTML_PAGE = f"""
215
  <span class="stat-label">WEIGHTED IMBALANCE SCORE</span>
216
  <span id="score-val" class="stat-value">0</span>
217
  </div>
218
- <div class="stat-box">
219
- <span class="stat-label">MARKET STRUCTURE</span>
220
- <div style="display:flex; justify-content:space-between;"><span>RESIST:</span><span id="res-val" class="red">---</span></div>
221
- <div style="display:flex; justify-content:space-between;"><span>SUPPORT:</span><span id="sup-val" class="green">---</span></div>
222
- </div>
223
  <div class="stat-box" style="border: 1px solid #444;">
224
  <span class="stat-label" style="color:var(--accent-green);">IMPACT PROJECTION</span>
225
  <span id="proj-val" class="stat-value">---</span>
226
  </div>
227
- <div class="terminal-box">
228
- <div class="term-header">> ALGO LOGS</div>
229
- <div id="term-logs"></div>
230
- </div>
231
  </div>
232
  </div>
233
  </div>
@@ -239,10 +218,7 @@ HTML_PAGE = f"""
239
  status: document.getElementById('loading-status'),
240
  price: document.getElementById('live-price'),
241
  scoreVal: document.getElementById('score-val'),
242
- resVal: document.getElementById('res-val'),
243
- supVal: document.getElementById('sup-val'),
244
- projVal: document.getElementById('proj-val'),
245
- logs: document.getElementById('term-logs')
246
  }};
247
 
248
  const chartCommon = {{
@@ -254,9 +230,20 @@ HTML_PAGE = f"""
254
  }};
255
 
256
  const priceChart = LightweightCharts.createChart(document.getElementById('tv-price'), chartCommon);
257
- const priceSeries = priceChart.addLineSeries({{ color: '#2962FF', lineWidth: 2 }});
258
- const predSeries = priceChart.addLineSeries({{ color: '#ff9800', lineWidth: 2, lineStyle: 2 }});
259
- let supportLine = null, resistanceLine = null;
 
 
 
 
 
 
 
 
 
 
 
260
 
261
  const rawChart = LightweightCharts.createChart(document.getElementById('tv-raw'), {{
262
  ...chartCommon,
@@ -288,15 +275,6 @@ HTML_PAGE = f"""
288
  }});
289
  ['tv-price', 'tv-raw', 'tv-net'].forEach(id => resizeObserver.observe(document.getElementById(id)));
290
 
291
- function log(msg, type='neutral') {{
292
- const div = document.createElement('div');
293
- div.className = 'log-line';
294
- div.style.borderLeftColor = type === 'bull' ? '#66fcf1' : type === 'bear' ? '#ff3b3b' : '#333';
295
- div.innerHTML = `<span style="opacity:0.5">${{new Date().toLocaleTimeString()}}</span> ${{msg}}`;
296
- dom.logs.prepend(div);
297
- if (dom.logs.children.length > 15) dom.logs.removeChild(dom.logs.lastChild);
298
- }}
299
-
300
  function connect() {{
301
  const proto = window.location.protocol === 'https:' ? 'wss' : 'ws';
302
  const url = `${{proto}}://${{window.location.host}}/ws`;
@@ -316,46 +294,35 @@ HTML_PAGE = f"""
316
  const t = Math.floor(d.t);
317
  if (!seen.has(t)) {{ seen.add(t); cleanHistory.push({{ time: t, value: d.p }}); }}
318
  }});
 
 
 
 
 
 
 
 
 
 
319
  if (cleanHistory.length) {{
320
  priceSeries.setData(cleanHistory);
 
 
321
  const last = cleanHistory[cleanHistory.length-1];
322
  dom.price.innerText = last.value.toLocaleString(undefined, {{minimumFractionDigits: 2}});
323
 
324
  if (data.analysis) {{
325
- const {{ projected, support, resistance, net_score }} = data.analysis;
326
 
327
- const forecast = [];
328
- forecast.push(last);
329
- for(let i=1; i<=10; i++) {{
330
- forecast.push({{ time: last.time + (i * 60), value: projected }});
331
- }}
332
- predSeries.setData(forecast);
333
 
334
  dom.projVal.innerText = projected.toLocaleString(undefined, {{minimumFractionDigits: 0, maximumFractionDigits: 0}});
335
 
336
  dom.scoreVal.innerText = net_score.toFixed(2);
337
  dom.scoreVal.className = net_score > 0 ? "stat-value green" : "stat-value red";
338
-
339
- if (support) {{
340
- dom.supVal.innerText = support.toFixed(0);
341
- if (!supportLine) supportLine = priceSeries.createPriceLine({{ price: support, color: '#00e676', title: 'SUP' }});
342
- else supportLine.applyOptions({{ price: support }});
343
- }} else {{
344
- dom.supVal.innerText = '---';
345
- if (supportLine) {{ priceSeries.removePriceLine(supportLine); supportLine = null; }}
346
- }}
347
- if (resistance) {{
348
- dom.resVal.innerText = resistance.toFixed(0);
349
- if (!resistanceLine) resistanceLine = priceSeries.createPriceLine({{ price: resistance, color: '#ff1744', title: 'RES' }});
350
- else resistanceLine.applyOptions({{ price: resistance }});
351
- }} else {{
352
- dom.resVal.innerText = '---';
353
- if (resistanceLine) {{ priceSeries.removePriceLine(resistanceLine); resistanceLine = null; }}
354
- }}
355
-
356
- if (Math.abs(net_score) > 20 && Math.random() > 0.98) {{
357
- log(net_score > 0 ? "Momentum: Buying Pressure" : "Momentum: Selling Pressure", net_score > 0 ? 'bull' : 'bear');
358
- }}
359
  }}
360
  }}
361
 
 
19
  market_state = {
20
  "bids": {},
21
  "asks": {},
22
+ "history": [],
23
+ "pred_history": [],
24
  "current_mid": 0.0,
25
  "prev_mid": 0.0,
26
  "ready": False
 
54
 
55
  projected_price = current_mid + impact
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  return {
58
  "projected": projected_price,
 
 
59
  "net_score": weighted_imbalance
60
  }
61
 
 
103
  chart_asks.append(vol_a)
104
 
105
  analysis = analyze_structure(diff_x, diff_y, mid)
106
+
107
+ now = time.time()
108
+ if analysis:
109
+ if not market_state['pred_history'] or (now - market_state['pred_history'][-1]['t'] > 0.5):
110
+ market_state['pred_history'].append({'t': now, 'p': analysis['projected']})
111
+ if len(market_state['pred_history']) > HISTORY_LENGTH:
112
+ market_state['pred_history'].pop(0)
113
 
114
  return {
115
  "mid": mid,
116
  "history": market_state['history'],
117
+ "pred_history": market_state['pred_history'],
118
  "depth_x": diff_x,
119
  "depth_net": diff_y,
120
  "depth_bids": chart_bids,
 
169
  .green {{ color: var(--accent-green); }}
170
  .red {{ color: var(--accent-red); }}
171
 
 
 
 
 
 
172
  #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); }}
173
  </style>
174
  </head>
 
203
  <span class="stat-label">WEIGHTED IMBALANCE SCORE</span>
204
  <span id="score-val" class="stat-value">0</span>
205
  </div>
 
 
 
 
 
206
  <div class="stat-box" style="border: 1px solid #444;">
207
  <span class="stat-label" style="color:var(--accent-green);">IMPACT PROJECTION</span>
208
  <span id="proj-val" class="stat-value">---</span>
209
  </div>
 
 
 
 
210
  </div>
211
  </div>
212
  </div>
 
218
  status: document.getElementById('loading-status'),
219
  price: document.getElementById('live-price'),
220
  scoreVal: document.getElementById('score-val'),
221
+ projVal: document.getElementById('proj-val')
 
 
 
222
  }};
223
 
224
  const chartCommon = {{
 
230
  }};
231
 
232
  const priceChart = LightweightCharts.createChart(document.getElementById('tv-price'), chartCommon);
233
+ const priceSeries = priceChart.addLineSeries({{ color: '#2962FF', lineWidth: 2, title: 'Price' }});
234
+
235
+ const pastPredSeries = priceChart.addLineSeries({{
236
+ color: '#555555',
237
+ lineWidth: 1,
238
+ title: 'Past Prediction'
239
+ }});
240
+
241
+ const futurePredSeries = priceChart.addLineSeries({{
242
+ color: '#ff9800',
243
+ lineWidth: 2,
244
+ lineStyle: 2,
245
+ title: 'Projection'
246
+ }});
247
 
248
  const rawChart = LightweightCharts.createChart(document.getElementById('tv-raw'), {{
249
  ...chartCommon,
 
275
  }});
276
  ['tv-price', 'tv-raw', 'tv-net'].forEach(id => resizeObserver.observe(document.getElementById(id)));
277
 
 
 
 
 
 
 
 
 
 
278
  function connect() {{
279
  const proto = window.location.protocol === 'https:' ? 'wss' : 'ws';
280
  const url = `${{proto}}://${{window.location.host}}/ws`;
 
294
  const t = Math.floor(d.t);
295
  if (!seen.has(t)) {{ seen.add(t); cleanHistory.push({{ time: t, value: d.p }}); }}
296
  }});
297
+
298
+ const predHistory = [];
299
+ const seenP = new Set();
300
+ if(data.pred_history) {{
301
+ data.pred_history.forEach(d => {{
302
+ const t = Math.floor(d.t);
303
+ if(!seenP.has(t)) {{ seenP.add(t); predHistory.push({{ time: t, value: d.p }}); }}
304
+ }});
305
+ }}
306
+
307
  if (cleanHistory.length) {{
308
  priceSeries.setData(cleanHistory);
309
+ pastPredSeries.setData(predHistory);
310
+
311
  const last = cleanHistory[cleanHistory.length-1];
312
  dom.price.innerText = last.value.toLocaleString(undefined, {{minimumFractionDigits: 2}});
313
 
314
  if (data.analysis) {{
315
+ const {{ projected, net_score }} = data.analysis;
316
 
317
+ futurePredSeries.setData([
318
+ last,
319
+ {{ time: last.time + 60, value: projected }}
320
+ ]);
 
 
321
 
322
  dom.projVal.innerText = projected.toLocaleString(undefined, {{minimumFractionDigits: 0, maximumFractionDigits: 0}});
323
 
324
  dom.scoreVal.innerText = net_score.toFixed(2);
325
  dom.scoreVal.className = net_score > 0 ? "stat-value green" : "stat-value red";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
  }}
327
  }}
328