RPyC Bridge Service Execution
Relevant source files
The following files were used as context for generating this wiki page:
The RPyC Bridge Service (now primarily implemented via a gRPC bridge in the latest versions) facilitates remote communication between a Linux-based Python environment and the MetaTrader 5 (MT5) terminal running under Wine. Its primary purpose is to expose the Windows-only MT5 Python API to external clients, allowing for programmatic trading, data retrieval, and account management from a Dockerized environment. Sources: README.md:12, docker/container/metatrader/bridge.py:1-24
This service is a critical component of the mt5-docker architecture, providing a standalone bridge that handles concurrent request processing, signal management for clean container shutdowns, and efficient data serialization (using orjson and numpy byte-streams). It effectively bridges the gap between the dual-Python architecture of the project: System Python 3.13 for orchestration and Wine Python 3.12 for MT5 compatibility. Sources: README.md:27-28, docker/container/metatrader/bridge.py:26-30
Architecture and Components
The bridge service operates within a Wine prefix inside the Docker container. It uses a gRPC-based service model that replaces older RPyC implementations for better asynchronous performance and security. Sources: docker/container/metatrader/mt5.proto:2-3, docker/container/metatrader/bridge.py:18-24
Bridge Execution Environment
The bridge is executed using Windows Python inside the Wine environment. It is typically started as a module or a standalone script.
Sources: docker/container/metatrader/bridge.py:32-34, docker/container/metatrader/setup.sh:317-328
The diagram above illustrates the relationship between the external client, the gRPC bridge, and the MT5 terminal within the container. Sources: README.md:267-270, docker/container/metatrader/bridge.py:1260-1280
Key Classes and Logic
The core logic resides in the MT5GRPCServicer class, which inherits from MT5ServiceServicerBase. This class implements the MT5 service definition provided in the protobuf files. Sources: docker/container/metatrader/bridge.py:183-195, docker/container/metatrader/mt5_pb2_grpc.py:246-250
- Concurrency Management: Uses
ThreadPoolExecutorto handle multiple incoming gRPC requests simultaneously. Sources: docker/container/metatrader/bridge.py:1265-1270 - Timeout Protection: A specialized
_call_mt5_with_timeouthelper wraps synchronous MT5 calls to prevent the bridge from hanging indefinitely if the terminal becomes unresponsive. Sources: docker/container/metatrader/bridge.py:133-162 - Serialization: Employs
orjsonfor rapid JSON serialization of dictionary data and convertsnumpyarrays to bytes for efficient transport. Sources: docker/container/metatrader/bridge.py:173-181, docker/container/metatrader/bridge.py:244-263
Deployment and Setup
The service files are copied into the Wine site-packages during the container setup phase to ensure they are available to the Wine Python interpreter. Sources: docker/container/metatrader/setup.sh:305-315
Installation Steps
| Phase | Action | Description |
|---|---|---|
| Copy | Bridge files transfer | bridge.py, mt5_pb2.py, and mt5_pb2_grpc.py are moved to drive_c/Python/Lib/site-packages/mt5linux. |
| Verification | Import Check | The setup script executes a wine python command to verify that the bridge stubs can be imported without protobuf runtime mismatches. |
| Startup | Service Initiation | The s6-overlay starts the bridge, binding it to port 8001 by default. |
Sources: docker/container/metatrader/setup.sh:305-345, README.md:267-270
Service Definition and API Surface
The service exposes a comprehensive API that mirrors the official MetaTrader 5 Python API, along with container-specific management functions. Sources: docker/container/metatrader/mt5.proto:200-261, tests/test_bridge_validation.py:27-185
Core API Categories
- Terminal Operations:
HealthCheck,Initialize,Login,Shutdown,Version,LastError,GetConstants. - Account/Symbol Info:
TerminalInfo,AccountInfo,SymbolsTotal,SymbolsGet,SymbolInfo,SymbolSelect. - Market Data:
CopyRatesFrom,CopyRatesRange,CopyTicksFrom,CopyTicksRange. - Trading:
OrderCalcMargin,OrderCalcProfit,OrderCheck,OrderSend. - Provisioning:
GetProvisionedAccount,CreateDemoAccount.
Sources: docker/container/metatrader/mt5.proto:200-261, docker/container/metatrader/bridge.py:316-1215
Provisioning Flow
The bridge includes logic to trigger a Linux-side GUI automation script for zero-touch demo account creation. Sources: docker/container/metatrader/bridge.py:79-130
The sequence above shows how the bridge coordinates cross-environment execution between Wine and Linux to handle GUI-based tasks. Sources: docker/container/metatrader/bridge.py:79-130, docker/container/metatrader/bridge.py:461-517
Service Execution Parameters
The bridge supports several command-line arguments for configuration during execution.
Sources: docker/container/metatrader/bridge.py:1283-1320
| Parameter | Flag | Default | Description |
|---|---|---|---|
| Host | --host | 0.0.0.0 | IP address to bind the service. |
| Port | --port | 50051 | TCP port for gRPC (Container internal). |
| Workers | --workers | 10 | Number of threads in the ThreadPool. |
| Debug | --debug | False | Enables verbose debug logging. |
| MT5 Timeout | --mt5-timeout | 30.0 | Maximum seconds to wait for an MT5 API call. |
Reliability and Health Monitoring
The service includes built-in safeguards to ensure continuous operation within the Docker container. Sources: README.md:213-220
- HealthCheck RPC: Allows clients and container health monitors to verify if the MT5 terminal is available, connected to a broker, and allowed to trade. Sources: docker/container/metatrader/bridge.py:316-360
- Signal Handling: Specifically captures
SIGTERMandSIGINTto trigger_graceful_shutdown, which stops the gRPC server with a 5-second grace period before exiting. Sources: docker/container/metatrader/bridge.py:1248-1260 - Fail-Fast Design: The bridge is designed to exit immediately on critical startup failures or if required dependencies like the MT5 terminal are not found. Sources: README.md:42, docker/container/metatrader/setup.sh:347-375
Conclusion
The gRPC Bridge Service is the primary interface for remote control of MetaTrader 5 within this Docker project. By encapsulating the complex interaction between Wine, Windows Python, and the MT5 terminal into a standard gRPC service, it provides a performant and reliable platform for automated trading. Its execution model emphasizes concurrency, timeout safety, and clean lifecycle management.