mt5cli 0.9.2__tar.gz → 0.9.4__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.
- {mt5cli-0.9.2 → mt5cli-0.9.4}/PKG-INFO +4 -4
- {mt5cli-0.9.2 → mt5cli-0.9.4}/README.md +3 -3
- {mt5cli-0.9.2 → mt5cli-0.9.4}/docs/api/public-contract.md +73 -47
- {mt5cli-0.9.2 → mt5cli-0.9.4}/docs/api/sdk.md +3 -3
- {mt5cli-0.9.2 → mt5cli-0.9.4}/docs/api/trading.md +2 -2
- {mt5cli-0.9.2 → mt5cli-0.9.4}/docs/index.md +1 -1
- {mt5cli-0.9.2 → mt5cli-0.9.4}/mt5cli/__init__.py +19 -3
- {mt5cli-0.9.2 → mt5cli-0.9.4}/mt5cli/client.py +1 -3
- {mt5cli-0.9.2 → mt5cli-0.9.4}/mt5cli/contract.py +70 -29
- {mt5cli-0.9.2 → mt5cli-0.9.4}/mt5cli/trading.py +279 -23
- {mt5cli-0.9.2 → mt5cli-0.9.4}/pyproject.toml +1 -1
- {mt5cli-0.9.2 → mt5cli-0.9.4}/tests/test_contracts.py +78 -4
- {mt5cli-0.9.2 → mt5cli-0.9.4}/tests/test_trading.py +628 -59
- {mt5cli-0.9.2 → mt5cli-0.9.4}/uv.lock +1 -1
- {mt5cli-0.9.2 → mt5cli-0.9.4}/.agents/skills/local-qa/SKILL.md +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/.agents/skills/local-qa/scripts/qa.sh +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/.agents/skills/mt5cli/SKILL.md +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/.agents/skills/pr-feedback-triage/SKILL.md +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/.claude/agents/codex.md +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/.claude/settings.json +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/.github/FUNDING.yml +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/.github/dependabot.yml +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/.github/renovate.json +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/.github/workflows/ci.yml +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/.github/workflows/claude.yml +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/.github/workflows/release.yml +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/.gitignore +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/AGENTS.md +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/CLAUDE.md +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/LICENSE +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/docs/api/cli.md +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/docs/api/client.md +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/docs/api/converters.md +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/docs/api/exceptions.md +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/docs/api/history.md +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/docs/api/index.md +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/docs/api/schemas.md +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/docs/api/storage.md +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/docs/api/utils.md +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/mkdocs.yml +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/mt5cli/__main__.py +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/mt5cli/cli.py +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/mt5cli/converters.py +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/mt5cli/exceptions.py +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/mt5cli/history.py +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/mt5cli/retry.py +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/mt5cli/schemas.py +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/mt5cli/sdk.py +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/mt5cli/storage.py +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/mt5cli/utils.py +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/tests/__init__.py +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/tests/conftest.py +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/tests/test_cli.py +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/tests/test_history.py +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/tests/test_sdk.py +0 -0
- {mt5cli-0.9.2 → mt5cli-0.9.4}/tests/test_utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mt5cli
|
|
3
|
-
Version: 0.9.
|
|
3
|
+
Version: 0.9.4
|
|
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>
|
|
@@ -55,7 +55,7 @@ pip install -U mt5cli MetaTrader5
|
|
|
55
55
|
|
|
56
56
|
## Python API (downstream packages)
|
|
57
57
|
|
|
58
|
-
Import `MT5Client` for generic MT5 data access, schema normalization, and optional order primitives.
|
|
58
|
+
Import `MT5Client` for generic MT5 data access, schema normalization, and optional order primitives.
|
|
59
59
|
|
|
60
60
|
```python
|
|
61
61
|
from datetime import UTC, datetime
|
|
@@ -274,7 +274,7 @@ eurusd_m1 = rates["EURUSD", "M1"] # closed bars only
|
|
|
274
274
|
|
|
275
275
|
- **Credential resolution**: use `resolve_account_spec()` / `resolve_account_specs()` to merge explicit override values over `AccountSpec` fields and expand `${ENV_VAR}` placeholders (via `substitute_env_placeholders()`), raising `ValueError` for missing variables. This keeps secrets out of plan/config files without coupling to any strategy code.
|
|
276
276
|
- **Throttled history updates**: use `ThrottledHistoryUpdater` to wrap `update_history()` with a minimum `interval_seconds` between successful runs (monotonic clock). Call `should_update()` / `update(client, symbols)` from an application loop; errors propagate by default, or pass `suppress_errors=True` to swallow recoverable `Mt5*Error`, `sqlite3.Error`, `ValueError`, `OSError`, and MT5 client capability errors for history API methods without advancing the throttle (other `AttributeError` / `TypeError` values always propagate). Pass `update_backend` to inject a custom history update callable (same keyword arguments as `update_history`) instead of monkey-patching `mt5cli.sdk.update_history`.
|
|
277
|
-
- **Trading session helpers**: use `mt5_trading_session()` for a trading-capable `pdmt5.Mt5TradingClient` that initializes/logs in via `Mt5Config.path` and always shuts down safely. Pair with `detect_position_side()`, `calculate_margin_and_volume()`, and `determine_order_limits()` for generic position and sizing utilities.
|
|
277
|
+
- **Trading session helpers**: use `mt5_trading_session()` for a trading-capable `pdmt5.Mt5TradingClient` that initializes/logs in via `Mt5Config.path` and always shuts down safely. Pair with `detect_position_side()`, `calculate_margin_and_volume()`, and `determine_order_limits()` for generic position and sizing utilities. Keep read-only collection on `mt5_session()` / `MT5Client`.
|
|
278
278
|
- **Granularity-keyed rate loading**: `load_rate_series_by_granularity()` builds targets with `build_rate_targets()`, loads them with `load_rate_series_from_sqlite()`, and returns a mapping keyed by `(symbol | None, granularity_name)` such as `("EURUSD", "M1")` to reduce downstream boilerplate.
|
|
279
279
|
- **MT5 session helper**: use the `mt5_session()` context manager to attach to (or, when `Mt5Config.path` is set, launch) an MT5 terminal, log in, and yield a connected `MT5Client` that shuts down on exit.
|
|
280
280
|
- **SQLite export helpers**: use `export_dataframe_to_sqlite()` for append mode, optional index export, and post-write deduplication by key columns.
|
|
@@ -341,7 +341,7 @@ finally:
|
|
|
341
341
|
client.shutdown()
|
|
342
342
|
```
|
|
343
343
|
|
|
344
|
-
Read-only collectors can keep using `mt5_session()` and `MT5Client
|
|
344
|
+
Read-only collectors can keep using `mt5_session()` and `MT5Client`.
|
|
345
345
|
|
|
346
346
|
## Development
|
|
347
347
|
|
|
@@ -31,7 +31,7 @@ pip install -U mt5cli MetaTrader5
|
|
|
31
31
|
|
|
32
32
|
## Python API (downstream packages)
|
|
33
33
|
|
|
34
|
-
Import `MT5Client` for generic MT5 data access, schema normalization, and optional order primitives.
|
|
34
|
+
Import `MT5Client` for generic MT5 data access, schema normalization, and optional order primitives.
|
|
35
35
|
|
|
36
36
|
```python
|
|
37
37
|
from datetime import UTC, datetime
|
|
@@ -250,7 +250,7 @@ eurusd_m1 = rates["EURUSD", "M1"] # closed bars only
|
|
|
250
250
|
|
|
251
251
|
- **Credential resolution**: use `resolve_account_spec()` / `resolve_account_specs()` to merge explicit override values over `AccountSpec` fields and expand `${ENV_VAR}` placeholders (via `substitute_env_placeholders()`), raising `ValueError` for missing variables. This keeps secrets out of plan/config files without coupling to any strategy code.
|
|
252
252
|
- **Throttled history updates**: use `ThrottledHistoryUpdater` to wrap `update_history()` with a minimum `interval_seconds` between successful runs (monotonic clock). Call `should_update()` / `update(client, symbols)` from an application loop; errors propagate by default, or pass `suppress_errors=True` to swallow recoverable `Mt5*Error`, `sqlite3.Error`, `ValueError`, `OSError`, and MT5 client capability errors for history API methods without advancing the throttle (other `AttributeError` / `TypeError` values always propagate). Pass `update_backend` to inject a custom history update callable (same keyword arguments as `update_history`) instead of monkey-patching `mt5cli.sdk.update_history`.
|
|
253
|
-
- **Trading session helpers**: use `mt5_trading_session()` for a trading-capable `pdmt5.Mt5TradingClient` that initializes/logs in via `Mt5Config.path` and always shuts down safely. Pair with `detect_position_side()`, `calculate_margin_and_volume()`, and `determine_order_limits()` for generic position and sizing utilities.
|
|
253
|
+
- **Trading session helpers**: use `mt5_trading_session()` for a trading-capable `pdmt5.Mt5TradingClient` that initializes/logs in via `Mt5Config.path` and always shuts down safely. Pair with `detect_position_side()`, `calculate_margin_and_volume()`, and `determine_order_limits()` for generic position and sizing utilities. Keep read-only collection on `mt5_session()` / `MT5Client`.
|
|
254
254
|
- **Granularity-keyed rate loading**: `load_rate_series_by_granularity()` builds targets with `build_rate_targets()`, loads them with `load_rate_series_from_sqlite()`, and returns a mapping keyed by `(symbol | None, granularity_name)` such as `("EURUSD", "M1")` to reduce downstream boilerplate.
|
|
255
255
|
- **MT5 session helper**: use the `mt5_session()` context manager to attach to (or, when `Mt5Config.path` is set, launch) an MT5 terminal, log in, and yield a connected `MT5Client` that shuts down on exit.
|
|
256
256
|
- **SQLite export helpers**: use `export_dataframe_to_sqlite()` for append mode, optional index export, and post-write deduplication by key columns.
|
|
@@ -317,7 +317,7 @@ finally:
|
|
|
317
317
|
client.shutdown()
|
|
318
318
|
```
|
|
319
319
|
|
|
320
|
-
Read-only collectors can keep using `mt5_session()` and `MT5Client
|
|
320
|
+
Read-only collectors can keep using `mt5_session()` and `MT5Client`.
|
|
321
321
|
|
|
322
322
|
## Development
|
|
323
323
|
|
|
@@ -8,20 +8,29 @@ downstream app -> mt5cli -> pdmt5 -> MetaTrader 5
|
|
|
8
8
|
```
|
|
9
9
|
|
|
10
10
|
Downstream packages should import from the package root (`from mt5cli import
|
|
11
|
-
...`) and
|
|
12
|
-
commands mirror the same behavior but are not importable Python
|
|
11
|
+
...`) and use the public tier sets in `mt5cli.contract` to distinguish API
|
|
12
|
+
stability. CLI commands mirror the same behavior but are not importable Python
|
|
13
|
+
APIs.
|
|
14
|
+
|
|
15
|
+
## Public API tiers
|
|
16
|
+
|
|
17
|
+
mt5cli classifies package-root imports by intended downstream use:
|
|
18
|
+
|
|
19
|
+
| Tier | Contract set | Meaning |
|
|
20
|
+
| ---------------- | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
21
|
+
| Stable core | `STABLE_SDK_EXPORTS` | Preferred SDK surface for downstream MT5 infrastructure adapters. Changes require a deliberate compatibility path. |
|
|
22
|
+
| Secondary public | `SECONDARY_PUBLIC_EXPORTS` | Public helpers for CLI/export/schema integrations and lower-level MT5 wrappers. Importable, but less central to the downstream trading SDK. |
|
|
13
23
|
|
|
14
24
|
## Stable downstream SDK API
|
|
15
25
|
|
|
16
26
|
These names are exported from `mt5cli` and covered by the contract in
|
|
17
|
-
`mt5cli.STABLE_SDK_EXPORTS` (defined in `mt5cli.contract`).
|
|
18
|
-
alias for new code.
|
|
27
|
+
`mt5cli.STABLE_SDK_EXPORTS` (defined in `mt5cli.contract`).
|
|
19
28
|
|
|
20
29
|
### Session lifecycle and configuration
|
|
21
30
|
|
|
22
31
|
| Symbol | Role |
|
|
23
32
|
| ----------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
|
|
24
|
-
| `MT5Client
|
|
33
|
+
| `MT5Client` | Read-only data client with optional `order_check` / `order_send` |
|
|
25
34
|
| `build_config` | Build `pdmt5.Mt5Config` from connection fields |
|
|
26
35
|
| `mt5_session` | Context manager: initialize, login, yield client, shutdown |
|
|
27
36
|
| `create_trading_client`, `mt5_trading_session` | Trading-capable `pdmt5.Mt5TradingClient` lifecycle |
|
|
@@ -40,23 +49,6 @@ Partial strings such as `"plan$pass"`, `"abc$ENV"`, or `"$ENV-suffix"` are
|
|
|
40
49
|
**never** expanded — only an exact `$IDENTIFIER` whole-string match qualifies.
|
|
41
50
|
Default is `False` to preserve backward compatibility.
|
|
42
51
|
|
|
43
|
-
### Read-only MT5 data access
|
|
44
|
-
|
|
45
|
-
Module-level helpers open a transient connection per call. Prefer `mt5_session`
|
|
46
|
-
or `MT5Client` when making many requests in one process.
|
|
47
|
-
|
|
48
|
-
| Area | Symbols |
|
|
49
|
-
| -------------------- | ---------------------------------------------------------------------------------------------------- |
|
|
50
|
-
| Rates | `copy_rates_from`, `copy_rates_from_pos`, `copy_rates_range`, `latest_rates`, `collect_latest_rates` |
|
|
51
|
-
| Ticks | `copy_ticks_from`, `copy_ticks_range`, `recent_ticks` |
|
|
52
|
-
| Account / terminal | `account_info`, `terminal_info`, `mt5_version`, `last_error`, `mt5_summary`, `mt5_summary_as_df` |
|
|
53
|
-
| Symbols / market | `symbols`, `symbol_info`, `symbol_info_tick`, `market_book`, `minimum_margins` |
|
|
54
|
-
| Trading state (read) | `orders`, `positions`, `history_orders`, `history_deals`, `recent_history_deals` |
|
|
55
|
-
|
|
56
|
-
Use `mt5_version` for MetaTrader 5 terminal version data. The name `version` at
|
|
57
|
-
the package root refers to `importlib.metadata.version` (package metadata), not
|
|
58
|
-
the MT5 SDK helper.
|
|
59
|
-
|
|
60
52
|
### Closed-bar rate helpers
|
|
61
53
|
|
|
62
54
|
MetaTrader 5 returns the still-forming bar as the last row when
|
|
@@ -71,7 +63,6 @@ timestamp normalization in downstream apps.
|
|
|
71
63
|
| `fetch_latest_closed_rates_indexed` | Same as above but returns a UTC `DatetimeIndex` named `"time"` (no time column) |
|
|
72
64
|
| `collect_latest_closed_rates_for_accounts` | Multi-account closed bars with optional retry wrapper |
|
|
73
65
|
| `collect_latest_closed_rates_by_granularity` | Same data keyed by `(symbol, granularity_name)` |
|
|
74
|
-
| `collect_latest_rates_for_accounts` | Latest bars including the forming bar when `start_pos=0` |
|
|
75
66
|
| `collect_latest_rates_for_accounts_with_retries` | Bounded exponential backoff for transient MT5 errors |
|
|
76
67
|
|
|
77
68
|
### SQLite history collection and rate loading
|
|
@@ -99,20 +90,25 @@ diagrams.
|
|
|
99
90
|
These helpers implement broker-facing calculations only. They do not encode
|
|
100
91
|
strategy entries, exits, Kelly sizing, or signal logic.
|
|
101
92
|
|
|
102
|
-
| Symbol
|
|
103
|
-
|
|
|
104
|
-
| `get_account_snapshot`, `get_symbol_snapshot`, `get_tick_snapshot`, `get_positions_frame`
|
|
105
|
-
| `
|
|
106
|
-
| `
|
|
107
|
-
| `
|
|
108
|
-
| `
|
|
109
|
-
| `
|
|
110
|
-
| `
|
|
111
|
-
| `
|
|
112
|
-
| `
|
|
113
|
-
| `
|
|
114
|
-
| `
|
|
115
|
-
| `
|
|
93
|
+
| Symbol | Role |
|
|
94
|
+
| ------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------- |
|
|
95
|
+
| `get_account_snapshot`, `get_symbol_snapshot`, `get_tick_snapshot`, `get_positions_frame` | Normalized account/symbol/tick/position views |
|
|
96
|
+
| `extract_tick_price` | Positive finite bid/ask extraction from tick mappings |
|
|
97
|
+
| `detect_position_side` | Net long / short / flat from open positions |
|
|
98
|
+
| `calculate_spread_ratio` | Relative bid-ask spread |
|
|
99
|
+
| `calculate_margin_and_volume`, `calculate_volume_by_margin`, `calculate_new_position_margin_ratio` | Margin budget and volume sizing |
|
|
100
|
+
| `normalize_order_volume`, `estimate_order_margin`, `calculate_positions_margin` | Broker volume normalization and margin totals |
|
|
101
|
+
| `calculate_positions_margin_by_symbol` | Per-symbol margin map (resilient, first-seen order) |
|
|
102
|
+
| `calculate_positions_margin_safe` | Summed total margin across symbols (failed symbols skipped) |
|
|
103
|
+
| `calculate_projected_margin_ratio` | Estimated symbol-scoped margin/equity after optional new exposure |
|
|
104
|
+
| `calculate_account_projected_margin_ratio` | Account snapshot margin/equity after optional new exposure |
|
|
105
|
+
| `calculate_symbol_group_margin_ratio` | Estimated symbol-group margin/equity with optional exposure |
|
|
106
|
+
| `determine_order_limits` | SL/TP price levels from ratios |
|
|
107
|
+
| `calculate_trailing_stop_updates` | Per-ticket generic trailing stop-loss update plan |
|
|
108
|
+
| `ensure_symbol_selected` | Select/verify Market Watch visibility |
|
|
109
|
+
| `place_market_order`, `close_open_positions`, `update_sltp_for_open_positions`, `update_trailing_stop_loss_for_open_positions` | Order execution helpers (`dry_run` supported) |
|
|
110
|
+
| `MarginVolume`, `OrderLimits`, `OrderExecutionResult` | Typed return contracts for order helpers |
|
|
111
|
+
| `OrderSide`, `OrderFillingMode`, `OrderTimeMode`, `PositionSide`, `ExecutionStatus` | Typed enums for order helpers |
|
|
116
112
|
|
|
117
113
|
`MT5Client.order_send()` and CLI `order-send --yes` are live execution paths.
|
|
118
114
|
|
|
@@ -135,13 +131,43 @@ and returned as `status="failed"` with normalized `request` / `response` details
|
|
|
135
131
|
| `normalize_mt5_exception`, `call_with_normalized_errors`, `is_recoverable_mt5_error` | Error normalization and retry classification |
|
|
136
132
|
| `Mt5Config`, `Mt5RuntimeError`, `Mt5TradingClient`, `Mt5TradingError` | Re-exported pdmt5 types for adapter convenience |
|
|
137
133
|
|
|
138
|
-
|
|
134
|
+
## Secondary public exports
|
|
135
|
+
|
|
136
|
+
These names remain importable from `mt5cli` and are covered by
|
|
137
|
+
`SECONDARY_PUBLIC_EXPORTS`, but they are oriented toward CLI/export/schema
|
|
138
|
+
integrations, parsing, and lower-level MT5 access rather than the stable core
|
|
139
|
+
SDK surface. Prefer the stable symbols above for downstream infrastructure
|
|
140
|
+
adapters.
|
|
141
|
+
|
|
142
|
+
### Read-only MT5 data wrappers
|
|
143
|
+
|
|
144
|
+
Module-level helpers open a transient connection per call. Prefer `mt5_session`
|
|
145
|
+
or `MT5Client` when making many requests in one process.
|
|
146
|
+
|
|
147
|
+
| Area | Symbols |
|
|
148
|
+
| -------------------- | ---------------------------------------------------------------------------------------------------- |
|
|
149
|
+
| Rates | `copy_rates_from`, `copy_rates_from_pos`, `copy_rates_range`, `latest_rates`, `collect_latest_rates` |
|
|
150
|
+
| Ticks | `copy_ticks_from`, `copy_ticks_range`, `recent_ticks` |
|
|
151
|
+
| Account / terminal | `account_info`, `terminal_info`, `mt5_version`, `last_error`, `mt5_summary`, `mt5_summary_as_df` |
|
|
152
|
+
| Symbols / market | `symbols`, `symbol_info`, `symbol_info_tick`, `market_book`, `minimum_margins` |
|
|
153
|
+
| Trading state (read) | `orders`, `positions`, `history_orders`, `history_deals`, `recent_history_deals` |
|
|
154
|
+
| Multi-account rates | `collect_latest_rates_for_accounts` |
|
|
155
|
+
|
|
156
|
+
Use `mt5_version` for MetaTrader 5 terminal version data. The name `version` at
|
|
157
|
+
the package root refers to `importlib.metadata.version` (package metadata), not
|
|
158
|
+
the MT5 SDK helper.
|
|
159
|
+
|
|
160
|
+
### Schema, export, and parser helpers
|
|
139
161
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
`
|
|
143
|
-
|
|
144
|
-
|
|
162
|
+
| Area | Symbols |
|
|
163
|
+
| -------------------- | ------------------------------------------------------------------------------------------------------------- |
|
|
164
|
+
| Dataset contracts | `DataKind`, `Dataset`, `IfExists`, `DEDUP_KEYS`, `REQUIRED_COLUMNS`, `TIME_COLUMNS`, `KNOWN_MT5_TIME_COLUMNS` |
|
|
165
|
+
| Schema normalization | `normalize_dataframe`, `normalize_time_columns`, `schema_columns`, `validate_schema` |
|
|
166
|
+
| Export helpers | `detect_format`, `export_dataframe`, `export_dataframe_to_sqlite` |
|
|
167
|
+
| Symbol parsing | `normalize_symbol`, `normalize_symbols` |
|
|
168
|
+
| Time parsing | `ensure_utc`, `parse_date_range`, `parse_datetime`, `recent_window` |
|
|
169
|
+
| MT5 parsing maps | `granularity_name`, `parse_tick_flags`, `parse_timeframe`, `TICK_FLAG_MAP`, `TIMEFRAME_MAP` |
|
|
170
|
+
| Trading data shapes | `POSITION_COLUMNS` |
|
|
145
171
|
|
|
146
172
|
## CLI commands
|
|
147
173
|
|
|
@@ -190,7 +216,7 @@ their own adapter layer.
|
|
|
190
216
|
|
|
191
217
|
## Contract verification
|
|
192
218
|
|
|
193
|
-
`tests/test_contracts.py` asserts that every name in
|
|
194
|
-
importable from `mt5cli`, documents key closed-bar, rate-view,
|
|
195
|
-
account-resolution, and trading-session behaviors, and keeps the
|
|
196
|
-
aligned with `__all__`.
|
|
219
|
+
`tests/test_contracts.py` asserts that every name in the stable and secondary
|
|
220
|
+
tier sets is importable from `mt5cli`, documents key closed-bar, rate-view,
|
|
221
|
+
SQLite loading, account-resolution, and trading-session behaviors, and keeps the
|
|
222
|
+
tier sets aligned with `__all__`.
|
|
@@ -31,7 +31,7 @@ rates = collect_latest_rates_for_accounts_with_retries(
|
|
|
31
31
|
### Latest closed rate bars
|
|
32
32
|
|
|
33
33
|
MetaTrader 5 `start_pos=0` includes the still-forming current bar as the last
|
|
34
|
-
row. `fetch_latest_closed_rates()` handles one connected `
|
|
34
|
+
row. `fetch_latest_closed_rates()` handles one connected `MT5Client`; use
|
|
35
35
|
`fetch_latest_closed_rates_for_trading_client()` from an active
|
|
36
36
|
`Mt5TradingClient` session. Multi-account helpers fetch `count + 1` bars, drop
|
|
37
37
|
that row with `drop_forming_rate_bar()`, and validate each series is non-empty. Returned frames are ordered
|
|
@@ -170,5 +170,5 @@ resulting `ValueError` is suppressed along with other recoverable errors.
|
|
|
170
170
|
## Trading-capable sessions
|
|
171
171
|
|
|
172
172
|
For order placement and trading calculations, use the dedicated
|
|
173
|
-
[Trading module](trading.md).
|
|
174
|
-
|
|
173
|
+
[Trading module](trading.md). Use `mt5_session()` / `MT5Client` for read-only
|
|
174
|
+
collection.
|
|
@@ -31,7 +31,7 @@ finally:
|
|
|
31
31
|
`login` accepts `int`, numeric `str`, or an empty string; empty strings are
|
|
32
32
|
treated as unset. `path`, `password`, `server`, and `timeout` are forwarded to
|
|
33
33
|
`pdmt5.Mt5Config`, and omitted `timeout` values keep the lower-level default.
|
|
34
|
-
|
|
34
|
+
Use `mt5_session()` / `MT5Client` for read-only data collection.
|
|
35
35
|
|
|
36
36
|
## State and order helpers
|
|
37
37
|
|
|
@@ -194,6 +194,6 @@ through the stable package root without embedding entry/exit policy.
|
|
|
194
194
|
| Local SL/TP price derivation | `determine_order_limits()` |
|
|
195
195
|
| Throttled SQLite history loop with ad-hoc error handling | `ThrottledHistoryUpdater(suppress_errors=True)` |
|
|
196
196
|
|
|
197
|
-
Keep read-only data collection on `mt5_session()` / `
|
|
197
|
+
Keep read-only data collection on `mt5_session()` / `MT5Client`; use
|
|
198
198
|
`mt5_trading_session()` only where order placement or trading calculations are
|
|
199
199
|
required.
|
|
@@ -29,7 +29,7 @@ pip install mt5cli
|
|
|
29
29
|
|
|
30
30
|
## Python API for downstream packages
|
|
31
31
|
|
|
32
|
-
Import `MT5Client` for generic MT5 data access, schema normalization, and optional order primitives.
|
|
32
|
+
Import `MT5Client` for generic MT5 data access, schema normalization, and optional order primitives.
|
|
33
33
|
|
|
34
34
|
```python
|
|
35
35
|
from datetime import UTC, datetime
|
|
@@ -11,7 +11,11 @@ from importlib.metadata import version
|
|
|
11
11
|
from pdmt5 import Mt5Config, Mt5RuntimeError, Mt5TradingClient, Mt5TradingError
|
|
12
12
|
|
|
13
13
|
from .client import MT5Client, build_config, mt5_session
|
|
14
|
-
from .contract import
|
|
14
|
+
from .contract import (
|
|
15
|
+
PUBLIC_EXPORT_TIERS,
|
|
16
|
+
SECONDARY_PUBLIC_EXPORTS,
|
|
17
|
+
STABLE_SDK_EXPORTS,
|
|
18
|
+
)
|
|
15
19
|
from .converters import (
|
|
16
20
|
ensure_utc,
|
|
17
21
|
granularity_name,
|
|
@@ -59,7 +63,6 @@ from .schemas import (
|
|
|
59
63
|
)
|
|
60
64
|
from .sdk import (
|
|
61
65
|
AccountSpec,
|
|
62
|
-
Mt5CliClient,
|
|
63
66
|
ThrottledHistoryUpdater,
|
|
64
67
|
account_info,
|
|
65
68
|
collect_history,
|
|
@@ -116,12 +119,16 @@ from .trading import (
|
|
|
116
119
|
OrderSide,
|
|
117
120
|
OrderTimeMode,
|
|
118
121
|
PositionSide,
|
|
122
|
+
calculate_account_projected_margin_ratio,
|
|
119
123
|
calculate_margin_and_volume,
|
|
120
124
|
calculate_new_position_margin_ratio,
|
|
121
125
|
calculate_positions_margin,
|
|
122
126
|
calculate_positions_margin_by_symbol,
|
|
123
127
|
calculate_positions_margin_safe,
|
|
128
|
+
calculate_projected_margin_ratio,
|
|
124
129
|
calculate_spread_ratio,
|
|
130
|
+
calculate_symbol_group_margin_ratio,
|
|
131
|
+
calculate_trailing_stop_updates,
|
|
125
132
|
calculate_volume_by_margin,
|
|
126
133
|
close_open_positions,
|
|
127
134
|
create_trading_client,
|
|
@@ -129,6 +136,7 @@ from .trading import (
|
|
|
129
136
|
determine_order_limits,
|
|
130
137
|
ensure_symbol_selected,
|
|
131
138
|
estimate_order_margin,
|
|
139
|
+
extract_tick_price,
|
|
132
140
|
fetch_latest_closed_rates_for_trading_client,
|
|
133
141
|
fetch_latest_closed_rates_indexed,
|
|
134
142
|
get_account_snapshot,
|
|
@@ -139,6 +147,7 @@ from .trading import (
|
|
|
139
147
|
normalize_order_volume,
|
|
140
148
|
place_market_order,
|
|
141
149
|
update_sltp_for_open_positions,
|
|
150
|
+
update_trailing_stop_loss_for_open_positions,
|
|
142
151
|
)
|
|
143
152
|
from .utils import (
|
|
144
153
|
TICK_FLAG_MAP,
|
|
@@ -154,7 +163,9 @@ __all__ = [
|
|
|
154
163
|
"DEDUP_KEYS",
|
|
155
164
|
"KNOWN_MT5_TIME_COLUMNS",
|
|
156
165
|
"POSITION_COLUMNS",
|
|
166
|
+
"PUBLIC_EXPORT_TIERS",
|
|
157
167
|
"REQUIRED_COLUMNS",
|
|
168
|
+
"SECONDARY_PUBLIC_EXPORTS",
|
|
158
169
|
"STABLE_SDK_EXPORTS",
|
|
159
170
|
"TICK_FLAG_MAP",
|
|
160
171
|
"TIMEFRAME_MAP",
|
|
@@ -166,7 +177,6 @@ __all__ = [
|
|
|
166
177
|
"IfExists",
|
|
167
178
|
"MT5Client",
|
|
168
179
|
"MarginVolume",
|
|
169
|
-
"Mt5CliClient",
|
|
170
180
|
"Mt5CliError",
|
|
171
181
|
"Mt5Config",
|
|
172
182
|
"Mt5ConnectionError",
|
|
@@ -187,12 +197,16 @@ __all__ = [
|
|
|
187
197
|
"build_config",
|
|
188
198
|
"build_rate_targets",
|
|
189
199
|
"build_rate_view_name",
|
|
200
|
+
"calculate_account_projected_margin_ratio",
|
|
190
201
|
"calculate_margin_and_volume",
|
|
191
202
|
"calculate_new_position_margin_ratio",
|
|
192
203
|
"calculate_positions_margin",
|
|
193
204
|
"calculate_positions_margin_by_symbol",
|
|
194
205
|
"calculate_positions_margin_safe",
|
|
206
|
+
"calculate_projected_margin_ratio",
|
|
195
207
|
"calculate_spread_ratio",
|
|
208
|
+
"calculate_symbol_group_margin_ratio",
|
|
209
|
+
"calculate_trailing_stop_updates",
|
|
196
210
|
"calculate_volume_by_margin",
|
|
197
211
|
"call_with_normalized_errors",
|
|
198
212
|
"close_open_positions",
|
|
@@ -217,6 +231,7 @@ __all__ = [
|
|
|
217
231
|
"estimate_order_margin",
|
|
218
232
|
"export_dataframe",
|
|
219
233
|
"export_dataframe_to_sqlite",
|
|
234
|
+
"extract_tick_price",
|
|
220
235
|
"fetch_latest_closed_rates",
|
|
221
236
|
"fetch_latest_closed_rates_for_trading_client",
|
|
222
237
|
"fetch_latest_closed_rates_indexed",
|
|
@@ -275,5 +290,6 @@ __all__ = [
|
|
|
275
290
|
"update_history",
|
|
276
291
|
"update_history_with_config",
|
|
277
292
|
"update_sltp_for_open_positions",
|
|
293
|
+
"update_trailing_stop_loss_for_open_positions",
|
|
278
294
|
"validate_schema",
|
|
279
295
|
]
|
|
@@ -24,9 +24,7 @@ class MT5Client(Mt5CliClient):
|
|
|
24
24
|
"""Public client for generic MT5 data access and order primitives.
|
|
25
25
|
|
|
26
26
|
Extends the read-only SDK client with optional order check/send helpers and
|
|
27
|
-
exposes the same connection lifecycle as :
|
|
28
|
-
Downstream applications such as private trading packages should prefer this
|
|
29
|
-
type over the legacy ``Mt5CliClient`` name.
|
|
27
|
+
exposes the same connection lifecycle as :func:`mt5_session`.
|
|
30
28
|
|
|
31
29
|
mt5cli intentionally exposes minimal execution primitives only. Trading
|
|
32
30
|
decisions, signals, strategies, backtests, and optimization remain the
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""Downstream SDK export tiers for mt5cli."""
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
STABLE_SDK_EXPORTS: frozenset[str] = frozenset({
|
|
6
6
|
"AccountSpec",
|
|
7
7
|
"MT5Client",
|
|
8
|
-
"Mt5CliClient",
|
|
9
8
|
"Mt5CliError",
|
|
10
9
|
"Mt5Config",
|
|
11
10
|
"Mt5ConnectionError",
|
|
@@ -24,38 +23,33 @@ STABLE_SDK_EXPORTS: frozenset[str] = frozenset({
|
|
|
24
23
|
"OrderLimits",
|
|
25
24
|
"RateTarget",
|
|
26
25
|
"ThrottledHistoryUpdater",
|
|
27
|
-
"account_info",
|
|
28
26
|
"build_config",
|
|
29
27
|
"build_rate_targets",
|
|
30
28
|
"build_rate_view_name",
|
|
29
|
+
"calculate_account_projected_margin_ratio",
|
|
31
30
|
"calculate_margin_and_volume",
|
|
32
31
|
"calculate_new_position_margin_ratio",
|
|
32
|
+
"calculate_projected_margin_ratio",
|
|
33
33
|
"calculate_positions_margin",
|
|
34
34
|
"calculate_positions_margin_by_symbol",
|
|
35
35
|
"calculate_positions_margin_safe",
|
|
36
36
|
"calculate_spread_ratio",
|
|
37
|
+
"calculate_symbol_group_margin_ratio",
|
|
38
|
+
"calculate_trailing_stop_updates",
|
|
37
39
|
"calculate_volume_by_margin",
|
|
38
40
|
"call_with_normalized_errors",
|
|
39
41
|
"close_open_positions",
|
|
40
42
|
"collect_history",
|
|
41
43
|
"collect_latest_closed_rates_by_granularity",
|
|
42
44
|
"collect_latest_closed_rates_for_accounts",
|
|
43
|
-
"collect_latest_rates",
|
|
44
|
-
"collect_latest_rates_for_accounts",
|
|
45
45
|
"collect_latest_rates_for_accounts_with_retries",
|
|
46
|
-
"copy_rates_from",
|
|
47
|
-
"copy_rates_from_pos",
|
|
48
|
-
"copy_rates_range",
|
|
49
|
-
"copy_ticks_from",
|
|
50
|
-
"copy_ticks_range",
|
|
51
46
|
"create_trading_client",
|
|
52
47
|
"detect_position_side",
|
|
53
48
|
"determine_order_limits",
|
|
54
49
|
"drop_forming_rate_bar",
|
|
55
50
|
"ensure_symbol_selected",
|
|
56
51
|
"estimate_order_margin",
|
|
57
|
-
"
|
|
58
|
-
"export_dataframe_to_sqlite",
|
|
52
|
+
"extract_tick_price",
|
|
59
53
|
"fetch_latest_closed_rates",
|
|
60
54
|
"fetch_latest_closed_rates_for_trading_client",
|
|
61
55
|
"fetch_latest_closed_rates_indexed",
|
|
@@ -63,29 +57,16 @@ STABLE_SDK_EXPORTS: frozenset[str] = frozenset({
|
|
|
63
57
|
"get_positions_frame",
|
|
64
58
|
"get_symbol_snapshot",
|
|
65
59
|
"get_tick_snapshot",
|
|
66
|
-
"history_deals",
|
|
67
|
-
"history_orders",
|
|
68
60
|
"is_recoverable_mt5_error",
|
|
69
|
-
"last_error",
|
|
70
|
-
"latest_rates",
|
|
71
61
|
"load_rate_data",
|
|
72
62
|
"load_rate_data_from_connection",
|
|
73
63
|
"load_rate_series_by_granularity",
|
|
74
64
|
"load_rate_series_from_sqlite",
|
|
75
|
-
"market_book",
|
|
76
|
-
"minimum_margins",
|
|
77
65
|
"mt5_session",
|
|
78
|
-
"mt5_summary",
|
|
79
|
-
"mt5_summary_as_df",
|
|
80
66
|
"mt5_trading_session",
|
|
81
|
-
"mt5_version",
|
|
82
67
|
"normalize_mt5_exception",
|
|
83
68
|
"normalize_order_volume",
|
|
84
|
-
"orders",
|
|
85
69
|
"place_market_order",
|
|
86
|
-
"positions",
|
|
87
|
-
"recent_history_deals",
|
|
88
|
-
"recent_ticks",
|
|
89
70
|
"resolve_account_spec",
|
|
90
71
|
"resolve_account_specs",
|
|
91
72
|
"resolve_history_datasets",
|
|
@@ -96,13 +77,73 @@ STABLE_SDK_EXPORTS: frozenset[str] = frozenset({
|
|
|
96
77
|
"resolve_rate_view_name",
|
|
97
78
|
"resolve_rate_view_names",
|
|
98
79
|
"substitute_env_placeholders",
|
|
80
|
+
"update_history",
|
|
81
|
+
"update_history_with_config",
|
|
82
|
+
"update_sltp_for_open_positions",
|
|
83
|
+
"update_trailing_stop_loss_for_open_positions",
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
SECONDARY_PUBLIC_EXPORTS: frozenset[str] = frozenset({
|
|
87
|
+
"DEDUP_KEYS",
|
|
88
|
+
"DataKind",
|
|
89
|
+
"Dataset",
|
|
90
|
+
"IfExists",
|
|
91
|
+
"KNOWN_MT5_TIME_COLUMNS",
|
|
92
|
+
"POSITION_COLUMNS",
|
|
93
|
+
"REQUIRED_COLUMNS",
|
|
94
|
+
"TICK_FLAG_MAP",
|
|
95
|
+
"TIMEFRAME_MAP",
|
|
96
|
+
"TIME_COLUMNS",
|
|
97
|
+
"account_info",
|
|
98
|
+
"collect_latest_rates",
|
|
99
|
+
"collect_latest_rates_for_accounts",
|
|
100
|
+
"copy_rates_from",
|
|
101
|
+
"copy_rates_from_pos",
|
|
102
|
+
"copy_rates_range",
|
|
103
|
+
"copy_ticks_from",
|
|
104
|
+
"copy_ticks_range",
|
|
105
|
+
"detect_format",
|
|
106
|
+
"ensure_utc",
|
|
107
|
+
"export_dataframe",
|
|
108
|
+
"export_dataframe_to_sqlite",
|
|
109
|
+
"granularity_name",
|
|
110
|
+
"history_deals",
|
|
111
|
+
"history_orders",
|
|
112
|
+
"last_error",
|
|
113
|
+
"latest_rates",
|
|
114
|
+
"market_book",
|
|
115
|
+
"minimum_margins",
|
|
116
|
+
"mt5_summary",
|
|
117
|
+
"mt5_summary_as_df",
|
|
118
|
+
"mt5_version",
|
|
119
|
+
"normalize_dataframe",
|
|
120
|
+
"normalize_symbol",
|
|
121
|
+
"normalize_symbols",
|
|
122
|
+
"normalize_time_columns",
|
|
123
|
+
"orders",
|
|
124
|
+
"parse_date_range",
|
|
125
|
+
"parse_datetime",
|
|
126
|
+
"parse_tick_flags",
|
|
127
|
+
"parse_timeframe",
|
|
128
|
+
"positions",
|
|
129
|
+
"recent_history_deals",
|
|
130
|
+
"recent_ticks",
|
|
131
|
+
"recent_window",
|
|
132
|
+
"schema_columns",
|
|
99
133
|
"symbol_info",
|
|
100
134
|
"symbol_info_tick",
|
|
101
135
|
"symbols",
|
|
102
136
|
"terminal_info",
|
|
103
|
-
"
|
|
104
|
-
"update_history_with_config",
|
|
105
|
-
"update_sltp_for_open_positions",
|
|
137
|
+
"validate_schema",
|
|
106
138
|
})
|
|
107
139
|
|
|
108
|
-
|
|
140
|
+
PUBLIC_EXPORT_TIERS: dict[str, frozenset[str]] = {
|
|
141
|
+
"stable": STABLE_SDK_EXPORTS,
|
|
142
|
+
"secondary": SECONDARY_PUBLIC_EXPORTS,
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
__all__ = [
|
|
146
|
+
"PUBLIC_EXPORT_TIERS",
|
|
147
|
+
"SECONDARY_PUBLIC_EXPORTS",
|
|
148
|
+
"STABLE_SDK_EXPORTS",
|
|
149
|
+
]
|