How arockiadassdev/ai-crypto-trading-bot works
Purpose and entry points
This is a Python crypto-trading research and paper-trading system combining Binance market data, technical indicators, ML/DL predictions, strategy signals, risk controls, and historical backtests. FastAPI is the service boundary at backend/app/main.py; REST routes are in backend/app/api/trading.py and backend/app/api/health.py. The operator-facing dashboard starts at streamlit_app/app.py.
Runtime and deployment topology
The intended runtime is Python 3.13+, with FastAPI/Uvicorn for the API and a separate Streamlit process. Containers are defined by Dockerfile, Dockerfile.streamlit, and docker-compose.yml. Material external dependencies are PostgreSQL 16, Redis 7, Binance through CCXT and its WebSocket API, and MLflow. Docker database initialization is at docker/postgres/init.sql.
API orchestration boundary
Trading endpoints accept API schemas, invoke domain services, and return operational state such as prices, signals, portfolio data, model predictions, backtests, and paper orders. The route layer should remain the boundary between HTTP concerns and trading behavior:
• backend/app/api/trading.py
• backend/app/schemas/trading.py
• backend/app/main.py
Health, readiness, and liveness checks are isolated from trading operations in backend/app/api/health.py and backend/app/schemas/health.py.
Market-data and feature pipeline
Historical acquisition and live streaming feed the system’s market-data path. Binance integration is encapsulated rather than exposed directly to strategies; data is processed into indicator/feature-bearing datasets before signal or model use. Local persistence directories distinguish raw, processed, and feature datasets.
• backend/app/services/exchange.py
• backend/app/websocket/client.py
• backend/app/services/data_pipeline.py
• backend/app/services/historical_data.py
• backend/app/indicators/compute.py
• data/raw, data/processed, data/features
Strategy, risk, and paper execution flow
The primary decision flow is market data/features → strategy engine and signal generator → risk validation/sizing → simulated execution → portfolio/trade state. Risk is a hard control boundary before execution; paper trading is the available execution implementation, not evidence of live-order placement.
• backend/app/strategy/engine.py
• backend/app/strategy/signal_generator.py
• backend/app/risk/manager.py
• backend/app/trading/paper_trading.py
• backend/app/portfolio/manager.py
Domain records for orders, trades, signals, portfolios, and market data are modeled under backend/app/models/order.py, backend/app/models/trade.py, backend/app/models/portfolio.py, backend/app/models/trading_signal.py, and backend/app/models/market_data.py.
ML and model lifecycle
The ML subsystem supports tree-based and LSTM models, preprocessing, and a registry layer. Training/inference is exposed through the API orchestration path and consumes prepared market features; trained artifacts are stored separately from source. MLflow is the experiment/model-tracking integration.
• backend/app/ml/xgboost_model.py
• backend/app/ml/lstm_model.py
• backend/app/ml/model_registry.py
• backend/app/utils/preprocessing.py
• models, mlflow
Backtesting and persistence
Historical strategy evaluation is separated from live/paper decisioning in backend/app/backtesting/engine.py, producing persisted backtest results via backend/app/models/backtest_result.py. PostgreSQL access uses async SQLAlchemy session/base modules, with schema evolution managed by Alembic:
• backend/app/database/session.py
• backend/app/database/base.py
• backend/alembic/versions/001_initial_schema.py
Operational outputs and generated pipeline results belong in artifacts; configuration and structured logging are centralized at backend/app/config/settings.py and backend/app/core/logging.py.
Main entry points: trading.py Trading routes & schemas · features Feature datasets · engine.py Backtest engine · main.py FastAPI service
Components 22
Service Interfaces
- FastAPI service · HTTP boundary for trading and health operations.
backend/app/main.py - Trading routes & schemas
backend/app/api/trading.py - Health routes
backend/app/api/health.py - Streamlit dashboard
streamlit_app/app.py
Market Data Pipeline
- Binance · Market-data source via CCXT and WebSocket APIs.
- Exchange & historical ingestion
backend/app/services/exchange.py - WebSocket client
backend/app/websocket/client.py - Data pipeline
backend/app/services/data_pipeline.py - Indicator computation
backend/app/indicators/compute.py - Feature datasets · Raw, processed, and feature-bearing market datasets.
data/features
Trading Decisioning
- Strategy engine
backend/app/strategy/engine.py - Signal generator
backend/app/strategy/signal_generator.py - Risk manager · Hard validation and sizing gate before execution.
backend/app/risk/manager.py - Paper trading · Available execution path; no live-order placement implied.
backend/app/trading/paper_trading.py - Portfolio manager
backend/app/portfolio/manager.py
ML & Backtesting
- XGBoost & LSTM models
backend/app/ml/xgboost_model.py - Model registry
backend/app/ml/model_registry.py - MLflow
mlflow - Backtest engine
backend/app/backtesting/engine.py
Persistence & Operations
- PostgreSQL · Async SQLAlchemy-backed state and persisted results.
- Initial schema migration
backend/alembic/versions/001_initial_schema.py - Artifacts · Operational outputs and generated pipeline results.
artifacts
Connections 26
- FastAPI service → Trading routes & schemas: mounts
- FastAPI service → Health routes: mounts
- Streamlit dashboard → FastAPI service: uses operational API
- Trading routes & schemas → Exchange & historical ingestion: requests prices and data
- Trading routes & schemas → Strategy engine: orchestrates signals
- Trading routes & schemas → XGBoost & LSTM models: requests predictions
- Trading routes & schemas → Backtest engine: runs evaluations
- Trading routes & schemas → Paper trading: submits paper orders
- Binance → Exchange & historical ingestion: historical market data
- Binance → WebSocket client: live market stream
- Exchange & historical ingestion → Data pipeline: acquired data
- WebSocket client → Data pipeline: live data
- Data pipeline → Indicator computation: prepared datasets
- Indicator computation → Feature datasets: writes features
- Feature datasets → Strategy engine: market features
- Feature datasets → XGBoost & LSTM models: training and inference features
- Strategy engine → Signal generator: generates
- Signal generator → Risk manager: proposed orders
- Risk manager → Paper trading: approved and sized orders
- Paper trading → Portfolio manager: fills and trade state
- Portfolio manager → PostgreSQL: persists state
- Backtest engine → PostgreSQL: persists results
- XGBoost & LSTM models → Model registry: registers artifacts
- Model registry → MLflow: tracks models and experiments
- Backtest engine → Artifacts: writes outputs
- Initial schema migration → PostgreSQL: evolves schema