mt5api 0.0.3__tar.gz → 0.1.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 (117) hide show
  1. mt5api-0.1.0/AGENTS.md +92 -0
  2. {mt5api-0.0.3 → mt5api-0.1.0}/PKG-INFO +66 -22
  3. mt5api-0.1.0/README.md +141 -0
  4. {mt5api-0.0.3 → mt5api-0.1.0}/docs/api/deployment.md +3 -4
  5. {mt5api-0.0.3 → mt5api-0.1.0}/docs/api/index.md +19 -7
  6. mt5api-0.1.0/docs/api/rest-api.md +317 -0
  7. {mt5api-0.0.3 → mt5api-0.1.0}/docs/index.md +15 -9
  8. {mt5api-0.0.3 → mt5api-0.1.0}/mt5api/config.py +38 -30
  9. mt5api-0.1.0/mt5api/constants.py +32 -0
  10. {mt5api-0.0.3 → mt5api-0.1.0}/mt5api/main.py +47 -29
  11. {mt5api-0.0.3 → mt5api-0.1.0}/mt5api/middleware.py +1 -54
  12. {mt5api-0.0.3 → mt5api-0.1.0}/mt5api/models.py +306 -2
  13. mt5api-0.1.0/mt5api/routers/__init__.py +7 -0
  14. mt5api-0.1.0/mt5api/routers/calc.py +87 -0
  15. {mt5api-0.0.3 → mt5api-0.1.0}/mt5api/routers/health.py +24 -0
  16. {mt5api-0.0.3 → mt5api-0.1.0}/mt5api/routers/history.py +87 -0
  17. {mt5api-0.0.3 → mt5api-0.1.0}/mt5api/routers/market.py +2 -2
  18. {mt5api-0.0.3 → mt5api-0.1.0}/mt5api/routers/symbols.py +19 -0
  19. mt5api-0.1.0/mt5api/routers/trading.py +218 -0
  20. {mt5api-0.0.3 → mt5api-0.1.0}/pyproject.toml +1 -2
  21. {mt5api-0.0.3 → mt5api-0.1.0}/specs/041-mt5-rest-api/quickstart.md +5 -5
  22. {mt5api-0.0.3 → mt5api-0.1.0}/tests/conftest.py +48 -5
  23. {mt5api-0.0.3 → mt5api-0.1.0}/tests/mt5_constants.py +8 -0
  24. {mt5api-0.0.3 → mt5api-0.1.0}/tests/test_auth.py +45 -1
  25. mt5api-0.1.0/tests/test_calc.py +296 -0
  26. {mt5api-0.0.3 → mt5api-0.1.0}/tests/test_cli.py +12 -12
  27. {mt5api-0.0.3 → mt5api-0.1.0}/tests/test_config.py +47 -32
  28. {mt5api-0.0.3 → mt5api-0.1.0}/tests/test_health.py +34 -0
  29. mt5api-0.1.0/tests/test_history.py +394 -0
  30. mt5api-0.1.0/tests/test_main.py +158 -0
  31. {mt5api-0.0.3 → mt5api-0.1.0}/tests/test_middleware.py +0 -24
  32. mt5api-0.1.0/tests/test_models.py +243 -0
  33. {mt5api-0.0.3 → mt5api-0.1.0}/tests/test_symbols.py +31 -0
  34. mt5api-0.1.0/tests/test_trading.py +523 -0
  35. {mt5api-0.0.3 → mt5api-0.1.0}/uv.lock +4 -101
  36. mt5api-0.0.3/AGENTS.md +0 -65
  37. mt5api-0.0.3/README.md +0 -96
  38. mt5api-0.0.3/docs/api/rest-api.md +0 -212
  39. mt5api-0.0.3/mt5api/constants.py +0 -31
  40. mt5api-0.0.3/mt5api/routers/__init__.py +0 -7
  41. mt5api-0.0.3/tests/test_history.py +0 -195
  42. mt5api-0.0.3/tests/test_main.py +0 -59
  43. mt5api-0.0.3/tests/test_models.py +0 -113
  44. {mt5api-0.0.3 → mt5api-0.1.0}/.agents/skills/local-qa/SKILL.md +0 -0
  45. {mt5api-0.0.3 → mt5api-0.1.0}/.agents/skills/local-qa/scripts/qa.sh +0 -0
  46. {mt5api-0.0.3 → mt5api-0.1.0}/.agents/skills/mt5api/SKILL.md +0 -0
  47. {mt5api-0.0.3 → mt5api-0.1.0}/.agents/skills/speckit-analyze/SKILL.md +0 -0
  48. {mt5api-0.0.3 → mt5api-0.1.0}/.agents/skills/speckit-checklist/SKILL.md +0 -0
  49. {mt5api-0.0.3 → mt5api-0.1.0}/.agents/skills/speckit-clarify/SKILL.md +0 -0
  50. {mt5api-0.0.3 → mt5api-0.1.0}/.agents/skills/speckit-constitution/SKILL.md +0 -0
  51. {mt5api-0.0.3 → mt5api-0.1.0}/.agents/skills/speckit-implement/SKILL.md +0 -0
  52. {mt5api-0.0.3 → mt5api-0.1.0}/.agents/skills/speckit-plan/SKILL.md +0 -0
  53. {mt5api-0.0.3 → mt5api-0.1.0}/.agents/skills/speckit-specify/SKILL.md +0 -0
  54. {mt5api-0.0.3 → mt5api-0.1.0}/.agents/skills/speckit-tasks/SKILL.md +0 -0
  55. {mt5api-0.0.3 → mt5api-0.1.0}/.agents/skills/speckit-taskstoissues/SKILL.md +0 -0
  56. {mt5api-0.0.3 → mt5api-0.1.0}/.claude/agents/codex.md +0 -0
  57. {mt5api-0.0.3 → mt5api-0.1.0}/.claude/agents/copilot.md +0 -0
  58. {mt5api-0.0.3 → mt5api-0.1.0}/.claude/commands/speckit.analyze.md +0 -0
  59. {mt5api-0.0.3 → mt5api-0.1.0}/.claude/commands/speckit.checklist.md +0 -0
  60. {mt5api-0.0.3 → mt5api-0.1.0}/.claude/commands/speckit.clarify.md +0 -0
  61. {mt5api-0.0.3 → mt5api-0.1.0}/.claude/commands/speckit.constitution.md +0 -0
  62. {mt5api-0.0.3 → mt5api-0.1.0}/.claude/commands/speckit.implement.md +0 -0
  63. {mt5api-0.0.3 → mt5api-0.1.0}/.claude/commands/speckit.plan.md +0 -0
  64. {mt5api-0.0.3 → mt5api-0.1.0}/.claude/commands/speckit.specify.md +0 -0
  65. {mt5api-0.0.3 → mt5api-0.1.0}/.claude/commands/speckit.tasks.md +0 -0
  66. {mt5api-0.0.3 → mt5api-0.1.0}/.claude/commands/speckit.taskstoissues.md +0 -0
  67. {mt5api-0.0.3 → mt5api-0.1.0}/.claude/settings.json +0 -0
  68. {mt5api-0.0.3 → mt5api-0.1.0}/.github/FUNDING.yml +0 -0
  69. {mt5api-0.0.3 → mt5api-0.1.0}/.github/dependabot.yml +0 -0
  70. {mt5api-0.0.3 → mt5api-0.1.0}/.github/renovate.json +0 -0
  71. {mt5api-0.0.3 → mt5api-0.1.0}/.github/workflows/ci.yml +0 -0
  72. {mt5api-0.0.3 → mt5api-0.1.0}/.github/workflows/claude-code.yml +0 -0
  73. {mt5api-0.0.3 → mt5api-0.1.0}/.gitignore +0 -0
  74. {mt5api-0.0.3 → mt5api-0.1.0}/.specify/memory/constitution.md +0 -0
  75. {mt5api-0.0.3 → mt5api-0.1.0}/.specify/scripts/bash/check-prerequisites.sh +0 -0
  76. {mt5api-0.0.3 → mt5api-0.1.0}/.specify/scripts/bash/common.sh +0 -0
  77. {mt5api-0.0.3 → mt5api-0.1.0}/.specify/scripts/bash/create-new-feature.sh +0 -0
  78. {mt5api-0.0.3 → mt5api-0.1.0}/.specify/scripts/bash/setup-plan.sh +0 -0
  79. {mt5api-0.0.3 → mt5api-0.1.0}/.specify/scripts/bash/update-agent-context.sh +0 -0
  80. {mt5api-0.0.3 → mt5api-0.1.0}/.specify/templates/agent-file-template.md +0 -0
  81. {mt5api-0.0.3 → mt5api-0.1.0}/.specify/templates/checklist-template.md +0 -0
  82. {mt5api-0.0.3 → mt5api-0.1.0}/.specify/templates/plan-template.md +0 -0
  83. {mt5api-0.0.3 → mt5api-0.1.0}/.specify/templates/spec-template.md +0 -0
  84. {mt5api-0.0.3 → mt5api-0.1.0}/.specify/templates/tasks-template.md +0 -0
  85. {mt5api-0.0.3 → mt5api-0.1.0}/CLAUDE.md +0 -0
  86. {mt5api-0.0.3 → mt5api-0.1.0}/LICENSE +0 -0
  87. {mt5api-0.0.3 → mt5api-0.1.0}/mkdocs.yml +0 -0
  88. {mt5api-0.0.3 → mt5api-0.1.0}/mt5api/__init__.py +0 -0
  89. {mt5api-0.0.3 → mt5api-0.1.0}/mt5api/__main__.py +0 -0
  90. {mt5api-0.0.3 → mt5api-0.1.0}/mt5api/auth.py +0 -0
  91. {mt5api-0.0.3 → mt5api-0.1.0}/mt5api/dependencies.py +0 -0
  92. {mt5api-0.0.3 → mt5api-0.1.0}/mt5api/formatters.py +0 -0
  93. {mt5api-0.0.3 → mt5api-0.1.0}/mt5api/routers/account.py +0 -0
  94. {mt5api-0.0.3 → mt5api-0.1.0}/specs/041-mt5-rest-api/contracts/openapi.yaml +0 -0
  95. {mt5api-0.0.3 → mt5api-0.1.0}/specs/041-mt5-rest-api/data-model.md +0 -0
  96. {mt5api-0.0.3 → mt5api-0.1.0}/specs/041-mt5-rest-api/plan.md +0 -0
  97. {mt5api-0.0.3 → mt5api-0.1.0}/specs/041-mt5-rest-api/research.md +0 -0
  98. {mt5api-0.0.3 → mt5api-0.1.0}/specs/041-mt5-rest-api/spec.md +0 -0
  99. {mt5api-0.0.3 → mt5api-0.1.0}/specs/041-mt5-rest-api/tasks.md +0 -0
  100. {mt5api-0.0.3 → mt5api-0.1.0}/specs/042-mt5-core-client/spec.md +0 -0
  101. {mt5api-0.0.3 → mt5api-0.1.0}/specs/043-mt5-dataframe-client/spec.md +0 -0
  102. {mt5api-0.0.3 → mt5api-0.1.0}/specs/044-mt5-trading-client/spec.md +0 -0
  103. {mt5api-0.0.3 → mt5api-0.1.0}/specs/045-mt5-utils/spec.md +0 -0
  104. {mt5api-0.0.3 → mt5api-0.1.0}/specs/046-mt5-api-service/spec.md +0 -0
  105. {mt5api-0.0.3 → mt5api-0.1.0}/specs/047-api-auth-rate-limit/spec.md +0 -0
  106. {mt5api-0.0.3 → mt5api-0.1.0}/specs/048-api-response-format/spec.md +0 -0
  107. {mt5api-0.0.3 → mt5api-0.1.0}/specs/049-api-routing-models/spec.md +0 -0
  108. {mt5api-0.0.3 → mt5api-0.1.0}/specs/050-api-runtime-deploy/spec.md +0 -0
  109. {mt5api-0.0.3 → mt5api-0.1.0}/specs/051-api-error-logging/spec.md +0 -0
  110. {mt5api-0.0.3 → mt5api-0.1.0}/specs/052-api-dependencies/spec.md +0 -0
  111. {mt5api-0.0.3 → mt5api-0.1.0}/tests/__init__.py +0 -0
  112. {mt5api-0.0.3 → mt5api-0.1.0}/tests/test_account.py +0 -0
  113. {mt5api-0.0.3 → mt5api-0.1.0}/tests/test_dependencies.py +0 -0
  114. {mt5api-0.0.3 → mt5api-0.1.0}/tests/test_formatters.py +0 -0
  115. {mt5api-0.0.3 → mt5api-0.1.0}/tests/test_integration.py +0 -0
  116. {mt5api-0.0.3 → mt5api-0.1.0}/tests/test_lifespan.py +0 -0
  117. {mt5api-0.0.3 → mt5api-0.1.0}/tests/test_market.py +0 -0
mt5api-0.1.0/AGENTS.md ADDED
@@ -0,0 +1,92 @@
1
+ # Repository Guidelines
2
+
3
+ ## Commands
4
+
5
+ ### Development Setup
6
+
7
+ - `uv sync --dev` installs runtime and development dependencies.
8
+ - `uv run uvicorn mt5api.main:app --host 0.0.0.0 --port 8000` starts the API locally.
9
+ - `uv run python -m mt5api` starts the app using `MT5API_HOST`, `MT5API_PORT`, and `MT5API_LOG_LEVEL`.
10
+ - `uv run mkdocs serve` previews the documentation site.
11
+
12
+ ### Code Quality and Documentation
13
+
14
+ **Important**: Run these before committing or creating a PR.
15
+
16
+ 1. **Format, lint, and test**: Use `local-qa` skill.
17
+ 2. **Documentation build**: Run `uv run mkdocs build` when documentation, configuration, or OpenAPI-visible behavior changes.
18
+
19
+ ## Architecture
20
+
21
+ ### Runtime Constraints
22
+
23
+ - The API server must run on Windows with a logged-in MetaTrader 5 terminal.
24
+ - Linux and macOS are for HTTP clients, documentation work, and local non-runtime development only.
25
+
26
+ ### Key Dependencies
27
+
28
+ - `pdmt5`: Underlying MetaTrader 5 client integration used by the API layer.
29
+ - `fastapi`: HTTP application framework and OpenAPI generation.
30
+
31
+ ### Package Structure
32
+
33
+ - `mt5api/`: Main FastAPI package.
34
+ - `main.py`: App wiring, lifespan handling, middleware, and router registration.
35
+ - `__main__.py`: CLI entry point for launching the API from environment-backed configuration.
36
+ - `config.py`: Environment-backed configuration normalization and validation.
37
+ - `auth.py`: Optional API key authentication helpers.
38
+ - `dependencies.py`: Shared endpoint dependencies and MT5 accessors.
39
+ - `formatters.py`: JSON and Parquet response formatting helpers.
40
+ - `models.py`: Pydantic API models.
41
+ - `middleware.py`: Request logging and error handling.
42
+ - `constants.py`: Shared constants and environment variable names.
43
+ - `routers/`: Endpoint groups for operations: `health.py`, `symbols.py`, `market.py`, `account.py`, `history.py`, `calc.py`, and `trading.py`.
44
+ - `tests/`: Pytest suite covering API behavior, configuration, middleware, and CLI entry points.
45
+ - `docs/`: MkDocs documentation, including REST API and deployment guidance.
46
+
47
+ ## Quality Standards
48
+
49
+ - Type hints required (pyright strict mode)
50
+ - Comprehensive linting with 35+ rule categories (ruff)
51
+ - Test coverage tracking with 100% (pytest-cov)
52
+ - Parametrized tests for input/result matrices using `pytest.mark.parametrize` (pytest)
53
+ - Test doubles (mocks, stubs) using `pytest_mock` for external dependencies (pytest-mock)
54
+ - Pydantic models for data validation and configuration
55
+
56
+ ## Documentation Workflow
57
+
58
+ 1. Update docstrings and docs when behavior, configuration, or OpenAPI-visible output changes.
59
+ 2. Local preview: `uv run mkdocs serve`
60
+ 3. Build validation: `uv run mkdocs build`
61
+
62
+ ## Commit & Pull Request Guidelines
63
+
64
+ - Run QA using `local-qa` skill before committing or creating a PR.
65
+ - Branch names use appropriate prefixes on creation (e.g., `feature/...`, `bugfix/...`, `refactor/...`, `docs/...`, `chore/...`).
66
+ - When instructed to create a PR, create it as a draft with appropriate labels by default.
67
+
68
+ ## Security & Configuration Tips
69
+
70
+ - Do not commit real MT5 credentials or API keys.
71
+ - Configure `MT5API_SECRET_KEY`, `MT5API_ROUTER_PREFIX`, and `MT5API_LOG_LEVEL` through environment variables.
72
+ - Keep runtime configuration in the environment instead of hardcoding deployment values.
73
+ - Authentication mode is fixed at process startup; cover both authenticated and unauthenticated behavior when changing auth-related code.
74
+
75
+ ## Code Design Principles
76
+
77
+ Always prefer the simplest design that works.
78
+
79
+ - **KISS**: Choose straightforward solutions and avoid unnecessary abstraction.
80
+ - **DRY**: Remove duplication when it improves clarity and maintainability.
81
+ - **YAGNI**: Do not add features, hooks, or flexibility until they are needed.
82
+ - **SOLID/Clean Code**: Apply these as tools only when they keep the design simpler and easier to change.
83
+
84
+ ## Development Methodology
85
+
86
+ Keep delivery incremental, test-backed, and easy to review.
87
+
88
+ - Make small, safe, reversible changes.
89
+ - Prefer `Red -> Green -> Refactor`.
90
+ - Do not mix feature work and refactoring in the same commit.
91
+ - Refactor when it improves clarity or removes real duplication (Rule of Three).
92
+ - Keep tests fast, focused, and self-validating.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mt5api
3
- Version: 0.0.3
3
+ Version: 0.1.0
4
4
  Summary: MetaTrader 5 REST API
5
5
  Project-URL: Repository, https://github.com/dceoy/mt5api.git
6
6
  Author-email: dceoy <dceoy@users.noreply.github.com>
@@ -24,7 +24,6 @@ Requires-Dist: pdmt5>=0.2.1
24
24
  Requires-Dist: pyarrow>=18.0.0
25
25
  Requires-Dist: python-jose[cryptography]>=3.3.0
26
26
  Requires-Dist: python-multipart>=0.0.9
27
- Requires-Dist: slowapi>=0.1.9
28
27
  Requires-Dist: uvicorn[standard]>=0.32.0
29
28
  Description-Content-Type: text/markdown
30
29
 
@@ -34,21 +33,49 @@ MetaTrader 5 REST API
34
33
 
35
34
  [![CI/CD](https://github.com/dceoy/mt5api/actions/workflows/ci.yml/badge.svg)](https://github.com/dceoy/mt5api/actions/workflows/ci.yml)
36
35
 
37
- mt5api exposes read-only MT5 market data, account info, and trading history
38
- over HTTP. It uses the `pdmt5` client internally and adds optional API-key
39
- auth, rate limiting, and JSON/Parquet response formatting.
36
+ mt5api exposes MT5 market data, account info, trading history, and trading
37
+ operations over HTTP. It uses the [`pdmt5`](https://github.com/dceoy/pdmt5)
38
+ client internally and adds optional API-key auth and JSON/Parquet response
39
+ formatting.
40
40
 
41
41
  The API server must run on Windows. The `MetaTrader5` Python package used by
42
42
  `pdmt5` is supported only on Windows, so you must host `mt5api` on a Windows
43
43
  machine with a logged-in MetaTrader 5 terminal. HTTP clients can connect from
44
44
  any operating system.
45
45
 
46
+ ## Architecture
47
+
48
+ ```mermaid
49
+ graph TB
50
+ Client["HTTP Client<br/>(Any OS)"]
51
+
52
+ subgraph "Windows Host"
53
+ subgraph "FastAPI Application"
54
+ Middleware["Middleware Stack<br/>Logging · Error Handler"]
55
+ Routers["Routers<br/>health · symbols · market · account · history · calc · trading"]
56
+ Auth["API Key Security Dependency<br/>Security(api_key_header) · verify_api_key"]
57
+ Deps["FastAPI Dependencies<br/>MT5 Client Singleton · Format Negotiation"]
58
+ Formatters["Response Formatters<br/>JSON · Parquet"]
59
+ Middleware --> Routers --> Deps --> Formatters
60
+ Auth -.-> Routers
61
+ Formatters --> Middleware
62
+ end
63
+
64
+ Deps --> pdmt5["pdmt5<br/>MT5 Client Library"]
65
+ pdmt5 --> MT5["MetaTrader 5<br/>Terminal"]
66
+ end
67
+
68
+ Client -- "HTTP/REST" --> Middleware
69
+ Middleware -- "JSON / Parquet" --> Client
70
+ ```
71
+
46
72
  ## Features
47
73
 
48
- - Read-only REST endpoints for symbols, market data, account info, orders, and history
74
+ - REST endpoints for symbols, market data, account info, orders, history,
75
+ calculations, and trading operations
49
76
  - JSON and Apache Parquet responses (content negotiation)
50
- - Optional API key authentication with per-minute rate limiting
51
- - Structured JSON logging and configurable CORS
77
+ - Optional API key authentication
78
+ - Structured JSON logging
52
79
  - OpenAPI/Swagger docs built into the API
53
80
 
54
81
  ## Requirements
@@ -72,7 +99,7 @@ uv sync
72
99
 
73
100
  ```powershell
74
101
  $env:MT5API_SECRET_KEY = "your-secret-api-key" # Optional: omit to disable auth
75
- $env:API_ROUTER_PREFIX = "/api/v1" # Optional: omit for root-level routes
102
+ $env:MT5API_ROUTER_PREFIX = "/api/v1" # Optional: omit for root-level routes
76
103
  uv run uvicorn mt5api.main:app --host 0.0.0.0 --port 8000
77
104
  ```
78
105
 
@@ -81,11 +108,14 @@ Docs:
81
108
  - Swagger UI: `http://localhost:8000/docs`
82
109
  - OpenAPI JSON: `http://localhost:8000/openapi.json`
83
110
 
84
- Set `API_ROUTER_PREFIX` to mount the read-only API endpoints under a shared path
85
- such as `/api/v1`. The default is `""`, which keeps routes like `/health` and
111
+ Set `MT5API_ROUTER_PREFIX` to mount the API endpoints under a shared path such
112
+ as `/api/v1`. The default is `""`, which keeps routes like `/health` and
86
113
  `/symbols` at the root. `"/api/v1"`, `"api/v1"`, and `"/api/v1/"` are treated
87
114
  the same.
88
115
 
116
+ Set `MT5API_MAX_MARKET_BOOK_SUBSCRIPTIONS` to cap active market-book
117
+ subscriptions. The default limit is `100`.
118
+
89
119
  ## Example Requests with curl
90
120
 
91
121
  Replace `windows-host` with the DNS name or IP address of the Windows machine
@@ -106,20 +136,34 @@ curl -H "X-API-Key: your-secret-api-key" "http://windows-host:8000/symbols?group
106
136
  curl -H "X-API-Key: your-secret-api-key" -H "Accept: application/parquet" "http://windows-host:8000/rates/from?symbol=EURUSD&timeframe=TIMEFRAME_M1&date_from=2024-01-01T00:00:00Z&count=100"
107
137
  ```
108
138
 
109
- Market-data endpoints accept MetaTrader 5 constants either by official name
110
- (`TIMEFRAME_M1`, `COPY_TICKS_ALL`) or by their integer value.
139
+ Market-data and calculation endpoints accept MetaTrader 5 constants either by
140
+ official name (`TIMEFRAME_M1`, `COPY_TICKS_ALL`, `ORDER_TYPE_BUY`) or by their
141
+ integer value.
142
+
143
+ ## Endpoints
144
+
145
+ If `MT5API_ROUTER_PREFIX` is set, prepend that value to every API route below.
146
+
147
+ ### Read-Only Endpoints
111
148
 
112
- ## Endpoints (Read-Only)
149
+ - Health: `GET /health`, `GET /version`, `GET /last-error`
150
+ - Symbols: `GET /symbols`, `GET /symbols/total`, `GET /symbols/{symbol}`,
151
+ `GET /symbols/{symbol}/tick`
152
+ - Market data: `GET /rates/from`, `GET /rates/from-pos`, `GET /rates/range`,
153
+ `GET /ticks/from`, `GET /ticks/range`, `GET /market-book/{symbol}`
154
+ - Calculations: `GET /calc/margin`, `GET /calc/profit`
155
+ - Account: `GET /account`, `GET /terminal`
156
+ - Trading state: `GET /positions`, `GET /positions/total`,
157
+ `GET /orders`, `GET /orders/total`
158
+ - History: `GET /history/orders`, `GET /history/orders/total`,
159
+ `GET /history/deals`, `GET /history/deals/total`
113
160
 
114
- - Health: `/health`, `/version`
115
- - Symbols: `/symbols`, `/symbols/{symbol}`, `/symbols/{symbol}/tick`
116
- - Market data: `/rates/from`, `/rates/from-pos`, `/rates/range`,
117
- `/ticks/from`, `/ticks/range`, `/market-book/{symbol}`
118
- - Account: `/account`, `/terminal`
119
- - Trading state: `/positions`, `/orders`
120
- - History: `/history/orders`, `/history/deals`
161
+ ### Operational Endpoints
121
162
 
122
- If `API_ROUTER_PREFIX` is set, prepend that value to every API route above.
163
+ - `POST /symbols/{symbol}/select` Show or hide symbol in MarketWatch
164
+ - `POST /market-book/{symbol}/subscribe` — Subscribe to DOM events
165
+ - `POST /market-book/{symbol}/unsubscribe` — Unsubscribe from DOM events
166
+ - `POST /order/check` — Validate a trade request without execution
123
167
 
124
168
  ## License
125
169
 
mt5api-0.1.0/README.md ADDED
@@ -0,0 +1,141 @@
1
+ # mt5api
2
+
3
+ MetaTrader 5 REST API
4
+
5
+ [![CI/CD](https://github.com/dceoy/mt5api/actions/workflows/ci.yml/badge.svg)](https://github.com/dceoy/mt5api/actions/workflows/ci.yml)
6
+
7
+ mt5api exposes MT5 market data, account info, trading history, and trading
8
+ operations over HTTP. It uses the [`pdmt5`](https://github.com/dceoy/pdmt5)
9
+ client internally and adds optional API-key auth and JSON/Parquet response
10
+ formatting.
11
+
12
+ The API server must run on Windows. The `MetaTrader5` Python package used by
13
+ `pdmt5` is supported only on Windows, so you must host `mt5api` on a Windows
14
+ machine with a logged-in MetaTrader 5 terminal. HTTP clients can connect from
15
+ any operating system.
16
+
17
+ ## Architecture
18
+
19
+ ```mermaid
20
+ graph TB
21
+ Client["HTTP Client<br/>(Any OS)"]
22
+
23
+ subgraph "Windows Host"
24
+ subgraph "FastAPI Application"
25
+ Middleware["Middleware Stack<br/>Logging · Error Handler"]
26
+ Routers["Routers<br/>health · symbols · market · account · history · calc · trading"]
27
+ Auth["API Key Security Dependency<br/>Security(api_key_header) · verify_api_key"]
28
+ Deps["FastAPI Dependencies<br/>MT5 Client Singleton · Format Negotiation"]
29
+ Formatters["Response Formatters<br/>JSON · Parquet"]
30
+ Middleware --> Routers --> Deps --> Formatters
31
+ Auth -.-> Routers
32
+ Formatters --> Middleware
33
+ end
34
+
35
+ Deps --> pdmt5["pdmt5<br/>MT5 Client Library"]
36
+ pdmt5 --> MT5["MetaTrader 5<br/>Terminal"]
37
+ end
38
+
39
+ Client -- "HTTP/REST" --> Middleware
40
+ Middleware -- "JSON / Parquet" --> Client
41
+ ```
42
+
43
+ ## Features
44
+
45
+ - REST endpoints for symbols, market data, account info, orders, history,
46
+ calculations, and trading operations
47
+ - JSON and Apache Parquet responses (content negotiation)
48
+ - Optional API key authentication
49
+ - Structured JSON logging
50
+ - OpenAPI/Swagger docs built into the API
51
+
52
+ ## Requirements
53
+
54
+ - Python 3.11+
55
+ - Windows host with MetaTrader 5 terminal installed and logged in
56
+ - Linux and macOS are not supported for the API server runtime, but they work
57
+ for HTTP clients
58
+
59
+ ## Installation
60
+
61
+ Install and run the API on the Windows machine where MetaTrader 5 is installed.
62
+
63
+ ```powershell
64
+ git clone https://github.com/dceoy/mt5api.git
65
+ cd mt5api
66
+ uv sync
67
+ ```
68
+
69
+ ## Running the API on Windows
70
+
71
+ ```powershell
72
+ $env:MT5API_SECRET_KEY = "your-secret-api-key" # Optional: omit to disable auth
73
+ $env:MT5API_ROUTER_PREFIX = "/api/v1" # Optional: omit for root-level routes
74
+ uv run uvicorn mt5api.main:app --host 0.0.0.0 --port 8000
75
+ ```
76
+
77
+ Docs:
78
+
79
+ - Swagger UI: `http://localhost:8000/docs`
80
+ - OpenAPI JSON: `http://localhost:8000/openapi.json`
81
+
82
+ Set `MT5API_ROUTER_PREFIX` to mount the API endpoints under a shared path such
83
+ as `/api/v1`. The default is `""`, which keeps routes like `/health` and
84
+ `/symbols` at the root. `"/api/v1"`, `"api/v1"`, and `"/api/v1/"` are treated
85
+ the same.
86
+
87
+ Set `MT5API_MAX_MARKET_BOOK_SUBSCRIPTIONS` to cap active market-book
88
+ subscriptions. The default limit is `100`.
89
+
90
+ ## Example Requests with curl
91
+
92
+ Replace `windows-host` with the DNS name or IP address of the Windows machine
93
+ running `mt5api`. If you run the request on that Windows host, `localhost` also
94
+ works. In PowerShell, use `curl.exe` if `curl` resolves to
95
+ `Invoke-WebRequest`.
96
+
97
+ ```console
98
+ curl "http://windows-host:8000/health"
99
+ ```
100
+
101
+ ```console
102
+ # Include X-API-Key only when MT5API_SECRET_KEY is configured on the server.
103
+ curl -H "X-API-Key: your-secret-api-key" "http://windows-host:8000/symbols?group=*USD*"
104
+ ```
105
+
106
+ ```console
107
+ curl -H "X-API-Key: your-secret-api-key" -H "Accept: application/parquet" "http://windows-host:8000/rates/from?symbol=EURUSD&timeframe=TIMEFRAME_M1&date_from=2024-01-01T00:00:00Z&count=100"
108
+ ```
109
+
110
+ Market-data and calculation endpoints accept MetaTrader 5 constants either by
111
+ official name (`TIMEFRAME_M1`, `COPY_TICKS_ALL`, `ORDER_TYPE_BUY`) or by their
112
+ integer value.
113
+
114
+ ## Endpoints
115
+
116
+ If `MT5API_ROUTER_PREFIX` is set, prepend that value to every API route below.
117
+
118
+ ### Read-Only Endpoints
119
+
120
+ - Health: `GET /health`, `GET /version`, `GET /last-error`
121
+ - Symbols: `GET /symbols`, `GET /symbols/total`, `GET /symbols/{symbol}`,
122
+ `GET /symbols/{symbol}/tick`
123
+ - Market data: `GET /rates/from`, `GET /rates/from-pos`, `GET /rates/range`,
124
+ `GET /ticks/from`, `GET /ticks/range`, `GET /market-book/{symbol}`
125
+ - Calculations: `GET /calc/margin`, `GET /calc/profit`
126
+ - Account: `GET /account`, `GET /terminal`
127
+ - Trading state: `GET /positions`, `GET /positions/total`,
128
+ `GET /orders`, `GET /orders/total`
129
+ - History: `GET /history/orders`, `GET /history/orders/total`,
130
+ `GET /history/deals`, `GET /history/deals/total`
131
+
132
+ ### Operational Endpoints
133
+
134
+ - `POST /symbols/{symbol}/select` — Show or hide symbol in MarketWatch
135
+ - `POST /market-book/{symbol}/subscribe` — Subscribe to DOM events
136
+ - `POST /market-book/{symbol}/unsubscribe` — Unsubscribe from DOM events
137
+ - `POST /order/check` — Validate a trade request without execution
138
+
139
+ ## License
140
+
141
+ MIT License - see [LICENSE](https://github.com/dceoy/mt5api/blob/main/LICENSE).
@@ -35,10 +35,9 @@ nssm install mt5api
35
35
  ```
36
36
  # Optional: set MT5API_SECRET_KEY only when you want to require X-API-Key headers.
37
37
  MT5API_SECRET_KEY=your-secret-api-key
38
- API_LOG_LEVEL=INFO
39
- API_RATE_LIMIT=100
40
- API_CORS_ORIGINS=*
41
- API_ROUTER_PREFIX=/api/v1
38
+ MT5API_LOG_LEVEL=INFO
39
+ MT5API_MAX_MARKET_BOOK_SUBSCRIPTIONS=100
40
+ MT5API_ROUTER_PREFIX=/api/v1
42
41
  ```
43
42
 
44
43
  6. Start the service:
@@ -10,8 +10,9 @@ logged in. Clients can call the HTTP API from any operating system.
10
10
 
11
11
  ### [REST API](rest-api.md)
12
12
 
13
- FastAPI-based REST API that exposes read-only MT5 data over HTTP with JSON and
14
- Parquet support.
13
+ FastAPI-based REST API that exposes MT5 market data, account info, trading
14
+ history, calculations, and non-executing operational endpoints over HTTP with
15
+ JSON and Parquet support.
15
16
 
16
17
  ### [Deployment](deployment.md)
17
18
 
@@ -21,10 +22,21 @@ Windows service deployment guide for hosting the REST API alongside MetaTrader 5
21
22
 
22
23
  mt5api provides a FastAPI layer on top of the MetaTrader 5 terminal runtime:
23
24
 
24
- 1. **API Layer** (`mt5api.main`, `mt5api.routers`): FastAPI app, routers, and response formatting
25
- 2. **Dependency Layer** (`mt5api.dependencies`): MT5 client lifecycle and format negotiation
26
- 3. **Model Layer** (`mt5api.models`): Response schemas and MT5 constant metadata helpers
27
- 4. **Formatter Layer** (`mt5api.formatters`): JSON and Parquet serialization helpers
25
+ 1. **API Layer** (`mt5api.main`, `mt5api.routers`): FastAPI app, routers, and
26
+ response formatting. Routers are grouped by domain:
27
+ - `health.py`: health check, version, last error
28
+ - `symbols.py`: symbol listing, details, tick, and total
29
+ - `market.py`: OHLCV rates, tick data, and market depth
30
+ - `calc.py`: margin and profit calculations
31
+ - `account.py`: account and terminal info
32
+ - `history.py`: positions, orders, history with totals
33
+ - `trading.py`: order check, symbol selection, market book subscriptions
34
+ 2. **Dependency Layer** (`mt5api.dependencies`): MT5 client lifecycle and
35
+ format negotiation
36
+ 3. **Model Layer** (`mt5api.models`): Response schemas and MT5 constant
37
+ metadata helpers (TIMEFRAME, COPY_TICKS, ORDER_TYPE)
38
+ 4. **Formatter Layer** (`mt5api.formatters`): JSON and Parquet serialization
39
+ helpers
28
40
 
29
41
  ## Usage Guidelines
30
42
 
@@ -38,7 +50,7 @@ mt5api provides a FastAPI layer on top of the MetaTrader 5 terminal runtime:
38
50
 
39
51
  ```powershell
40
52
  $env:MT5API_SECRET_KEY = "your-secret-api-key" # Optional: omit to disable auth
41
- $env:API_ROUTER_PREFIX = "/api/v1" # Optional: omit for root-level routes
53
+ $env:MT5API_ROUTER_PREFIX = "/api/v1" # Optional: omit for root-level routes
42
54
  uv run uvicorn mt5api.main:app --host 0.0.0.0 --port 8000
43
55
  ```
44
56