Spaces:
Sleeping
Sleeping
File size: 22,685 Bytes
0342bed 57a6af0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 | # trade_analysis/enhanced_api.py
import os
from fastapi import FastAPI, Query, HTTPException
from pydantic import BaseModel
import httpx
from typing import Dict, Any, List
import pandas as pd
import numpy as np
import asyncio
from datetime import datetime
from pathlib import Path
# Import only modules that still exist
from .data import UnifiedDataProvider
from .indicators import enrich_with_indicators, identify_current_setup
from .enhanced_sentiment import EnhancedFinancialSentimentAnalyzer, analyze_momentum_sentiment
from .momentum_trading_engine import IntegratedMomentumEngine
from .enhanced_llm import EnhancedLLMEngine, generate_enhanced_llm_signal
from .tft_model import GapPredictionTFT
from .agent import TradingAgent, analyze_agent_performance
# Global dictionary to store TFT models
api_tft_models = {}
trading_agent = None
def sanitize_for_json(data: any) -> any:
"""Recursively converts numpy and pandas types to JSON-serializable types."""
if isinstance(data, dict):
return {key: sanitize_for_json(value) for key, value in data.items()}
elif isinstance(data, list):
return [sanitize_for_json(item) for item in data]
elif isinstance(data, np.bool_):
return bool(data)
elif isinstance(data, (np.integer, np.int64)):
return int(data)
elif isinstance(data, np.floating):
return float(data)
elif isinstance(data, pd.Timestamp):
return data.isoformat()
elif isinstance(data, (pd.Series, pd.Index, np.ndarray)):
return data.tolist()
return data
class EnhancedSignalResponse(BaseModel):
"""Enhanced response model with momentum and LLM analysis"""
symbol: str
signal: str
confidence: float
reasoning: str
position_size: float
status: str
details: Dict[str, Any]
# Enhanced fields
momentum_analysis: Dict[str, Any] = {}
llm_ensemble: Dict[str, Any] = {}
options_strategy: Dict[str, Any] = {}
timeframe_recommendation: str = "15m"
expected_hold_time: str = "Unknown"
# Enhanced FastAPI App
app = FastAPI(
title="Enhanced Intraday Momentum Engine",
version="2.0.0",
description="SOTA Financial AI with multi-LLM ensemble and momentum analysis"
)
# Initialize enhanced components
data_provider = UnifiedDataProvider()
sentiment_analyzer = EnhancedFinancialSentimentAnalyzer()
momentum_engine = IntegratedMomentumEngine()
llm_engine = EnhancedLLMEngine()
tft_predictor = GapPredictionTFT(context_length=96, prediction_length=1)
@app.on_event("startup")
async def startup_event():
"""Initialize all AI models on startup and launch the agent."""
print("π Starting Enhanced Trading Engine...")
# --- This new logic checks the environment before loading models ---
from .deploy import DeploymentConfig
config = DeploymentConfig.auto_detect()
# Load sentiment models regardless of environment
print("π Loading sentiment models...")
sentiment_analyzer.initialize_models()
# Only load LLMs if we are NOT on a CPU
if config.device != "cpu":
print("π§ Loading LLM ensemble...")
llm_engine.initialize_llm_models()
else:
print("π« CPU environment detected. Skipping LLM loading.")
# Load TFT models
print("π€ Loading TFT models...")
# (Your existing TFT model loading logic here, ensure it writes to /tmp if needed)
symbols = ['QQQ', 'SPY', 'MSFT', 'TSLA', 'NVDA', 'META']
for symbol in symbols:
model_path = f"/tmp/tft_{symbol}_validated.pth" # Use /tmp for models
tft_instance = GapPredictionTFT()
# (The rest of your TFT loading logic...)
api_tft_models[symbol] = tft_instance
# Initialize and run the agent as a background task
global trading_agent
trading_agent = TradingAgent(api_url="http://localhost:7860")
print("π€ Launching Trading Agent as a background task...")
asyncio.create_task(trading_agent.run())
print("β
Enhanced Trading Engine startup complete!")
@app.get("/")
def read_root():
"""Enhanced root endpoint with system info"""
import torch
gpu_info = "CPU only"
if torch.cuda.is_available():
gpu_name = torch.cuda.get_device_name(0)
gpu_memory = torch.cuda.get_device_properties(0).total_memory / 1e9
gpu_info = f"{gpu_name} ({gpu_memory:.1f} GB)"
return {
"status": "operational",
"engine": "Enhanced Intraday Momentum Engine v2.0.0",
"gpu_info": gpu_info,
"features": [
"Multi-LLM Ensemble Analysis",
"Advanced Sentiment Analysis (10+ models)",
"High-Frequency Momentum Engine",
"Options Strategy Generation",
"TFT Gap Prediction",
"Autonomous Trading Agent"
],
"timestamp": datetime.now().isoformat()
}
@app.post("/predict/enhanced/", response_model=EnhancedSignalResponse)
async def predict_enhanced_signal(
symbol: str = Query(..., description="Stock symbol (e.g., QQQ, SPY)"),
timeframe: str = Query("5m", description="Trading timeframe: 1m, 5m, 15m, 1h"),
strategy_mode: str = Query("momentum", description="Strategy: momentum, scalp, gap, swing")
):
"""
Enhanced prediction endpoint with full AI stack
"""
try:
start_time = datetime.now()
# Fetch market data
async with httpx.AsyncClient() as client:
print(f"π Fetching data for {symbol}...")
# Multi-timeframe OHLCV data
ohlcv_data = await data_provider.fetch_multi_timeframe_stock_data(symbol)
# News and social data - FIXED SYNTAX
news_data, _ = await data_provider.fetch_news(symbol, client)
reddit_data, _ = await data_provider.fetch_reddit_data(symbol)
# Alternative data
alt_data = data_provider.get_alternative_data(symbol)
# Process dataframes
news_df = pd.DataFrame(news_data) if news_data else pd.DataFrame()
reddit_df = pd.DataFrame(reddit_data) if reddit_data else pd.DataFrame()
# Technical analysis for each timeframe
tech_setups = {}
for tf, df in ohlcv_data.items():
if not df.empty:
enriched_df = enrich_with_indicators(df.copy(), tf)
tech_setups[tf] = identify_current_setup(enriched_df, tf)
print("π Running AI analysis...")
# 1. Enhanced Sentiment Analysis
sentiment_analysis = await asyncio.get_event_loop().run_in_executor(
None,
analyze_momentum_sentiment,
news_df, reddit_df, symbol, timeframe
)
# 2. Momentum Analysis
momentum_analysis = momentum_engine.generate_enhanced_signal(
ohlcv_data, sentiment_analysis, alt_data
)
# 3. TFT Prediction
daily_df = ohlcv_data.get("daily")
tft_prediction = None
tft_model = api_tft_models.get(symbol.upper())
if daily_df is not None and len(daily_df) >= 96 and tft_model:
if tft_model.is_trained:
tft_prediction = tft_model.predict_gap_probability(daily_df)
print(f"π Using pretrained TFT model for {symbol}")
else:
print(f"π€ Training TFT model for {symbol}...")
tft_model.train(daily_df, epochs=20)
tft_prediction = tft_model.predict_gap_probability(daily_df)
else:
if tft_model:
tft_prediction = tft_model._default_prediction()
else:
temp_tft = GapPredictionTFT()
tft_prediction = temp_tft._default_prediction()
# 4. LLM Ensemble Analysis
llm_analysis = {}
try:
llm_analysis = llm_engine.generate_enhanced_trading_signal(
ohlcv_data, sentiment_analysis, momentum_analysis, alt_data
)
except Exception as e:
print(f"LLM analysis failed: {e}")
conditions = {
"is_vix_high": alt_data.get('vix_level', 0) > 25,
"is_15m_rsi_bullish": tech_setups.get("15m", {}).get('rsi', 50) > 65,
"is_15m_rsi_bearish": tech_setups.get("15m", {}).get('rsi', 50) < 35,
"is_15m_volume_spike": tech_setups.get("15m", {}).get('volume_spike', False),
"is_hourly_trend_bullish": tech_setups.get("hourly", {}).get('direction') == 'up',
"is_hourly_trend_bearish": tech_setups.get("hourly", {}).get('direction') == 'down'
}
llm_analysis = generate_enhanced_llm_signal(conditions)
# 5. Master Signal Generation - FIXED FUNCTION NAME
master_signal = _generate_master_signal(
momentum_analysis, llm_analysis, sentiment_analysis, tft_prediction,
timeframe, strategy_mode
)
# 6. Options Strategy - FIXED FUNCTION NAME
options_strategy = _generate_options_strategy(
master_signal, momentum_analysis, alt_data, timeframe, strategy_mode
)
# Calculate processing time
processing_time = (datetime.now() - start_time).total_seconds()
# Prepare response
sanitized_details = sanitize_for_json({
"tech_setups": tech_setups,
"sentiment": sentiment_analysis,
"alternative_data": alt_data,
"tft_prediction": tft_prediction,
"processing_time_seconds": processing_time,
"data_sources": {
"news_articles": len(news_df),
"social_posts": len(reddit_df),
"timeframes_analyzed": list(ohlcv_data.keys())
}
})
return EnhancedSignalResponse(
symbol=symbol,
signal=master_signal["signal"],
confidence=master_signal["confidence"],
reasoning=master_signal["reasoning"],
position_size=master_signal["position_size"],
status="Success",
details=sanitized_details,
momentum_analysis=sanitize_for_json(momentum_analysis),
llm_ensemble=sanitize_for_json(llm_analysis),
options_strategy=sanitize_for_json(options_strategy),
timeframe_recommendation=master_signal.get("timeframe", timeframe),
expected_hold_time=master_signal.get("hold_time", "Unknown")
)
except Exception as e:
import traceback
traceback.print_exc()
raise HTTPException(status_code=500, detail=f"Enhanced analysis failed: {e}")
@app.get("/agent/start")
async def start_agent():
"""Start the autonomous agent"""
if trading_agent:
asyncio.create_task(trading_agent.run())
return {"status": "Agent started"}
return {"status": "Agent not initialized"}
@app.get("/agent/stats")
async def get_agent_stats():
"""Get agent's performance stats"""
if trading_agent:
return trading_agent.get_stats()
return {"error": "Agent not initialized"}
@app.get("/agent/positions")
async def get_agent_positions():
"""Get current positions"""
if trading_agent:
return {"positions": trading_agent.positions}
return {"positions": {}}
@app.get("/agent/analyze")
async def analyze_agent():
"""Analyze agent's performance"""
try:
analyze_agent_performance()
return {"status": "Analysis complete - check console output"}
except Exception as e:
return {"error": str(e)}
def _generate_master_signal(momentum_analysis: Dict, llm_analysis: Dict,
sentiment_analysis: Dict, tft_prediction: Dict,
timeframe: str = "15m", strategy_mode: str = "momentum") -> Dict:
"""Generate master trading signal from all analyses - FIXED VERSION"""
# Extract signals
momentum_signal = momentum_analysis.get("signal", "HOLD")
momentum_confidence = momentum_analysis.get("confidence", 50)
# Use the actual momentum analysis results
momentum_master = momentum_analysis.get("momentum_analysis", {}).get("master_signal", {})
momentum_strategy = momentum_master.get("strategy", "WAIT")
llm_signal = llm_analysis.get("signal", "HOLD")
sentiment_composite = sentiment_analysis.get("composite_score", 0)
tft_direction = tft_prediction.get("expected_direction", "FLAT") if tft_prediction else "FLAT"
# TIMEFRAME-SPECIFIC THRESHOLDS
timeframe_configs = {
"1m": {
"threshold": 0.2,
"min_confidence": 70,
"hold_time": "1-2 minutes",
"position_multiplier": 0.5
},
"5m": {
"threshold": 0.25,
"min_confidence": 65,
"hold_time": "2-5 minutes",
"position_multiplier": 0.7
},
"15m": {
"threshold": 0.3,
"min_confidence": 60,
"hold_time": "10-30 minutes",
"position_multiplier": 1.0
},
"1h": {
"threshold": 0.35,
"min_confidence": 55,
"hold_time": "30-60 minutes",
"position_multiplier": 1.2
}
}
config = timeframe_configs.get(timeframe, timeframe_configs["15m"])
# STRATEGY MODE ADJUSTMENTS
if strategy_mode == "scalp":
config["threshold"] *= 0.8
config["hold_time"] = "1-3 minutes"
elif strategy_mode == "gap" and tft_prediction:
if tft_direction != "FLAT" and tft_prediction.get("gap_probability", 50) > 70:
config["min_confidence"] -= 10
# Calculate weighted score
if momentum_strategy in ["AGGRESSIVE_SCALP", "STANDARD_MOMENTUM"]:
weighted_score = momentum_master.get("conviction", 0)
weighted_confidence = momentum_confidence
else:
weights = {
"momentum": 0.4,
"llm": 0.25,
"sentiment": 0.2,
"tft": 0.15
}
signal_scores = {}
signal_scores["momentum"] = 1.0 if momentum_signal == "CALLS" else -1.0 if momentum_signal == "PUTS" else 0.0
signal_scores["llm"] = 1.0 if llm_signal == "CALLS" else -1.0 if llm_signal == "PUTS" else 0.0
signal_scores["sentiment"] = np.clip(sentiment_composite, -1, 1)
signal_scores["tft"] = 0.7 if tft_direction == "UP" else -0.7 if tft_direction == "DOWN" else 0.0
weighted_score = sum(signal_scores[k] * weights[k] for k in weights)
weighted_confidence = (momentum_confidence * 0.4 +
llm_analysis.get("conviction", 50) * 0.3 +
(sentiment_analysis.get("confidence", "LOW") == "HIGH") * 80 * 0.3)
# Generate final signal
if weighted_score > config["threshold"] and weighted_confidence > config["min_confidence"]:
final_signal = "CALLS"
position_size = min(0.5, (weighted_confidence / 100) * config["position_multiplier"])
elif weighted_score < -config["threshold"] and weighted_confidence > config["min_confidence"]:
final_signal = "PUTS"
position_size = min(0.5, (weighted_confidence / 100) * config["position_multiplier"])
else:
final_signal = "HOLD"
position_size = 0.0
config["hold_time"] = "Wait for better setup"
# Build reasoning
reasoning = []
reasoning.append(f"{strategy_mode.upper()} {timeframe}: {final_signal}")
reasoning.append(f"Confidence: {weighted_confidence:.0f}%")
if momentum_strategy != "WAIT":
reasoning.append(f"Momentum: {momentum_strategy}")
if abs(sentiment_composite) > 0.3:
reasoning.append(f"Sentiment: {'Bullish' if sentiment_composite > 0 else 'Bearish'}")
if tft_prediction and tft_prediction.get("gap_probability", 50) > 70:
reasoning.append(f"Gap probability: {tft_prediction['gap_probability']:.0f}%")
return {
"signal": final_signal,
"confidence": int(weighted_confidence),
"reasoning": ". ".join(reasoning),
"position_size": position_size,
"timeframe": timeframe,
"hold_time": config["hold_time"],
"weighted_score": weighted_score,
"strategy_mode": strategy_mode,
"momentum_strategy": momentum_strategy
}
def _generate_options_strategy(master_signal: Dict, momentum_analysis: Dict,
alt_data: Dict, timeframe: str = "15m",
strategy_mode: str = "momentum") -> Dict:
"""Generate options strategy with timeframe awareness"""
signal = master_signal["signal"]
confidence = master_signal["confidence"]
vix_level = alt_data.get("vix_level", 20)
if signal == "HOLD":
return {
"strategy": "WAIT",
"reasoning": "No clear directional bias",
"contracts": [],
"risk_management": "Wait for better setup"
}
# TIMEFRAME-SPECIFIC STRATEGIES
if timeframe in ["1m", "5m"] and strategy_mode == "scalp":
strategy = {
"strategy": "0DTE_SCALP",
"reasoning": f"{timeframe} scalp: {signal} with {confidence}% confidence",
"contracts": [
{
"type": "CALL" if signal == "CALLS" else "PUT",
"strike": "ATM",
"quantity": min(int(confidence / 8), 15),
"dte": 0,
"target_profit": 20,
"stop_loss": 10
}
],
"max_hold_time": f"{timeframe} bars (max 5 minutes)",
"risk_management": "Ultra-tight stops, quick exits"
}
elif timeframe == "15m" and confidence > 70:
strategy = {
"strategy": "MOMENTUM_15M",
"reasoning": f"15-minute momentum {signal} play, {confidence}% confidence",
"contracts": [
{
"type": "CALL" if signal == "CALLS" else "PUT",
"strike": "1% ITM",
"quantity": min(int(confidence / 12), 8),
"dte": 1,
"target_profit": 40,
"stop_loss": 20
}
],
"max_hold_time": "30 minutes",
"risk_management": "Standard momentum stops"
}
elif timeframe == "1h":
strategy = {
"strategy": "HOURLY_SWING",
"reasoning": f"Hourly swing {signal}, {confidence}% confidence",
"contracts": [
{
"type": "CALL_SPREAD" if signal == "CALLS" else "PUT_SPREAD",
"long_strike": "ATM",
"short_strike": "3% OTM",
"quantity": min(int(confidence / 15), 5),
"dte": 3,
"target_profit": 35,
"stop_loss": 25
}
],
"max_hold_time": "2-4 hours",
"risk_management": "Defined risk spreads"
}
else:
strategy = {
"strategy": "CONSERVATIVE",
"reasoning": f"Lower conviction {signal}, using conservative approach",
"contracts": [
{
"type": "CALL_SPREAD" if signal == "CALLS" else "PUT_SPREAD",
"long_strike": "ATM",
"short_strike": "5% OTM",
"quantity": 3,
"dte": 7,
"target_profit": 25,
"stop_loss": 20
}
],
"max_hold_time": "End of day",
"risk_management": "Limited risk, defined reward"
}
# VIX adjustments
if vix_level > 30:
strategy["reasoning"] += f". High VIX ({vix_level}) - reduced size"
for contract in strategy["contracts"]:
contract["quantity"] = max(1, contract["quantity"] // 2)
return strategy
@app.post("/backtest/enhanced/")
async def enhanced_backtest(
symbol: str = Query(..., description="Stock symbol"),
start_date: str = Query(..., description="Start date (YYYY-MM-DD)"),
end_date: str = Query(..., description="End date (YYYY-MM-DD)"),
strategy_mode: str = Query("momentum", description="Strategy mode"),
initial_capital: float = Query(100000, description="Initial capital")
):
"""Enhanced backtesting with momentum strategies"""
try:
return {
"status": "success",
"message": "Enhanced backtesting ready",
"features": [
"Multi-timeframe momentum analysis",
"LLM ensemble signal validation",
"Options strategy backtesting",
"Risk-adjusted performance metrics",
"Slippage and commission modeling"
]
}
except Exception as e:
return {"status": "error", "message": str(e)}
@app.get("/health/detailed")
async def detailed_health_check():
"""Detailed system health check"""
import torch
health_status = {
"timestamp": datetime.now().isoformat(),
"overall_status": "healthy",
"components": {}
}
# Check GPU
if torch.cuda.is_available():
gpu_memory_used = torch.cuda.memory_allocated(0) / 1e9
gpu_memory_total = torch.cuda.get_device_properties(0).total_memory / 1e9
health_status["components"]["gpu"] = {
"status": "available",
"device": torch.cuda.get_device_name(0),
"memory_used_gb": gpu_memory_used,
"memory_total_gb": gpu_memory_total,
"utilization": f"{gpu_memory_used/gpu_memory_total*100:.1f}%"
}
else:
health_status["components"]["gpu"] = {"status": "not_available"}
# Check model status
health_status["components"]["sentiment_models"] = {
"loaded": len(sentiment_analyzer.models),
"status": "ready" if sentiment_analyzer.models else "not_loaded"
}
health_status["components"]["llm_models"] = {
"loaded": len(llm_engine.models),
"status": "ready" if llm_engine.models else "not_loaded"
}
health_status["components"]["tft_model"] = {
"status": "trained" if tft_predictor.is_trained else "not_trained"
}
health_status["components"]["agent"] = {
"status": "initialized" if trading_agent else "not_initialized"
}
return health_status
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000) |