dix price ccalls
This commit is contained in:
@ -53,6 +53,13 @@ class StandardizedDataProvider(DataProvider):
|
||||
self.cob_data_cache[symbol] = None
|
||||
self.cob_imbalance_history[symbol] = deque(maxlen=300) # 5 minutes of 1s data
|
||||
|
||||
# Ensure live price cache exists (in case parent didn't initialize it)
|
||||
if not hasattr(self, 'live_price_cache'):
|
||||
self.live_price_cache: Dict[str, Tuple[float, datetime]] = {}
|
||||
if not hasattr(self, 'live_price_cache_ttl'):
|
||||
from datetime import timedelta
|
||||
self.live_price_cache_ttl = timedelta(milliseconds=500)
|
||||
|
||||
# COB provider integration
|
||||
self.cob_provider: Optional[MultiExchangeCOBProvider] = None
|
||||
self._initialize_cob_provider()
|
||||
@ -476,10 +483,37 @@ class StandardizedDataProvider(DataProvider):
|
||||
else:
|
||||
logger.warning(f"No 'close' column found in OHLCV data for {symbol}")
|
||||
return []
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting recent prices for {symbol}: {e}")
|
||||
return []
|
||||
|
||||
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"Error stopping real-time processing: {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