Symbol Information
Relevant source files
The following files were used as context for generating this wiki page:
The Symbol Information module in mt5linux provides a comprehensive interface for retrieving and managing financial instrument data from MetaTrader 5 (MT5). It encompasses the retrieval of static properties (e.g., contract size, digits, and profit calculation modes), real-time pricing via ticks, and the management of symbols within the "Market Watch" window. This system is critical for trade validation, price calculations, and market data subscriptions.
The module operates through a gRPC bridge that connects a Linux-based client to an MT5 terminal running under Wine. It provides both synchronous and asynchronous APIs, utilizing Pydantic models to ensure type safety and structured data handling across the network boundary.
Sources: mt5linux/client.py, mt5linux/async_client.py:681-744, mt5linux/mt5.proto:186-191
Architectural Overview
The architecture for retrieving symbol information follows a layered approach, moving from the high-level user API down to the gRPC service definition.
The diagram shows the request flow from the Python client to the MT5 terminal.
Sources: mt5linux/client.py:34-45, mt5linux/async_client.py:80-112, mt5linux/mt5.proto:186-191
Core Data Structures
Symbol data is encapsulated in structured models that represent the exhaustive set of properties defined by the MT5 API.
SymbolInfo Model
The SymbolInfo model is a comprehensive Pydantic class containing 96 fields that mirror the real MT5 SymbolInfo namedtuple. It covers identification, currency, selection state, timing, digits, trading modes, and current prices.
| Category | Key Fields | Type | Description |
|---|---|---|---|
| Identification | name | str | Financial instrument name (e.g., "EURUSD"). |
| Price | bid, ask, last | float | Current bid, ask, and last prices. |
| Execution | trade_exemode | int | Trade execution mode (Request, Instant, Market, Exchange). |
| Volume | volume_min, volume_max, volume_step | float | Broker-defined volume limits and increments. |
| Contract | trade_contract_size | float | Trade contract size. |
| Calculation | trade_calc_mode | int | Profit calculation mode (Forex, CFD, Futures, etc.). |
Sources: mt5linux/models.py:207-320, tests/unit/test_model_schema_validation.py:61-69
Tick Model
The Tick model represents real-time price updates for a specific symbol.
| Field | Type | Description |
|---|---|---|
time | int | Time of the last price update in seconds. |
bid | float | Current bid price. |
ask | float | Current ask price. |
flags | int | Tick flags (e.g., BID, ASK, LAST, VOLUME). |
time_msc | int | Time of the last price update in milliseconds. |
Sources: mt5linux/models.py:348-358, mt5linux/constants.py:465-472
API Endpoints and Logic
The following methods are implemented in both AsyncMetaTrader5 and the synchronous MetaTrader5 wrapper.
Retrieval Methods
The sequence diagram illustrates the chunked retrieval logic for multiple symbols.
Sources: mt5linux/async_client.py:702-720, mt5linux/mt5.proto:217
symbols_total(): Returns the total number of available financial instruments.- Source: mt5linux/async_client.py:686-694
symbols_get(group): Retrieves a list of symbols, optionally filtered by a group pattern (e.g., "*, !USD"). It uses chunked JSON data transfer to handle large symbol sets efficiently.- Source: mt5linux/async_client.py:702-720
symbol_info(symbol): Retrieves all 96 properties of a specific instrument.- Source: mt5linux/async_client.py:722-734
symbol_info_tick(symbol): Retrieves the most recent price tick for a symbol.- Source: mt5linux/async_client.py:736-748
Management Methods
symbol_select(symbol, enable): Adds or removes a symbol from the MT5 Market Watch window. This is often required before retrieving ticks or rates for certain brokers.- Source: mt5linux/async_client.py:750-762
Constants and Enums
The MT5Constants class organizes domain-specific magic numbers and flags used for symbol properties.
Trading and Calculation Modes
The CalcMode determines how the terminal calculates profit and margin for a symbol.
| Constant | Value | Description |
|---|---|---|
c.Symbol.CalcMode.FOREX | 0 | Standard Forex profit calculation. |
c.Symbol.CalcMode.FUTURES | 1 | Futures profit calculation. |
c.Symbol.CalcMode.CFD | 2 | CFD profit calculation. |
Sources: mt5linux/constants.py:530-545
Expiration and Filling Modes
These flags define the order types and execution rules supported by an instrument.
- Expiration Mode: GTC (1), DAY (2), SPECIFIED (4).
- Filling Mode: FOK (1), IOC (2).
- Order Mode: MARKET (1), LIMIT (2), STOP (4), SL (16), TP (32).
Sources: mt5linux/constants.py:588-608
Implementation Example
The following code demonstrates how to retrieve symbol information using the synchronous client.
from mt5linux import MetaTrader5
from mt5linux.constants import MT5Constants as c
with MetaTrader5() as mt5:
# Select symbol in Market Watch
mt5.symbol_select("EURUSD", enable=True)
# Get detailed info
info = mt5.symbol_info("EURUSD")
if info:
print(f"Spread: {info.spread}")
print(f"Contract Size: {info.trade_contract_size}")
# Get current price
tick = mt5.symbol_info_tick("EURUSD")
if tick:
print(f"Bid: {tick.bid}, Ask: {tick.ask}")
Sources: mt5linux/client.py:270-305, tests/integration/test_parametrized.py:90-110
Conclusion
Symbol Information is a core module that provides the necessary metadata for all other trading and market data operations. By strictly mirroring the MT5 API while providing Pydantic-based validation and gRPC-optimized retrieval (such as chunked symbol lists), the module ensures that Linux-based trading applications have access to reliable and accurate instrument specifications.