Observability Module¶
mt5cli.observability ¶
Observability snapshot orchestration for account, position, order, terminal state.
This module owns when and what to snapshot, using the canonical
:class:~mt5cli.contract.ObservabilityClient contract rather than raw pdmt5
method names, and the SQLite snapshot tables the results are persisted into.
__all__
module-attribute
¶
__all__ = [
"ObservabilitySnapshot",
"capture_observability_snapshot",
"create_snapshot_tables",
"insert_account_snapshot",
"insert_order_snapshots",
"insert_position_snapshots",
"insert_terminal_snapshot",
"persist_observability_snapshot",
"record_snapshot_run",
"start_snapshot_run",
"update_observability",
"update_observability_with_config",
]
ObservabilitySnapshot
dataclass
¶
ObservabilitySnapshot(
observed_at: int,
account: DataFrame | None,
positions: DataFrame | None,
orders: DataFrame | None,
terminal: DataFrame | None,
symbols: tuple[str, ...] | None,
)
MT5-independent captured account/position/order/terminal state.
Produced by :func:capture_observability_snapshot on the MT5 connection
thread; consumed by :func:persist_observability_snapshot, which never
touches MT5 or any connection-scoped MT5 state and may run on a different
thread. A field is None when its include_* flag was False at capture
time, distinct from an empty DataFrame returned by MT5 itself.
capture_observability_snapshot ¶
capture_observability_snapshot(
*,
client: ObservabilityClient,
symbols: Sequence[str] | None = None,
include_account: bool = True,
include_positions: bool = True,
include_orders: bool = True,
include_terminal: bool = True,
) -> ObservabilitySnapshot
Capture current account/position/order/terminal state as plain data.
Reads MT5 state via client and returns it as a serialization-safe,
MT5-independent snapshot. Performs no SQLite access. Never places orders
or modifies trading state. Pass the result to
:func:persist_observability_snapshot -- from any thread, without MT5
access -- to write it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
ObservabilityClient
|
Connected MT5 client implementation. |
required |
symbols
|
Sequence[str] | None
|
Optional symbol filter for positions and orders. When None, all positions and orders are captured. |
None
|
include_account
|
bool
|
Capture account info. |
True
|
include_positions
|
bool
|
Capture open positions. |
True
|
include_orders
|
bool
|
Capture active orders. |
True
|
include_terminal
|
bool
|
Capture terminal info. |
True
|
Returns:
| Type | Description |
|---|---|
ObservabilitySnapshot
|
The captured snapshot. |
Source code in mt5cli/observability.py
create_snapshot_tables ¶
insert_account_snapshot ¶
Append one account state row to account_snapshots.
Source code in mt5cli/observability.py
insert_order_snapshots ¶
insert_order_snapshots(
conn: Connection,
run_id: int,
login: int | None,
rows: list[dict[str, object]],
) -> None
Append order rows to order_snapshots; no-op when rows is empty.
Source code in mt5cli/observability.py
insert_position_snapshots ¶
insert_position_snapshots(
conn: Connection,
run_id: int,
login: int | None,
rows: list[dict[str, object]],
) -> None
Append position rows to position_snapshots; no-op when rows is empty.
Source code in mt5cli/observability.py
insert_terminal_snapshot ¶
Append one terminal state row to terminal_snapshots.
Source code in mt5cli/observability.py
persist_observability_snapshot ¶
persist_observability_snapshot(
snapshot: ObservabilitySnapshot, *, output: Path | str
) -> None
Persist a :func:capture_observability_snapshot result into SQLite.
Opens and owns its own SQLite connection (WAL journal mode,
synchronous=NORMAL). Never accesses MT5 or any connection-scoped MT5
state, so it is safe to call from a dedicated persistence-writer thread
that holds no MT5 client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
snapshot
|
ObservabilitySnapshot
|
A snapshot from :func: |
required |
output
|
Path | str
|
SQLite database path. |
required |
Source code in mt5cli/observability.py
record_snapshot_run ¶
record_snapshot_run(
conn: Connection,
run_id: int,
status: str,
detail: str | None = None,
) -> None
Finalize a snapshot run by setting its status.
Source code in mt5cli/observability.py
start_snapshot_run ¶
Insert a snapshot_runs row with status 'running' and return its run_id.
Returns:
| Type | Description |
|---|---|
int
|
The auto-assigned run_id for the new row. |
Source code in mt5cli/observability.py
update_observability ¶
update_observability(
*,
client: ObservabilityClient,
output: Path | str,
symbols: Sequence[str] | None = None,
include_account: bool = True,
include_positions: bool = True,
include_orders: bool = True,
include_terminal: bool = True,
) -> None
Snapshot current account/position/order/terminal state into SQLite.
Reads the current MT5 state and appends timestamped snapshot rows. Never places orders or modifies trading state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
ObservabilityClient
|
Connected MT5 client implementation. |
required |
output
|
Path | str
|
SQLite database path. |
required |
symbols
|
Sequence[str] | None
|
Optional symbol filter for positions and orders. When None, all positions and orders are snapshotted. |
None
|
include_account
|
bool
|
Snapshot account info into |
True
|
include_positions
|
bool
|
Snapshot open positions into |
True
|
include_orders
|
bool
|
Snapshot active orders into |
True
|
include_terminal
|
bool
|
Snapshot terminal info into |
True
|
Source code in mt5cli/observability.py
update_observability_with_config ¶
update_observability_with_config(
*,
output: Path | str,
config: Mt5Config | None = None,
symbols: Sequence[str] | None = None,
include_account: bool = True,
include_positions: bool = True,
include_orders: bool = True,
include_terminal: bool = True,
) -> None
Snapshot current MT5 state, opening and closing the MT5 connection.
Convenience wrapper around :func:update_observability for standalone use.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
Path | str
|
SQLite database path. |
required |
config
|
Mt5Config | None
|
MT5 connection configuration. Defaults to an empty config that attaches to a running terminal. |
None
|
symbols
|
Sequence[str] | None
|
Optional symbol filter for positions and orders. |
None
|
include_account
|
bool
|
Snapshot account info. |
True
|
include_positions
|
bool
|
Snapshot open positions. |
True
|
include_orders
|
bool
|
Snapshot active orders. |
True
|
include_terminal
|
bool
|
Snapshot terminal info. |
True
|
Source code in mt5cli/observability.py
Snapshot persistence (SQLite)¶
These helpers append timestamped account/position/order/terminal snapshot
rows into a SQLite database. The snapshot tables are created on demand with
idempotent DDL (CREATE TABLE IF NOT EXISTS).
| Symbol | Role |
|---|---|
update_observability |
Append one timestamped snapshot row per data type from a connected client implementation |
update_observability_with_config |
Standalone wrapper: opens/closes MT5 connection automatically around update_observability |
Both functions write to the SQLite path given by output=. The optional
symbols parameter filters positions / orders by symbol.
Pass the MT5Client yielded by mt5_session() directly to
update_observability(client=...). This workflow calls the facade's canonical
data methods (account_info, positions, orders, terminal_info); callers
never need a pdmt5 client, and no raw pdmt5 method-name fallback logic is
involved.
from mt5cli import mt5_session, update_observability
with mt5_session() as client:
update_observability(client=client, output="observability.db")
The snapshot table DDL and row inserts (create_snapshot_tables,
start_snapshot_run, insert_*_snapshot(s), record_snapshot_run) live in
this module alongside the orchestration that decides when and what to
snapshot.