dix price ccalls
This commit is contained in:
@ -4931,4 +4931,35 @@ class DataProvider:
|
||||
try:
|
||||
callback(symbol, data)
|
||||
except Exception as e:
|
||||
logger.error(f"Error in bucketed COB callback: {e}")
|
||||
logger.error(f"Error in bucketed COB callback: {e}")
|
||||
|
||||
def get_live_price_from_api(self, symbol: str) -> Optional[float]:
|
||||
"""FORCE fetch live price from Binance API for low-latency updates"""
|
||||
# Check cache first to avoid excessive API calls
|
||||
if symbol in self.live_price_cache:
|
||||
price, timestamp = self.live_price_cache[symbol]
|
||||
if datetime.now() - timestamp < self.live_price_cache_ttl:
|
||||
return price
|
||||
|
||||
try:
|
||||
import requests
|
||||
binance_symbol = symbol.replace('/', '')
|
||||
url = f"https://api.binance.com/api/v3/ticker/price?symbol={binance_symbol}"
|
||||
response = requests.get(url, timeout=0.5) # Use a short timeout for low latency
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
price = float(data['price'])
|
||||
|
||||
# Update cache and current prices
|
||||
self.live_price_cache[symbol] = (price, datetime.now())
|
||||
self.current_prices[symbol] = price
|
||||
|
||||
logger.info(f"LIVE PRICE for {symbol}: ${price:.2f}")
|
||||
return price
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.warning(f"Failed to get live price for {symbol} from API: {e}")
|
||||
# Fallback to last known current price
|
||||
return self.current_prices.get(symbol)
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error getting live price for {symbol}: {e}")
|
||||
return self.current_prices.get(symbol)
|
Reference in New Issue
Block a user