204 lines
7.9 KiB
Python
204 lines
7.9 KiB
Python
from typing import Dict, Optional, List, Any, Union
|
|
import asyncio
|
|
import logging
|
|
from decimal import Decimal
|
|
from datetime import datetime
|
|
|
|
from cdp_langchain.agent_toolkits import CdpToolkit
|
|
from cdp_langchain.utils import CdpAgentkitWrapper
|
|
from langchain_openai import ChatOpenAI
|
|
|
|
from ai.models.market_predictor import MarketPredictionModel
|
|
from ai.training.trainer import AIModelTrainer
|
|
from analysis.market_analyzer import MarketAnalyzer
|
|
from analysis.pattern_detector import PatternDetector
|
|
from analysis.sentiment_scanner import SentimentScanner
|
|
from risk.portfolio_manager import PortfolioManager
|
|
from risk.position_calculator import PositionCalculator
|
|
from strategies.arbitrage.cdp_dex_monitor import CDPDEXMonitor
|
|
from strategies.mev.cdp_mev_detector import MEVDetector
|
|
from utils.logger import get_logger
|
|
from utils.config_manager import ConfigManager
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
class EnhancedCDPCore:
|
|
"""
|
|
Enhanced CDP Core system that integrates AI capabilities with CDP's trading functionality.
|
|
This core system serves as the foundation for advanced trading operations.
|
|
"""
|
|
|
|
def __init__(self, config: Dict[str, Any]):
|
|
"""Initialize the enhanced CDP core with AI components."""
|
|
self.config = ConfigManager(config)
|
|
|
|
# Initialize CDP components
|
|
self.llm = ChatOpenAI(model=self.config.get("llm_model", "gpt-4o-mini"))
|
|
self.cdp = CdpAgentkitWrapper()
|
|
self.toolkit = CdpToolkit.from_cdp_agentkit_wrapper(self.cdp)
|
|
self.tools = self.toolkit.get_tools()
|
|
|
|
# Initialize AI components
|
|
self.market_predictor = self._initialize_market_predictor()
|
|
self.pattern_detector = PatternDetector(self.config.get_pattern_config())
|
|
self.market_analyzer = MarketAnalyzer(self.config.get_market_config())
|
|
self.sentiment_scanner = SentimentScanner(self.config.get_sentiment_config())
|
|
|
|
# Initialize risk management
|
|
self.portfolio_manager = PortfolioManager(self.config.get_risk_config())
|
|
self.position_calculator = PositionCalculator(self.config.get_risk_config())
|
|
|
|
# Initialize strategy components
|
|
self.dex_monitor = CDPDEXMonitor(self)
|
|
self.mev_detector = MEVDetector(self)
|
|
|
|
# Initialize training system
|
|
self.model_trainer = AIModelTrainer(
|
|
models={
|
|
"market": self.market_predictor,
|
|
"pattern": self.pattern_detector.model,
|
|
"sentiment": self.sentiment_scanner.model
|
|
},
|
|
config=self.config.get_training_config()
|
|
)
|
|
|
|
# Initialize state management
|
|
self.monitoring_tokens = set()
|
|
self.active_alerts = {}
|
|
self.active_positions = {}
|
|
self.wallet_data = None
|
|
|
|
async def initialize_system(self) -> Dict[str, Any]:
|
|
"""Initialize the complete trading system."""
|
|
try:
|
|
# Initialize wallet
|
|
self.wallet_data = await self._initialize_wallet()
|
|
|
|
# Initialize market monitoring
|
|
await self._start_market_monitoring()
|
|
|
|
# Initialize position monitoring
|
|
await self._start_position_monitoring()
|
|
|
|
# Initialize risk management
|
|
await self._initialize_risk_management()
|
|
|
|
return {
|
|
"status": "success",
|
|
"wallet": self.wallet_data,
|
|
"market_status": await self.market_analyzer.get_market_status(),
|
|
"risk_profile": await self.portfolio_manager.get_risk_profile()
|
|
}
|
|
|
|
except Exception as e:
|
|
logger.error(f"System initialization failed: {str(e)}")
|
|
return {"status": "error", "message": str(e)}
|
|
|
|
async def execute_trading_strategy(self, strategy_type: str, params: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""Execute a trading strategy with AI optimization."""
|
|
try:
|
|
# Analyze market conditions
|
|
market_analysis = await self.market_analyzer.analyze_market_conditions()
|
|
sentiment_data = await self.sentiment_scanner.analyze_market_sentiment()
|
|
|
|
# Generate strategy parameters
|
|
strategy_params = await self._generate_strategy_parameters(
|
|
strategy_type=strategy_type,
|
|
base_params=params,
|
|
market_analysis=market_analysis,
|
|
sentiment_data=sentiment_data
|
|
)
|
|
|
|
# Validate strategy
|
|
validation_result = await self._validate_strategy(strategy_params)
|
|
if not validation_result["approved"]:
|
|
return {
|
|
"status": "rejected",
|
|
"reason": validation_result["reason"],
|
|
"analysis": validation_result["analysis"]
|
|
}
|
|
|
|
# Execute strategy
|
|
execution_result = await self._execute_strategy(
|
|
strategy_type=strategy_type,
|
|
params=strategy_params
|
|
)
|
|
|
|
# Record results
|
|
await self._record_execution_results(execution_result)
|
|
|
|
return {
|
|
"status": "success",
|
|
"execution": execution_result,
|
|
"analysis": {
|
|
"market": market_analysis,
|
|
"sentiment": sentiment_data
|
|
}
|
|
}
|
|
|
|
except Exception as e:
|
|
logger.error(f"Strategy execution failed: {str(e)}")
|
|
return {"status": "error", "message": str(e)}
|
|
|
|
async def monitor_position(
|
|
self,
|
|
position_id: str,
|
|
monitoring_params: Dict[str, Any]
|
|
) -> Dict[str, Any]:
|
|
"""Monitor a trading position with AI-enhanced analysis."""
|
|
try:
|
|
if position_id not in self.active_positions:
|
|
self.active_positions[position_id] = monitoring_params
|
|
|
|
# Start monitoring task
|
|
asyncio.create_task(
|
|
self._position_monitoring_loop(
|
|
position_id=position_id,
|
|
params=monitoring_params
|
|
)
|
|
)
|
|
|
|
return {
|
|
"status": "success",
|
|
"message": "Position monitoring started",
|
|
"monitoring_params": monitoring_params
|
|
}
|
|
|
|
return {
|
|
"status": "error",
|
|
"message": "Position already being monitored"
|
|
}
|
|
|
|
except Exception as e:
|
|
logger.error(f"Position monitoring setup failed: {str(e)}")
|
|
return {"status": "error", "message": str(e)}
|
|
|
|
async def _position_monitoring_loop(
|
|
self,
|
|
position_id: str,
|
|
params: Dict[str, Any]
|
|
):
|
|
"""Internal position monitoring loop with AI analysis."""
|
|
while position_id in self.active_positions:
|
|
try:
|
|
# Get current position state
|
|
position_state = await self._get_position_state(position_id)
|
|
|
|
# Generate AI prediction
|
|
prediction = await self.market_predictor.predict_position_outcome(
|
|
position_state=position_state,
|
|
market_state=await self.market_analyzer.get_current_state()
|
|
)
|
|
|
|
# Check if action needed
|
|
if prediction["requires_action"]:
|
|
await self._handle_position_action(
|
|
position_id=position_id,
|
|
prediction=prediction
|
|
)
|
|
|
|
await asyncio.sleep(self.config.get("position_check_interval", 60))
|
|
|
|
except Exception as e:
|
|
logger.error(f"Position monitoring error: {str(e)}")
|
|
await asyncio.sleep(self.config.get("error_retry_interval", 300)) |