cleanup, cob ladder still broken

This commit is contained in:
Dobromir Popov
2025-07-07 01:07:48 +03:00
parent 97d9bc97ee
commit 9101448e78
75 changed files with 546 additions and 12123 deletions

View File

@ -34,7 +34,7 @@ class COBIntegration:
Integration layer for Multi-Exchange COB data with gogo2 trading system
"""
def __init__(self, data_provider: DataProvider = None, symbols: List[str] = None):
def __init__(self, data_provider: Optional[DataProvider] = None, symbols: Optional[List[str]] = None):
"""
Initialize COB Integration
@ -45,15 +45,8 @@ class COBIntegration:
self.data_provider = data_provider
self.symbols = symbols or ['BTC/USDT', 'ETH/USDT']
# Initialize COB provider
self.cob_provider = MultiExchangeCOBProvider(
symbols=self.symbols,
bucket_size_bps=1.0 # 1 basis point granularity
)
# Register callbacks
self.cob_provider.subscribe_to_cob_updates(self._on_cob_update)
self.cob_provider.subscribe_to_bucket_updates(self._on_bucket_update)
# Initialize COB provider to None, will be set in start()
self.cob_provider = None
# CNN/DQN integration
self.cnn_callbacks: List[Callable] = []
@ -75,13 +68,23 @@ class COBIntegration:
self.liquidity_alerts[symbol] = []
self.arbitrage_opportunities[symbol] = []
logger.info("COB Integration initialized")
logger.info("COB Integration initialized (provider will be started in async)")
logger.info(f"Symbols: {self.symbols}")
async def start(self):
"""Start COB integration"""
logger.info("Starting COB Integration")
# Initialize COB provider here, within the async context
self.cob_provider = MultiExchangeCOBProvider(
symbols=self.symbols,
bucket_size_bps=1.0 # 1 basis point granularity
)
# Register callbacks
self.cob_provider.subscribe_to_cob_updates(self._on_cob_update)
self.cob_provider.subscribe_to_bucket_updates(self._on_bucket_update)
# Start COB provider
await self.cob_provider.start_streaming()
@ -94,7 +97,8 @@ class COBIntegration:
async def stop(self):
"""Stop COB integration"""
logger.info("Stopping COB Integration")
await self.cob_provider.stop_streaming()
if self.cob_provider:
await self.cob_provider.stop_streaming()
logger.info("COB Integration stopped")
def add_cnn_callback(self, callback: Callable[[str, Dict], None]):
@ -293,7 +297,9 @@ class COBIntegration:
"""Generate formatted data for dashboard visualization"""
try:
# Get fixed bucket size for the symbol
bucket_size = self.cob_provider.fixed_usd_buckets.get(symbol, 1.0)
bucket_size = 1.0 # Default bucket size
if self.cob_provider:
bucket_size = self.cob_provider.fixed_usd_buckets.get(symbol, 1.0)
# Calculate price range for buckets
mid_price = cob_snapshot.volume_weighted_mid
@ -338,15 +344,16 @@ class COBIntegration:
# Get actual Session Volume Profile (SVP) from trade data
svp_data = []
try:
svp_result = self.cob_provider.get_session_volume_profile(symbol, bucket_size)
if svp_result and 'data' in svp_result:
svp_data = svp_result['data']
logger.debug(f"Retrieved SVP data for {symbol}: {len(svp_data)} price levels")
else:
logger.warning(f"No SVP data available for {symbol}")
except Exception as e:
logger.error(f"Error getting SVP data for {symbol}: {e}")
if self.cob_provider:
try:
svp_result = self.cob_provider.get_session_volume_profile(symbol, bucket_size)
if svp_result and 'data' in svp_result:
svp_data = svp_result['data']
logger.debug(f"Retrieved SVP data for {symbol}: {len(svp_data)} price levels")
else:
logger.warning(f"No SVP data available for {symbol}")
except Exception as e:
logger.error(f"Error getting SVP data for {symbol}: {e}")
# Generate market stats
stats = {
@ -381,19 +388,21 @@ class COBIntegration:
stats['svp_price_levels'] = 0
stats['session_start'] = ''
# Add real-time statistics for NN models
try:
realtime_stats = self.cob_provider.get_realtime_stats(symbol)
if realtime_stats:
stats['realtime_1s'] = realtime_stats.get('1s_stats', {})
stats['realtime_5s'] = realtime_stats.get('5s_stats', {})
else:
# Get additional real-time stats
realtime_stats = {}
if self.cob_provider:
try:
realtime_stats = self.cob_provider.get_realtime_stats(symbol)
if realtime_stats:
stats['realtime_1s'] = realtime_stats.get('1s_stats', {})
stats['realtime_5s'] = realtime_stats.get('5s_stats', {})
else:
stats['realtime_1s'] = {}
stats['realtime_5s'] = {}
except Exception as e:
logger.error(f"Error getting real-time stats for {symbol}: {e}")
stats['realtime_1s'] = {}
stats['realtime_5s'] = {}
except Exception as e:
logger.error(f"Error getting real-time stats for {symbol}: {e}")
stats['realtime_1s'] = {}
stats['realtime_5s'] = {}
return {
'type': 'cob_update',
@ -463,9 +472,10 @@ class COBIntegration:
while True:
try:
for symbol in self.symbols:
cob_snapshot = self.cob_provider.get_consolidated_orderbook(symbol)
if cob_snapshot:
await self._analyze_cob_patterns(symbol, cob_snapshot)
if self.cob_provider:
cob_snapshot = self.cob_provider.get_consolidated_orderbook(symbol)
if cob_snapshot:
await self._analyze_cob_patterns(symbol, cob_snapshot)
await asyncio.sleep(1)
@ -540,18 +550,26 @@ class COBIntegration:
def get_cob_snapshot(self, symbol: str) -> Optional[COBSnapshot]:
"""Get latest COB snapshot for a symbol"""
if not self.cob_provider:
return None
return self.cob_provider.get_consolidated_orderbook(symbol)
def get_market_depth_analysis(self, symbol: str) -> Optional[Dict]:
"""Get detailed market depth analysis"""
if not self.cob_provider:
return None
return self.cob_provider.get_market_depth_analysis(symbol)
def get_exchange_breakdown(self, symbol: str) -> Optional[Dict]:
"""Get liquidity breakdown by exchange"""
if not self.cob_provider:
return None
return self.cob_provider.get_exchange_breakdown(symbol)
def get_price_buckets(self, symbol: str) -> Optional[Dict]:
"""Get fine-grain price buckets"""
if not self.cob_provider:
return None
return self.cob_provider.get_price_buckets(symbol)
def get_recent_signals(self, symbol: str, count: int = 20) -> List[Dict]:
@ -560,6 +578,16 @@ class COBIntegration:
def get_statistics(self) -> Dict[str, Any]:
"""Get COB integration statistics"""
if not self.cob_provider:
return {
'cnn_callbacks': len(self.cnn_callbacks),
'dqn_callbacks': len(self.dqn_callbacks),
'dashboard_callbacks': len(self.dashboard_callbacks),
'cached_features': list(self.cob_feature_cache.keys()),
'total_signals': {symbol: len(signals) for symbol, signals in self.cob_signals.items()},
'provider_status': 'Not initialized'
}
provider_stats = self.cob_provider.get_statistics()
return {
@ -574,6 +602,11 @@ class COBIntegration:
def get_realtime_stats_for_nn(self, symbol: str) -> Dict:
"""Get real-time statistics formatted for NN models"""
try:
# Check if COB provider is initialized
if not self.cob_provider:
logger.debug(f"COB provider not initialized yet for {symbol}")
return {}
realtime_stats = self.cob_provider.get_realtime_stats(symbol)
if not realtime_stats:
return {}
@ -608,4 +641,66 @@ class COBIntegration:
except Exception as e:
logger.error(f"Error getting NN stats for {symbol}: {e}")
return {}
return {}
def get_realtime_stats(self):
# Added null check to ensure the COB provider is initialized
if self.cob_provider is None:
logger.warning("COB provider is uninitialized; attempting initialization.")
self.initialize_provider()
if self.cob_provider is None:
logger.error("COB provider failed to initialize; returning default empty snapshot.")
return COBSnapshot(
symbol="",
timestamp=0,
exchanges_active=0,
total_bid_liquidity=0,
total_ask_liquidity=0,
price_buckets=[],
volume_weighted_mid=0,
spread_bps=0,
liquidity_imbalance=0,
consolidated_bids=[],
consolidated_asks=[]
)
try:
snapshot = self.cob_provider.get_realtime_stats()
return snapshot
except Exception as e:
logger.error(f"Error retrieving COB snapshot: {e}")
return COBSnapshot(
symbol="",
timestamp=0,
exchanges_active=0,
total_bid_liquidity=0,
total_ask_liquidity=0,
price_buckets=[],
volume_weighted_mid=0,
spread_bps=0,
liquidity_imbalance=0,
consolidated_bids=[],
consolidated_asks=[]
)
def stop_streaming(self):
pass
def _initialize_cob_integration(self):
"""Initialize COB integration with high-frequency data handling"""
logger.info("Initializing COB integration...")
if not COB_INTEGRATION_AVAILABLE:
logger.warning("COB integration not available - skipping initialization")
return
try:
if not hasattr(self.orchestrator, 'cob_integration') or self.orchestrator.cob_integration is None:
logger.info("Creating new COB integration instance")
self.orchestrator.cob_integration = COBIntegration(self.data_provider)
else:
logger.info("Using existing COB integration from orchestrator")
# Start simple COB data collection for both symbols
self._start_simple_cob_collection()
logger.info("COB integration initialized successfully")
except Exception as e:
logger.error(f"Error initializing COB integration: {e}")