mt5cli 0.2.0__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. mt5cli-0.4.0/.agents/skills/local-qa/SKILL.md +22 -0
  2. mt5cli-0.4.0/.agents/skills/local-qa/scripts/qa.sh +26 -0
  3. mt5cli-0.4.0/.agents/skills/mt5cli/SKILL.md +113 -0
  4. {mt5cli-0.2.0 → mt5cli-0.4.0}/.github/workflows/ci.yml +1 -2
  5. {mt5cli-0.2.0 → mt5cli-0.4.0}/AGENTS.md +3 -1
  6. {mt5cli-0.2.0 → mt5cli-0.4.0}/PKG-INFO +37 -22
  7. {mt5cli-0.2.0 → mt5cli-0.4.0}/README.md +36 -21
  8. {mt5cli-0.2.0 → mt5cli-0.4.0}/docs/api/index.md +39 -6
  9. mt5cli-0.4.0/docs/api/sdk.md +3 -0
  10. mt5cli-0.4.0/docs/api/utils.md +3 -0
  11. {mt5cli-0.2.0 → mt5cli-0.4.0}/docs/index.md +77 -8
  12. {mt5cli-0.2.0 → mt5cli-0.4.0}/mkdocs.yml +2 -0
  13. mt5cli-0.4.0/mt5cli/__init__.py +56 -0
  14. mt5cli-0.4.0/mt5cli/cli.py +612 -0
  15. mt5cli-0.4.0/mt5cli/sdk.py +1023 -0
  16. mt5cli-0.4.0/mt5cli/utils.py +408 -0
  17. {mt5cli-0.2.0 → mt5cli-0.4.0}/pyproject.toml +1 -5
  18. {mt5cli-0.2.0 → mt5cli-0.4.0}/tests/test_cli.py +615 -310
  19. mt5cli-0.4.0/tests/test_sdk.py +466 -0
  20. mt5cli-0.4.0/tests/test_utils.py +335 -0
  21. {mt5cli-0.2.0 → mt5cli-0.4.0}/uv.lock +10 -10
  22. mt5cli-0.2.0/.agents/skills/local-qa/SKILL.md +0 -25
  23. mt5cli-0.2.0/.agents/skills/local-qa/scripts/qa.sh +0 -19
  24. mt5cli-0.2.0/.agents/skills/mt5cli/SKILL.md +0 -104
  25. mt5cli-0.2.0/mt5cli/__init__.py +0 -12
  26. mt5cli-0.2.0/mt5cli/cli.py +0 -899
  27. {mt5cli-0.2.0 → mt5cli-0.4.0}/.claude/agents/codex.md +0 -0
  28. {mt5cli-0.2.0 → mt5cli-0.4.0}/.claude/settings.json +0 -0
  29. {mt5cli-0.2.0 → mt5cli-0.4.0}/.github/FUNDING.yml +0 -0
  30. {mt5cli-0.2.0 → mt5cli-0.4.0}/.github/dependabot.yml +0 -0
  31. {mt5cli-0.2.0 → mt5cli-0.4.0}/.github/renovate.json +0 -0
  32. {mt5cli-0.2.0 → mt5cli-0.4.0}/.github/workflows/claude.yml +0 -0
  33. {mt5cli-0.2.0 → mt5cli-0.4.0}/.github/workflows/release.yml +0 -0
  34. {mt5cli-0.2.0 → mt5cli-0.4.0}/.gitignore +0 -0
  35. {mt5cli-0.2.0 → mt5cli-0.4.0}/CLAUDE.md +0 -0
  36. {mt5cli-0.2.0 → mt5cli-0.4.0}/LICENSE +0 -0
  37. {mt5cli-0.2.0 → mt5cli-0.4.0}/docs/api/cli.md +0 -0
  38. {mt5cli-0.2.0 → mt5cli-0.4.0}/mt5cli/__main__.py +0 -0
  39. {mt5cli-0.2.0 → mt5cli-0.4.0}/tests/__init__.py +0 -0
@@ -0,0 +1,22 @@
1
+ ---
2
+ name: local-qa
3
+ description: Run local QA including formatting, linting, and testing for the repository. Use whenever any file has been updated.
4
+ disable-model-invocation: false
5
+ ---
6
+
7
+ # Local QA (format, lint, and test)
8
+
9
+ Run the local QA script `scripts/qa.sh` in this skill.
10
+
11
+ ## Procedure
12
+
13
+ - Execute the script exactly as shown above when this skill is triggered.
14
+ - Capture and summarize key output (success/failure, major warnings, and any files modified).
15
+ - If the script fails due to missing tooling (`command not found`, missing executable, or equivalent), install the missing tool(s) and rerun `./scripts/qa.sh`.
16
+ - Install tools using this order of preference:
17
+ 1. Use the project's package manager when applicable (`uv`/`poetry` for Python, package manager scripts/dependencies for Node.js).
18
+ 2. Use a system package manager (`brew` on macOS, `apt` on Debian/Ubuntu) when project-local install is not applicable.
19
+ 3. Use language-specific installers as fallback (`pipx`/`pip`, `npm`, `go install`, etc.).
20
+ - If multiple tools are missing, repeat install -> rerun until QA completes or you hit a blocker.
21
+ - If installation fails or requires unavailable privileges, report what was attempted, the exact failure, and stop.
22
+ - Do not run unrelated commands; only run commands needed for QA and missing-tool installation.
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -euox pipefail
4
+ cd "$(git rev-parse --show-toplevel)"
5
+
6
+ # Python
7
+ uv run ruff format .
8
+ uv run ruff check --fix .
9
+ uv run pyright .
10
+ uv run pytest
11
+
12
+ # Markdown
13
+ npx -y prettier --write './**/*.md'
14
+
15
+ # GitHub Actions
16
+ case "${OSTYPE}" in
17
+ darwin* | linux* )
18
+ zizmor --fix=safe .github/workflows
19
+ git ls-files -z -- '.github/workflows/*.yml' | xargs -0 -t actionlint
20
+ git ls-files -z -- '.github/workflows/*.yml' | xargs -0 -t yamllint -d '{"extends": "relaxed", "rules": {"line-length": "disable"}}'
21
+ checkov --framework=all --output=github_failed_only --directory=.
22
+ ;;
23
+ * )
24
+ echo "GitHub Actions linting is only supported on Linux and macOS."
25
+ ;;
26
+ esac
@@ -0,0 +1,113 @@
1
+ ---
2
+ name: mt5cli
3
+ description: Use the `mt5cli` CLI to export MetaTrader 5 data (rates, ticks, account, symbols, orders, positions, history) to CSV, JSON, Parquet, or SQLite3. Invoke when the user asks to export, dump, download, or fetch MT5 market data or account data to a file.
4
+ ---
5
+
6
+ # mt5cli
7
+
8
+ Export MetaTrader 5 data to CSV, JSON, Parquet, or SQLite3 via the `mt5cli`
9
+ command. Output format is auto-detected from the file extension (`.csv`,
10
+ `.json`, `.parquet`/`.pq`, `.db`/`.sqlite`/`.sqlite3`) or overridden with
11
+ `--format/-f`.
12
+
13
+ ## Requirements
14
+
15
+ - Python 3.11+ on Windows with MetaTrader 5 installed (pdmt5 requires the
16
+ MT5 terminal).
17
+ - Install: `pip install -U mt5cli MetaTrader5`.
18
+ - In this repo, run via `uv run mt5cli ...` or `uv run python -m mt5cli ...`.
19
+
20
+ ## Invocation shape
21
+
22
+ ```
23
+ mt5cli [GLOBAL OPTIONS] -o OUTPUT COMMAND [COMMAND OPTIONS]
24
+ ```
25
+
26
+ Global options MUST precede the subcommand.
27
+
28
+ ### Global options (apply to every subcommand)
29
+
30
+ | Option | Purpose |
31
+ | --------------------- | ------------------------------------------------------------- |
32
+ | `-o, --output PATH` | Output file path (required). |
33
+ | `-f, --format FORMAT` | `csv`, `json`, `parquet`, or `sqlite3` (auto from extension). |
34
+ | `--table NAME` | Table name for SQLite3 output (default: `data`). |
35
+ | `--login INT` | MT5 trading account login. |
36
+ | `--password TEXT` | MT5 trading account password. |
37
+ | `--server TEXT` | MT5 trading server name. |
38
+ | `--path TEXT` | Path to MetaTrader 5 terminal EXE. |
39
+ | `--timeout INT` | Connection timeout in milliseconds. |
40
+ | `--log-level LEVEL` | `DEBUG`, `INFO`, `WARNING` (default), `ERROR`. |
41
+
42
+ ### Parameter value formats
43
+
44
+ - **Datetimes** (`--date-from`, `--date-to`): ISO 8601 (`2024-01-01` or
45
+ `2024-01-01T12:00:00+00:00`). Naive values are treated as UTC.
46
+ - **Timeframe** (`--timeframe`): `M1`, `M2`, `M3`, `M4`, `M5`, `M6`, `M10`,
47
+ `M12`, `M15`, `M20`, `M30`, `H1`, `H2`, `H3`, `H4`, `H6`, `H8`, `H12`,
48
+ `D1`, `W1`, `MN1`, or the raw integer.
49
+ - **Tick flags** (`--flags`): `ALL`, `INFO`, `TRADE`, or the raw integer.
50
+
51
+ ## Commands
52
+
53
+ | Command | Required options | Optional options |
54
+ | ----------------- | ----------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
55
+ | `rates-from` | `--symbol`, `--timeframe`, `--date-from`, `--count` | — |
56
+ | `rates-from-pos` | `--symbol`, `--timeframe`, `--start-pos`, `--count` | — |
57
+ | `rates-range` | `--symbol`, `--timeframe`, `--date-from`, `--date-to` | — |
58
+ | `ticks-from` | `--symbol`, `--date-from`, `--count`, `--flags` | — |
59
+ | `ticks-range` | `--symbol`, `--date-from`, `--date-to`, `--flags` | — |
60
+ | `account-info` | — | — |
61
+ | `terminal-info` | — | — |
62
+ | `symbols` | — | `--group` (e.g., `*USD*`) |
63
+ | `symbol-info` | `--symbol` | — |
64
+ | `orders` | — | `--symbol`, `--group`, `--ticket` |
65
+ | `positions` | — | `--symbol`, `--group`, `--ticket` |
66
+ | `history-orders` | — | `--date-from`, `--date-to`, `--group`, `--symbol`, `--ticket`, `--position` |
67
+ | `history-deals` | — | `--date-from`, `--date-to`, `--group`, `--symbol`, `--ticket`, `--position` |
68
+ | `collect-history` | `--symbol` (repeatable), `--date-from`, `--date-to` | `--dataset` (repeatable; rates/ticks/history-orders/history-deals; default all), `--timeframe` (M1; recorded on rates), `--flags` (ALL), `--if-exists` (append/replace/fail; default fail), `--with-views` (SQLite3 output only) |
69
+
70
+ ## Examples
71
+
72
+ ```bash
73
+ # Account snapshot as CSV.
74
+ mt5cli -o account.csv account-info
75
+
76
+ # EURUSD M1 bars (1000 rows) from a start date to Parquet.
77
+ mt5cli -o rates.parquet rates-from \
78
+ --symbol EURUSD --timeframe M1 --date-from 2024-01-01 --count 1000
79
+
80
+ # EURUSD tick stream for a date range to JSON.
81
+ mt5cli -o ticks.json ticks-range \
82
+ --symbol EURUSD --date-from 2024-01-01 --date-to 2024-01-02 --flags ALL
83
+
84
+ # USD symbols into a named table in SQLite3.
85
+ mt5cli -o data.db --table symbols symbols --group "*USD*"
86
+
87
+ # Historical deals filtered by symbol (using an already-logged-in MT5 terminal).
88
+ mt5cli -o deals.csv history-deals --symbol EURUSD --date-from 2024-01-01
89
+
90
+ # Bundle selected historical datasets into one SQLite db, appending to any
91
+ # existing tables, plus cash_events and positions_reconstructed views.
92
+ mt5cli -o history.db collect-history \
93
+ --symbol EURUSD --symbol GBPUSD \
94
+ --date-from 2024-01-01 --date-to 2024-02-01 \
95
+ --dataset rates --dataset history-deals \
96
+ --timeframe M1 --flags ALL --if-exists append --with-views
97
+ ```
98
+
99
+ ## Guidelines
100
+
101
+ - Pick the output extension to avoid passing `--format`.
102
+ - Use `--table` only with SQLite3 outputs; it is otherwise ignored.
103
+ - `--count` is required for `rates-from`, `rates-from-pos`, and `ticks-from`.
104
+ Prefer `rates-range` / `ticks-range` when a fixed window is known.
105
+ - Credentials (`--login`, `--password`, `--server`) are optional when the
106
+ local MT5 terminal is already logged in.
107
+ - Avoid passing `--password` on the command line in shared or logged
108
+ environments — it is visible in `ps`, shell history, and CI logs. Prefer
109
+ logging in through the MT5 terminal first, then omit credentials here.
110
+ - Reach for `--log-level DEBUG` when a command fails silently — MT5
111
+ connection errors surface there.
112
+ - If the user asks to run from source in this repo, prefix with `uv run`
113
+ (e.g., `uv run mt5cli -o out.csv account-info`).
@@ -35,7 +35,7 @@ jobs:
35
35
  || (github.event_name == 'workflow_dispatch' && inputs.workflow == 'lint-and-test')
36
36
  permissions:
37
37
  contents: read
38
- uses: dceoy/gh-actions-for-devops/.github/workflows/python-package-lint-and-scan.yml@main # zizmor: ignore[unpinned-uses]
38
+ uses: dceoy/gh-actions-for-devops/.github/workflows/python-package-lint-and-scan.yml@main # zizmor: ignore[unpinned-uses]
39
39
  with:
40
40
  package-path: .
41
41
  runs-on: windows-latest
@@ -59,7 +59,6 @@ jobs:
59
59
  uses: dceoy/gh-actions-for-devops/.github/workflows/python-package-mkdocs-gh-deploy.yml@main # zizmor: ignore[unpinned-uses]
60
60
  with:
61
61
  package-path: .
62
- mkdocs-theme: material
63
62
  runs-on: ubuntu-slim
64
63
  secrets:
65
64
  GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -29,9 +29,11 @@ uv sync
29
29
  - `mt5cli/`: Main package directory
30
30
  - `__init__.py`: Package initialization and exports (`detect_format`, `export_dataframe`)
31
31
  - `cli.py`: CLI application with typer-based commands for data export
32
+ - `utils.py`: Constants, enums, parameter types, parsers, and export utilities
32
33
  - `__main__.py`: Entry point for `python -m mt5cli`
33
34
  - `tests/`: Comprehensive test suite (pytest-based)
34
- - `test_cli.py`: Tests for CLI commands, parameter types, and export functions
35
+ - `test_cli.py`: Tests for CLI commands and collect-history behavior
36
+ - `test_utils.py`: Tests for utility constants, parameter types, parsers, and export functions
35
37
  - `docs/`: MkDocs documentation with API reference
36
38
  - `docs/index.md`: Main documentation
37
39
  - `docs/api/`: Auto-generated API documentation for all modules
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mt5cli
3
- Version: 0.2.0
3
+ Version: 0.4.0
4
4
  Summary: Command-line tool for MetaTrader 5
5
5
  Project-URL: Repository, https://github.com/dceoy/mt5cli.git
6
6
  Author-email: dceoy <dceoy@users.noreply.github.com>
@@ -74,30 +74,45 @@ python -m mt5cli -o account.csv account-info
74
74
 
75
75
  ## Commands
76
76
 
77
- | Command | Description |
78
- | ------------------ | ------------------------------------------- |
79
- | `rates-from` | Export rates from a start date |
80
- | `rates-from-pos` | Export rates from a start position |
81
- | `rates-range` | Export rates for a date range |
82
- | `ticks-from` | Export ticks from a start date |
83
- | `ticks-range` | Export ticks for a date range |
84
- | `account-info` | Export account information |
85
- | `terminal-info` | Export terminal information |
86
- | `version` | Export MetaTrader 5 version information |
87
- | `last-error` | Export the last error information |
88
- | `symbols` | Export symbol list |
89
- | `symbol-info` | Export symbol details |
90
- | `symbol-info-tick` | Export the last tick for a symbol |
91
- | `market-book` | Export market depth (order book) |
92
- | `orders` | Export active orders |
93
- | `positions` | Export open positions |
94
- | `history-orders` | Export historical orders |
95
- | `history-deals` | Export historical deals |
96
- | `order-check` | Check funds sufficiency for a trade request |
97
- | `order-send` | Send a trade request to the trade server (`--yes` required) |
77
+ | Command | Description |
78
+ | ------------------ | ------------------------------------------------------------------------------------------------------------ |
79
+ | `rates-from` | Export rates from a start date |
80
+ | `rates-from-pos` | Export rates from a start position |
81
+ | `rates-range` | Export rates for a date range |
82
+ | `ticks-from` | Export ticks from a start date |
83
+ | `ticks-range` | Export ticks for a date range |
84
+ | `account-info` | Export account information |
85
+ | `terminal-info` | Export terminal information |
86
+ | `version` | Export MetaTrader 5 version information |
87
+ | `last-error` | Export the last error information |
88
+ | `symbols` | Export symbol list |
89
+ | `symbol-info` | Export symbol details |
90
+ | `symbol-info-tick` | Export the last tick for a symbol |
91
+ | `market-book` | Export market depth (order book) |
92
+ | `orders` | Export active orders |
93
+ | `positions` | Export open positions |
94
+ | `history-orders` | Export historical orders |
95
+ | `history-deals` | Export historical deals |
96
+ | `order-check` | Check funds sufficiency for a trade request |
97
+ | `order-send` | Send a trade request to the trade server (`--yes` required) |
98
+ | `collect-history` | Bundle rates, ticks, history-orders, and history-deals for one or more symbols into a single SQLite database |
98
99
 
99
100
  Use `order-check` to validate a request payload before running `order-send --yes`.
100
101
 
102
+ ### `collect-history`
103
+
104
+ Collect several historical datasets per symbol into one SQLite database in a single MT5 session. Pick datasets with repeatable `--dataset` (default: all four), choose conflict behavior with `--if-exists append|replace|fail` (default: `fail`), and optionally derive `cash_events` / `positions_reconstructed` views from `history_deals` via `--with-views`.
105
+
106
+ ```bash
107
+ mt5cli -o history.db collect-history \
108
+ --symbol EURUSD --symbol GBPUSD \
109
+ --date-from 2024-01-01 --date-to 2024-02-01 \
110
+ --dataset rates --dataset history-deals \
111
+ --timeframe M1 --flags ALL --if-exists append --with-views
112
+ ```
113
+
114
+ History orders and deals are fetched per symbol and concatenated, so the symbol filter is applied consistently across all datasets. The `cash_events` view is derived from symbol-filtered `history_deals`, so account-level cash events with empty or non-matching symbols may be excluded. The `rates` table records the requested `timeframe` so appended runs at different timeframes remain distinguishable. The `positions_reconstructed` view aggregates trade deals by `position_id`, excludes positions without closing deals, and uses volume-weighted open/close prices; reversal deals (`DEAL_ENTRY_INOUT`) are reported via `volume_reversal` / `reversal_count` columns and do not contribute to the weighted prices.
115
+
101
116
  ## Requirements
102
117
 
103
118
  - Python 3.11+
@@ -50,30 +50,45 @@ python -m mt5cli -o account.csv account-info
50
50
 
51
51
  ## Commands
52
52
 
53
- | Command | Description |
54
- | ------------------ | ------------------------------------------- |
55
- | `rates-from` | Export rates from a start date |
56
- | `rates-from-pos` | Export rates from a start position |
57
- | `rates-range` | Export rates for a date range |
58
- | `ticks-from` | Export ticks from a start date |
59
- | `ticks-range` | Export ticks for a date range |
60
- | `account-info` | Export account information |
61
- | `terminal-info` | Export terminal information |
62
- | `version` | Export MetaTrader 5 version information |
63
- | `last-error` | Export the last error information |
64
- | `symbols` | Export symbol list |
65
- | `symbol-info` | Export symbol details |
66
- | `symbol-info-tick` | Export the last tick for a symbol |
67
- | `market-book` | Export market depth (order book) |
68
- | `orders` | Export active orders |
69
- | `positions` | Export open positions |
70
- | `history-orders` | Export historical orders |
71
- | `history-deals` | Export historical deals |
72
- | `order-check` | Check funds sufficiency for a trade request |
73
- | `order-send` | Send a trade request to the trade server (`--yes` required) |
53
+ | Command | Description |
54
+ | ------------------ | ------------------------------------------------------------------------------------------------------------ |
55
+ | `rates-from` | Export rates from a start date |
56
+ | `rates-from-pos` | Export rates from a start position |
57
+ | `rates-range` | Export rates for a date range |
58
+ | `ticks-from` | Export ticks from a start date |
59
+ | `ticks-range` | Export ticks for a date range |
60
+ | `account-info` | Export account information |
61
+ | `terminal-info` | Export terminal information |
62
+ | `version` | Export MetaTrader 5 version information |
63
+ | `last-error` | Export the last error information |
64
+ | `symbols` | Export symbol list |
65
+ | `symbol-info` | Export symbol details |
66
+ | `symbol-info-tick` | Export the last tick for a symbol |
67
+ | `market-book` | Export market depth (order book) |
68
+ | `orders` | Export active orders |
69
+ | `positions` | Export open positions |
70
+ | `history-orders` | Export historical orders |
71
+ | `history-deals` | Export historical deals |
72
+ | `order-check` | Check funds sufficiency for a trade request |
73
+ | `order-send` | Send a trade request to the trade server (`--yes` required) |
74
+ | `collect-history` | Bundle rates, ticks, history-orders, and history-deals for one or more symbols into a single SQLite database |
74
75
 
75
76
  Use `order-check` to validate a request payload before running `order-send --yes`.
76
77
 
78
+ ### `collect-history`
79
+
80
+ Collect several historical datasets per symbol into one SQLite database in a single MT5 session. Pick datasets with repeatable `--dataset` (default: all four), choose conflict behavior with `--if-exists append|replace|fail` (default: `fail`), and optionally derive `cash_events` / `positions_reconstructed` views from `history_deals` via `--with-views`.
81
+
82
+ ```bash
83
+ mt5cli -o history.db collect-history \
84
+ --symbol EURUSD --symbol GBPUSD \
85
+ --date-from 2024-01-01 --date-to 2024-02-01 \
86
+ --dataset rates --dataset history-deals \
87
+ --timeframe M1 --flags ALL --if-exists append --with-views
88
+ ```
89
+
90
+ History orders and deals are fetched per symbol and concatenated, so the symbol filter is applied consistently across all datasets. The `cash_events` view is derived from symbol-filtered `history_deals`, so account-level cash events with empty or non-matching symbols may be excluded. The `rates` table records the requested `timeframe` so appended runs at different timeframes remain distinguishable. The `positions_reconstructed` view aggregates trade deals by `position_id`, excludes positions without closing deals, and uses volume-weighted open/close prices; reversal deals (`DEAL_ENTRY_INOUT`) are reported via `volume_reversal` / `reversal_count` columns and do not contribute to the weighted prices.
91
+
77
92
  ## Requirements
78
93
 
79
94
  - Python 3.11+
@@ -10,12 +10,22 @@ The mt5cli package consists of the following modules:
10
10
 
11
11
  Command-line interface module providing typer-based commands for exporting MetaTrader 5 data to CSV, JSON, Parquet, and SQLite3 formats.
12
12
 
13
+ ### [Utils](utils.md)
14
+
15
+ Utility module providing constants, enums, Click parameter types, and helper functions for parsing and exporting data.
16
+
17
+ ### [SDK](sdk.md)
18
+
19
+ Programmatic SDK for read-only MetaTrader 5 data collection. Returns pandas DataFrames and provides `collect_history` for SQLite bulk collection.
20
+
13
21
  ## Architecture Overview
14
22
 
15
23
  The package follows a simple architecture built on top of pdmt5:
16
24
 
17
- 1. **CLI Layer** (`cli.py`): Typer application with subcommands for each data type, custom Click parameter types for datetime/timeframe/tick flags parsing, and format detection/export utilities.
18
- 2. **Data Layer** (via `pdmt5`): Uses `Mt5DataClient` and `Mt5Config` from the pdmt5 package for all MetaTrader 5 data access.
25
+ 1. **CLI Layer** (`cli.py`): Typer application with subcommands that delegate to the SDK and export results.
26
+ 2. **SDK Layer** (`sdk.py`): Read-only data access functions, `Mt5CliClient`, and `collect_history` orchestration.
27
+ 3. **Utils Layer** (`utils.py`): Constants, enums, custom Click parameter types, parsing helpers, and format detection/export utilities.
28
+ 4. **Data Layer** (via `pdmt5`): Uses `Mt5DataClient` and `Mt5Config` from the pdmt5 package for all MetaTrader 5 data access.
19
29
 
20
30
  ## Usage Guidelines
21
31
 
@@ -47,15 +57,38 @@ mt5cli -o data.db --table symbols symbols --group "*USD*"
47
57
  ## Python API
48
58
 
49
59
  ```python
50
- from mt5cli import detect_format, export_dataframe
51
- import pandas as pd
60
+ from datetime import UTC, datetime
61
+ from pathlib import Path
62
+
63
+ from mt5cli import (
64
+ Mt5CliClient,
65
+ collect_history,
66
+ copy_rates_range,
67
+ detect_format,
68
+ export_dataframe,
69
+ )
70
+
71
+ # Fetch rates programmatically
72
+ rates = copy_rates_range(
73
+ "EURUSD",
74
+ timeframe="H1",
75
+ date_from="2024-01-01",
76
+ date_to="2024-02-01",
77
+ )
52
78
 
53
79
  # Detect output format from file extension
54
80
  fmt = detect_format(Path("output.parquet")) # Returns "parquet"
55
81
 
56
82
  # Export a DataFrame
57
- df = pd.DataFrame({"symbol": ["EURUSD"], "bid": [1.1234]})
58
- export_dataframe(df, Path("output.csv"), "csv")
83
+ export_dataframe(rates, Path("output.csv"), "csv")
84
+
85
+ # Collect history into SQLite
86
+ collect_history(
87
+ Path("history.db"),
88
+ symbols=["EURUSD"],
89
+ date_from=datetime(2024, 1, 1, tzinfo=UTC),
90
+ date_to=datetime(2024, 2, 1, tzinfo=UTC),
91
+ )
59
92
  ```
60
93
 
61
94
  ## Examples
@@ -0,0 +1,3 @@
1
+ # SDK Module
2
+
3
+ ::: mt5cli.sdk
@@ -0,0 +1,3 @@
1
+ # Utils Module
2
+
3
+ ::: mt5cli.utils
@@ -20,6 +20,44 @@ mt5cli is a CLI application that exports MetaTrader 5 trading data to multiple f
20
20
  pip install mt5cli
21
21
  ```
22
22
 
23
+ ## Programmatic usage / SDK usage
24
+
25
+ mt5cli can be used as a small Python SDK for read-only MetaTrader 5 data collection. SDK functions return pandas DataFrames without writing files. Use `export_dataframe` when you need to persist results.
26
+
27
+ ```python
28
+ from datetime import UTC, datetime
29
+ from pathlib import Path
30
+
31
+ from mt5cli import Mt5CliClient, collect_history, copy_rates_range, export_dataframe
32
+
33
+ # One-off fetch with module-level helpers
34
+ rates = copy_rates_range(
35
+ "EURUSD",
36
+ timeframe="H1",
37
+ date_from="2024-01-01",
38
+ date_to="2024-02-01",
39
+ )
40
+ export_dataframe(rates, Path("rates.csv"), "csv")
41
+
42
+ # Reuse one MT5 connection for multiple calls
43
+ with Mt5CliClient(login=12345, password="secret", server="Broker-Demo") as client:
44
+ account = client.account_info()
45
+ positions = client.positions()
46
+
47
+ # Bulk SQLite collection (same behavior as the collect-history CLI command)
48
+ collect_history(
49
+ Path("history.db"),
50
+ symbols=["EURUSD", "GBPUSD"],
51
+ date_from=datetime(2024, 1, 1, tzinfo=UTC),
52
+ date_to=datetime(2024, 2, 1, tzinfo=UTC),
53
+ timeframe="M1",
54
+ flags="ALL",
55
+ with_views=True,
56
+ )
57
+ ```
58
+
59
+ Timeframes, tick flags, and ISO 8601 date strings are accepted wherever noted in the SDK API.
60
+
23
61
  ## Quick Start
24
62
 
25
63
  ```bash
@@ -74,17 +112,46 @@ mt5cli --login 12345 --password mypass --server MyBroker-Demo \
74
112
 
75
113
  ### Trading
76
114
 
77
- | Command | Description |
78
- | ---------------- | ------------------------------------------- |
79
- | `orders` | Export active orders |
80
- | `positions` | Export open positions |
81
- | `history-orders` | Export historical orders |
82
- | `history-deals` | Export historical deals |
83
- | `order-check` | Check funds sufficiency for a trade request |
115
+ | Command | Description |
116
+ | ---------------- | ----------------------------------------------------------- |
117
+ | `orders` | Export active orders |
118
+ | `positions` | Export open positions |
119
+ | `history-orders` | Export historical orders |
120
+ | `history-deals` | Export historical deals |
121
+ | `order-check` | Check funds sufficiency for a trade request |
84
122
  | `order-send` | Send a trade request to the trade server (`--yes` required) |
85
123
 
86
124
  Use `order-check` to validate a request payload before running `order-send --yes`.
87
125
 
126
+ ### Bulk Collection
127
+
128
+ | Command | Description |
129
+ | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
130
+ | `collect-history` | Collect rates, ticks, history-orders, and history-deals for one or more symbols into a single SQLite database (optional cash-event/position views) |
131
+
132
+ ```bash
133
+ mt5cli -o history.db collect-history \
134
+ --symbol EURUSD --symbol GBPUSD \
135
+ --date-from 2024-01-01 --date-to 2024-02-01 \
136
+ --dataset rates --dataset history-deals \
137
+ --timeframe M1 --flags ALL --if-exists append --with-views
138
+ ```
139
+
140
+ `collect-history` options:
141
+
142
+ | Option | Default | Description |
143
+ | -------------- | ---------- | --------------------------------------------------------------------------------------------- |
144
+ | `--symbol/-s` | _required_ | Symbol to collect (repeat for multiple). |
145
+ | `--date-from` | _required_ | Start date in ISO 8601. |
146
+ | `--date-to` | _required_ | End date in ISO 8601. |
147
+ | `--dataset` | all four | Repeatable: `rates`, `ticks`, `history-orders`, `history-deals`. |
148
+ | `--timeframe` | `M1` | Rates timeframe; recorded in a `timeframe` column on the `rates` table. |
149
+ | `--flags` | `ALL` | Tick copy flags forwarded to `copy_ticks_range`. |
150
+ | `--if-exists` | `fail` | `append`, `replace`, or `fail` when a target table already exists. |
151
+ | `--with-views` | off | Add `cash_events` and `positions_reconstructed` views (requires the `history-deals` dataset). |
152
+
153
+ History orders and deals are fetched per symbol and concatenated, so the symbol filter is applied consistently across all datasets. The `cash_events` view is derived from symbol-filtered `history_deals`, so account-level cash events with empty or non-matching symbols may be excluded. The `positions_reconstructed` view excludes positions with no closing deal, uses volume-weighted open/close prices, and reports reversal deals (`DEAL_ENTRY_INOUT`) via `volume_reversal` / `reversal_count`.
154
+
88
155
  ## Global Options
89
156
 
90
157
  | Option | Description |
@@ -109,7 +176,9 @@ Use `order-check` to validate a request payload before running `order-send --yes
109
176
 
110
177
  Browse the API documentation for detailed module information:
111
178
 
112
- - [CLI Module](api/cli.md) - CLI application with export commands and utility functions
179
+ - [CLI Module](api/cli.md) - CLI application with export commands
180
+ - [SDK Module](api/sdk.md) - Programmatic read-only data collection API
181
+ - [Utils Module](api/utils.md) - Constants, parameter types, parsers, and export utilities
113
182
 
114
183
  ## Development
115
184
 
@@ -56,6 +56,8 @@ nav:
56
56
  - API Reference:
57
57
  - Overview: api/index.md
58
58
  - CLI: api/cli.md
59
+ - SDK: api/sdk.md
60
+ - Utils: api/utils.md
59
61
 
60
62
  markdown_extensions:
61
63
  - admonition
@@ -0,0 +1,56 @@
1
+ """mt5cli: Command-line tool and SDK for MetaTrader 5."""
2
+
3
+ from importlib.metadata import version
4
+
5
+ from .sdk import (
6
+ Mt5CliClient,
7
+ account_info,
8
+ build_config,
9
+ collect_history,
10
+ copy_rates_from,
11
+ copy_rates_from_pos,
12
+ copy_rates_range,
13
+ copy_ticks_from,
14
+ copy_ticks_range,
15
+ history_deals,
16
+ history_orders,
17
+ last_error,
18
+ market_book,
19
+ orders,
20
+ positions,
21
+ symbol_info,
22
+ symbol_info_tick,
23
+ symbols,
24
+ terminal_info,
25
+ )
26
+ from .sdk import (
27
+ version as mt5_version,
28
+ )
29
+ from .utils import detect_format, export_dataframe
30
+
31
+ __version__ = version(__package__) if __package__ else None
32
+
33
+ __all__ = [
34
+ "Mt5CliClient",
35
+ "account_info",
36
+ "build_config",
37
+ "collect_history",
38
+ "copy_rates_from",
39
+ "copy_rates_from_pos",
40
+ "copy_rates_range",
41
+ "copy_ticks_from",
42
+ "copy_ticks_range",
43
+ "detect_format",
44
+ "export_dataframe",
45
+ "history_deals",
46
+ "history_orders",
47
+ "last_error",
48
+ "market_book",
49
+ "mt5_version",
50
+ "orders",
51
+ "positions",
52
+ "symbol_info",
53
+ "symbol_info_tick",
54
+ "symbols",
55
+ "terminal_info",
56
+ ]