Market Intelligence Agent (MIA) - A production-ready, event-driven trading intelligence platform designed to detect market anomalies, evaluate trading signals through professional frameworks, and route execution decisions to multiple venues.
- Overview
- Key Features
- System Architecture
- Prerequisites
- Installation
- Configuration
- Quick Start
- Project Structure
- API Reference
- Development
- Monitoring & Metrics
- Troubleshooting
- Documentation
MIA (Market Intelligence Agent) is a layered, event-driven platform that combines:
- Real-time market data ingestion from multiple cryptocurrency exchanges (KuCoin, Coinbase, Kraken, Binance, ByBit, CoinGecko)
- Six professional signal frameworks for signal evaluation and validation
- Structural + indicator-based signal detection with dynamic confidence scaling
- Position-aware execution with feedback loops preventing cascade trades
- Multi-adapter distribution supporting SSE, WebSocket, Database, and Webhook outputs
- Automated Trading Systems - MirrorCore-X integration
- Governance Triggers - MtaaDAO participation
- Risk Management - Real-time market monitoring
- Market Analysis - Live dashboard and reporting
- External Agents - REST API data access
- Information Ratio Framework - Quantifies signal edge and alpha generation
- Regime-First Analysis - Market context awareness (trending, ranging, volatile)
- Liquidity Stack Framework - Order book depth analysis
- Lead-Lag Causality - Cross-symbol correlation and dependency detection
- Capital Flow Funnel - Volume and momentum analysis
- Asymmetric Risk Framework - Downside protection evaluation
- Real-time WebSocket feeds for 6+ exchanges
- REST API fallback with adaptive retry logic
- Unified symbol normalization across all venues
- Gap detection and data integrity validation
- Intelligent rate limiting with circuit breakers
- Market Graph Construction - Symbol correlation and relationship mapping
- Timeframe Synthesis - Multi-timeframe analysis from single candle stream
- Regime Classification - Automatic market condition detection
- Structural Confidence Scoring - Dynamic signal weighting
- Real-time event propagation < 100-500ms latency
- Redis event fabric for distributed processing
- Signal consumer with multi-adapter distribution
- Webhook integration for external systems
- Position feedback loops for intelligent execution
- PostgreSQL (Neon) for durable state
- Redis caching for high-speed access
- Schema versioning and migration support
┌─────────────────────────────────────────────────────────────┐
│ Market Intelligence Agent (MIA) │
└─────────────────────────────────────────────────────────────┘
┌─ Layer 1: Market Discovery ──────────────────────────────┐
│ • Market Selector │
│ • Symbol Registry & Normalization │
│ • Exchange Market Enumeration │
└──────────────────────────────────────────────────────────┘
↓
┌─ Layer 2: Data Ingestion ────────────────────────────────┐
│ • Multi-Exchange Adapters (KuCoin, Coinbase, Kraken) │
│ • WebSocket Manager & Connection Pooling │
│ • Batch Fetcher & Fallback Logic │
│ • Adaptive Load Balancer & Circuit Breaker │
└──────────────────────────────────────────────────────────┘
↓
┌─ Layer 3: Data Integrity ────────────────────────────────┐
│ • Gap Detection & Validation │
│ • Timeframe Synthesis │
│ • Candle Schema Validation │
│ • Data Quality Scoring │
└──────────────────────────────────────────────────────────┘
↓
┌─ Layer 3.5: Market Graph Construction ─────────────────┐
│ • Graph-based Structural Analysis │
│ • Lead-Lag Relationships │
│ • Correlation Breaking Detection │
│ • Sector Rotation Analysis │
└──────────────────────────────────────────────────────────┘
↓
┌─ Layer 4: Signal Evaluation ─────────────────────────────┐
│ • 6 Professional Analysis Frameworks │
│ • Regime Classification │
│ • Indicator Analysis (RSI, MACD, Bollinger, ADX) │
│ • Confidence Scaling & Edge Quantification │
└──────────────────────────────────────────────────────────┘
↓
┌─ Layer 5: Event Routing ─────────────────────────────────┐
│ • Event Engine & Fabric │
│ • Signal Consumer & Distribution │
│ • Multi-Adapter Output (SSE, WS, DB, Webhook) │
│ • Position Feedback Integration │
└──────────────────────────────────────────────────────────┘
↓
┌─ Layer 6: Execution Gateway ─────────────────────────────┐
│ • Position Management │
│ • Risk Enforcement │
│ • Execution Routing │
│ • Position Feedback Loop │
└──────────────────────────────────────────────────────────┘
┌─ Supporting Services ────────────────────────────────────┐
│ • PostgreSQL (Neon) Data Store │
│ • Redis Event Fabric & Caching │
│ • MCP Server for Agent Integration │
│ • REST/WebSocket APIs for External Access │
└──────────────────────────────────────────────────────────┘
For detailed architectural documentation, see MIA_SYSTEM_ARCHITECTURE.md.
- Python 3.10+
- PostgreSQL 14+ (or Neon database)
- Redis 6.0+
- pip or poetry for dependency management
- Minimum 2GB RAM (4GB+ recommended)
- Internet connectivity for exchange API access
- Modern CPU (2+ cores recommended)
You'll need API keys from the exchanges you want to use:
- KuCoin - For real-time crypto market data
- Coinbase - For US exchange data
- Kraken - For European coverage
- Binance - For global exchange data (optional)
- ByBit - For derivatives data (optional)
- CoinGecko - For on-chain metrics (optional, has free tier)
git clone https://github.com/yourusername/mia.git
cd miapython -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepip install -r requirements.txtCore Dependencies:
fastapi- Web frameworkuvicorn- ASGI serverccxt- Crypto exchange integrationaiohttp- Async HTTP clientasyncpg- PostgreSQL async driverredis- Redis clientpydantic- Data validationpython-dotenv- Environment configuration
# Create database
createdb mia_db
# Run migrations (if available)
python -m alembic upgrade head# Using Docker
docker run -d -p 6379:6379 redis:latest
# Or local installation
redis-serverCreate a .env file in the project root:
# PostgreSQL Configuration
DATABASE_URL=postgresql://user:password@localhost/mia_db
DB_HOST=localhost
DB_PORT=5432
DB_NAME=mia_db
DB_USER=postgres
DB_PASSWORD=your_password
# Redis Configuration
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DB=0
REDIS_PASSWORD=
# Exchange API Keys
KUCOIN_API_KEY=your_key
KUCOIN_API_SECRET=your_secret
COINBASE_API_KEY=your_key
KRAKEN_API_KEY=your_key
BINANCE_API_KEY=your_key
BYBIT_API_KEY=your_key
COINGECKO_API_KEY=your_key # Optional, has free tier
# System Configuration
LOG_LEVEL=INFO
ENVIRONMENT=development
DEBUG=false
# Signal Configuration
SIGNAL_IR_THRESHOLD=1.0 # Information Ratio threshold
SIGNAL_CONFIDENCE_MIN=0.65 # Minimum confidence for signals
POSITION_LIMIT_PER_SYMBOL=100000 # Maximum position size
MAX_CONCURRENT_SIGNALS=10 # Max concurrent signal processing
# Webhook Configuration (optional)
WEBHOOK_URL=https://your-domain.com/webhook
WEBHOOK_SECRET=your_secret_key- config/signal_detection_config.py - Signal parameters
- docs/CONFIG_GUIDE.md - Detailed configuration guide
- docs/ENVIRONMENT_SETUP.md - Environment setup steps
python -m tests.diagnose_db_connection
python -m tests.check_table_schemaTerminal 1:
python main.pyThis will:
- Initialize exchange adapters
- Connect to WebSocket feeds
- Start market data ingestion
- Begin signal detection
- Emit events to Redis fabric
Expected Output:
INFO:__main__:Starting Market Data Collection...
INFO:adapter_manager:Initializing KuCoinAdapter
INFO:adapter_manager:WebSocket connected to KuCoin
INFO:event_engine:Event engine initialized
INFO:market_integration:Market graph constructed
Terminal 2:
python -m uvicorn api.intelligence_api:app --host 0.0.0.0 --port 8001 --reloadThis provides:
- REST API endpoints
- WebSocket real-time feed
- Live dashboard UI
Terminal 3:
# Open in your browser:
http://localhost:8001Or via curl:
# Get current signals
curl http://localhost:8001/api/signals
# Get market intelligence
curl http://localhost:8001/api/markets
# Get arbitrage opportunities
curl http://localhost:8001/api/arbitrageTerminal 4:
# Example: Get all active signals
curl http://localhost:8001/api/signals
# Example: Get market data for specific symbol
curl http://localhost:8001/api/markets/BTC-USDT
# Example: Stream real-time updates
curl -N http://localhost:8001/stream/signalsmia/
├── main.py # Entry point - Market data collection
├── metrics.py # System metrics and monitoring
├── requirements.txt # Python dependencies
│
├── analysis/ # Signal Analysis Frameworks
│ ├── indicators.py # Technical indicators (RSI, MACD, etc)
│ ├── regime_classifier.py # Market regime detection
│ ├── structural_confidence.py # Confidence scoring
│ ├── asymmetric_risk.py # Risk framework
│ ├── causality_validator.py # Lead-lag analysis
│ ├── liquidity_stack.py # Liquidity analysis
│ └── edge_calculation.py # Information ratio & edge metrics
│
├── ingestion/ # Exchange Data Ingestion
│ ├── adapter_manager.py # Adapter orchestration
│ ├── adapter_cluster.py # Optimized adapter cluster
│ ├── kucoin_adapter.py # KuCoin integration
│ ├── coinbase_adapter.py # Coinbase integration
│ ├── kraken_adapter.py # Kraken integration
│ ├── binance_adapter.py # Binance integration
│ ├── bybit_adapter.py # ByBit integration
│ ├── websocket_manager.py # WebSocket connection pooling
│ ├── batch_fetcher.py # REST API fallback
│ ├── circuit_breaker.py # Load balancing & circuit breaking
│ ├── gap_detector.py # Data integrity validation
│ ├── timeframe_synthesizer.py # Multi-timeframe synthesis
│ └── multi_exchange_intelligence.py # Cross-exchange analysis
│
├── events/ # Event-Driven Architecture
│ ├── event_engine.py # Core event processing
│ ├── event_fabric.py # Redis-based event distribution
│ ├── signal_consumer.py # Signal consumption & routing
│ ├── signal_quality.py # Signal quality assessment
│ ├── execution_gateway.py # Position management & execution
│ ├── notifier.py # Notification system
│ └── adapters/ # Output adapters
│ ├── redis_consumer_adapter.py
│ ├── websocket_adapter.py
│ ├── database_adapter.py
│ └── webhook_adapter.py
│
├── market/ # Market Analysis
│ ├── market_graph.py # Symbol relationship graph
│ ├── market_selector.py # Market filtering & selection
│ ├── market_integration.py # Market integration layer
│ ├── position_manager.py # Position tracking
│ └── capital_flow_funnel.py # Capital flow analysis
│
├── api/ # API Layer
│ ├── intelligence_api.py # Main REST API
│ ├── data_access_api.py # Data retrieval API
│ ├── query_api.py # Query builder
│ └── web_interface.py # Dashboard UI
│
├── cache/ # Caching Layer
│ └── redis_layer.py # Redis client & operations
│
├── config/ # Configuration
│ └── signal_detection_config.py # Signal parameters
│
├── normalization/ # Symbol Normalization
│ ├── normalizer.py # Symbol normalization logic
│ ├── symbol_registry.py # Symbol registry
│ ├── symbol_resolver.py # Symbol resolution
│ └── symbol_utils.py # Utility functions
│
├── mia_mcp/ # MCP Server Integration
│ └── mcp_server.py # Model Context Protocol server
│
├── neondb/ # Database Schema
│ └── [migration files]
│
├── redis/ # Redis Configuration
│ └── [scripts and config]
│
├── tests/ # Testing & Diagnostics
│ ├── diagnose_db_connection.py
│ ├── diagnose_data_flow.py
│ ├── check_tables.py
│ └── [other test utilities]
│
├── docs/ # Documentation
│ ├── README_STARTUP.md # Startup guide
│ ├── MIA_SYSTEM_ARCHITECTURE.md # Architecture details
│ ├── API_GUIDE.md # API documentation
│ ├── CONFIG_GUIDE.md # Configuration guide
│ ├── ENVIRONMENT_SETUP.md # Environment setup
│ ├── EVENT_FABRIC_GUIDE.md # Event system guide
│ └── [other documentation]
│
└── .env # Environment configuration (git-ignored)
# Get all active signals
GET /api/signals
# Get signals for specific symbol
GET /api/signals?symbol=BTC-USDT
# Get signals with minimum confidence
GET /api/signals?confidence=0.75
# Get signal history
GET /api/signals/history?limit=100&offset=0# Get all available markets
GET /api/markets
# Get market data for specific symbol
GET /api/markets/{symbol}
# Get market metrics
GET /api/markets/{symbol}/metrics
# Get market correlations
GET /api/markets/correlations?symbols=BTC-USDT,ETH-USDT# Get arbitrage opportunities
GET /api/arbitrage
# Get arbitrage for specific symbol
GET /api/arbitrage?symbol=BTC-USDT
# Get arbitrage by exchange pair
GET /api/arbitrage?exchange1=kucoin&exchange2=coinbase# System health check
GET /health
# Get system status
GET /api/status
# Get metrics
GET /api/metrics# Connect to real-time signal stream
ws://localhost:8001/ws/signals
# Connect to market data stream
ws://localhost:8001/ws/markets
# Connect to all events stream
ws://localhost:8001/ws/eventsFor complete API documentation, see API_GUIDE.md.
# Run all tests
pytest
# Run specific test file
pytest tests/test_analysis.py
# Run with coverage
pytest --cov=.# Check database connection
python -m tests.diagnose_db_connection
# Verify table schema
python -m tests.check_table_schema
# Diagnose data flow
python -m tests.diagnose_data_flow# Format code
black .
# Lint code
flake8 .
# Type checking
mypy .- Create adapter in
ingestion/new_exchange_adapter.py - Inherit from base adapter class
- Implement required methods (connect, subscribe, parse)
- Register in
ingestion/adapter_manager.py - Add to
main.pyinitialization
See ADAPTER_OPTIMIZATION_GUIDE.md for details.
The system tracks:
- Async task counts - Active coroutines
- Batch flush times - Data pipeline latency
- Event throughput - Events per second
- Processing latencies - End-to-end latency
- Queue depths - Signal backlog monitoring
- Exchange connectivity - Feed health status
# Via API
curl http://localhost:8001/api/metrics
# Via logs
grep METRIC main.log | tail -20
# Via dashboard
http://localhost:8001#metrics- Adjust batch sizes in
config/signal_detection_config.py - Configure circuit breaker thresholds in
ingestion/circuit_breaker.py - Tune Redis connection pool in
cache/redis_layer.py
# Check connectivity
python -m tests.diagnose_db_connection
# Verify credentials in .env
cat .env | grep DATABASE_URL
# Test connection manually
psql $DATABASE_URL -c "SELECT 1"# Check Redis is running
redis-cli ping
# Start Redis if needed
redis-server
# Or with Docker
docker run -d -p 6379:6379 redis:latestThe system includes adaptive retry logic with exponential backoff. Check:
- API key rate limits with your exchange
RATE_LIMIT_BACKOFF_MSin config- Circuit breaker thresholds
# From ingestion/circuit_breaker.py
BACKOFF_BASE = 1000 # 1 second initial backoff
MAX_RETRIES = 5Enable reconnection logic in your adapter:
# Auto-reconnect with exponential backoff
await adapter.connect_with_backoff(max_retries=5)Verify:
# 1. Data is flowing
curl http://localhost:8001/api/markets
# 2. Market graph is built
python -c "from market.market_graph import MarketGraph; print(MarketGraph().symbols_count())"
# 3. Event engine is running
tail -f main.log | grep "EventEngine"# In .env
LOG_LEVEL=DEBUG
# Or in Python
import logging
logging.basicConfig(level=logging.DEBUG)python tests/diagnose_data_flow.py- README_STARTUP.md - Quick startup guide
- MIA_SYSTEM_ARCHITECTURE.md - Complete architecture
- API_GUIDE.md - REST API reference
- CONFIG_GUIDE.md - Configuration options
- ENVIRONMENT_SETUP.md - Environment setup
- EVENT_FABRIC_GUIDE.md - Event system
- ADAPTER_OPTIMIZATION_GUIDE.md - Exchange adapters
- ROBUSTNESS_IMPROVEMENTS.md - Reliability features
- NETWORK_TROUBLESHOOTING.md - Network issues
| Metric | Target | Status |
|---|---|---|
| Signal Latency (candle → signal) | < 500ms | ✅ |
| Event Propagation | < 100ms | ✅ |
| Exchange Connection Setup | < 2s | ✅ |
| Database Query (95th percentile) | < 100ms | ✅ |
| Redis Operations | < 10ms | ✅ |
| Max Concurrent Signals | 10+ | ✅ |
| Data Integrity Score | > 95% | ✅ |
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for new functionality
- Commit with clear messages (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For issues, questions, or suggestions:
- 📧 Email: gichurejames001@gmail.com
- 💬 Issues: GitHub Issues
- 📚 Documentation: docs/
- 🐛 Bug Reports: Include system info, .env (sanitized), and logs
- Multi-timeframe regime detection enhancements
- Additional exchange integrations (Deribit, OKX)
- Advanced position sizing algorithms
- Machine learning signal filtering
- Risk management enhancements
- Kubernetes deployment templates
- Advanced metrics dashboard
- Built with FastAPI
- Exchange data via CCXT
- Event streaming with Redis
- Data storage with PostgreSQL
MIA - Bringing Professional-Grade Market Intelligence to Crypto Trading
Made with ❤️ for traders and developers