Implements three observability improvements: #82 — publish_grafana_copy(): Uses SQLite online backup API (WAL-safe) to atomically publish a consistent read-only copy beside the target. Adds --publish-copy option to grafana-schema and snapshot CLI commands. #83 — examples/grafana/: Minimal working Grafana setup with docker-compose, provisioning datasource/dashboard YAML, and three dashboard JSON files (mt5cli-overview, mt5cli-trades, mt5cli-market). All queries use grafana_* views; no credentials or private paths included. #84 — mt5cli/telemetry.py: Optional OTel metrics behind mt5cli[otel] extra. Base install is unaffected. Adds _Mt5Metrics singleton (no-op until configure_metrics() is called), wraps update_history() and update_observability() with record_history_update / record_snapshot_update context managers, and emits account/position gauges from snapshots. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Grafana Integration for mt5cli
This directory contains example configuration and dashboard files for visualising mt5cli SQLite data in Grafana using the Grafana SQLite datasource plugin.
Prerequisites
- mt5cli installed and able to connect to MetaTrader 5
- Grafana 10+ with the
frser-sqlite-datasourceplugin installed - (Optional) Docker and Docker Compose for the containerised setup
Generating the SQLite database
Collect historical data and snapshot current account state:
# Collect OHLCV history
mt5cli -o history.db collect-history --symbol EURUSD --date-from 2024-01-01 --date-to 2024-12-31
# Create Grafana-ready views and indexes
mt5cli -o history.db grafana-schema
# Snapshot current account, positions, and orders
mt5cli -o history.db snapshot --with-grafana-schema
Publishing a Grafana-readable copy
Grafana reads the SQLite file directly. To avoid read/write conflicts, publish a consistent copy after each update:
mt5cli -o history.db grafana-schema --publish-copy history.grafana.db
mt5cli -o history.db snapshot --publish-copy history.grafana.db
The --publish-copy option uses the SQLite online backup API, which is safe
even when the source database uses WAL journal mode.
Configuring the datasource path
Edit provisioning/datasources/mt5cli-sqlite.yaml and set the path field
to the absolute path of your published .db file:
jsonData:
path: /absolute/path/to/history.grafana.db
Running Grafana on Windows (native)
- Download and install Grafana from https://grafana.com/grafana/download/.
- Install the SQLite plugin:
grafana-cli plugins install frser-sqlite-datasource. - Copy
provisioning/into%ProgramFiles%\GrafanaLabs\grafana\conf\provisioning\. - Import the dashboards from
dashboards/via the Grafana UI (Dashboards → Import → Upload JSON file).
Running with Docker Compose
# From the examples/grafana directory
docker compose up -d
Then open http://localhost:3000 (default credentials: admin / admin).
The Compose file mounts this directory and the SQLite file into the container.
Edit docker-compose.yml to point MT5CLI_DB_PATH at your .db file.
Dashboard overview
| Dashboard | Description |
|---|---|
mt5cli-overview.json |
Account balance, equity, margin, and snapshot freshness |
mt5cli-trades.json |
Trade P/L, win rate, symbol breakdown |
mt5cli-market.json |
OHLCV rates, spreads, and tick volume |
All panel queries use the grafana_* views; they do not read internal storage
tables directly.
Importing dashboards
- Open Grafana and navigate to Dashboards → Import.
- Click Upload JSON file and select one of the files in
dashboards/. - Select the
mt5cli-SQLitedatasource when prompted. - Click Import.