How phongquach239/freqtrade works
Project Type and Purpose
• This is a full-stack, Python-based crypto-trading bot (Freqtrade) that provides:
• A CLI tool (“freqtrade” command)
• A built-in web UI / REST & WebSocket API server
• Telegram/Discord RPC integration
• Backtesting, hyperparameter optimization (Hyperopt), edge-sizing, plotting, and FreqAI (ML) modules
• Live trading via multiple exchange adapters (REST & WS) with persistence (SQLite)
Top-Level Architecture Components
a. User Interfaces
• CLI
• Web UI (served by FastAPI/Uvicorn in freqtrade/rpc/api_server)
• Telegram/Discord bots (freqtrade/rpc/telegram.py, discord.py)
b. Core Engine
• Command dispatcher & CLI options (freqtrade/commands)
• Configuration loader & validator (freqtrade/configuration)
• Strategy executor & wrapper (freqtrade/strategy)
• Data provider & history manager (freqtrade/data)
• Backtesting & analysis (freqtrade/optimize, freqtrade/data/btanalysis)
• Hyperopt subsystem (freqtrade/optimize/hyperopt)
• Edge-position sizing (freqtrade/edge)
• Plotting & reporting (freqtrade/plot)
• FreqAI ML models & RL environments (freqtrade/freqai)
c. Adapters & Infrastructure
• Exchange adapters (REST + WS) for Binance, Kraken, etc. (freqtrade/exchange)
• Persistence layer with SQLite / SQLAlchemy (freqtrade/persistence)
• Plugin loaders (pairlist, protections) (freqtrade/plugins)
• RPC manager & message routing (freqtrade/rpc/rpc_manager.py)
d. External Services
• Exchanges (via CCXT underneath exchange modules)
• Telegram API
• Discord API
• (Optional) other webhook endpoints
Relationships & Data Flows
• User → CLI/Web UI/Telegram → RPC Manager → Core Engine
• Core Engine queries config subsystem, then:
• For live trading: Data Provider pulls live market data from Exchange WS/REST → Strategy module generates signals → Exchange adapter executes orders → Persistence records trades
• For backtesting: Data Provider retrieves historical data (feather/parquet/json) → Backtest engine simulates strategy → Reporting & Plotting
• For hyperopt: Hyperopt engine generates parameter sets → Backtest engine → Hyperopt loss evaluator → Result stored in user_data/hyperopts
• For FreqAI: Data Kitchen prepares features → ML model trains/predicts → Strategy uses predictions
• Persistence is a common sink for configs, live trades, backtest results, logs
Architectural Patterns & Principles
• Hexagonal (Ports & Adapters): Core engine is isolated from exchanges, UIs, persistence via well-defined interfaces and resolvers (freqtrade/resolvers)
• Plugin architecture: Pairlist and protection filters loaded dynamically via plugin managers
• Single responsibility: Separate modules for commands, data handling, strategy logic, execution, ML, optimization
• Asynchronous I/O: Web server and exchange WS clients leverage asyncio (freqtrade/system/asyncio_config)
• Config-driven: The entire setup is driven by a unified JSON config schema (freqtrade/config_schema)