How shinnytech/tqsdk-python works
Project Type and Purpose
• This project is a specialized trading strategy development package. It is designed to be the core engine that enables users to subscribe to market data, execute algorithmic trading strategies (both in simulation/backtesting and real trades), manage risk, calculate technical indicators, and provide a web-based GUI for visualization and debugging.
• The README emphasizes the core components: connecting to行情网关 (Market Data Gateway) and交易中继网关 (Trade Gateway) via the Diff协议 and providing robust backtesting, simulation, and real trading functionalities.
File Structure
• The top-level structure includes documentation (doc/), standard configuration files (setup.py, requirements.txt, etc.), and the main package directory “tqsdk”.
• API Initialization and Utility Files: files like tqsdk/api.py, tqsdk/connect.py, tqsdk/baseApi.py, and tqsdk/utils.py encapsulate the main API interactions.
• Market & Trade Components: files such as tqsdk/account.py, tqsdk/backtest.py with its submodules, tqsdk/sim/ and tqsdk/trade_extension.py implement functionalities for backtesting, simulation, account management, and actual trades.
• Algorithm and Demo Modules: the “algorithm” subdirectory and “demo” subdirectories (e.g., tqsdk/demo/*) contain example strategy implementations and algorithmic trading examples.
• Risk Management and Position Handling: files such as tqsdk/risk_manager.py, tqsdk/risk_rule.py, and TargetPosTask utility (encapsulated within the library) support risk control and target positioning.
• Web and GUI Components: the “tqsdk/web” folder provides web assets (HTML, CSS, JavaScript) for the integrated graphical interface.
• Integration with External Gateways: although the gateways themselves (Open Md Gateway / Open Trade Gateway) are separate projects, TqSdk’s core connectivity (via tqsdk/connect.py, tqsdk/channel.py) is clearly designed to conform to the Diff协议.
Extract and Emphasize README Insights
• The two gates: Open Md Gateway for market data and Open Trade Gateway for trading.
• Diff协议 as the integration layer between TqSdk and the gateways.
• A demonstration snippet that shows how the user’s strategy code instantiates the API, subscribes to quotes, and uses the TargetPosTask to adjust positions.
• This reinforces that the main architectonic design involves a central API engine that seamlessly interacts with external services and offers multiple modes (real trading, simulation, backtesting).
a. Main Components:
• User Strategy Application: The client that uses the library (i.e., developer-written strategy code using TqSdk).
• TqSdk Core Engine: This includes modules for the API, connection handling, and the in-memory data management.
• Diff协议 Layer: Abstract layer ensuring communication with external gateways.
• Market Data & Order Management: Modules handling market data (tqsdk/api, tqsdk/channel) and executing orders.
• Backtesting & Simulation: Separate submodules for replaying historical data (tqsdk/backtest) and simulating trades (tqsdk/sim, tqsdk/tradeable/sim).
• Risk & Trade Management: Modules like risk_manager.py, risk_rule.py, and TargetPosTask within lib/ to enforce risk control and manage positions.
• Technical Analysis: The algorithm and TA functions (tqsdk/algorithm, tqsdk/ta, tqsdk/tafunc) for technical indicator computations.
• Demo & Examples: Represent the sample strategies that illustrate usage.
• Web GUI: Stand-alone or integrated component showing interactive visual representation (found in tqsdk/web).
b. Relationships and Interactions:
• That the User Strategy Application interacts with the TqSdk Core Engine via a well-defined API. This API internally orchestrates the flow of data between:
• The Diff协议 Layer and external gateways: Market Data Gateway (Open Md Gateway) and Trade Gateway (Open Trade Gateway).
• The in-memory database holding real-time market data and order status.
• The backtesting/simulation modules, which can work with historical data and simulate execution.
• External components (Market and Trade Gateways) towards the Diff协议 Module.
• The Diff协议 Module into the Core Engine.
• The Core Engine (especially order and risk management) back out to external trade systems for real trading.
• User code calling into API functions that drive trading decisions.
c. Architectural Patterns and Design Principles:
• Separation of Concerns: Clearly separate components that handle market data retrieval, order execution, risk management, and simulation/backtesting.
• Modularity: Each module (e.g., backtest, sim, algorithm) is self-contained, supporting extensibility.
• Abstraction via the Diff协议 layer: Encapsulates communication to varying external gateways.
d. Technologies and Libraries:
• Python 3.7+ forms the base environment.
• Support for pandas and numpy is explicitly optimized within the system.
• Web assets that provide a GUI (using JavaScript/CSS/HTML from tqsdk/web).
Elements
• Clearly labeled nodes for each major component (e.g., “User Strategy Code”, “TqSdk API Engine”, “Diff协议 Adapter”, “Market Data Gateway”, “Trade Gateway”, “Backtesting Module”, “Risk Manager”, “Web GUI”).
• Data flow (market data, order execution signals, trading commands) from external systems to the API engine and back.
• Dependencies between modules (for instance, the User Strategy relies on API updates that are processed by the risk manager and order management modules).
• Differentiate between external systems (gateways), core engine modules, and user interface components.
• Annotations or callouts where needed (e.g., “Diff协议 layer facilitates uniform communication”, “In-memory database for high-speed data access”, “Risk rules enforced before order execution”).
Be Detailed but Organized
• The goal is to allow a principal software engineer to quickly grasp the architecture’s modularity, data flow, and integration boundaries.