Core¶
Result[T], errors, and execution primitives.
Result[T]¶
Result¶
Result
¶
Bases: BaseModel, Generic[T]
Typed outcome of an SDK operation.
A Result is either successful (success=True, data is set)
or failed (success=False, error_code and error_message are
set). The model validator enforces this invariant at construction time
so an invalid state is impossible to construct.
| ATTRIBUTE | DESCRIPTION |
|---|---|
success |
TYPE:
|
data |
The operation's return value. Always
TYPE:
|
error_code |
MT5 error code on failure;
TYPE:
|
error_message |
Human-readable error description;
TYPE:
|
context |
The MT5 API function name that was called (e.g.
TYPE:
|
operation |
Logical operation name, usually the same as
TYPE:
|
ok
classmethod
¶
Construct a successful result.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
The value to carry. May be
TYPE:
|
context
|
MT5 API function name for tracing.
TYPE:
|
operation
|
Logical operation name for tracing.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[T]
|
A |
fail
classmethod
¶
Construct a failed result from an MT5ErrorInfo.
| PARAMETER | DESCRIPTION |
|---|---|
error
|
The error captured from
TYPE:
|
context
|
MT5 API function name where the failure occurred.
TYPE:
|
operation
|
Logical operation name for tracing.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Result[T]
|
A |
Result[T]
|
error fields populated from |
Errors¶
MT5Error¶
MT5Error
¶
Bases: Exception
Base exception for all SDK-level errors.
MT5ConnectionError¶
MT5ConnectionError
¶
Bases: MT5Error
Raised when a connection lifecycle operation fails unrecoverably.
MT5ExecutionError¶
MT5ExecutionError
¶
Bases: MT5Error
Raised when a data retrieval or trade execution fails unrecoverably.
MT5ErrorInfo¶
MT5ErrorInfo
¶
Bases: BaseModel
Structured representation of an MT5 error.
Produced by mt5.last_error() after every MT5 API call and carried
inside Result objects so callers can inspect failure details without
catching exceptions.
| ATTRIBUTE | DESCRIPTION |
|---|---|
code |
MT5 error code (0 means no error; negative codes are SDK-internal).
TYPE:
|
message |
Human-readable description of the error.
TYPE:
|
Usage¶
Every operation returns Result[T]:
res = mt5.positions()
if res.success:
# Success path
for p in res.data:
print(p.symbol)
else:
# Failure path
print(f"Operation: {res.operation}")
print(f"Error code: {res.error_code}")
print(f"Message: {res.error_message}")
Exceptions are reserved for unrecoverable situations (e.g., during object construction). You will not encounter them in normal usage.