Skip to content

PyMT5 Project MapΒΆ

Complete architecture guide. Shows the three-tier system, component interactions, and file organization.


πŸ—ΊοΈ Project Architecture OverviewΒΆ

PyMT5 is structured into three clean, decoupled layers designed to satisfy both low-level quantitative control and high-level strategy convenience:

PyMT5/
β”œβ”€β”€ πŸ“¦ Layer 1: MT5Account (Low-level gRPC)
β”œβ”€β”€ πŸ”§ Layer 2: MT5Service (Typed wrapper methods)
└── ⭐ Layer 3: MT5Sugar (High-level convenience API)

πŸ“Š Component Interaction DiagramΒΆ

YOUR CODE (User-facing)
  β”œβ”€ Orchestrators (strategy implementations)
  β”œβ”€ Presets (multi-strategy combinations)
  └─ Examples (learning materials)
                  β”‚
                  β”‚ uses
                  ↓
MT5Sugar (Layer 3 - Convenience)
  β”œβ”€ Auto-normalization (volumes, prices)
  β”œβ”€ Risk management (CalculateVolume, BuyByRisk)
  β”œβ”€ Points-based methods (BuyLimitPoints, etc.)
  └─ Batch operations (CloseAll, CancelAll)
                  β”‚
                  β”‚ uses
                  ↓
MT5Service (Layer 2 - Wrappers)
  β”œβ”€ Direct data returns
  β”œβ”€ Type conversions (proto β†’ native primitives)
  └─ Simplified signatures (no raw proto required)
                  β”‚
                  β”‚ uses
                  ↓
MT5Account (Layer 1 - Low-level)
  β”œβ”€ Proto Request/Response handling
  β”œβ”€ gRPC communication & channel management
  β”œβ”€ Auto-reconnection on transient faults
  └─ Real-time streaming subscriptions
                  β”‚
                  β”‚ gRPC
                  ↓
MT5 Gateway (mt5term) or MT5 Terminal
  └─ MetaTrader 5 with gRPC server

πŸ” Layer BreakdownΒΆ

Layer 1: MT5Account (Low-Level gRPC)ΒΆ

  • Direct gRPC stubs communicating with the terminal.
  • Handles protobuf message serialization/deserialization.
  • Responsible for connection recovery, channel state monitoring, and raw streaming calls.
  • Ideal when you need complete control over protobuf payload fields.

Layer 2: MT5Service (Wrappers)ΒΆ

  • Translates protobuf messages into native Python primitives and data models.
  • Removes boilerplate request/response wrapper instantiation.
  • Simplifies method signatures for common terminal actions (Positions, Orders, and Deals).

Layer 3: MT5Sugar (Convenience Layer)ΒΆ

  • Automates lot size calculation and margin checks based on risk percentages.
  • Automatically normalizes prices and volumes to broker specifications (step size, digits, minimum lot).
  • Provides one-line batch operations: close all profitable positions, cancel pending orders.
  • Point-offset helpers (BuyLimitPoints, SellStopPoints).

🎯 Which Layer Should You Use?¢

  • 90% of trading applications: Start with MT5Sugar. It protects you from off-quote rejections and invalid volume errors.
  • Custom algorithmic execution: Use MT5Service when you manage your own volume normalization logic.
  • Protocol engineering: Drop to MT5Account when you need raw protobuf structures.