mt5cli 0.7.2__tar.gz → 0.8.1__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 (60) hide show
  1. mt5cli-0.8.1/.agents/skills/pr-feedback-triage/SKILL.md +149 -0
  2. {mt5cli-0.7.2 → mt5cli-0.8.1}/.github/workflows/ci.yml +15 -0
  3. mt5cli-0.8.1/AGENTS.md +37 -0
  4. {mt5cli-0.7.2 → mt5cli-0.8.1}/PKG-INFO +37 -4
  5. {mt5cli-0.7.2 → mt5cli-0.8.1}/README.md +36 -3
  6. {mt5cli-0.7.2 → mt5cli-0.8.1}/docs/api/history.md +29 -3
  7. {mt5cli-0.7.2 → mt5cli-0.8.1}/docs/api/index.md +9 -1
  8. mt5cli-0.8.1/docs/api/public-contract.md +184 -0
  9. {mt5cli-0.7.2 → mt5cli-0.8.1}/docs/api/sdk.md +20 -5
  10. mt5cli-0.8.1/docs/api/trading.md +161 -0
  11. {mt5cli-0.7.2 → mt5cli-0.8.1}/mkdocs.yml +1 -0
  12. {mt5cli-0.7.2 → mt5cli-0.8.1}/mt5cli/__init__.py +61 -1
  13. mt5cli-0.8.1/mt5cli/contract.py +101 -0
  14. {mt5cli-0.7.2 → mt5cli-0.8.1}/mt5cli/history.py +72 -10
  15. {mt5cli-0.7.2 → mt5cli-0.8.1}/mt5cli/sdk.py +29 -14
  16. mt5cli-0.8.1/mt5cli/trading.py +1044 -0
  17. {mt5cli-0.7.2 → mt5cli-0.8.1}/mt5cli/utils.py +14 -0
  18. {mt5cli-0.7.2 → mt5cli-0.8.1}/pyproject.toml +1 -1
  19. {mt5cli-0.7.2 → mt5cli-0.8.1}/tests/test_contracts.py +183 -1
  20. {mt5cli-0.7.2 → mt5cli-0.8.1}/tests/test_history.py +36 -0
  21. {mt5cli-0.7.2 → mt5cli-0.8.1}/tests/test_sdk.py +60 -3
  22. mt5cli-0.8.1/tests/test_trading.py +1930 -0
  23. {mt5cli-0.7.2 → mt5cli-0.8.1}/uv.lock +1 -1
  24. mt5cli-0.7.2/AGENTS.md +0 -81
  25. mt5cli-0.7.2/docs/api/trading.md +0 -70
  26. mt5cli-0.7.2/mt5cli/trading.py +0 -210
  27. mt5cli-0.7.2/tests/test_trading.py +0 -356
  28. {mt5cli-0.7.2 → mt5cli-0.8.1}/.agents/skills/local-qa/SKILL.md +0 -0
  29. {mt5cli-0.7.2 → mt5cli-0.8.1}/.agents/skills/local-qa/scripts/qa.sh +0 -0
  30. {mt5cli-0.7.2 → mt5cli-0.8.1}/.agents/skills/mt5cli/SKILL.md +0 -0
  31. {mt5cli-0.7.2 → mt5cli-0.8.1}/.claude/agents/codex.md +0 -0
  32. {mt5cli-0.7.2 → mt5cli-0.8.1}/.claude/settings.json +0 -0
  33. {mt5cli-0.7.2 → mt5cli-0.8.1}/.github/FUNDING.yml +0 -0
  34. {mt5cli-0.7.2 → mt5cli-0.8.1}/.github/dependabot.yml +0 -0
  35. {mt5cli-0.7.2 → mt5cli-0.8.1}/.github/renovate.json +0 -0
  36. {mt5cli-0.7.2 → mt5cli-0.8.1}/.github/workflows/claude.yml +0 -0
  37. {mt5cli-0.7.2 → mt5cli-0.8.1}/.github/workflows/release.yml +0 -0
  38. {mt5cli-0.7.2 → mt5cli-0.8.1}/.gitignore +0 -0
  39. {mt5cli-0.7.2 → mt5cli-0.8.1}/CLAUDE.md +0 -0
  40. {mt5cli-0.7.2 → mt5cli-0.8.1}/LICENSE +0 -0
  41. {mt5cli-0.7.2 → mt5cli-0.8.1}/docs/api/cli.md +0 -0
  42. {mt5cli-0.7.2 → mt5cli-0.8.1}/docs/api/client.md +0 -0
  43. {mt5cli-0.7.2 → mt5cli-0.8.1}/docs/api/converters.md +0 -0
  44. {mt5cli-0.7.2 → mt5cli-0.8.1}/docs/api/exceptions.md +0 -0
  45. {mt5cli-0.7.2 → mt5cli-0.8.1}/docs/api/schemas.md +0 -0
  46. {mt5cli-0.7.2 → mt5cli-0.8.1}/docs/api/storage.md +0 -0
  47. {mt5cli-0.7.2 → mt5cli-0.8.1}/docs/api/utils.md +0 -0
  48. {mt5cli-0.7.2 → mt5cli-0.8.1}/docs/index.md +0 -0
  49. {mt5cli-0.7.2 → mt5cli-0.8.1}/mt5cli/__main__.py +0 -0
  50. {mt5cli-0.7.2 → mt5cli-0.8.1}/mt5cli/cli.py +0 -0
  51. {mt5cli-0.7.2 → mt5cli-0.8.1}/mt5cli/client.py +0 -0
  52. {mt5cli-0.7.2 → mt5cli-0.8.1}/mt5cli/converters.py +0 -0
  53. {mt5cli-0.7.2 → mt5cli-0.8.1}/mt5cli/exceptions.py +0 -0
  54. {mt5cli-0.7.2 → mt5cli-0.8.1}/mt5cli/retry.py +0 -0
  55. {mt5cli-0.7.2 → mt5cli-0.8.1}/mt5cli/schemas.py +0 -0
  56. {mt5cli-0.7.2 → mt5cli-0.8.1}/mt5cli/storage.py +0 -0
  57. {mt5cli-0.7.2 → mt5cli-0.8.1}/tests/__init__.py +0 -0
  58. {mt5cli-0.7.2 → mt5cli-0.8.1}/tests/conftest.py +0 -0
  59. {mt5cli-0.7.2 → mt5cli-0.8.1}/tests/test_cli.py +0 -0
  60. {mt5cli-0.7.2 → mt5cli-0.8.1}/tests/test_utils.py +0 -0
@@ -0,0 +1,149 @@
1
+ ---
2
+ name: pr-feedback-triage
3
+ description: Triage pull request review comments into fixes, replies, clarification requests, or open follow-ups while respecting safe execution modes.
4
+ ---
5
+
6
+ # PR Feedback Triage
7
+
8
+ Triage pull request review feedback, decide what action each thread needs, make focused fixes when allowed, and report or resolve only what is actually handled.
9
+
10
+ ## When to Use
11
+
12
+ - A PR has review comments, requested changes, unresolved review threads, or bot review findings.
13
+ - The user asks to address, respond to, or resolve PR feedback.
14
+ - The user provides a PR URL/number, a branch with an associated PR, or copied comments.
15
+
16
+ Do not use this skill for a first-pass code review with no existing feedback; use a code review skill instead.
17
+
18
+ ## Inputs
19
+
20
+ - Pull request URL or number, or a current branch that has an associated pull request.
21
+ - Repository checkout or platform access sufficient to inspect the PR diff and review feedback.
22
+ - Optional reviewer priorities from the user, such as "only address blocking comments" or "do not reply on the PR platform".
23
+ - Optional operating mode flags: `dry_run`, `no_push`, and `no_reply`.
24
+
25
+ If no PR or review comments are identifiable, ask for the target PR or the copied comments before proceeding.
26
+
27
+ ## Modes
28
+
29
+ - `dry_run`: inspect review feedback and report the triage only. Do not edit files, run write-mode formatters, commit, push, post replies, or resolve review threads.
30
+ - `no_push`: local edits and verification are allowed, but do not push commits or otherwise update the remote branch. Report the local diff or local commits that still need to be pushed.
31
+ - `no_reply`: do not post replies, submit reviews, or resolve review threads. Provide suggested replies and resolution actions in the final report instead.
32
+
33
+ When a mode disables an action, skip that destructive or externally visible action even if normal workflow text would otherwise allow it.
34
+
35
+ ## Preflight
36
+
37
+ 1. Identify the current branch and target PR.
38
+ 2. Check tracked local changes with `git diff --name-only` and `git diff --cached --name-only`. Ignore untracked files unless the review feedback explicitly concerns them.
39
+ 3. Check unpushed commits before relying on remote review feedback.
40
+ 4. If tracked local changes or unpushed commits exist, warn that existing PR comments may not cover the latest local state. In `normal` mode, push only when the user request or repository workflow allows it; otherwise continue with a clearly reported limitation.
41
+
42
+ ## Feedback Collection
43
+
44
+ Gather the complete feedback set before editing:
45
+
46
+ - Fetch unresolved review threads, requested-change reviews, PR-level summary comments, and copied comments.
47
+ - Use platform-native APIs/CLI when available. Paginate results; do not inspect only the first page of threads or comments.
48
+ - For bot reviewers that post both summary comments and inline comments, collect both. Summary comments often contain severity, rationale, and fix instructions; inline comments contain the exact file and line context.
49
+ - Preserve each thread/comment identifier needed to reply or resolve later.
50
+ - Compare each comment with the current diff and file contents because review lines can become outdated.
51
+
52
+ ## Deduplication and Ordering
53
+
54
+ Build one triage record per distinct finding:
55
+
56
+ - Prefer exact review-thread identity when available.
57
+ - For duplicate bot findings appearing in both summary and inline comments, merge by exact issue title first, then by file path plus line range as a fallback.
58
+ - Prefer inline comments for location and current code context.
59
+ - Prefer summary comments for severity, category, rationale, and detailed agent prompts.
60
+ - Preserve the reviewer’s exact issue title and original wording where practical. Do not rename findings in a way that would make replies hard to map back to comments.
61
+ - Preserve the reviewer’s original ordering unless the user asks for priority reordering. Many review bots already order findings by severity.
62
+
63
+ Each triage record should track: original title, reviewer, source IDs, location, current applicability, severity/priority if available, disposition, planned action, verification, reply text if any, and resolution decision.
64
+
65
+ ## Resolution Policy
66
+
67
+ In normal mode, `Resolve conversation` is the default action for any review thread that has been fully handled. A thread is handled when the requested change is implemented and verified, the current code already satisfies the comment, the comment is outdated and no longer applies, or a deliberate deferral/won't-fix response has been posted with a clear reason.
68
+
69
+ Keep a thread open only when it still needs reviewer, maintainer, or product input, the fix is local-only and not pushed, verification is missing for a material change, or the user explicitly requested `dry_run`, `no_push`, or `no_reply` behavior that prevents resolution.
70
+
71
+ When resolving a thread, add a concise reply first only if it provides useful context, such as what changed, why no code change was needed, why a finding was intentionally deferred, or why the original comment is now outdated. Do not add noisy replies for self-evident fixes unless project norms require them.
72
+
73
+ ## Flow
74
+
75
+ ```mermaid
76
+ flowchart TD
77
+ A[Identify PR and branch state] --> B[Collect all review feedback]
78
+ B --> C[Deduplicate and preserve source IDs]
79
+ C --> D[Inspect current diff and code]
80
+ D --> E{Classify each triage record}
81
+ E -->|Fix| F[Implement minimal change]
82
+ E -->|Answer| G[Prepare concise reply]
83
+ E -->|Clarify| H[Leave open with question]
84
+ E -->|Already addressed or Outdated| I[Prepare evidence]
85
+ E -->|Defer or Won't fix| J[Document reason]
86
+ F --> K[Verify]
87
+ G --> L{Mode}
88
+ H --> L
89
+ I --> L
90
+ J --> L
91
+ K --> L
92
+ L -->|dry_run| M[Report triage only]
93
+ L -->|no_push| N[Report local diff or commits]
94
+ L -->|no_reply| O[Report suggested replies/actions]
95
+ L -->|normal| P[Commit/push if changed, then batch reply and resolve handled threads]
96
+ M --> Q[Final summary]
97
+ N --> Q
98
+ O --> Q
99
+ P --> Q
100
+ ```
101
+
102
+ ## Compact Workflow
103
+
104
+ 1. **Collect all relevant feedback**
105
+ - Identify the PR and gather unresolved review threads, requested-change reviews, PR-level summaries, inline comments, and copied comments.
106
+ - Paginate all platform calls and keep comment/thread IDs for later replies and resolution.
107
+ - For bot reviews, collect both summary and inline comments, then merge duplicates rather than fixing the same finding twice.
108
+
109
+ 2. **Classify each triage record**
110
+ - **Fix**: Valid requested change; make the smallest focused edit when not in `dry_run`.
111
+ - **Answer**: No code change needed; prepare a concise explanation.
112
+ - **Clarify**: Ambiguous, conflicting, or missing context; leave open with a question.
113
+ - **Already addressed**: Current code already satisfies it; prepare evidence.
114
+ - **Outdated**: Commented code or issue no longer exists; prepare evidence.
115
+ - **Defer / Won't fix**: Valid concern intentionally not changed now; document a specific reason.
116
+
117
+ 3. **Act according to the classification and mode**
118
+ - Keep edits scoped to the review feedback.
119
+ - Follow reviewer-provided fix instructions literally when they are still applicable; deviate only when the current code proves the instruction is stale or unsafe.
120
+ - In `dry_run`, stop at triage, proposed fixes, suggested replies, and verification plan.
121
+ - In `no_push`, local edits are allowed, but do not push or resolve threads for local-only fixes.
122
+ - In `no_reply`, do not post replies or resolve threads; report suggested replies/actions instead.
123
+ - In normal mode, batch replies where practical, then resolve every handled thread by default after the fix, explanation, or deferral reason is available on the PR.
124
+
125
+ 4. **Verify before claiming completion**
126
+ - For fixes, run appropriate checks or explain why they could not run.
127
+ - Re-inspect the updated diff and comment context to confirm the concern is resolved.
128
+ - Do not mark a thread resolved if it still needs reviewer, maintainer, or product input.
129
+
130
+ 5. **Finish**
131
+ - Normal mode: commit/push changes when appropriate, post useful replies or a summary, and resolve all handled threads by default.
132
+ - Safe modes: report the local state and the exact replies/resolution actions a human could take.
133
+
134
+ ## Reply Guidance
135
+
136
+ - Keep inline replies short and tied to the original title or concern.
137
+ - For fixed findings, mention the concrete change or commit if useful.
138
+ - For already-addressed or outdated findings, cite the current code path or behavior that makes the finding no longer applicable.
139
+ - For deferred or won't-fix findings, provide the reason and any follow-up issue or owner if known.
140
+ - If a reply or resolve operation fails, continue with the remaining threads and report the failure in the final summary.
141
+
142
+ ## Final Summary Checklist
143
+
144
+ - Mode used: `normal`, `dry_run`, `no_push`, or `no_reply`
145
+ - Counts by disposition: fixed, answered, clarified/left open, already addressed, outdated, deferred/won't-fix
146
+ - Threads resolved, intentionally left open, or resolution actions skipped by mode
147
+ - Verification run or planned
148
+ - Commits pushed, local diff/commits, or "none"
149
+ - Remaining open items and who needs to respond
@@ -62,6 +62,19 @@ jobs:
62
62
  runs-on: ubuntu-slim
63
63
  secrets:
64
64
  GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
65
+ github-codeql-analysis:
66
+ if: >
67
+ github.event_name == 'push'
68
+ || github.event_name == 'pull_request'
69
+ || (github.event_name == 'workflow_dispatch' && inputs.workflow == 'lint-and-test')
70
+ permissions:
71
+ contents: read
72
+ security-events: write
73
+ actions: read
74
+ uses: dceoy/gh-actions-for-devops/.github/workflows/github-codeql-analysis.yml@main # zizmor: ignore[unpinned-uses]
75
+ with:
76
+ language: >
77
+ ["python"]
65
78
  dependabot-auto-merge:
66
79
  if: >
67
80
  github.event_name == 'pull_request' && github.actor == 'dependabot[bot]'
@@ -73,5 +86,7 @@ jobs:
73
86
  contents: write
74
87
  pull-requests: write
75
88
  actions: read
89
+ checks: read
90
+ statuses: read
76
91
  with:
77
92
  unconditional: true
mt5cli-0.8.1/AGENTS.md ADDED
@@ -0,0 +1,37 @@
1
+ # Repository Guidelines
2
+
3
+ ## Project Structure & Module Organization
4
+
5
+ `mt5cli/` contains the package source. Important modules include `cli.py` for the Typer command-line app, `client.py` and `sdk.py` for public MT5 client/session APIs, `history.py` for SQLite history collection, `storage.py` and `converters.py` for export behavior, and `schemas.py` for normalized dataset contracts. `tests/` holds pytest coverage for CLI behavior, SDK contracts, trading helpers, history, and utilities. `docs/` and `mkdocs.yml` define the MkDocs site and API reference. `skills/mt5cli/SKILL.md` documents the mt5cli agent skill.
6
+
7
+ ## Build, Test, and Development Commands
8
+
9
+ - `uv sync` installs runtime and development dependencies from `pyproject.toml` and `uv.lock`.
10
+ - `uv run mt5cli --help` runs the local CLI entry point.
11
+ - `uv run ruff format .` formats Python files.
12
+ - `uv run ruff check --fix .` lints and applies safe fixes.
13
+ - `uv run pyright .` runs strict type checking.
14
+ - `uv run pytest` runs doctests, branch coverage, and the test suite.
15
+ - `uv run mkdocs serve` previews documentation locally; `uv run mkdocs build` validates the docs build.
16
+
17
+ Use `.agents/skills/local-qa/SKILL.md` for pre-handoff QA. It runs `.agents/skills/local-qa/scripts/qa.sh`, which formats, lints, type-checks, tests, formats Markdown, and checks GitHub workflows.
18
+
19
+ ## Coding Style & Naming Conventions
20
+
21
+ Target Python `>=3.11,<3.14`. Use Ruff’s configured 88-character line length and Google-style docstrings. Pyright is strict, so prefer explicit public type annotations and narrow exception handling. Keep module, function, and variable names in `snake_case`; classes and enums use `PascalCase`. Preserve the package’s small, typed helper style rather than adding broad abstractions.
22
+
23
+ ## Design Principles
24
+
25
+ Apply KISS, DRY, and YAGNI when changing code. Prefer the simplest implementation that satisfies the current CLI/API contract. Remove duplication when shared behavior is already proven by at least two concrete call sites, but avoid generic helpers for speculative reuse. Do not add configuration flags, extension hooks, or alternate backends until a real repository use case requires them.
26
+
27
+ ## Testing Guidelines
28
+
29
+ Tests use pytest, pytest-mock, doctests, and pytest-cov. Test files should match `tests/test_*.py`, classes `Test*`, and functions `test_*`. Coverage is configured with `fail_under = 100`, so add focused tests for every behavior change. Mock MT5/pdmt5 boundaries; do not require a live MetaTrader terminal in unit tests.
30
+
31
+ ## Commit & Pull Request Guidelines
32
+
33
+ Recent history uses concise imperative commits, sometimes with conventional prefixes such as `feat:` or `chore:` and PR numbers appended by GitHub. Keep commits scoped to one logical change. Pull requests should describe behavior changes, note tests run, link related issues, and call out MT5/live-trading risk where relevant.
34
+
35
+ ## Security & Configuration Tips
36
+
37
+ Never commit account credentials, broker passwords, exported private data, or local `.venv` contents. Treat `order_send` and CLI `order-send --yes` as live execution paths; gate examples and tests so they cannot place real trades accidentally.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mt5cli
3
- Version: 0.7.2
3
+ Version: 0.8.1
4
4
  Summary: Generic MT5 data and execution infrastructure for Python applications
5
5
  Project-URL: Repository, https://github.com/dceoy/mt5cli.git
6
6
  Author-email: dceoy <dceoy@users.noreply.github.com>
@@ -28,6 +28,8 @@ Description-Content-Type: text/markdown
28
28
 
29
29
  Generic MT5 data and execution infrastructure for Python applications. Export from the CLI or import a small, stable Python API in downstream packages.
30
30
 
31
+ The [Public API Contract](docs/api/public-contract.md) lists stable SDK exports (`mt5cli.STABLE_SDK_EXPORTS`), CLI commands, internal helpers, and responsibilities that remain out of scope (strategy logic, backtests, optimization).
32
+
31
33
  Built on top of [pdmt5](https://github.com/dceoy/pdmt5), a pandas-based data handler for MetaTrader 5.
32
34
 
33
35
  ## Architecture
@@ -109,6 +111,37 @@ Schema contracts live in `mt5cli.schemas` (`DataKind`, `validate_schema`, `norma
109
111
 
110
112
  `MT5Client.order_send()` is a live execution primitive: it can place real trades on the connected account. mt5cli does not implement strategy logic, signal generation, backtesting, or optimization — downstream applications must gate live execution explicitly.
111
113
 
114
+ ### Trading lifecycle and state helpers
115
+
116
+ Trading applications can depend on `mt5cli` imports only; terminal path,
117
+ credentials, server, and timeout are forwarded to `pdmt5.Mt5Config`, numeric
118
+ login strings are coerced to integers, and empty login strings are treated as
119
+ unset.
120
+
121
+ ```python
122
+ from mt5cli import (
123
+ calculate_spread_ratio,
124
+ create_trading_client,
125
+ get_account_snapshot,
126
+ mt5_trading_session,
127
+ )
128
+
129
+ with mt5_trading_session(
130
+ path=r"C:\Program Files\MetaTrader 5\terminal64.exe",
131
+ login="12345",
132
+ password="from-env-or-secret-store",
133
+ server="Broker-Demo",
134
+ ) as client:
135
+ account = get_account_snapshot(client)
136
+ spread = calculate_spread_ratio(client, "EURUSD")
137
+
138
+ client = create_trading_client(login=12345, server="Broker-Demo")
139
+ try:
140
+ positions = client.positions_get_as_df(symbol="EURUSD")
141
+ finally:
142
+ client.shutdown()
143
+ ```
144
+
112
145
  ## CLI usage
113
146
 
114
147
  ```bash
@@ -220,7 +253,7 @@ update_history_with_config(
220
253
  - **`collect-history`**: explicit date-range export into SQLite.
221
254
  - **`update_history`**: incremental append based on existing SQLite `MAX(time)` per symbol (and timeframe for rates); account-level deals use a separate cursor when `include_account_events=True`.
222
255
  - **`rates` table**: normalized storage with `symbol` and `timeframe` columns.
223
- - **Rate compatibility views**: mt5cli manages all `rate_*` views. Naming is `rate_<symbol>__<timeframe>` when a symbol has one timeframe, otherwise `rate_<symbol>__<granularity>_<timeframe>` (for example `rate_EURUSD__M1_1`). Stale `rate_*` views are dropped and recreated when rates change for offline tools such as mteor optimize.
256
+ - **Rate compatibility views**: mt5cli manages all `rate_*` views. Naming is `rate_<symbol>__<timeframe>` when a symbol has one timeframe, otherwise `rate_<symbol>__<granularity>_<timeframe>` (for example `rate_EURUSD__M1_1`). Stale `rate_*` views are dropped and recreated when rates change for offline downstream tools.
224
257
  - **Rate view resolution**: use `resolve_rate_view_name()` / `resolve_rate_view_names()` to map symbols and granularities to existing SQLite compatibility views without creating databases. Both accept `None` (or a missing path) and return deterministic default names unless `require_existing=True`.
225
258
  - **Rate view loading**: use `load_rate_data()` / `load_rate_data_from_connection()` to load a SQLite rate table or view into a `DatetimeIndex` DataFrame.
226
259
  - **Multi-series rate loading**: use `build_rate_targets()` to build neutral `RateTarget(symbol, timeframe)` pairs, `resolve_rate_tables()` to map them to table/view names (pass `require_existing=True` for strict resolution), and `load_rate_series_from_sqlite()` to load them into a mapping keyed by `(symbol, integer timeframe)`. The loader requires existing managed views unless `explicit_tables` is supplied, and rejects duplicate `(symbol, timeframe)` targets.
@@ -253,12 +286,12 @@ eurusd_m1 = rates["EURUSD", "M1"] # closed bars only
253
286
  - Windows OS (MetaTrader 5 requirement)
254
287
  - MetaTrader 5 platform installed
255
288
 
256
- ### Migration note for mteor
289
+ ### Migration note for downstream trading apps
257
290
 
258
291
  Replace local MT5 lifecycle and trading helper code with mt5cli imports:
259
292
 
260
293
  ```python
261
- # Before (local mteor helpers)
294
+ # Before (local application helpers)
262
295
  # with local_mt5_trading_session(config) as client:
263
296
  # side = local_detect_position_side(client, symbol)
264
297
  # sizing = local_calculate_margin_and_volume(client, symbol, unit_ratio, preserved_ratio)
@@ -4,6 +4,8 @@
4
4
 
5
5
  Generic MT5 data and execution infrastructure for Python applications. Export from the CLI or import a small, stable Python API in downstream packages.
6
6
 
7
+ The [Public API Contract](docs/api/public-contract.md) lists stable SDK exports (`mt5cli.STABLE_SDK_EXPORTS`), CLI commands, internal helpers, and responsibilities that remain out of scope (strategy logic, backtests, optimization).
8
+
7
9
  Built on top of [pdmt5](https://github.com/dceoy/pdmt5), a pandas-based data handler for MetaTrader 5.
8
10
 
9
11
  ## Architecture
@@ -85,6 +87,37 @@ Schema contracts live in `mt5cli.schemas` (`DataKind`, `validate_schema`, `norma
85
87
 
86
88
  `MT5Client.order_send()` is a live execution primitive: it can place real trades on the connected account. mt5cli does not implement strategy logic, signal generation, backtesting, or optimization — downstream applications must gate live execution explicitly.
87
89
 
90
+ ### Trading lifecycle and state helpers
91
+
92
+ Trading applications can depend on `mt5cli` imports only; terminal path,
93
+ credentials, server, and timeout are forwarded to `pdmt5.Mt5Config`, numeric
94
+ login strings are coerced to integers, and empty login strings are treated as
95
+ unset.
96
+
97
+ ```python
98
+ from mt5cli import (
99
+ calculate_spread_ratio,
100
+ create_trading_client,
101
+ get_account_snapshot,
102
+ mt5_trading_session,
103
+ )
104
+
105
+ with mt5_trading_session(
106
+ path=r"C:\Program Files\MetaTrader 5\terminal64.exe",
107
+ login="12345",
108
+ password="from-env-or-secret-store",
109
+ server="Broker-Demo",
110
+ ) as client:
111
+ account = get_account_snapshot(client)
112
+ spread = calculate_spread_ratio(client, "EURUSD")
113
+
114
+ client = create_trading_client(login=12345, server="Broker-Demo")
115
+ try:
116
+ positions = client.positions_get_as_df(symbol="EURUSD")
117
+ finally:
118
+ client.shutdown()
119
+ ```
120
+
88
121
  ## CLI usage
89
122
 
90
123
  ```bash
@@ -196,7 +229,7 @@ update_history_with_config(
196
229
  - **`collect-history`**: explicit date-range export into SQLite.
197
230
  - **`update_history`**: incremental append based on existing SQLite `MAX(time)` per symbol (and timeframe for rates); account-level deals use a separate cursor when `include_account_events=True`.
198
231
  - **`rates` table**: normalized storage with `symbol` and `timeframe` columns.
199
- - **Rate compatibility views**: mt5cli manages all `rate_*` views. Naming is `rate_<symbol>__<timeframe>` when a symbol has one timeframe, otherwise `rate_<symbol>__<granularity>_<timeframe>` (for example `rate_EURUSD__M1_1`). Stale `rate_*` views are dropped and recreated when rates change for offline tools such as mteor optimize.
232
+ - **Rate compatibility views**: mt5cli manages all `rate_*` views. Naming is `rate_<symbol>__<timeframe>` when a symbol has one timeframe, otherwise `rate_<symbol>__<granularity>_<timeframe>` (for example `rate_EURUSD__M1_1`). Stale `rate_*` views are dropped and recreated when rates change for offline downstream tools.
200
233
  - **Rate view resolution**: use `resolve_rate_view_name()` / `resolve_rate_view_names()` to map symbols and granularities to existing SQLite compatibility views without creating databases. Both accept `None` (or a missing path) and return deterministic default names unless `require_existing=True`.
201
234
  - **Rate view loading**: use `load_rate_data()` / `load_rate_data_from_connection()` to load a SQLite rate table or view into a `DatetimeIndex` DataFrame.
202
235
  - **Multi-series rate loading**: use `build_rate_targets()` to build neutral `RateTarget(symbol, timeframe)` pairs, `resolve_rate_tables()` to map them to table/view names (pass `require_existing=True` for strict resolution), and `load_rate_series_from_sqlite()` to load them into a mapping keyed by `(symbol, integer timeframe)`. The loader requires existing managed views unless `explicit_tables` is supplied, and rejects duplicate `(symbol, timeframe)` targets.
@@ -229,12 +262,12 @@ eurusd_m1 = rates["EURUSD", "M1"] # closed bars only
229
262
  - Windows OS (MetaTrader 5 requirement)
230
263
  - MetaTrader 5 platform installed
231
264
 
232
- ### Migration note for mteor
265
+ ### Migration note for downstream trading apps
233
266
 
234
267
  Replace local MT5 lifecycle and trading helper code with mt5cli imports:
235
268
 
236
269
  ```python
237
- # Before (local mteor helpers)
270
+ # Before (local application helpers)
238
271
  # with local_mt5_trading_session(config) as client:
239
272
  # side = local_detect_position_side(client, symbol)
240
273
  # sizing = local_calculate_margin_and_volume(client, symbol, unit_ratio, preserved_ratio)
@@ -167,19 +167,45 @@ Resolution rules:
167
167
 
168
168
  ### Rate data loading
169
169
 
170
- Use `load_rate_data()` to load a table or view from a SQLite path, or
171
- `load_rate_data_from_connection()` when you already have a connection:
170
+ The canonical normalized rate table is `rates`; compatibility views are named
171
+ with `rate_<symbol>__<timeframe>` for single-timeframe symbols or
172
+ `rate_<symbol>__<granularity>_<timeframe>` when a symbol has multiple stored
173
+ timeframes. `resolve_rate_table_name()` returns `rates`, while
174
+ `resolve_rate_view_name()` returns the per-symbol compatibility view name.
175
+
176
+ Use `load_rate_data()` or `load_rate_series_from_sqlite(..., table=...)` to load
177
+ a single table or view from a SQLite path. Use
178
+ `load_rate_series_by_granularity()` to load multiple instrument/granularity
179
+ targets without hard-coding view names:
172
180
 
173
181
  ```python
174
182
  from pathlib import Path
175
183
 
176
- from mt5cli import load_rate_data
184
+ from mt5cli import (
185
+ load_rate_data,
186
+ load_rate_series_by_granularity,
187
+ load_rate_series_from_sqlite,
188
+ resolve_rate_table_name,
189
+ )
177
190
  from mt5cli.history import resolve_rate_view_name
178
191
 
179
192
  view = resolve_rate_view_name(Path("history.db"), "EURUSD", "M1", require_existing=True)
180
193
  rates = load_rate_data(Path("history.db"), view, count=1000)
194
+ same_rates = load_rate_series_from_sqlite(Path("history.db"), table=view, count=1000)
195
+
196
+ table = resolve_rate_table_name("EURUSD", "M1") # "rates"
197
+ series = load_rate_series_by_granularity(
198
+ Path("history.db"),
199
+ symbols=["EURUSD", "GBPUSD"],
200
+ granularities=["M1", "H1"],
201
+ count=500,
202
+ )
181
203
  ```
182
204
 
205
+ `count` returns the latest rows while preserving chronological order. Missing
206
+ tables/views and mismatched `explicit_tables` lengths raise `ValueError` with
207
+ the requested database target in the message.
208
+
183
209
  The loader accepts close-based OHLC rate data or tick-like bid/ask data. It
184
210
  validates that `time` exists, parses timestamps with pandas, and returns a
185
211
  DataFrame indexed by ascending `DatetimeIndex` named `time`.
@@ -2,10 +2,15 @@
2
2
 
3
3
  This section documents the mt5cli public Python API and CLI modules.
4
4
 
5
+ Start with the [Public API Contract](public-contract.md) for the stable
6
+ downstream SDK surface, CLI boundary, internal modules, and out-of-scope strategy
7
+ responsibilities.
8
+
5
9
  ## Public API layers
6
10
 
7
11
  | Module | Purpose |
8
12
  | ----------------------------------------- | ------------------------------------------------------------------------- |
13
+ | [Public API Contract](public-contract.md) | Stable downstream SDK exports, CLI boundary, and out-of-scope items |
9
14
  | [Client](client.md) | `MT5Client` session abstraction for data access and order primitives |
10
15
  | [Schemas](schemas.md) | Canonical DataFrame contracts and normalization helpers |
11
16
  | [Storage](storage.md) | CSV/JSON/Parquet/SQLite export and history collection helpers |
@@ -30,7 +35,10 @@ flowchart TD
30
35
  SDK --> PDMT5["pdmt5.Mt5DataClient"]
31
36
  ```
32
37
 
33
- Downstream packages should depend on the package root exports (`MT5Client`, `DataKind`, `normalize_dataframe`, `export_dataframe`, `collect_history`, etc.) rather than private modules.
38
+ Downstream packages should depend on the package root exports documented in the
39
+ [Public API Contract](public-contract.md) (`MT5Client`,
40
+ `DataKind`, `normalize_dataframe`, `collect_history`, `load_rate_data`,
41
+ `resolve_rate_view_name`, etc.) rather than private modules.
34
42
 
35
43
  `MT5Client.order_send()` is a live execution primitive that can place real trades. mt5cli exposes minimal execution helpers only; strategy logic, signals, backtests, and optimization remain out of scope and must be implemented downstream with explicit execution gating.
36
44
 
@@ -0,0 +1,184 @@
1
+ # Public API Contract
2
+
3
+ mt5cli is the generic MT5 data and execution infrastructure layer for downstream
4
+ Python applications. The intended dependency direction is:
5
+
6
+ ```text
7
+ downstream app -> mt5cli -> pdmt5 -> MetaTrader 5
8
+ ```
9
+
10
+ Downstream packages should import from the package root (`from mt5cli import
11
+ ...`) and treat the symbols listed below as the stable SDK contract. CLI
12
+ commands mirror the same behavior but are not importable Python APIs.
13
+
14
+ ## Stable downstream SDK API
15
+
16
+ These names are exported from `mt5cli` and covered by the contract in
17
+ `mt5cli.STABLE_SDK_EXPORTS` (defined in `mt5cli.contract`). Prefer `MT5Client` over the legacy `Mt5CliClient`
18
+ alias for new code.
19
+
20
+ ### Session lifecycle and configuration
21
+
22
+ | Symbol | Role |
23
+ | ----------------------------------------------- | ---------------------------------------------------------------- |
24
+ | `MT5Client`, `Mt5CliClient` | Read-only data client with optional `order_check` / `order_send` |
25
+ | `build_config` | Build `pdmt5.Mt5Config` from connection fields |
26
+ | `mt5_session` | Context manager: initialize, login, yield client, shutdown |
27
+ | `create_trading_client`, `mt5_trading_session` | Trading-capable `pdmt5.Mt5TradingClient` lifecycle |
28
+ | `AccountSpec` | Generic account group: symbols plus optional credentials |
29
+ | `resolve_account_spec`, `resolve_account_specs` | Merge overrides and expand `${ENV_VAR}` placeholders |
30
+ | `substitute_env_placeholders` | Replace `${NAME}` substrings from the environment |
31
+
32
+ Credential resolution is generic: any environment variable name may appear inside
33
+ `${...}`. mt5cli does not hard-code application-specific keys such as
34
+ `mt5_login` or `mt5_exe`.
35
+
36
+ ### Read-only MT5 data access
37
+
38
+ Module-level helpers open a transient connection per call. Prefer `mt5_session`
39
+ or `MT5Client` when making many requests in one process.
40
+
41
+ | Area | Symbols |
42
+ | -------------------- | ---------------------------------------------------------------------------------------------------- |
43
+ | Rates | `copy_rates_from`, `copy_rates_from_pos`, `copy_rates_range`, `latest_rates`, `collect_latest_rates` |
44
+ | Ticks | `copy_ticks_from`, `copy_ticks_range`, `recent_ticks` |
45
+ | Account / terminal | `account_info`, `terminal_info`, `mt5_version`, `last_error`, `mt5_summary`, `mt5_summary_as_df` |
46
+ | Symbols / market | `symbols`, `symbol_info`, `symbol_info_tick`, `market_book`, `minimum_margins` |
47
+ | Trading state (read) | `orders`, `positions`, `history_orders`, `history_deals`, `recent_history_deals` |
48
+
49
+ Use `mt5_version` for MetaTrader 5 terminal version data. The name `version` at
50
+ the package root refers to `importlib.metadata.version` (package metadata), not
51
+ the MT5 SDK helper.
52
+
53
+ ### Closed-bar rate helpers
54
+
55
+ MetaTrader 5 returns the still-forming bar as the last row when
56
+ `start_pos=0`. Use these helpers instead of reimplementing bar trimming or
57
+ timestamp normalization in downstream apps.
58
+
59
+ | Symbol | Role |
60
+ | ------------------------------------------------ | ------------------------------------------------------------ |
61
+ | `drop_forming_rate_bar` | Remove the last row from chronologically ordered rate data |
62
+ | `fetch_latest_closed_rates` | Single connected client: fetch `count + 1`, drop forming bar |
63
+ | `collect_latest_closed_rates_for_accounts` | Multi-account closed bars with optional retry wrapper |
64
+ | `collect_latest_closed_rates_by_granularity` | Same data keyed by `(symbol, granularity_name)` |
65
+ | `collect_latest_rates_for_accounts` | Latest bars including the forming bar when `start_pos=0` |
66
+ | `collect_latest_rates_for_accounts_with_retries` | Bounded exponential backoff for transient MT5 errors |
67
+
68
+ ### SQLite history collection and rate loading
69
+
70
+ | Symbol | Role |
71
+ | ----------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------- |
72
+ | `collect_history` | One-shot date-range export into SQLite |
73
+ | `update_history`, `update_history_with_config` | Incremental append from `MAX(time)` cursors |
74
+ | `ThrottledHistoryUpdater` | Minimum interval between successful incremental updates |
75
+ | `resolve_history_datasets`, `resolve_history_timeframes`, `resolve_history_tick_flags` | History pipeline configuration |
76
+ | `build_rate_view_name`, `resolve_rate_table_name`, `resolve_rate_view_name`, `resolve_rate_view_names`, `resolve_rate_tables` | Map symbols/timeframes to mt5cli-managed table or view names |
77
+ | `RateTarget`, `build_rate_targets` | Neutral `(symbol, timeframe)` series descriptors |
78
+ | `load_rate_data`, `load_rate_data_from_connection` | Load one table/view into a time-indexed DataFrame |
79
+ | `load_rate_series_from_sqlite`, `load_rate_series_by_granularity` | Load one or many series; fail clearly when managed views are missing |
80
+
81
+ Pass `require_existing=True` to rate view resolution helpers when downstream
82
+ code must fail instead of receiving a best-guess view name. Multi-series loaders
83
+ require existing managed `rate_*__*` views unless `explicit_tables` is supplied.
84
+
85
+ See [History Collection (SQLite)](history.md) for schema, view naming, and ER
86
+ diagrams.
87
+
88
+ ### Trading and sizing primitives (generic)
89
+
90
+ These helpers implement broker-facing calculations only. They do not encode
91
+ strategy entries, exits, Kelly sizing, or signal logic.
92
+
93
+ | Symbol | Role |
94
+ | -------------------------------------------------------------------------------------------------- | --------------------------------------------- |
95
+ | `get_account_snapshot`, `get_symbol_snapshot`, `get_tick_snapshot`, `get_positions_frame` | Normalized account/symbol/tick/position views |
96
+ | `detect_position_side` | Net long / short / flat from open positions |
97
+ | `calculate_spread_ratio` | Relative bid-ask spread |
98
+ | `calculate_margin_and_volume`, `calculate_volume_by_margin`, `calculate_new_position_margin_ratio` | Margin budget and volume sizing |
99
+ | `determine_order_limits` | SL/TP price levels from ratios |
100
+ | `ensure_symbol_selected` | Select/verify Market Watch visibility |
101
+ | `place_market_order`, `close_open_positions`, `update_sltp_for_open_positions` | Order execution helpers (`dry_run` supported) |
102
+ | `MarginVolume`, `OrderLimits`, `OrderExecutionResult` | Typed return contracts for order helpers |
103
+ | `OrderSide`, `OrderFillingMode`, `OrderTimeMode`, `PositionSide`, `ExecutionStatus` | Typed enums for order helpers |
104
+
105
+ `MT5Client.order_send()` and CLI `order-send --yes` are live execution paths.
106
+
107
+ Order helpers validate broker stop-level distance in `determine_order_limits()` and
108
+ raise `Mt5TradingError` when computed SL/TP prices are too close to the entry
109
+ quote. Validation uses `trade_stops_level * point` from the current quote and
110
+ symbol metadata as a pre-check only; it does not guarantee live order acceptance
111
+ after price movement and does not inspect `trade_freeze_level`. Live
112
+ `place_market_order()` and SL/TP updates call
113
+ `ensure_symbol_selected()` so hidden symbols are added to Market Watch before
114
+ sending requests. Failed, malformed, or unknown broker retcodes are fail-closed
115
+ and returned as `status="failed"` with normalized `request` / `response` details;
116
+ `dry_run=True` never calls `ensure_symbol_selected()` or `order_send()`.
117
+
118
+ ### Errors and MT5 type re-exports
119
+
120
+ | Symbol | Role |
121
+ | ------------------------------------------------------------------------------------ | ----------------------------------------------- |
122
+ | `Mt5CliError`, `Mt5ConnectionError`, `Mt5OperationError`, `Mt5SchemaError` | Stable mt5cli exception types |
123
+ | `normalize_mt5_exception`, `call_with_normalized_errors`, `is_recoverable_mt5_error` | Error normalization and retry classification |
124
+ | `Mt5Config`, `Mt5RuntimeError`, `Mt5TradingClient`, `Mt5TradingError` | Re-exported pdmt5 types for adapter convenience |
125
+
126
+ ### Additional public exports (secondary)
127
+
128
+ The package root also exports schema, storage, and parsing helpers (for example
129
+ `DataKind`, `Dataset`, `normalize_dataframe`, `export_dataframe`,
130
+ `parse_timeframe`, `TIMEFRAME_MAP`). These are public but oriented toward export
131
+ pipelines and advanced integration. Prefer the stable symbols above for core
132
+ infrastructure.
133
+
134
+ ## CLI commands
135
+
136
+ The Typer application in `mt5cli.cli` exposes file-export commands documented in
137
+ [CLI Module](cli.md) and the project README. CLI commands:
138
+
139
+ - Require `-o/--output` and write CSV, JSON, Parquet, or SQLite.
140
+ - Accept global MT5 connection options (`--login`, `--password`, `--server`,
141
+ `--path`, `--timeout`).
142
+ - Delegate to the same Python APIs described here; they are not duplicated
143
+ business logic.
144
+
145
+ `order-send` requires `--yes` before placing live trades.
146
+
147
+ ## Internal helpers (not stable)
148
+
149
+ Do not import these for downstream contracts; they may change without a semver
150
+ notice:
151
+
152
+ | Module | Examples |
153
+ | ------------------------ | ------------------------------------------------------------------------- |
154
+ | `mt5cli.sdk` | `connected_client`, `_run_with_client`, private coercion helpers |
155
+ | `mt5cli.history` | `write_*_dataset`, `deduplicate_history_tables`, `parse_sqlite_timestamp` |
156
+ | `mt5cli.retry` | `retry_with_backoff` |
157
+ | `mt5cli.cli` | Typer command handlers and Click parameter types |
158
+ | Leading-underscore names | Any `_`-prefixed function or method |
159
+
160
+ Use the package-root stable exports instead of reaching into submodule
161
+ internals.
162
+
163
+ ## Explicitly out of scope
164
+
165
+ mt5cli must **not** implement downstream strategy or research responsibilities.
166
+ The following belong in consuming applications, not in mt5cli:
167
+
168
+ - Signal detection (for example AR-GARCH or other model-specific triggers)
169
+ - Backtesting, walk-forward analysis, or parameter optimization
170
+ - Strategy-specific risk policy, position sizing systems, or Kelly fractions
171
+ - Entry/exit decision logic or YAML strategy semantics
172
+ - Application-specific credential schema keys wired into mt5cli internals
173
+
174
+ mt5cli provides connection lifecycle, normalized data access, SQLite history
175
+ machinery, closed-bar helpers, generic margin/volume/spread/SL/TP utilities, and
176
+ optional order primitives so downstream apps can focus on strategy code behind
177
+ their own adapter layer.
178
+
179
+ ## Contract verification
180
+
181
+ `tests/test_contracts.py` asserts that every name in `STABLE_SDK_EXPORTS` is
182
+ importable from `mt5cli`, documents key closed-bar, rate-view, SQLite loading,
183
+ account-resolution, and trading-session behaviors, and keeps the contract set
184
+ aligned with `__all__`.