Files
aitbc/apps/ai-engine/src/ai_service.py
AITBC System f74a208cbc ```
chore(gitignore): add development log file patterns

- Add patterns for .log, .out, and .err files
- Add wget-log and download.log patterns
- Organize under new "Development Logs" section
```
2026-03-08 12:16:01 +01:00

206 lines
6.6 KiB
Python

#!/usr/bin/env python3
"""
AITBC AI Service - Simplified Version
Basic AI-powered trading and analytics
"""
import asyncio
import json
import numpy as np
from datetime import datetime
from fastapi import FastAPI
from pydantic import BaseModel
from typing import Dict, Any, List
app = FastAPI(title="AITBC AI Service API", version="1.0.0")
# Models
class TradingRequest(BaseModel):
symbol: str
strategy: str = "ai_enhanced"
class AnalysisRequest(BaseModel):
symbol: str
analysis_type: str = "full"
# Simple AI Engine
class SimpleAITradingEngine:
"""Simplified AI trading engine"""
def __init__(self):
self.models_loaded = True
async def analyze_market(self, symbol: str) -> Dict[str, Any]:
"""Simple market analysis"""
# Generate realistic-looking analysis
current_price = np.random.uniform(0.001, 0.01)
price_change = np.random.uniform(-0.05, 0.05)
return {
'symbol': symbol,
'current_price': current_price,
'price_change_24h': price_change,
'volume_24h': np.random.uniform(1000, 10000),
'rsi': np.random.uniform(30, 70),
'macd': np.random.uniform(-0.01, 0.01),
'volatility': np.random.uniform(0.01, 0.05),
'ai_predictions': {
'price_prediction': {
'predicted_change': np.random.uniform(-0.02, 0.02),
'confidence': np.random.uniform(0.7, 0.9)
},
'risk_assessment': {
'risk_score': np.random.uniform(0.2, 0.8),
'volatility': np.random.uniform(0.01, 0.05)
},
'sentiment_analysis': {
'sentiment_score': np.random.uniform(-1.0, 1.0),
'overall_sentiment': np.random.choice(['bullish', 'bearish', 'neutral'])
}
},
'timestamp': datetime.utcnow()
}
async def make_trading_decision(self, symbol: str) -> Dict[str, Any]:
"""Make AI trading decision"""
analysis = await self.analyze_market(symbol)
# Simple decision logic
price_pred = analysis['ai_predictions']['price_prediction']['predicted_change']
sentiment = analysis['ai_predictions']['sentiment_analysis']['sentiment_score']
risk = analysis['ai_predictions']['risk_assessment']['risk_score']
# Calculate signal strength
signal_strength = (price_pred * 0.5) + (sentiment * 0.3) - (risk * 0.2)
if signal_strength > 0.2:
signal = "buy"
elif signal_strength < -0.2:
signal = "sell"
else:
signal = "hold"
confidence = abs(signal_strength)
quantity = 1000 * confidence # Base position size
return {
'symbol': symbol,
'signal': signal,
'confidence': confidence,
'quantity': quantity,
'price': analysis['current_price'],
'reasoning': f"Signal strength: {signal_strength:.3f}",
'timestamp': datetime.utcnow()
}
# Global AI engine
ai_engine = SimpleAITradingEngine()
@app.post("/api/ai/analyze")
async def analyze_market(request: AnalysisRequest):
"""AI market analysis"""
try:
analysis = await ai_engine.analyze_market(request.symbol)
return {
"status": "success",
"analysis": analysis,
"timestamp": datetime.utcnow()
}
except Exception as e:
return {"status": "error", "message": str(e)}
@app.post("/api/ai/trade")
async def execute_ai_trade(request: TradingRequest):
"""Execute AI-powered trade"""
try:
decision = await ai_engine.make_trading_decision(request.symbol)
return {
"status": "success",
"decision": decision,
"timestamp": datetime.utcnow()
}
except Exception as e:
return {"status": "error", "message": str(e)}
@app.get("/api/ai/predict/{symbol}")
async def predict_market(symbol: str):
"""AI market prediction"""
try:
analysis = await ai_engine.analyze_market(symbol)
return {
"status": "success",
"predictions": {
"price": analysis['ai_predictions']['price_prediction'],
"risk": analysis['ai_predictions']['risk_assessment'],
"sentiment": analysis['ai_predictions']['sentiment_analysis']
},
"timestamp": datetime.utcnow()
}
except Exception as e:
return {"status": "error", "message": str(e)}
@app.get("/api/ai/dashboard")
async def get_ai_dashboard():
"""AI dashboard overview"""
try:
# Generate dashboard data
symbols = ['AITBC/BTC', 'AITBC/ETH', 'AITBC/USDT']
dashboard_data = {
'market_overview': {
'total_volume': np.random.uniform(100000, 1000000),
'active_symbols': len(symbols),
'ai_models_active': 3,
'last_update': datetime.utcnow()
},
'symbol_analysis': {}
}
for symbol in symbols:
analysis = await ai_engine.analyze_market(symbol)
dashboard_data['symbol_analysis'][symbol] = {
'price': analysis['current_price'],
'change': analysis['price_change_24h'],
'signal': (await ai_engine.make_trading_decision(symbol))['signal'],
'confidence': (await ai_engine.make_trading_decision(symbol))['confidence']
}
return {
"status": "success",
"dashboard": dashboard_data,
"timestamp": datetime.utcnow()
}
except Exception as e:
return {"status": "error", "message": str(e)}
@app.get("/api/ai/status")
async def get_ai_status():
"""Get AI service status"""
return {
"status": "active",
"models_loaded": ai_engine.models_loaded,
"services": {
"trading_engine": "active",
"market_analysis": "active",
"predictions": "active"
},
"capabilities": [
"market_analysis",
"trading_decisions",
"price_predictions",
"risk_assessment",
"sentiment_analysis"
],
"timestamp": datetime.utcnow()
}
@app.get("/api/health")
async def health_check():
"""Health check endpoint"""
return {"status": "ok", "timestamp": datetime.utcnow()}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8005)