Alvin3y1 commited on
Commit
78b3589
·
verified ·
1 Parent(s): 7c6576b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -15
app.py CHANGED
@@ -436,7 +436,7 @@ HTML_PAGE = f"""
436
  const askSeries = denChart.addAreaSeries({{ lineColor: '#ff3b3b', topColor: 'rgba(255, 59, 59, 0.15)', bottomColor: 'rgba(0,0,0,0)', lineWidth: 1 }});
437
 
438
  let activeLines = [];
439
- let activeCandleLines = []; // New array to track lines on the candle chart
440
 
441
  new ResizeObserver(entries => {{
442
  for(let entry of entries) {{
@@ -505,31 +505,27 @@ HTML_PAGE = f"""
505
  low: c.low,
506
  close: c.close
507
  }}));
 
508
  const uniqueCandles = [...new Map(candles.map(i => [i.time, i])).values()];
509
  candleSeries.setData(uniqueCandles);
510
  }}
511
 
512
  if (data.walls) {{
513
- // Remove old lines from both charts
514
  activeLines.forEach(l => priceSeries.removePriceLine(l));
515
  activeLines = [];
516
  activeCandleLines.forEach(l => candleSeries.removePriceLine(l));
517
  activeCandleLines = [];
518
-
519
  let html = "";
520
  const addWall = (w, type) => {{
521
  const color = type === 'BID' ? '#00ff9d' : '#ff3b3b';
522
  const lineOpts = {{ price: w.price, color: color, lineWidth: 1, lineStyle: 2, axisLabelVisible: false }};
523
 
524
- // Add to Price Chart
525
  activeLines.push(priceSeries.createPriceLine(lineOpts));
526
-
527
- // Add to Candle Chart
528
  activeCandleLines.push(candleSeries.createPriceLine(lineOpts));
529
-
530
  html += `<div class="list-item"><span style="color:${{color}}">${{type}} ${{w.price}}</span><span class="c-dim">Z:${{w.z_score.toFixed(1)}}</span></div>`;
531
  }};
532
-
533
  data.walls.asks.forEach(w => addWall(w, 'ASK'));
534
  data.walls.bids.forEach(w => addWall(w, 'BID'));
535
  dom.wallList.innerHTML = html || '<span class="c-dim" style="font-size:11px">Scanning...</span>';
@@ -571,6 +567,7 @@ HTML_PAGE = f"""
571
  async def kraken_worker():
572
  global market_state
573
  try:
 
574
  async with aiohttp.ClientSession() as session:
575
  url = "https://api.kraken.com/0/public/OHLC?pair=XBTUSD&interval=1"
576
  async with session.get(url) as response:
@@ -646,27 +643,61 @@ async def kraken_worker():
646
  for trade in data:
647
  try:
648
  qty = float(trade['qty'])
 
649
  side = trade['side']
 
 
650
  if side == 'buy': market_state['current_vol_window']['buy'] += qty
651
  else: market_state['current_vol_window']['sell'] += qty
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
652
  except: pass
653
 
654
  elif channel == "ohlc":
655
  for candle in data:
656
  try:
 
 
657
  c_data = {
658
- 'time': int(float(candle['endtime'])),
659
  'open': float(candle['open']),
660
  'high': float(candle['high']),
661
  'low': float(candle['low']),
662
  'close': float(candle['close'])
663
  }
664
- if market_state['ohlc_history'] and market_state['ohlc_history'][-1]['time'] == c_data['time']:
665
- market_state['ohlc_history'][-1] = c_data
666
- else:
667
- market_state['ohlc_history'].append(c_data)
668
- if len(market_state['ohlc_history']) > 100:
669
- market_state['ohlc_history'].pop(0)
 
 
 
670
  except Exception as e:
671
  pass
672
 
 
436
  const askSeries = denChart.addAreaSeries({{ lineColor: '#ff3b3b', topColor: 'rgba(255, 59, 59, 0.15)', bottomColor: 'rgba(0,0,0,0)', lineWidth: 1 }});
437
 
438
  let activeLines = [];
439
+ let activeCandleLines = [];
440
 
441
  new ResizeObserver(entries => {{
442
  for(let entry of entries) {{
 
505
  low: c.low,
506
  close: c.close
507
  }}));
508
+ // Deduplicate candles
509
  const uniqueCandles = [...new Map(candles.map(i => [i.time, i])).values()];
510
  candleSeries.setData(uniqueCandles);
511
  }}
512
 
513
  if (data.walls) {{
 
514
  activeLines.forEach(l => priceSeries.removePriceLine(l));
515
  activeLines = [];
516
  activeCandleLines.forEach(l => candleSeries.removePriceLine(l));
517
  activeCandleLines = [];
518
+
519
  let html = "";
520
  const addWall = (w, type) => {{
521
  const color = type === 'BID' ? '#00ff9d' : '#ff3b3b';
522
  const lineOpts = {{ price: w.price, color: color, lineWidth: 1, lineStyle: 2, axisLabelVisible: false }};
523
 
 
524
  activeLines.push(priceSeries.createPriceLine(lineOpts));
 
 
525
  activeCandleLines.push(candleSeries.createPriceLine(lineOpts));
526
+
527
  html += `<div class="list-item"><span style="color:${{color}}">${{type}} ${{w.price}}</span><span class="c-dim">Z:${{w.z_score.toFixed(1)}}</span></div>`;
528
  }};
 
529
  data.walls.asks.forEach(w => addWall(w, 'ASK'));
530
  data.walls.bids.forEach(w => addWall(w, 'BID'));
531
  dom.wallList.innerHTML = html || '<span class="c-dim" style="font-size:11px">Scanning...</span>';
 
567
  async def kraken_worker():
568
  global market_state
569
  try:
570
+ # Fetch initial history (Kraken REST API uses start time for candles)
571
  async with aiohttp.ClientSession() as session:
572
  url = "https://api.kraken.com/0/public/OHLC?pair=XBTUSD&interval=1"
573
  async with session.get(url) as response:
 
643
  for trade in data:
644
  try:
645
  qty = float(trade['qty'])
646
+ price = float(trade['price'])
647
  side = trade['side']
648
+
649
+ # Update Vol stats
650
  if side == 'buy': market_state['current_vol_window']['buy'] += qty
651
  else: market_state['current_vol_window']['sell'] += qty
652
+
653
+ # LIVE CANDLE UPDATE (Crucial for "actual price")
654
+ current_minute_start = int(time.time()) // 60 * 60
655
+
656
+ if market_state['ohlc_history']:
657
+ last_candle = market_state['ohlc_history'][-1]
658
+
659
+ # If still in the same minute, update the current candle
660
+ if last_candle['time'] == current_minute_start:
661
+ last_candle['close'] = price
662
+ if price > last_candle['high']: last_candle['high'] = price
663
+ if price < last_candle['low']: last_candle['low'] = price
664
+
665
+ # If new minute started, create new candle
666
+ elif current_minute_start > last_candle['time']:
667
+ new_candle = {
668
+ 'time': current_minute_start,
669
+ 'open': price,
670
+ 'high': price,
671
+ 'low': price,
672
+ 'close': price
673
+ }
674
+ market_state['ohlc_history'].append(new_candle)
675
+ if len(market_state['ohlc_history']) > 200:
676
+ market_state['ohlc_history'].pop(0)
677
+
678
  except: pass
679
 
680
  elif channel == "ohlc":
681
  for candle in data:
682
  try:
683
+ # Kraken WS sends 'endtime'. We convert to 'starttime' to match REST data and charting lib.
684
+ start_time = int(float(candle['endtime'])) - 60
685
  c_data = {
686
+ 'time': start_time,
687
  'open': float(candle['open']),
688
  'high': float(candle['high']),
689
  'low': float(candle['low']),
690
  'close': float(candle['close'])
691
  }
692
+
693
+ # Sync with existing history if found
694
+ if market_state['ohlc_history']:
695
+ if market_state['ohlc_history'][-1]['time'] == start_time:
696
+ market_state['ohlc_history'][-1] = c_data
697
+ elif market_state['ohlc_history'][-1]['time'] < start_time:
698
+ market_state['ohlc_history'].append(c_data)
699
+ if len(market_state['ohlc_history']) > 200:
700
+ market_state['ohlc_history'].pop(0)
701
  except Exception as e:
702
  pass
703