Account Information
Relevant source files
The following files were used as context for generating this wiki page:
Account Information in the mt5linux project encompasses the data structures and API methods used to retrieve, monitor, and provision trading account details from a MetaTrader 5 (MT5) terminal. The system provides three distinct views of an account: the standard MT5 fields (balance, equity, margin), a unified health-plus-account view (CurrentAccount), and a persistence-based recovery view (ProvisionedAccount) for containerized environments.
This module is designed to bridge the gap between the Linux-based client and the Windows-hosted MT5 terminal via gRPC, ensuring that account state is accessible even when the terminal is temporarily offline or in the process of re-initialization.
Sources: mt5linux/models.py:273-280, mt5linux/async_client.py:23-28
Account Architecture and Data Flow
The account information system is built on a layered architecture where the gRPC service retrieves raw data from the MT5 terminal, and the client-side Pydantic models validate and structure this data for the user.
This diagram illustrates the logic of the current_account() extension, which provides a comprehensive view of the container's status regardless of the terminal's login state.
Sources: mt5linux/async_client.py:804-834, mt5linux/mt5.proto:211-212
Data Models
The project utilizes Pydantic 2 models to ensure type safety and validation of account data received from the MT5 bridge.
MT5Models.AccountInfo
This is the standard model representing the 28 fields provided by the native MetaTrader 5 API. It includes critical financial metrics such as balance, equity, margin, and leverage.
Sources: mt5linux/models.py:242-271, tests/unit/test_models.py:183-255
MT5Models.CurrentAccount
An extension unique to mt5linux, this model combines connection health metrics with live account data. It is intended to answer the question, "Which broker/login is this container currently handling?" even if the terminal is not yet authorized.
Sources: mt5linux/models.py:273-290, mt5linux/async_client.py:804-814
MT5Models.ProvisionedAccount
This model represents the account identity recovered from local configuration files (auto_demo.json). It allows the client to identify an automatically created demo account even when the terminal is offline.
Sources: mt5linux/models.py:292-311, mt5linux/async_client.py:852-861
| Model | Purpose | Key Fields |
|---|---|---|
AccountInfo | Native MT5 data | login, balance, equity, margin, leverage |
CurrentAccount | Unified health view | host, port, connected, trade_allowed, login |
ProvisionedAccount | Recovery identity | login, server, email, created_at, source |
| Sources: mt5linux/models.py:242-311 |
Account Provisioning
The project supports the dynamic creation and recovery of demo accounts, particularly useful in automated or Docker-based testing environments.
Demo Account Creation
The create_demo_account() method drives the MT5 demo account wizard via the bridge. It uses the CreateDemoSpec model to pass required parameters like broker server, email, and phone number.
Sources: mt5linux/models.py:313-326, mt5linux/async_client.py:871-885
Provisioned Account Recovery
The recover_provisioned_account() method is a read-only, idempotent operation that retrieves the container's provisioned identity from the bridge. This is critical for zero-touch environments where credentials are set at launch via environment variables.
Sources: mt5linux/async_client.py:852-869, mt5linux/mt5.proto:173-184
API Reference
The following table summarizes the primary methods for interacting with account information.
| Method | Sync/Async | Return Type | Description |
|---|---|---|---|
account_info() | Both | AccountInfo | None | Retrieves live MT5 account statistics. Returns None if terminal is not connected. |
current_account() | Both | CurrentAccount | Returns host/port and connection state. Includes account details if connected. |
recover_provisioned_account() | Both | ProvisionedAccount | Recovers identity from local container configuration. |
create_demo_account(spec) | Both | ProvisionedAccount | Initiates the MT5 GUI wizard to create a new demo account. |
| Sources: mt5linux/async_client.py:789-885, mt5linux/client.py:196-224 |
# Example: Retrieving unified account state (Sync)
from mt5linux import MetaTrader5
with MetaTrader5(host="localhost", port=50051) as mt5:
# current_account works even if terminal is not initialized
status = mt5.current_account()
print(f"Connected: {status.connected}, Broker: {status.server}")
if status.connected:
print(f"Balance: {status.balance} {status.currency}")
Sources: mt5linux/client.py:21-25, mt5linux/async_client.py:804-814
Summary
Account Information in mt5linux is more than just a wrapper around the MT5 AccountInfo structure. It provides a resilient, container-aware system for identifying and managing accounts. By separating "provisioned" identity from "live" terminal state, it allows developers to build robust trading applications that can gracefully handle terminal disconnections and automated account provisioning.
Sources: mt5linux/async_client.py:804-814, mt5linux/models.py:273-280