How tienan92it/backtesting-system works
Project Type and Purpose
• The system is a specialized backtesting engine for simulating and evaluating cryptocurrency trading strategies using historical data.
• It uses an event‑driven architecture where market data flows through various components to generate trading signals and simulate executions.
• Additionally, there is an AI‑driven layer that parses natural language strategy descriptions and automatically generates Python code to integrate with the backtester.
Analyzing the File Structure
• The “backtesting” directory contains all the core modules for the backtesting engine:
– Data (data handlers using Binance or CCXT data)
– Strategy (strategy definitions and examples)
– Portfolio (position and risk management)
– Execution (order simulation and execution handling)
– Metrics (performance measurements)
– Engine (backtester control logic, event loop, and event coordinating)
• The “ai_driven” folder introduces AI‑enhancements:
– API submodule for interfacing with external services or exposing endpoints.
– Parser (for converting natural language strategy instructions into structured data)
– Code Generator (to produce strategy code automatically)
– Report generation and UI components for visualizing outcomes
• Other folders (cli, examples, tests) support command line operations, example scenarios, and testing respectively.
Main Components
• Data Handler: Fetches and formats market data.
• Strategy: Applies trading algorithms and generates signal events.
• Portfolio: Manages assets, positions, and risk.
• Execution Handler: Simulates order placement, fills, and tracks trading orders.
• Event Loop: Coordinates and propagates various market and strategy events.
• Backtester: Orchestrates the overall simulation, manages components, and computes performance metrics.
b. AI-Driven Modules (can be a separate cluster connected to the core engine):
• Strategy Parser Module: Converts natural language instructions into structured strategy specifications.
• Strategy Code Generator Module: Works with the parsed strategy to generate executable Python code that integrates with the backtesting engine.
• UI and Reporting: Enables interaction and visualizes generated reports/templates.
c. Supporting Components:
• API Layer: For exposing functionality (under ai_driven/api).
• CLI Interface: Allows users to run backtests via the command line.
• External Data Sources: Indicate connection to crypto exchanges like Binance (shown as external systems feeding data).
Relationships and Interactions
• Data flows from the Data Handler into the Event Loop by triggering Market Events.
• The Event Loop distributes events (Market, Signal, Order, Fill, Portfolio updates) across the Strategy, Portfolio, and Execution Handler.
• The Backtester coordinates these interactions and aggregates performance results.
• In the AI‑driven cluster:
• The Parser’s output feeds into the Code Generator, which produces executable code that then can influence the Strategy component of the core engine.
• Optionally, display integration with the API and UI components for external interaction.
Architectural Patterns/Design Principles
• Event‑driven nature using a central Event Loop that orchestrates all components.
• Modularity and separation of concerns (each module handles its specific tasks: data fetching, strategy logic, portfolio management, etc.).
• How AI modules extend the system by transforming natural language into structured trading strategies.