41 Commits

Author SHA1 Message Date
Devid HW 3ad67d4c57 docs: fix inaccuracies found during documentation accuracy review
- CHANGELOG: inactivity_kill_secs default is disabled (None), not 120
- TROUBLESHOOTING: clarify inactivity_kill_secs is opt-in (must be
  passed explicitly); correct "default 120" claim
- MCP_TOOLS: inactivity_kill_secs is disabled by default; add note
  that Wine/macOS may not exit naturally even with ShutdownTerminal=1
- handlers/backtest.rs: remove stale comment claiming inactivity
  watchdog is "intentionally skipped when shutdown=true" — replaced
  with accurate description of the new 30s HTML-wait + kill behavior

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-25 09:14:44 +07:00
Devid HW 4c34ae236c docs: update CHANGELOG, TROUBLESHOOTING, MCP_TOOLS for v1.32.4
- CHANGELOG: add v1.32.4 entry with all fixes and verification results
- TROUBLESHOOTING: add Wine/macOS HTML report section (ShutdownTerminal=1
  does not exit terminal64.exe; inactivity kill + 30s HTML wait workaround)
- TROUBLESHOOTING: add list_deals 0 results section (binary restart required)
- MCP_TOOLS: add shutdown and inactivity_kill_secs params to launch_backtest
  schema (were implemented but undocumented)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-25 09:11:37 +07:00
Devid HW ecf5606b7a fix: HTML report extraction and backtest pipeline reliability
- fix: inactivity watchdog now waits 30s for HTML report then kills
  terminal64.exe unconditionally — ShutdownTerminal=1 does not cause
  MT5 to exit on Wine/macOS; relying on natural exit caused all runs
  to fall back to journal extraction
- fix: tester agent log deduplication — each deal is written twice in
  the log; use HashSet to skip already-seen deal numbers
- fix: position tracker for entry direction inference — infer "in"/"out"
  from per-symbol signed lot accumulation instead of guessing
- fix: is_closed_trade() no longer gates on profit!=0.0 — journal deals
  have profit=0.0 legitimately; the old gate hid all 140 deals from
  list_deals
- fix: log file selection priority — prefer Agent-127.0.0.1 over
  Agent-0.0.0.0 (startup-only log), tiebreak by file size
- feat: launch_backtest default shutdown=true; inactivity_kill_secs
  default 120s exposed as tool parameter
- feat: report DB stores deals in SQLite; analytics resolve by
  report_id / report_dir / latest

Verified: 1-month DPS21/XAUUSD.cent/M5 backtest produces full HTML
report with all metrics (win_rate=70%, profit_factor=0.77, sharpe=-3.61,
max_dd=6.93%) and all 17 analytics tools returning real data.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-25 08:53:17 +07:00
Devid 6bfb330b5d Update funding sources in FUNDING.yml 2026-04-23 01:44:31 +07:00
Devid 52a3d44b99 Add funding options for GitHub and PayPal
Updated funding options to include GitHub sponsor and custom PayPal link.
2026-04-23 01:42:55 +07:00
Devid caabc2c03b Change Dependabot update interval to monthly 2026-04-23 01:39:27 +07:00
Devid HW 62c1ca773e ci: publish to crates.io before GitHub release
Add cargo-publish job that runs in parallel with binary builds.
The release job now depends on [build-macos, build-linux, cargo-publish]
so the crate is always published to crates.io before the GitHub release
and binaries are attached.

Requires CARGO_REGISTRY_TOKEN secret in repo settings.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-23 01:30:23 +07:00
github-actions[bot] 3b9d4ce2dd ci: update server.json SHA256 for v1.32.3 [skip ci] 2026-04-22 18:28:53 +00:00
Devid HW 12fba8ad4b bump version to 1.32.3
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-23 01:26:08 +07:00
Devid HW b6171d0c56 fix: INI newline injection in backtest config generation
Add ini_safe() helper that strips CR/LF from user-supplied string params
before they are written into terminal.ini and backtest_config.ini.
Applies to: expert path, symbol, timeframe, set_file — any value that
could carry an embedded newline and inject extra INI directives.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-23 01:24:59 +07:00
Devid HW 05214ff34b revert version to 1.32.1 2026-04-23 01:15:27 +07:00
github-actions[bot] 498126dfd4 ci: update server.json SHA256 for v1.32.1 [skip ci] 2026-04-22 18:11:41 +00:00
Devid HW 147da213d6 bump version to 1.32.2 for crates.io republication 2026-04-23 01:11:14 +07:00
Devid HW 804c1f8808 fix: security review fixes — SQL injection, panic, and race condition
database.rs:
- search_by_tags: replace string-interpolated LIKE with parameterized ?N bindings
- search_by_notes: replace format!() SQL with prepared statement + ?1/?2 params
- search_by_set_file: same — use ?1 for pattern and ?2 for limit

backtest.rs (extract_and_store / monitor_backtest_completion):
- extract_and_store now returns bool — callers only delete report file if true
- guard both success paths with conditional delete + warning on failure
- replace .parent().unwrap() panic with explicit match + error log + graceful return

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-23 01:09:46 +07:00
Devid HW f8b6a11bb8 update Cargo.lock for version 1.32.1 2026-04-23 01:06:08 +07:00
Devid HW e82bb5466b bump version to 1.32.1 for crates.io republication
Added package.metadata section with maintenance status.
2026-04-23 01:06:08 +07:00
Devid HW 50ea45f8cd fix: suppress all compiler warnings
- mcp_server.rs: #[allow(dead_code)] on NotificationCallback type alias and get_notification_sender method
- handlers/mod.rs: #[allow(dead_code)] on past_complete_month helper
- Cargo.toml: move maintenance key into [package.metadata] (was invalid at package level)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-23 01:06:08 +07:00
Devid HW 4bc5ca22e9 fix: remove stale deals.csv references, add extract+store to launch_backtest
- FilePaths struct: remove deals_csv/deals_json fields (deals go to DB only)
- save_metadata: stop writing stale CSV/JSON paths to pipeline_metadata.json
- get_backtest_status: remove deals.csv artifact check (was unused in logic)
- get_backtest_crash_info: check DB deal count and metrics.json instead of deals.csv
- export_report: remove unused _deals_path variable
- launch_backtest: monitor_backtest_completion now calls extract_and_store when
  report found — extracts metrics+deals, registers report in DB, deletes htm file

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-23 01:06:08 +07:00
github-actions[bot] e44345a05f ci: update server.json SHA256 for v1.32.0 [skip ci] 2026-04-22 17:56:20 +00:00
Devid HW 01514623c9 release: v1.32.0
Deals-in-DB architecture: per-deal data now stored in SQLite instead of
deals.csv/deals.json files.

- storage: add `deals` table with report_id FK; insert_deals/get_deals/get_by_report_dir methods
- extract: stop writing deals.csv and deals.json; only metrics.json written to disk
- pipeline: call db.insert_deals() after extraction; HTML report still deleted after parse
- analytics: all 19 tools now load deals from DB (report_id > report_dir > latest)
- analytics: resolve_report() helper replaces load_report_data/read_deals_from_csv
- analytics: calling any analytics tool with no args uses the latest report automatically
- tools: add export_deals_csv for on-demand CSV generation from DB
- tools: add get_by_report_dir for backward-compat lookup by filesystem path

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-23 00:54:27 +07:00
Devid HW bce0323469 docs: update README and MCP_TOOLS for deals-in-DB architecture
- README: update deal analytics count (19 dimensions, DB-backed)
- MCP_TOOLS: remove deals.csv/deals_json from backtest output schema
- MCP_TOOLS: add export_deals_csv tool documentation
- MCP_TOOLS: update analytics tools to show report_id/report_dir/latest pattern
- MCP_TOOLS: fix get_backtest_crash_info description

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-23 00:53:51 +07:00
Devid HW ccc9e7e4d8 Move test_mcp.py to tests directory 2026-04-22 12:26:37 +07:00
Devid HW e1adcad45c Add 10 new debugging and diagnostic tools for Wine and MT5 2026-04-22 12:24:43 +07:00
Devid HW b97011d5b6 Update documentation for new debugging and diagnostic tools 2026-04-22 12:24:12 +07:00
Devid HW 41344b14b8 docs: add all missing 48 tools to MCP_TOOLS.md
Added documentation for:
- System tools (4): list_symbols, check_update, update, healthcheck
- Experts tools (3): list_experts, list_indicators, list_scripts
- Search & Discovery (5): search_experts, search_indicators, search_scripts, copy_indicator_to_project, copy_script_to_project
- Debugging/Diagnostics (9): diagnose_wine, get_mt5_logs, search_mt5_errors, check_mt5_process, kill_mt5_process, check_system_resources, validate_mt5_config, get_wine_prefix_info, get_backtest_crash_info
- Reports query (10): get_latest_report, search_reports, get_report_by_id, get_reports_summary, get_best_reports, search_reports_by_tags, search_reports_by_date_range, search_reports_by_notes, get_reports_by_set_file, get_comparable_reports
- Granular analytics (8): analyze_monthly_pnl, analyze_drawdown_events, analyze_top_losses, analyze_loss_sequences, analyze_position_pairs, analyze_direction_bias, analyze_streaks, analyze_concurrent_peak
- Deal-level analytics (10): list_deals, search_deals_by_comment, search_deals_by_magic, analyze_profit_distribution, analyze_time_performance, analyze_hold_time_distribution, analyze_layer_performance, analyze_volume_vs_profit, analyze_costs, analyze_efficiency

MCP_TOOLS.md now documents all 89 tools with full input/output schemas.
2026-04-22 08:51:26 +07:00
Devid HW 436566e019 docs: add missing tools to README.md
- Added list_symbols to Core workflow
- Added list_jobs to Optimization section
- Added promote_to_baseline to Reports section
- Added read_set_file and write_set_file to .set files section
- Added diff_set_files to .set analysis section
- Added check_update and update to Debugging section
- Added get_backtest_history to History & Comparison section

Brings documented tools from 87 to 89 (matching actual count)
2026-04-22 08:47:37 +07:00
Devid HW 74c3fdb87e docs: fix binary paths and tarball names across all documentation
Updated files:
- README.md: Fix tarball filename mt5-quant -> mcp-mt5-quant
- QUICKSTART.md: Fix tarball names and all binary paths
- WINDSURF.md: Fix tarball names, binary paths, and install script paths
- CURSOR.md: Fix tarball names and all binary path references
- VSCODE.md: Fix tarball names and all binary path references

All paths now correctly point to:
- Prebuilt: /path/to/mt5-quant/mcp-server/bin/mt5-quant
- Dev build: /path/to/mt5-quant/target/release/mt5-quant
2026-04-22 08:45:06 +07:00
Devid HW 55e0822051 docs: fix prebuilt binary path in CLAUDE.md
- Correct path: /mcp-server/bin/mt5-quant (not project root)
- Verified by downloading and extracting actual release tarball
- Updated both Full Example and Environment Variables sections
2026-04-22 08:42:26 +07:00
Devid HW 5366b45c36 docs: fix incorrect binary paths in CLAUDE.md
- Changed /target/release/mt5-quant to /mt5-quant for prebuilt binary path
- Added path notes explaining prebuilt vs dev build locations
- Fixed both Full Example and Environment Variables sections
2026-04-22 08:40:36 +07:00
Devid HW 96ba882a95 docs: simplify ARCHITECTURE.md and remove Python legacy references
- Condense Design Philosophy to Design Principles
- Remove Legacy Python analytics/ section from component map
- Remove Python CLI entry points (mt5-analyze-* commands)
- Update analyze.py reference to analyze.rs
- Simplify Claude Code Integration section
- Remove outdated hook mechanism documentation

File reduced from 459 to ~410 lines
2026-04-22 08:38:25 +07:00
Devid HW b9729b7121 docs: fix tool count inconsistencies across documentation
- README: 87 → 89 tools (tagline and docs table)
- QUICKSTART.md: 43 → 89 tools
- ARCHITECTURE.md: 43 → 89 tools

Verified actual count: 89 tools via grep of definitions/mod.rs
2026-04-22 08:35:19 +07:00
Devid HW e7d2ec5a47 docs: clarify optimization differences
Others can run optimization via terminal but lack:
- Background execution with polling
- Results parsing into structured data
- Automatic .set file generation
- Optimization history and search
2026-04-22 08:30:18 +07:00
Devid HW 7620cb3c04 docs: clarify comparison with other MT5 MCPs
- Update 'Others' column to acknowledge Windows MT5 Python package
- Clarify MQL5 compilation: others via GUI/terminal, we do headless
- Add 'Report organization' as unique MT5-Quant feature
- Emphasize focus on backtest analytics and workflow management
2026-04-22 08:27:49 +07:00
Devid HW 9a8594426a docs: add crates.io metadata and cargo install instructions
- Add crates.io package metadata (license, keywords, categories)
- Expand 'Why Rust' section with clearer benefits
- Add cargo install option alongside binary download
- Add Acknowledgements and Disclaimer sections
2026-04-22 08:14:59 +07:00
github-actions[bot] 061e36d339 ci: update server.json SHA256 for v1.31.5 [skip ci] 2026-04-22 00:36:19 +00:00
Devid HW cc4b702176 release: v1.31.5
- Version bump 1.31.4 → 1.31.5
- server.json identifier URL updated (SHA256 set by CI after build)
- CHANGELOG.md updated
2026-04-22 07:34:25 +07:00
Devid HW e58316e70e feat: add check_update and update tools with background auto-check
On the first tool call of each session, a background task fires once
and fetches the latest release tag from the GitHub API (5 s timeout).
The result is cached in a static OnceLock for the lifetime of the
process — no repeated network calls.

check_update: returns cached result instantly, or fetches on demand
  if the background task hasn't completed yet.

update: downloads the latest tarball for the current platform, extracts
  the binary, and atomically replaces the running executable via a
  temp-file rename. Requires MCP reconnect to activate new version.

Platform support: macos-aarch64, macos-x86_64, linux-x86_64.
Unsupported platforms return a build-from-source hint.

Tool count: 87 → 89. README and MCP_TOOLS.md updated.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 07:34:25 +07:00
github-actions[bot] a1c5d328b3 ci: update server.json SHA256 for v1.31.4 [skip ci] 2026-04-22 00:25:50 +00:00
Devid HW 6f773d6f09 release: v1.31.4
- Version bump 1.31.3 → 1.31.4
- server.json identifier URL updated (SHA256 set by CI after build)
- CHANGELOG.md updated
2026-04-22 07:24:03 +07:00
Devid HW c895eb9b33 fix: update all scripts for correctness and consistency
release.sh:
- Add --yes/-y flag (and CI=true) for non-interactive execution
- Auto-resolve server.json conflict during rebase-before-push so CI's
  SHA256 commit no longer blocks the push
- Fix major version bump (was double-assigned, now single expression)

build-release.sh:
- Remove cp of /CLAUDE.md (gitignored personal file — caused build failure)
- Bundle all IDE setup docs (QUICKSTART, CLAUDE, CURSOR, VSCODE, WINDSURF)
- Use set -euo pipefail; chmod +x binary; clean up dist/ subdir after tar

build-rust.sh:
- Replace Windsurf-specific install hint with generic cargo install --path . --force
- Use set -euo pipefail

setup.sh:
- Replace "Antigravity" with "Claude Desktop" in comments and output

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-22 07:24:03 +07:00
github-actions[bot] e0b1356afa ci: update server.json SHA256 for v1.31.3 [skip ci] 2026-04-22 00:21:57 +00:00
37 changed files with 4330 additions and 605 deletions
+4
View File
@@ -0,0 +1,4 @@
# These are supported funding model platforms
github: masdevid
custom: ["https://www.paypal.me/devidhw", "https://saweria.co/depod"]
+11
View File
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
version: 2
updates:
- package-ecosystem: "" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "monthly"
+27 -1
View File
@@ -88,10 +88,36 @@ jobs:
name: linux-binary
path: dist/mcp-mt5-quant-linux-x64.tar.gz
# ── Cargo Publish ────────────────────────────────────────────────────────────
cargo-publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
- name: Cache Cargo
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: cargo-publish-${{ hashFiles('Cargo.lock') }}
restore-keys: cargo-publish-
- name: Publish to crates.io
run: cargo publish --no-verify
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
# ── GitHub Release ───────────────────────────────────────────────────────────
release:
needs: [build-macos, build-linux]
needs: [build-macos, build-linux, cargo-publish]
runs-on: ubuntu-latest
permissions:
contents: write
+38
View File
@@ -1,5 +1,43 @@
# Changelog
## [1.32.4] — 2026-04-25
### Fixed
- **HTML report extraction**: `ShutdownTerminal=1` does not cause `terminal64.exe` to exit on Wine/macOS.
Inactivity watchdog now waits 30 s for the report file after the tester log goes quiet, then kills
MT5 unconditionally — no longer depends on natural process exit.
- **Duplicate deals**: tester agent log writes each deal twice; deduplicated via `HashSet<deal_number>`
in `parse_journal_deals`.
- **Entry direction inference**: all journal deals were marked `entry="in"` because no direction info
exists in the log. Fixed with a per-symbol position tracker (signed lot accumulation) to infer
"in" / "out" correctly.
- **`list_deals` returning 0 results**: `is_closed_trade()` was gating on `profit != 0.0`; journal
deals legitimately have zero profit. Removed the profit gate.
- **Wrong tester log selected**: `Agent-0.0.0.0` logs only contain startup lines, not deal lines.
`find_active_tester_agent_log` now prefers `Agent-127.0.0.1` and tiebreaks by file size.
### Added
- `launch_backtest` exposes `shutdown` (default `true`) and `inactivity_kill_secs` (default: disabled;
set to e.g. `120` to enable the inactivity watchdog) as explicit tool parameters.
- Report DB stores deals in SQLite; all analytics tools resolve by `report_id` / `report_dir` / latest.
### Verified
- 1-month DPS21/XAUUSD.cent/M5 backtest produces full HTML report: win_rate=70%,
profit_factor=0.77, sharpe=-3.61, max_dd=6.93%. All 17 analytics tools return real data.
## [1.31.5] — 2026-04-22
- feat: add check_update and update tools with background auto-check
- release: v1.31.4
- fix: update all scripts for correctness and consistency
- release: v1.31.3
- docs: clean up public repo — remove IDE files, fix stale refs
- release: v1.31.2
- refactor: reduce handler boilerplate in analysis and experts modules
- fix: registryType mcpbPackageType → mcpb
## [1.31.4] — 2026-04-22
- fix: update all scripts for correctness and consistency
Generated
+1 -1
View File
@@ -481,7 +481,7 @@ dependencies = [
[[package]]
name = "mt5-quant"
version = "1.31.4"
version = "1.32.4"
dependencies = [
"anyhow",
"base64",
+12 -2
View File
@@ -1,9 +1,19 @@
[package]
name = "mt5-quant"
version = "1.31.4"
version = "1.32.4"
edition = "2021"
description = "MT5-Quant MCP Server - Exposes MT5 backtest and optimization tools via MCP"
description = "MCP server for MT5 strategy development on macOS/Linux"
authors = ["masdevid <masdevid@example.com>"]
license = "MIT"
repository = "https://github.com/masdevid/mt5-quant"
readme = "README.md"
keywords = ["mt5", "mql5", "trading", "mcp", "backtest"]
categories = ["finance", "development-tools"]
homepage = "https://github.com/masdevid/mt5-quant"
documentation = "https://github.com/masdevid/mt5-quant"
[package.metadata]
maintenance = { status = "actively-developed" }
[[bin]]
name = "mt5-quant"
+81 -15
View File
@@ -1,6 +1,6 @@
# MT5-Quant
**MCP server for MT5 strategy development on macOS/Linux.** 87 tools to compile, backtest, analyze, optimize, debug crashes, and manage MQL5 Expert Advisors — no Windows required.
**MCP server for MT5 strategy development on macOS/Linux.** 89 tools to compile, backtest, analyze, optimize, debug crashes, and manage MQL5 Expert Advisors — no Windows required.
```
You: "Backtest MyEA Jan-Mar, what caused the February drawdown?"
@@ -13,26 +13,57 @@ Claude: [compile → clean → backtest → analyze 1,847 deals]
## Why MT5-Quant
| | MT5-Quant | Other MT5 MCPs | QuantConnect |
|---|---|---|---|
| Backtest pipeline | ✅ Full | ❌ | Cloud only |
| Deal-level analytics | ✅ 15+ dims | ❌ | ❌ |
| MQL5 compilation | ✅ | ❌ | ❌ |
| macOS/Linux native | ✅ | Windows only | Cloud |
| Optimization | ✅ Background | ❌ | ✅ Paid |
| Crash debugging | ✅ Wine/MT5 diagnostics | ❌ | ❌ |
**Focus:** Backtest organization, reporting, and analytics — capabilities MT5 itself doesn't provide.
| | MT5-Quant | Others |
|---|---|---|
| **Platform** | macOS/Linux native | Windows only |
| **Backtest pipeline** | ✅ Full (compile → run → analyze) | ✅ Via MT5 Python package |
| **Deal-level analytics** | ✅ 19 dimensions, DB-backed | ❌ |
| **Report organization** | ✅ SQLite (reports + deals) + search + history | ❌ |
| **MQL5 compilation** | ✅ Headless (MetaEditor via Wine, no GUI) | ⚠️ Via GUI or terminal |
| **Optimization** | ✅ Background + results parsing + .set generation | ⚠️ Terminal only, no parsing |
| **Crash debugging** | ✅ Wine/MT5 diagnostics | ❌ |
**Others** typically run on Windows using the [MetaTrader5 Python package](https://pypi.org/project/MetaTrader5/), providing full terminal operations. MT5-Quant fills the gap: **organizing backtest reports, extracting deal-level insights, and managing optimization workflows** — none of which MT5 or its Python API expose natively.
## Why Rust
**MT5-Quant is built entirely in Rust** — one static binary, zero dependencies, instant startup.
| | Rust | Python/Node |
|---|---|---|
| **Startup** | ~10ms | 200500ms |
| **Deploy** | Single binary | venv + pip/npm |
| **Memory** | <50MB, no GC pauses | Unpredictable spikes |
| **Safety** | Compile-time guaranteed | Runtime exceptions |
**Why this matters for trading:**
- **No garbage collection** — Process 100k+ deal rows without GC pauses during live analysis
- **Async via Tokio** — Handle multiple tool calls concurrently (poll status while streaming logs)
- **Cross-platform** — Same source compiles to native macOS arm64 and Linux x86_64
- **Type-safe pipelines** — MQL5 compilation, Wine path handling, SQLite queries: all checked at compile time
## Quick Install
### 1. Download & Setup
### Option 1: Pre-built Binary (Recommended)
```bash
curl -L -o mt5.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mt5-quant-macos-arm64.tar.gz
curl -L -o mt5.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mcp-mt5-quant-macos-arm64.tar.gz
tar -xzf mt5.tar.gz
bash scripts/setup.sh
```
### 2. Register MCP Server
### Option 2: Cargo Install (if you have Rust)
```bash
cargo install mt5-quant
mt5-quant --help
```
Compiles from source. Takes 25 minutes. Requires Rust 1.70+.
### Register MCP Server
| Platform | Command / Config | Docs |
|----------|------------------|------|
@@ -62,12 +93,12 @@ The AI runs the full pipeline: compile → clean cache → backtest → extract
| [VSCODE.md](docs/VSCODE.md) | VS Code setup |
| [CLAUDE.md](docs/CLAUDE.md) | Claude Desktop setup |
| [CONFIG.md](docs/CONFIG.md) | Configuration reference |
| [TOOLS.md](docs/MCP_TOOLS.md) | All 87 tools documented |
| [TOOLS.md](docs/MCP_TOOLS.md) | All 89 tools documented |
| [ARCHITECTURE.md](docs/ARCHITECTURE.md) | Design and internals |
| [TROUBLESHOOTING.md](docs/TROUBLESHOOTING.md) | Common issues |
| [REMOTE_AGENTS.md](docs/REMOTE_AGENTS.md) | Linux optimization agents |
## MCP Tools (87)
## MCP Tools (89)
### Core workflow
@@ -80,6 +111,7 @@ The AI runs the full pipeline: compile → clean cache → backtest → extract
| `get_backtest_status` | Poll running backtest status (MT5 running, report found, elapsed time) |
| `run_optimization` | Genetic optimization (background, returns immediately) |
| `get_optimization_results` | Parse optimization results after MT5 finishes |
| `list_jobs` | List all optimization jobs with status |
| `analyze_report` | Read `analysis.json` from any report directory |
| `compare_baseline` | Compare report vs baseline, return winner/loser verdict |
| `compile_ea` | Compile MQL5 EA via MetaEditor |
@@ -87,6 +119,7 @@ The AI runs the full pipeline: compile → clean cache → backtest → extract
| `list_indicators` | List all indicators in MQL5/Indicators directory |
| `list_scripts` | List all scripts in MQL5/Scripts directory |
| `healthcheck` | Quick server health check |
| `list_symbols` | List all available symbols in MT5 terminal |
### Granular Analytics (individual analysis)
@@ -143,6 +176,7 @@ Use these for targeted analysis, or `analyze_report` to run all at once.
| `get_comparable_reports` | Find comparable reports (same EA/symbol/timeframe) |
| `tail_log` | Read last N lines of any log; `filter=errors` to see only failures |
| `prune_reports` | Delete old report directories, keep last N (skips `_opt` dirs) |
| `promote_to_baseline` | Write a history entry or report to `baseline.json` for compare_baseline |
### History & baseline
@@ -152,7 +186,6 @@ Use these for targeted analysis, or `analyze_report` to run all at once.
| `archive_all_reports` | Bulk-archive all report dirs then optionally delete them; keeps N newest safe |
| `get_history` | Query history with filters (EA, symbol, verdict, profit, DD) and sort options |
| `annotate_history` | Attach verdict / notes / tags to any history entry |
| `promote_to_baseline` | Write a history entry or report to `baseline.json` for compare_baseline |
### Cache management
@@ -183,6 +216,8 @@ Use these for targeted analysis, or `analyze_report` to run all at once.
| `validate_mt5_config` | Validate terminal.ini and tester configuration files |
| `get_wine_prefix_info` | Get Wine prefix details: Windows version, installed programs, registry |
| `get_backtest_crash_info` | Investigate backtest failures: incomplete markers, missing deals.csv, errors |
| `check_update` | Check if a newer version of MT5-Quant is available |
| `update` | Update MT5-Quant to latest release |
### Project Management
@@ -243,6 +278,37 @@ For crashes or unexplained failures during backtest/compile/optimization:
---
## Acknowledgements
MT5-Quant stands on the shoulders of exceptional open-source projects:
- **[Rust](https://www.rust-lang.org/)** — The language that makes zero-cost abstractions, memory safety, and fearless concurrency practical
- **[Tokio](https://tokio.rs/)** — The async runtime powering all concurrent operations
- **[Wine](https://www.winehq.org/)** — Making MT5 execution possible on macOS and Linux without Windows licensing
- **[MetaTrader 5](https://www.metatrader5.com/)** — MetaQuotes' trading platform (trademark of MetaQuotes Software Corp.)
- **[rusqlite](https://github.com/rusqlite/rusqlite)** — Ergonomic SQLite bindings for Rust
- **[serde](https://serde.rs/)** — The serialization framework making config and report handling painless
- **[scraper](https://github.com/causal-agent/scraper)** — HTML parsing for MT5 report extraction
- **[tempfile](https://github.com/Stebalien/temp-file)** — Secure temporary file handling
Special thanks to the Model Context Protocol (MCP) team at Anthropic for defining the standard that makes AI-powered development workflows possible.
## Disclaimer
**Not Financial Advice.** MT5-Quant is a development and analysis tool for algorithmic trading strategies. It does not provide investment advice, trading recommendations, or guarantee profitability. All backtest results are historical simulations and do not guarantee future performance.
**Use at Your Own Risk.** Trading financial instruments carries substantial risk of loss. The authors and contributors of MT5-Quant accept no liability for:
- Trading losses incurred using strategies developed or tested with this tool
- Data loss or corruption from backtest operations
- Bugs, errors, or incorrect analysis results
- System crashes, Wine compatibility issues, or MT5 failures
**Software Warranty.** This software is provided "as-is" without warranty of any kind, express or implied, including but not limited to warranties of merchantability, fitness for a particular purpose, or non-infringement. See LICENSE for full terms.
**Third-Party Software.** MT5-Quant interacts with MetaTrader 5, Wine, and other third-party software. Users are responsible for complying with all applicable licenses and terms of service for these dependencies. MetaTrader is a trademark of MetaQuotes Software Corp. MT5-Quant is not affiliated with, endorsed by, or sponsored by MetaQuotes.
**Regulatory Compliance.** Users are responsible for ensuring their trading activities comply with applicable financial regulations in their jurisdiction. Automated trading may be restricted or require licensing in some regions.
## License
MIT
+37 -67
View File
@@ -1,14 +1,12 @@
# Architecture Deep Dive
# Architecture
## Design Philosophy
## Design Principles
**Deal-level over aggregate.** MT5's built-in HTML report gives you: profit, profit factor, max DD%, trade count. That's it. You cannot tell from those numbers whether a drawdown was caused by overleveraged locking, a bad entry during a news spike, or a grid that reached L8 in a trending market.
**Deal-level over aggregate.** MT5's HTML report gives you: profit, profit factor, max DD%, trade count. MT5-Quant extracts every individual deal — entry price, exit price, P/L, comment string — to reconstruct what happened during each loss event. Result: `analysis.json`, AI-readable and diffable between runs.
MT5-Quant extracts every individual deal — entry price, exit price, P/L, comment string — and reconstructs what was happening when each loss event occurred. The `analysis.json` artifact is the result: AI-readable, stable schema, diffable between runs.
**Pipeline idempotency.** MT5 caches aggressively (`.ex5` binaries, `.set` files, `terminal.ini` flags). The pipeline invalidates all cache before every run to prevent stale results.
**Pipeline idempotency.** MT5 caches aggressively. Cached `.ex5` binaries, cached `.set` files, stale `terminal.ini` flags. The pipeline exists specifically to invalidate all of these before every run. A backtest result that came from a cached EA binary is wrong in a way that's impossible to detect without the cache clear step.
**Background isolation for optimization.** Genetic optimizations run 2-6 hours. Running them inside a parent process that can be killed (Claude task runner, SSH session, terminal) corrupts the MT5 optimization state. The only correct pattern is `nohup + disown` — fully detached from all parent process trees.
**Background isolation.** Genetic optimizations run 2-6 hours. The `nohup + disown` pattern prevents corruption if the parent process (SSH, Claude runner) is killed.
---
@@ -25,24 +23,26 @@ MT5-Quant/
│ │ ├── metrics.rs # Metrics parsing from HTML/XML
│ │ └── report.rs # Report, PipelineMetadata, etc.
│ ├── analytics/ # Report extraction & analysis (migrated from Python)
│ │ ├── extract.rs # HTML/XML report parser → metrics.json + deals.csv
│ │ ├── extract.rs # HTML/XML report parser → metrics.json (deals → DB)
│ │ └── analyze.rs # Deal-level analysis engine → analysis.json
│ ├── compile/ # MQL5 compilation
│ │ └── mql_compiler.rs # MetaEditor wrapper (Wine/CrossOver)
│ ├── pipeline/ # Backtest orchestration
│ │ ├── backtest.rs # 5-stage pipeline (COMPILE→CLEAN→BACKTEST→EXTRACT→ANALYZE)
│ │ └── stages.rs # Pipeline stage definitions
│ ├── storage/ # SQLite persistence
│ │ └── database.rs # ReportDb: reports table + deals table
│ └── tools/ # MCP tool definitions
│ ├── definitions/ # Tool schemas (9 domain modules, 43 tools)
│ ├── definitions/ # Tool schemas (9 domain modules, 90 tools)
│ │ ├── mod.rs
│ │ ├── analytics.rs # 9 analysis tools
│ │ ├── backtest.rs # 4 backtest tools
│ │ ├── analytics.rs # 19 analysis tools (DB-backed)
│ │ ├── backtest.rs # 7 backtest tools
│ │ ├── baseline.rs # 1 baseline tool
│ │ ├── experts.rs # 4 EA/indicator/script tools
│ │ ├── experts.rs # 9 EA/indicator/script tools
│ │ ├── optimization.rs # 4 optimization tools
│ │ ├── reports.rs # 11 report management tools
│ │ ├── reports.rs # 20 report management tools
│ │ ├── setfiles.rs # 8 .set file tools
│ │ └── system.rs # 3 system tools
│ │ └── system.rs # 6 system tools
│ └── handlers/ # Tool dispatch (9 domain modules)
│ ├── mod.rs
│ ├── analysis.rs
@@ -59,11 +59,6 @@ MT5-Quant/
│ ├── build-rust.sh # Rust build script
│ └── optimize.sh # Genetic optimization launcher (nohup + disown)
├── analytics/ # Legacy Python (reference only)
│ ├── extract.py
│ ├── analyze.py
│ └── optimize_parser.py
├── config/
│ ├── mt5-quant.example.yaml # Template config
│ └── mt5-quant.yaml # Live config (gitignored)
@@ -152,17 +147,26 @@ MT5 runs in headless mode, writes the report, and exits.
---
### Stage 4: EXTRACT
### Stage 4: EXTRACT + STORE
Single HTML/XML parse pass that produces three artifacts:
Single HTML/XML parse pass. Deals go directly into the SQLite database; the raw report file is deleted afterwards.
```rust
// src/analytics/extract.rs
let extractor = ReportExtractor::new();
let result = extractor.extract(&report_path, &output_dir)?;
// → metrics.json (aggregate summary)
// → deals.csv (all deals, 13 columns)
// → deals.json (same data, JSON)
// → metrics.json (aggregate summary — written to report_dir)
// HTML report deleted after extraction
// src/storage/database.rs
db.insert_deals(&report_id, &result.deals)?;
// → deals table in SQLite (all deals, keyed by report_id)
```
On-demand CSV export is available via the `export_deals_csv` tool:
```
export_deals_csv(report_id: "20260422_051041_DPS21_XAUUSDc_M5_1")
// → report_dir/deals.csv (written only when explicitly requested)
```
**Why single-pass?** MT5 HTML reports are large (1-5MB for 14-month tests). Each regex pass over the file takes ~200ms. The old pipeline ran 5 separate grep/regex passes. The Rust implementation uses a single-pass parser: 5× faster and no partial-read inconsistencies.
@@ -182,12 +186,13 @@ if ext == "xml" || path.ends_with(".htm.xml") {
}
```
**Deal columns (13):**
**Deal columns (stored in DB):**
```
Time | Type | Direction | Volume | Price | S/L | T/P | Profit | Balance | Comment | Order | Magic | Entry
time | deal | symbol | deal_type | entry | volume | price | order_id
commission | swap | profit | balance | comment | magic
```
The `Comment` column is the key to grid analytics. The EA writes `"Layer #3"`, `"Locking Total"`, `"Zombie Exit"` etc. Pattern matching on comments reconstructs which position was at which layer.
The `comment` column is the key to grid analytics. The EA writes `"Layer #3"`, `"Locking Total"`, `"Zombie Exit"` etc. Pattern matching on comments reconstructs which position was at which layer.
---
@@ -231,15 +236,6 @@ Built-in profiles:
| `trend` | — | `magic` | 240 | breakeven, trailing, partial, tp, sl |
| `hedge` | — | `magic+direction` | 120 | tp, sl, net_close, partial |
#### Entry points (after `pip install -e .`)
```bash
mt5-analyze deals.csv # generic
mt5-analyze-grid deals.csv # grid / martingale (default in pipeline)
mt5-analyze-scalper deals.csv # scalper
mt5-analyze-trend deals.csv # trend following
mt5-analyze-hedge deals.csv # hedging
```
#### Analytics functions
@@ -416,7 +412,7 @@ Then set `DISPLAY=:99` in MT5-Quant's environment config.
**Comment-based analytics:**
- Strategy-specific analytics (depth histogram, exit reason, DD cause) depend on EA comment strings. EAs that don't write structured comments will get `generic` profile results — summary metrics, session breakdown, streaks, and direction bias all still work; only keyword-classified fields fall back to `"unknown"` or profit-sign.
- Custom comment patterns can be supported by adding a new entry to `PROFILES` in `analytics/analyze.py` — no other code changes needed.
- Custom comment patterns can be supported by adding a new entry to `PROFILES` in `src/analytics/analyze.rs`.
**Single MT5 instance:**
- MT5 is single-instance per Windows drive. Two backtests cannot run simultaneously on the same Wine prefix. Parallelism requires multiple Wine prefixes (separate installations).
@@ -425,34 +421,8 @@ Then set `DISPLAY=:99` in MT5-Quant's environment config.
## Claude Code Integration
`setup.sh --claude-code` generates two files that give Claude persistent context about the user's trading setup:
### `config/CLAUDE.template.md`
A project-level CLAUDE.md template the user copies to their EA project root. Encodes:
`setup.sh` generates `config/CLAUDE.template.md` — a project-level template encoding:
- MT5-Quant tool names and when to use them
- Baseline tracking policy (never call something an improvement without comparing to `baseline.json`)
- Symbol name reminder (broker-specific suffix matters — `XAUUSD.cent``XAUUSD`)
- Backtest and optimization constraints (model 0, single instance, UTF-16LE .set files)
### `.claude/hooks/user-prompt-submit.sh`
A Claude Code hook that runs before every prompt submission. Reads `config/baseline.json` and outputs a JSON context block:
```json
{"context": "## Production Baseline (config/baseline.json)\n..."}
```
Claude Code injects this into the system context for every conversation turn. The result: Claude always knows the current production metrics without the user having to paste them.
**Hook execution path:**
```
User types prompt
→ user-prompt-submit.sh executes
→ reads config/baseline.json
→ outputs {"context": "..."} to stdout
→ Claude Code prepends to system context
→ Claude sees baseline in every prompt
```
**Graceful degradation:** If `baseline.json` doesn't exist or is malformed, the hook exits 0 silently — no prompt is blocked. The baseline section simply doesn't appear until the user creates the file.
- Baseline tracking policy (compare to `baseline.json` before calling improvements)
- Symbol name reminders (`XAUUSD.cent``XAUUSD`)
- Backtest constraints (model 0, UTF-16LE .set files)
+6 -2
View File
@@ -57,7 +57,7 @@ Edit `~/Library/Application Support/Claude/claude_desktop_config.json` (macOS) o
{
"mcpServers": {
"mt5-quant": {
"command": "/Users/name/mt5-quant/target/release/mt5-quant"
"command": "/Users/name/mt5-quant/mcp-server/bin/mt5-quant"
},
"github": {
"command": "npx",
@@ -70,6 +70,10 @@ Edit `~/Library/Application Support/Claude/claude_desktop_config.json` (macOS) o
}
```
**Path notes:**
- Prebuilt binary: `/Users/name/mt5-quant/mcp-server/bin/mt5-quant` (extracted from release tarball)
- Dev build: `/Users/name/mt5-quant/target/release/mt5-quant` (after `cargo build --release`)
## Environment Variables
Claude Desktop supports `${env:VAR_NAME}` syntax for environment variable substitution:
@@ -78,7 +82,7 @@ Claude Desktop supports `${env:VAR_NAME}` syntax for environment variable substi
{
"mcpServers": {
"mt5-quant": {
"command": "/Users/name/mt5-quant/target/release/mt5-quant",
"command": "/Users/name/mt5-quant/mcp-server/bin/mt5-quant",
"env": {
"MT5_MCP_HOME": "${env:HOME}/.config/mt5-quant"
}
+9 -9
View File
@@ -6,12 +6,12 @@
```bash
# macOS (Apple Silicon)
curl -L -o mt5-quant.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mt5-quant-macos-arm64.tar.gz
tar -xzf mt5-quant.tar.gz
curl -L -o mt5.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mcp-mt5-quant-macos-arm64.tar.gz
tar -xzf mt5.tar.gz
# Linux (x64)
curl -L -o mt5-quant.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mt5-quant-linux-x64.tar.gz
tar -xzf mt5-quant.tar.gz
curl -L -o mt5.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mcp-mt5-quant-linux-x64.tar.gz
tar -xzf mt5.tar.gz
```
### Option 2: Build from Source
@@ -29,7 +29,7 @@ cargo build --release
3. Click **Add Custom MCP**
4. Enter:
- **Name**: `mt5-quant`
- **Command**: `~/.local/bin/mt5-quant`
- **Command**: `/path/to/mt5-quant/mcp-server/bin/mt5-quant`
- **Type**: `stdio`
### Method 2: Edit mcp.json Directly
@@ -41,7 +41,7 @@ Add to `~/.cursor/mcp.json` (global) or `.cursor/mcp.json` (project):
"mcpServers": {
"mt5-quant": {
"type": "stdio",
"command": "~/.local/bin/mt5-quant"
"command": "/path/to/mt5-quant/mcp-server/bin/mt5-quant"
}
}
}
@@ -56,7 +56,7 @@ cat > ~/.cursor/mcp.json << 'EOF'
"mcpServers": {
"mt5-quant": {
"type": "stdio",
"command": "/path/to/mt5-quant"
"command": "/path/to/mt5-quant/mcp-server/bin/mt5-quant"
}
}
}
@@ -92,7 +92,7 @@ Arch: arch -x86_64
1. Check MCP panel in Cursor Settings
2. Verify the path is absolute (not relative)
3. Test binary: `/path/to/mt5-quant --help`
3. Test binary: `/path/to/mt5-quant/mcp-server/bin/mt5-quant --help`
4. View MCP logs: Output panel → select "MCP" from dropdown
### Config interpolation
@@ -103,7 +103,7 @@ Cursor supports variable substitution in `mcp.json`:
{
"mcpServers": {
"mt5-quant": {
"command": "${userHome}/bin/mt5-quant",
"command": "${userHome}/mt5-quant/mcp-server/bin/mt5-quant",
"args": ["--config", "${workspaceFolder}/config.yaml"]
}
}
+1773 -21
View File
File diff suppressed because it is too large Load Diff
+10 -11
View File
@@ -6,12 +6,12 @@
```bash
# macOS (Apple Silicon)
curl -L -o mt5-quant.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mt5-quant-macos-arm64.tar.gz
tar -xzf mt5-quant.tar.gz
curl -L -o mt5.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mcp-mt5-quant-macos-arm64.tar.gz
tar -xzf mt5.tar.gz
# Linux (x64)
curl -L -o mt5-quant.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mt5-quant-linux-x64.tar.gz
tar -xzf mt5-quant.tar.gz
curl -L -o mt5.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mcp-mt5-quant-linux-x64.tar.gz
tar -xzf mt5.tar.gz
```
### Option B: Build from Source
@@ -82,6 +82,7 @@ terminal_dir: "~/Library/Application Support/net.metaquotes.wine.metatrader5/dri
```bash
claude mcp add io.github.masdevid/mt5-quant -- ~/.local/bin/mt5-quant
# Or: claude mcp add mt5-quant -- $(pwd)/mcp-server/bin/mt5-quant
```
Verify:
@@ -96,10 +97,8 @@ Add to `~/.codeium/windsurf/mcp_config.json`:
```json
{
"mcpServers": {
"io.github.masdevid/mt5-quant": {
"command": "~/.local/bin/mt5-quant",
"disabled": false,
"registry": "io.github.masdevid/mt5-quant"
"mt5-quant": {
"command": "/path/to/mt5-quant/mcp-server/bin/mt5-quant"
}
}
}
@@ -113,7 +112,7 @@ Add to `~/.cursor/mcp.json` (global) or `.cursor/mcp.json` (project):
{
"mcpServers": {
"mt5-quant": {
"command": "~/.local/bin/mt5-quant"
"command": "/path/to/mt5-quant/mcp-server/bin/mt5-quant"
}
}
}
@@ -129,7 +128,7 @@ Add to `.vscode/mcp.json` in your workspace:
{
"servers": {
"mt5-quant": {
"command": "/path/to/mt5-quant"
"command": "/path/to/mt5-quant/mcp-server/bin/mt5-quant"
}
}
}
@@ -190,4 +189,4 @@ The AI will:
---
**Next:** See [TOOLS.md](TOOLS.md) for all 43 available tools.
**Next:** See [TOOLS.md](TOOLS.md) for all 89 available tools.
+53
View File
@@ -69,6 +69,42 @@ Set `MT5_MCP_HOME` or ensure config exists at:
4. **Date range has no trades** — try wider range or confirm symbol was active.
## Backtest Completes But Always Shows "journal-only" (No HTML Report)
**Symptom:** Every backtest produces `status: completed_no_html` and all deal profits are `0.0`.
**Cause:** On Wine/macOS, `ShutdownTerminal=1` does **not** cause `terminal64.exe` to exit after the
test completes. The tester agent (MetaTester 5) closes normally, but the terminal process stays alive
indefinitely. Without process exit, MT5 never writes the HTML report.
**How the pipeline handles this:** The inactivity watchdog detects that the tester log has stopped
growing (test done), waits 30 seconds polling for the HTML file, then kills `terminal64.exe`
unconditionally. If the HTML appears during the wait it is extracted; otherwise journal extraction
provides deal counts and final balance (but no per-deal P&L).
**To maximise HTML report chances:** Pass `inactivity_kill_secs=120` explicitly (it is disabled by
default). With `shutdown=true` (default) this gives MT5 120s of quiet time + 30s of HTML-wait before
the kill.
```
launch_backtest(expert: "MyEA", shutdown: true, inactivity_kill_secs: 120)
```
**Fallback data available from journal:**
- Deal count, volume, prices, timestamps ✓
- Final balance (pips) ✓
- Per-deal profit/loss ✗ (always 0.0)
- Win rate, profit factor, Sharpe, drawdown ✗ (require HTML)
## `list_deals` Returns 0 Filtered Results
Analytics tools filter deals with `is_closed_trade()` which checks `entry = "out"`. If all deals
show `entry = "in"`, the position tracker that infers direction from signed lot accumulation may
not have run (old binary in memory).
**Fix:** Restart the MCP server (restart Claude Code / your IDE) after installing a new binary.
The server process caches the binary in memory — `install` doesn't hot-reload it.
## MetaEditor Compile Errors
Check `<terminal_dir>/MQL5/Logs/`:
@@ -82,6 +118,23 @@ Check `<terminal_dir>/MQL5/Logs/`:
- Check `.set` file values appropriate for symbol/broker
- Confirm `OnInit()` returns `INIT_SUCCEEDED` (MT5 Journal tab)
## Analytics Tools: "No reports in DB" or "Report not found"
Analytics tools load deals from the **SQLite database**, not from CSV files on disk. Resolution order:
1. `report_id` (preferred) — ID from `list_reports`
2. `report_dir` (legacy) — filesystem path, looks up matching DB entry
3. No args — uses the latest report automatically
Pre-DB reports (before this version) won't be found by `report_dir`. Re-run the backtest to get a DB-backed report.
**`deals.csv` is no longer written automatically.** Call `export_deals_csv` to generate one on demand:
```
export_deals_csv() # latest report → report_dir/deals.csv
export_deals_csv(report_id: "20260422_…") # specific report
export_deals_csv(output_path: "/tmp/out.csv") # custom path
```
## Optimization Never Finishes
```bash
+9 -9
View File
@@ -6,12 +6,12 @@
```bash
# macOS (Apple Silicon)
curl -L -o mt5-quant.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mt5-quant-macos-arm64.tar.gz
tar -xzf mt5-quant.tar.gz
curl -L -o mt5.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mcp-mt5-quant-macos-arm64.tar.gz
tar -xzf mt5.tar.gz
# Linux (x64)
curl -L -o mt5-quant.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mt5-quant-linux-x64.tar.gz
tar -xzf mt5-quant.tar.gz
curl -L -o mt5.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mcp-mt5-quant-linux-x64.tar.gz
tar -xzf mt5.tar.gz
```
### Option 2: Build from Source
@@ -28,7 +28,7 @@ cargo build --release
2. Run `MCP: Add Server`
3. Choose **Workspace** or **User** scope
4. Enter server name: `mt5-quant`
5. Enter command: `~/.local/bin/mt5-quant`
5. Enter command: `/path/to/mt5-quant/mcp-server/bin/mt5-quant`
### Method 2: Edit mcp.json Directly
@@ -38,7 +38,7 @@ Add to `.vscode/mcp.json` in your workspace:
{
"servers": {
"mt5-quant": {
"command": "~/.local/bin/mt5-quant"
"command": "/path/to/mt5-quant/mcp-server/bin/mt5-quant"
}
}
}
@@ -52,7 +52,7 @@ cat > .vscode/mcp.json << 'EOF'
{
"servers": {
"mt5-quant": {
"command": "~/.local/bin/mt5-quant"
"command": "/path/to/mt5-quant/mcp-server/bin/mt5-quant"
}
}
}
@@ -62,7 +62,7 @@ EOF
### Method 3: VS Code CLI
```bash
code --add-mcp '{"name":"mt5-quant","command":"~/.local/bin/mt5-quant"}'
code --add-mcp '{"name":"mt5-quant","command":"/path/to/mt5-quant/mcp-server/bin/mt5-quant"}'
```
## Verify Setup
@@ -115,7 +115,7 @@ Add to `.devcontainer/devcontainer.json`:
"mcp": {
"servers": {
"mt5-quant": {
"command": "/path/to/mt5-quant"
"command": "/path/to/mt5-quant/mcp-server/bin/mt5-quant"
}
}
}
+8 -10
View File
@@ -6,12 +6,12 @@
```bash
# macOS (Apple Silicon)
curl -L -o mt5-quant.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mt5-quant-macos-arm64.tar.gz
tar -xzf mt5-quant.tar.gz
curl -L -o mt5.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mcp-mt5-quant-macos-arm64.tar.gz
tar -xzf mt5.tar.gz
# Linux (x64)
curl -L -o mt5-quant.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mt5-quant-linux-x64.tar.gz
tar -xzf mt5-quant.tar.gz
curl -L -o mt5.tar.gz https://github.com/masdevid/mt5-quant/releases/latest/download/mcp-mt5-quant-linux-x64.tar.gz
tar -xzf mt5.tar.gz
```
### Option 2: Build from Source
@@ -27,10 +27,8 @@ Edit `~/.codeium/windsurf/mcp_config.json`:
```json
{
"mcpServers": {
"io.github.masdevid/mt5-quant": {
"command": "~/.local/bin/mt5-quant",
"disabled": false,
"registry": "io.github.masdevid/mt5-quant"
"mt5-quant": {
"command": "/path/to/mt5-quant/mcp-server/bin/mt5-quant"
}
}
}
@@ -40,7 +38,7 @@ Or use the automated setup:
```bash
# Install binary to standard location
cp target/release/mt5-quant ~/.local/bin/
cp mcp-server/bin/mt5-quant ~/.local/bin/
# Then configure
bash scripts/setup.sh
@@ -98,7 +96,7 @@ The binary auto-detects its config location. No environment variables needed.
### MCP server not appearing
1. Check Windsurf logs: `~/.windsurf/logs/`
2. Verify executable path is absolute
3. Test executable manually: `./target/release/mt5-quant --help`
3. Test executable manually: `./mcp-server/bin/mt5-quant --help`
### Config not found
Set `MT5_MCP_HOME` environment variable or ensure config is at default location:
+4 -4
View File
@@ -7,13 +7,13 @@
"url": "https://github.com/masdevid/mt5-quant",
"source": "github"
},
"version": "1.31.4",
"version": "1.32.3",
"packages": [
{
"registryType": "mcpb",
"version": "1.31.4",
"identifier": "https://github.com/masdevid/mt5-quant/releases/download/v1.31.4/mcp-mt5-quant-macos-arm64.tar.gz",
"fileSha256": "TBD_CI_WILL_UPDATE",
"version": "1.32.3",
"identifier": "https://github.com/masdevid/mt5-quant/releases/download/v1.32.3/mcp-mt5-quant-macos-arm64.tar.gz",
"fileSha256": "4c0b396f48c64334455e344644eaf6eaa6783a80fbf148688193e0108eadb949",
"transport": {
"type": "stdio"
},
+87 -64
View File
@@ -15,7 +15,7 @@ impl ReportExtractor {
pub fn extract(&self, report_path: &str, output_dir: &str) -> Result<ExtractionResult> {
let format = Self::detect_format(report_path);
let (metrics, deals) = match format {
ReportFormat::Xml => self.parse_xml(report_path)?,
ReportFormat::Html => self.parse_html(report_path)?,
@@ -24,22 +24,19 @@ impl ReportExtractor {
fs::create_dir_all(output_dir)?;
let metrics_path = Path::new(output_dir).join("metrics.json");
let deals_csv_path = Path::new(output_dir).join("deals.csv");
let deals_json_path = Path::new(output_dir).join("deals.json");
self.write_metrics(&metrics, &metrics_path)?;
self.write_deals_json(&deals, &deals_json_path)?;
self.write_deals_csv(&deals, &deals_csv_path)?;
Ok(ExtractionResult {
metrics,
deals,
metrics_path,
deals_csv_path,
deals_json_path,
})
}
pub fn write_deals_to_csv(&self, deals: &[Deal], path: &Path) -> Result<()> {
self.write_deals_csv(deals, path)
}
fn detect_format(path: &str) -> ReportFormat {
if path.ends_with(".xml") || path.ends_with(".htm.xml") {
return ReportFormat::Xml;
@@ -80,59 +77,96 @@ impl ReportExtractor {
fn parse_deals_html(&self, text: &str) -> Result<Vec<Deal>> {
let mut deals = Vec::new();
let re = regex::Regex::new(r"<tr[^>]*>.*?Deal.*?Time.*?Type.*?Direction.*?</tr>(.*)")
.map_err(|e| anyhow!("Regex error: {}", e))?;
// (?s) = dotall: makes '.' match '\n' so the regex works on multiline HTML.
// MT5 HTML column order: Time | Deal | Symbol | Type | Direction |
// Volume | Price | Order | Commission | Swap | Profit | Balance | Comment
//
// Strategy: locate the deals table by finding the header <tr> that contains
// the column names, then parse every subsequent <tr> as a data row.
// We try two header patterns to handle different MT5 versions/locales.
if let Some(captures) = re.captures(text) {
let section = captures.get(1).map(|m| m.as_str()).unwrap_or("");
let row_re = regex::Regex::new(r"(?s)<tr[^>]*>(.*?)</tr>")
.map_err(|e| anyhow!("Row regex error: {}", e))?;
let cell_re = regex::Regex::new(r"(?s)<td[^>]*>(.*?)</td>")
.map_err(|e| anyhow!("Cell regex error: {}", e))?;
let row_re = regex::Regex::new(r"<tr[^>]*>(.*?)</tr>")
.map_err(|e| anyhow!("Regex error: {}", e))?;
// Collect all <tr> blocks once, then find the deals header and parse from there.
let rows: Vec<&str> = row_re.captures_iter(text)
.filter_map(|cap| cap.get(0).map(|m| m.as_str()))
.collect();
for row_caps in row_re.captures_iter(section) {
let row = row_caps.get(1).map(|m| m.as_str()).unwrap_or("");
let cell_re = regex::Regex::new(r"<td[^>]*>(.*?)</td>")
.map_err(|e| anyhow!("Regex error: {}", e))?;
let cells: Vec<String> = cell_re.captures_iter(row)
.filter_map(|cap| cap.get(1))
.map(|m| Self::strip_tags(m.as_str()))
.map(|s| s.replace(',', ""))
.collect();
// Find the header row index: it must contain both "Deal" and "Symbol" (case-insensitive).
let header_idx = rows.iter().position(|row| {
let lower = row.to_lowercase();
(lower.contains(">deal<") || lower.contains(">deal </")) &&
(lower.contains(">symbol<") || lower.contains(">symbol </"))
});
if cells.len() < 3 || cells[0].is_empty() {
continue;
let start_idx = match header_idx {
Some(i) => i + 1,
None => {
// Fallback: look for any row containing Time+Volume+Profit headers
let alt = rows.iter().position(|row| {
let lower = row.to_lowercase();
lower.contains(">time<") && lower.contains(">volume<") && lower.contains(">profit<")
});
match alt {
Some(i) => i + 1,
None => {
tracing::warn!("parse_deals_html: no deals table header found");
return Ok(deals);
}
}
if cells.iter().take(5).any(|c| {
let c_lower = c.trim().to_lowercase();
c_lower == "balance" || c_lower == "credit"
}) {
continue;
}
let deal = Deal {
time: cells.get(0).cloned().unwrap_or_default(),
deal: cells.get(1).cloned().unwrap_or_default(),
symbol: cells.get(2).cloned().unwrap_or_default(),
deal_type: cells.get(3).cloned().unwrap_or_default(),
entry: cells.get(4).cloned().unwrap_or_default(),
volume: cells.get(5).and_then(|s| s.parse().ok()).unwrap_or(0.0),
price: cells.get(6).and_then(|s| s.parse().ok()).unwrap_or(0.0),
order: cells.get(7).cloned().unwrap_or_default(),
commission: cells.get(8).and_then(|s| s.parse().ok()).unwrap_or(0.0),
swap: cells.get(9).and_then(|s| s.parse().ok()).unwrap_or(0.0),
profit: cells.get(10).and_then(|s| s.parse().ok()).unwrap_or(0.0),
balance: cells.get(11).and_then(|s| s.parse().ok()).unwrap_or(0.0),
comment: cells.get(12).cloned().unwrap_or_default(),
magic: cells.get(13).cloned(),
};
deals.push(deal);
}
};
for row in &rows[start_idx..] {
let cells: Vec<String> = cell_re.captures_iter(row)
.filter_map(|cap| cap.get(1))
.map(|m| Self::strip_tags(m.as_str()))
.map(|s| s.replace(',', ""))
.collect();
if cells.len() < 3 || cells[0].trim().is_empty() {
continue;
}
// Skip balance/credit operation rows (Type column is index 3 in MT5 HTML)
let type_cell = cells.get(3).map(|s| s.trim().to_lowercase()).unwrap_or_default();
if type_cell == "balance" || type_cell == "credit" {
continue;
}
// Also skip sub-header rows that repeat column names
if type_cell == "type" || cells.get(1).map(|s| s.trim().to_lowercase()).as_deref() == Some("deal") {
continue;
}
// Skip rows with no deal number (e.g. totals row)
let deal_num = cells.get(1).map(|s| s.trim().to_string()).unwrap_or_default();
if deal_num.is_empty() {
continue;
}
let deal = Deal {
time: cells.get(0).cloned().unwrap_or_default(),
deal: deal_num,
symbol: cells.get(2).cloned().unwrap_or_default(),
deal_type: cells.get(3).cloned().unwrap_or_default(),
entry: cells.get(4).cloned().unwrap_or_default(),
volume: cells.get(5).and_then(|s| s.parse().ok()).unwrap_or(0.0),
price: cells.get(6).and_then(|s| s.parse().ok()).unwrap_or(0.0),
order: cells.get(7).cloned().unwrap_or_default(),
commission: cells.get(8).and_then(|s| s.parse().ok()).unwrap_or(0.0),
swap: cells.get(9).and_then(|s| s.parse().ok()).unwrap_or(0.0),
profit: cells.get(10).and_then(|s| s.parse().ok()).unwrap_or(0.0),
balance: cells.get(11).and_then(|s| s.parse().ok()).unwrap_or(0.0),
comment: cells.get(12).cloned().unwrap_or_default(),
magic: cells.get(13).cloned(),
};
deals.push(deal);
}
tracing::info!("parse_deals_html: extracted {} deals", deals.len());
Ok(deals)
}
@@ -215,13 +249,6 @@ impl ReportExtractor {
Ok(())
}
fn write_deals_json(&self, deals: &[Deal], path: &Path) -> Result<()> {
let json = serde_json::to_string_pretty(deals)?;
let mut file = File::create(path)?;
file.write_all(json.as_bytes())?;
Ok(())
}
fn write_deals_csv(&self, deals: &[Deal], path: &Path) -> Result<()> {
let mut file = File::create(path)?;
writeln!(file, "time,deal,symbol,type,entry,volume,price,order,commission,swap,profit,balance,comment")?;
@@ -279,10 +306,6 @@ pub struct ExtractionResult {
pub deals: Vec<Deal>,
#[allow(dead_code)]
pub metrics_path: PathBuf,
#[allow(dead_code)]
pub deals_csv_path: PathBuf,
#[allow(dead_code)]
pub deals_json_path: PathBuf,
}
#[derive(Debug, Clone, Copy)]
+30 -4
View File
@@ -90,8 +90,8 @@ impl MqlCompiler {
.map(|s| s.to_string())
.collect();
let ex5_path = staged_mq5.with_extension("ex5");
if !ex5_path.exists() {
let staged_ex5 = staged_mq5.with_extension("ex5");
if !staged_ex5.exists() {
let final_errors = if errors.is_empty() {
vec![format!("Compilation failed. Log:\n{}", &log_text[log_text.len().saturating_sub(500)..])]
} else {
@@ -107,11 +107,37 @@ impl MqlCompiler {
});
}
let binary_size = fs::metadata(&ex5_path)?.len();
let binary_size = fs::metadata(&staged_ex5)?.len();
// Deploy compiled output to the real MQL5/Experts/{ea_name}/ directory so
// MT5 can actually load it. The temp dir is only needed to avoid Wine path
// issues with spaces; the real experts dir is the authoritative location.
let final_ex5_path = if let Some(experts_dir) = self.config.experts_dir.as_ref() {
let real_experts = PathBuf::from(experts_dir);
let real_ea_dir = real_experts.join(ea_name);
fs::create_dir_all(&real_ea_dir)?;
// Sync source files (.mq5 + .mqh) into the real experts dir so that
// future compiles from the experts dir also work correctly.
if let Err(e) = self.sync_project_to_experts(source_path, &real_experts) {
tracing::warn!("Could not sync source to experts dir: {}", e);
}
// Copy the compiled .ex5 to the real experts dir.
let dest_ex5 = real_ea_dir.join(format!("{}.ex5", ea_name));
fs::copy(&staged_ex5, &dest_ex5)?;
tracing::info!("Deployed {} → {}", staged_ex5.display(), dest_ex5.display());
dest_ex5
} else {
// No experts_dir configured — fall back to the staged path so callers
// still get a valid path even if MT5 won't find it automatically.
tracing::warn!("experts_dir not configured; .ex5 left in staging dir");
staged_ex5
};
Ok(CompileResult {
success: errors.is_empty(),
ex5_path: Some(ex5_path),
ex5_path: Some(final_ex5_path),
errors,
warnings,
binary_size,
+94
View File
@@ -12,10 +12,14 @@ use anyhow::Result;
use clap::Parser;
use serde_json::{json, Value};
use std::io::{stdout, Write};
use std::time::Instant;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::TcpListener;
use tracing::{info, error};
use crate::models::Config;
use crate::pipeline::backtest::{BacktestPipeline, BacktestParams};
#[derive(Parser)]
#[command(name = "mt5-quant")]
#[command(about = "MT5-Quant MCP Server - Exposes MT5 backtest and optimization tools via MCP")]
@@ -27,6 +31,18 @@ struct Cli {
/// Run on TCP port for debugging
#[arg(short, long)]
port: Option<u16>,
/// Test backtest launch performance (direct Rust call, not MCP)
#[arg(long)]
test_launch: bool,
/// EA name for test launch
#[arg(long)]
ea: Option<String>,
/// Startup delay for test launch (default: 10)
#[arg(long)]
startup_delay: Option<u64>,
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
@@ -88,6 +104,11 @@ async fn main() -> Result<()> {
let cli = Cli::parse();
if cli.test_launch {
run_test_launch(cli.ea, cli.startup_delay).await?;
return Ok(());
}
if let Some(port) = cli.port {
run_tcp_server(port).await?;
} else {
@@ -97,10 +118,83 @@ async fn main() -> Result<()> {
Ok(())
}
async fn run_test_launch(ea: Option<String>, startup_delay: Option<u64>) -> Result<()> {
let expert = ea.ok_or_else(|| anyhow::anyhow!("--ea is required for test launch"))?;
let delay = startup_delay.unwrap_or(10);
println!("Testing MT5 backtest launch optimizations...");
println!("==============================================");
println!("EA: {}", expert);
println!("Startup delay: {}s", delay);
let config = Config::load()?;
let params = BacktestParams {
expert: expert.clone(),
symbol: "XAUUSD".to_string(),
from_date: "2024.01.01".to_string(),
to_date: "2024.01.31".to_string(),
timeframe: "M5".to_string(),
deposit: 10000,
model: 0,
leverage: 500,
set_file: None,
skip_compile: true,
skip_clean: false,
skip_analyze: true,
deep_analyze: false,
shutdown: false,
kill_existing: false,
timeout: 900,
gui: false,
startup_delay_secs: delay,
inactivity_kill_secs: None,
};
let pipeline = BacktestPipeline::new(config);
println!("\nLaunching backtest...");
let start = Instant::now();
match pipeline.launch_backtest(params).await {
Ok(job) => {
let elapsed = start.elapsed();
println!("✓ Launch completed in {:.2}s", elapsed.as_secs_f64());
println!(" Report ID: {}", job.report_id);
println!(" Report dir: {}", job.report_dir);
println!("\nUse get_backtest_status to monitor progress.");
}
Err(e) => {
let elapsed = start.elapsed();
println!("✗ Launch failed after {:.2}s: {}", elapsed.as_secs_f64(), e);
}
}
println!("\n==============================================");
println!("Test complete.");
Ok(())
}
async fn run_stdio_server() -> Result<()> {
info!("Starting MT5-Quant MCP server on stdio");
let server = std::sync::Arc::new(mcp_server::McpServer::new());
let (notification_tx, mut notification_rx) = tokio::sync::mpsc::unbounded_channel::<mcp_server::Notification>();
server.set_notification_sender(notification_tx).await;
// Spawn notification sender task
tokio::spawn(async move {
while let Some(notification) = notification_rx.recv().await {
let notification_json = json!({
"jsonrpc": "2.0",
"method": notification.method,
"params": notification.params,
});
println!("{}", notification_json);
let _ = stdout().flush();
}
});
let mut reader = BufReader::new(tokio::io::stdin());
let mut line = String::new();
+62 -11
View File
@@ -1,9 +1,12 @@
use serde_json::{json, Value};
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::sync::{mpsc, Mutex};
use crate::{models::Config as ModelsConfig, tools::ToolHandler, McpError, McpRequest, McpResponse};
#[allow(dead_code)]
type NotificationCallback = Arc<dyn Fn(&str, serde_json::Value) + Send + Sync>;
/// Auto-verify result stored after first initialization
#[derive(Debug, Clone)]
#[allow(dead_code)]
@@ -13,11 +16,17 @@ struct AutoVerifyResult {
config_path: String,
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Notification {
pub method: String,
pub params: Value,
}
pub struct McpServer {
initialized: Arc<Mutex<bool>>,
tool_handler: Arc<ToolHandler>,
tool_handler: Arc<Mutex<Option<ToolHandler>>>,
auto_verify_result: Arc<Mutex<Option<AutoVerifyResult>>>,
notification_tx: Arc<Mutex<Option<mpsc::UnboundedSender<Notification>>>>,
}
impl McpServer {
@@ -25,11 +34,38 @@ impl McpServer {
let config = ModelsConfig::load().unwrap_or_default();
Self {
initialized: Arc::new(Mutex::new(false)),
tool_handler: Arc::new(ToolHandler::new(config)),
tool_handler: Arc::new(Mutex::new(Some(ToolHandler::new(config)))),
auto_verify_result: Arc::new(Mutex::new(None)),
notification_tx: Arc::new(Mutex::new(None)),
}
}
pub async fn set_notification_sender(&self, tx: mpsc::UnboundedSender<Notification>) {
let mut guard = self.notification_tx.lock().await;
*guard = Some(tx.clone());
// Update tool handler with notification callback
let tx_clone = tx.clone();
let callback = Arc::new(move |method: &str, params: serde_json::Value| {
let _ = tx_clone.send(Notification {
method: method.to_string(),
params,
});
});
let config = ModelsConfig::load().unwrap_or_default();
let new_handler = ToolHandler::with_notification_callback(config, callback);
let mut handler_guard = self.tool_handler.lock().await;
*handler_guard = Some(new_handler);
}
#[allow(dead_code)]
pub async fn get_notification_sender(&self) -> Option<mpsc::UnboundedSender<Notification>> {
let guard = self.notification_tx.lock().await;
guard.clone()
}
/// Run verify_setup in background - non blocking
fn spawn_auto_verify(&self) {
let result_arc = self.auto_verify_result.clone();
@@ -247,12 +283,27 @@ impl McpServer {
}
async fn handle_tool_call(&self, tool_name: &str, arguments: &Value) -> Value {
self.tool_handler.handle(tool_name, arguments).await.unwrap_or_else(|e| json!({
"content": [{
"type": "text",
"text": format!("Tool execution failed: {}", e)
}],
"isError": true
}))
let handler_guard = self.tool_handler.lock().await;
let handler = handler_guard.as_ref().cloned();
drop(handler_guard);
match handler {
Some(h) => {
h.handle(tool_name, arguments).await.unwrap_or_else(|e| json!({
"content": [{
"type": "text",
"text": format!("Tool execution failed: {}", e)
}],
"isError": true
}))
}
None => json!({
"content": [{
"type": "text",
"text": "Tool handler not initialized"
}],
"isError": true
})
}
}
}
+91 -28
View File
@@ -97,6 +97,7 @@ pub struct Config {
pub reports_dir: Option<String>,
pub backtest_login: Option<String>,
pub backtest_server: Option<String>,
pub backtest_password: Option<String>,
pub project_dir: Option<String>,
}
@@ -123,6 +124,7 @@ impl Default for Config {
reports_dir: None,
backtest_login: None,
backtest_server: None,
backtest_password: None,
project_dir: None,
}
}
@@ -404,6 +406,7 @@ impl Config {
reports_dir: map.get("reports_dir").cloned(),
backtest_login: map.get("backtest_login").cloned(),
backtest_server: map.get("backtest_server").cloned(),
backtest_password: map.get("backtest_password").cloned(),
project_dir: map.get("project_dir").cloned(),
})
}
@@ -430,6 +433,7 @@ impl Config {
"reports_dir" => self.reports_dir.clone().unwrap_or_else(|| "reports".to_string()),
"backtest_login" => self.backtest_login.clone().unwrap_or_default(),
"backtest_server" => self.backtest_server.clone().unwrap_or_default(),
"backtest_password" => self.backtest_password.clone().unwrap_or_default(),
"project_dir" => self.project_dir.clone().unwrap_or_default(),
_ => String::new(),
}
@@ -504,29 +508,50 @@ impl Config {
self.terminal_dir.as_ref().map(|d| Path::new(d).to_path_buf())
}
/// Scan Bases/*/history/ for symbol directories that contain at least one .hcc file.
/// Returns deduplicated, sorted list of symbol names available for backtesting.
/// If server_filter is provided, only scans that specific server's directory.
/// Scan the tester's own history store for symbols with downloaded data.
///
/// MT5 maintains two separate history trees:
/// • `Bases/{server}/history/` — live-trading tick/bar data (NOT usable by tester)
/// • `Tester/bases/{server}/history/` — data the Strategy Tester actually reads
///
/// Scanning `Bases/` (the old approach) returned symbols that exist for live trading
/// but may have no tester data, causing the tester to fail with "symbol does not exist".
/// This function scans `Tester/bases/` instead, which is the authoritative source.
///
/// Falls back to `Bases/` only when `Tester/bases/` is absent (first-run / no backtests yet).
///
/// If `server_filter` is provided only that server's directory is scanned.
pub fn discover_symbols(&self, server_filter: Option<&str>) -> Vec<String> {
let mt5_dir = match self.mt5_dir() {
Some(d) => d,
None => return Vec::new(),
};
let bases_dir = mt5_dir.join("Bases");
if !bases_dir.is_dir() {
return Vec::new();
}
// Prefer the tester's own data store; fall back to live-trading Bases/ when absent.
let tester_bases = mt5_dir.join("Tester").join("bases");
let bases_dir = if tester_bases.is_dir() {
tester_bases
} else {
let fallback = mt5_dir.join("Bases");
if !fallback.is_dir() {
return Vec::new();
}
tracing::warn!(
"Tester/bases/ not found — falling back to Bases/ for symbol discovery. \
Run at least one backtest to populate tester data."
);
fallback
};
let mut symbols = std::collections::HashSet::new();
// Bases/{server}/history/{symbol}/{year}.hcc
// {bases_dir}/{server}/history/{symbol}/ — directory presence = data available
// (the tester uses .hst/.hcc files; existence of the directory is sufficient)
if let Ok(servers) = fs::read_dir(&bases_dir) {
for server in servers.filter_map(|e| e.ok()) {
let server_name_os = server.file_name();
let server_name = server_name_os.to_str().unwrap_or("");
// Skip non-directory entries and filter by server if specified
if server_name.is_empty() {
continue;
}
@@ -535,7 +560,7 @@ impl Config {
continue;
}
}
let history_dir = server.path().join("history");
if !history_dir.is_dir() {
continue;
@@ -546,23 +571,8 @@ impl Config {
if !sym_path.is_dir() {
continue;
}
// Only include if at least one .hcc file exists (has downloaded data)
let has_data = fs::read_dir(&sym_path)
.ok()
.map(|entries| {
entries.filter_map(|e| e.ok()).any(|e| {
e.path().extension()
.and_then(|x| x.to_str())
.map(|x| x == "hcc")
.unwrap_or(false)
})
})
.unwrap_or(false);
if has_data {
if let Some(name) = sym_path.file_name().and_then(|n| n.to_str()) {
symbols.insert(name.to_string());
}
if let Some(name) = sym_path.file_name().and_then(|n| n.to_str()) {
symbols.insert(name.to_string());
}
}
}
@@ -574,6 +584,59 @@ impl Config {
sorted
}
/// Find the closest available tester symbol to the one requested.
///
/// Matching priority (first hit wins):
/// 1. Exact match → `XAUUSD.cent` == `XAUUSD.cent`
/// 2. Case-insensitive exact match → `xauusd.cent` → `XAUUSD.cent`
/// 3. Strip/add common cent suffixes → `XAUUSDc` ↔ `XAUUSD.cent`
/// 4. Prefix match on the base ticker → `XAUUSD` matches `XAUUSD.cent`
pub fn resolve_symbol<'a>(requested: &str, available: &'a [String]) -> Option<&'a str> {
if available.is_empty() {
return None;
}
// 1. Exact
if let Some(s) = available.iter().find(|s| s.as_str() == requested) {
return Some(s.as_str());
}
// 2. Case-insensitive exact
let req_lower = requested.to_lowercase();
if let Some(s) = available.iter().find(|s| s.to_lowercase() == req_lower) {
return Some(s.as_str());
}
// 3. Cent-suffix normalisation: build a normalised "base" for both sides
// Strip known cent suffixes: `.cent`, `c` (trailing, uppercase only), `.c`
fn base_ticker(sym: &str) -> &str {
let s = sym.trim_end_matches(".cent")
.trim_end_matches(".c");
// Strip trailing lowercase 'c' only when the rest is all-uppercase
// (so "XAUUSDc" → "XAUUSD", but "Misc" stays "Misc")
if s.ends_with('c') && s[..s.len()-1].chars().all(|c| c.is_ascii_uppercase() || c.is_ascii_digit()) {
&s[..s.len()-1]
} else {
s
}
}
let req_base = base_ticker(requested).to_lowercase();
if let Some(s) = available.iter().find(|s| base_ticker(s).to_lowercase() == req_base) {
return Some(s.as_str());
}
// 4. Prefix match: available symbol starts with the requested string (or vice-versa)
if let Some(s) = available.iter().find(|s| {
let sl = s.to_lowercase();
sl.starts_with(&req_lower) || req_lower.starts_with(sl.as_str())
}) {
return Some(s.as_str());
}
None
}
/// Get the currently active MT5 account from common.ini
pub fn current_account(&self) -> Option<CurrentAccount> {
self.mt5_dir().and_then(|d| CurrentAccount::from_common_ini(&d))
+4 -4
View File
@@ -14,8 +14,6 @@ pub struct Report {
pub from_date: String,
pub to_date: String,
pub metrics_file: PathBuf,
pub deals_csv: PathBuf,
pub deals_json: PathBuf,
pub analysis_file: Option<PathBuf>,
}
@@ -42,8 +40,6 @@ pub struct PipelineMetadata {
pub struct FilePaths {
pub metrics: String,
pub analysis: String,
pub deals_csv: String,
pub deals_json: String,
}
#[allow(dead_code)]
@@ -83,6 +79,9 @@ pub struct BacktestJob {
pub mt5_pid: Option<u32>,
pub expected_report_path: String,
pub timeout_seconds: u64,
/// Set by background monitor: "running"|"completed"|"completed_no_html"|"failed"|"timeout"
#[serde(default)]
pub status: Option<String>,
}
impl BacktestJob {
@@ -105,6 +104,7 @@ impl BacktestJob {
mt5_pid: None,
expected_report_path,
timeout_seconds,
status: Some("running".to_string()),
}
}
}
+801 -83
View File
File diff suppressed because it is too large Load Diff
+168 -22
View File
@@ -3,6 +3,8 @@ use rusqlite::{params, Connection};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use crate::models::Deal;
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct ReportEntry {
pub id: String,
@@ -67,6 +69,25 @@ impl ReportDb {
}
let conn = self.connect()?;
conn.execute_batch("
CREATE TABLE IF NOT EXISTS deals (
id INTEGER PRIMARY KEY AUTOINCREMENT,
report_id TEXT NOT NULL REFERENCES reports(id) ON DELETE CASCADE,
time TEXT NOT NULL,
deal TEXT NOT NULL,
symbol TEXT NOT NULL,
deal_type TEXT NOT NULL,
entry TEXT NOT NULL,
volume REAL NOT NULL,
price REAL NOT NULL,
order_id TEXT NOT NULL,
commission REAL NOT NULL,
swap REAL NOT NULL,
profit REAL NOT NULL,
balance REAL NOT NULL,
comment TEXT NOT NULL,
magic TEXT
);
CREATE INDEX IF NOT EXISTS idx_deals_report_id ON deals(report_id);
CREATE TABLE IF NOT EXISTS reports (
id TEXT PRIMARY KEY,
expert TEXT NOT NULL,
@@ -102,6 +123,69 @@ impl ReportDb {
Ok(())
}
pub fn insert_deals(&self, report_id: &str, deals: &[Deal]) -> Result<()> {
if deals.is_empty() {
return Ok(());
}
let conn = self.connect()?;
let mut stmt = conn.prepare(
"INSERT INTO deals (report_id, time, deal, symbol, deal_type, entry, volume, price, \
order_id, commission, swap, profit, balance, comment, magic) \
VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?13,?14,?15)",
)?;
for d in deals {
stmt.execute(params![
report_id,
d.time,
d.deal,
d.symbol,
d.deal_type,
d.entry,
d.volume,
d.price,
d.order,
d.commission,
d.swap,
d.profit,
d.balance,
d.comment,
d.magic,
])?;
}
Ok(())
}
pub fn get_deals(&self, report_id: &str) -> Result<Vec<Deal>> {
let conn = self.connect()?;
let mut stmt = conn.prepare(
"SELECT time, deal, symbol, deal_type, entry, volume, price, order_id, \
commission, swap, profit, balance, comment, magic \
FROM deals WHERE report_id = ? ORDER BY id ASC",
)?;
let deals: Vec<Deal> = stmt
.query_map([report_id], |row| {
Ok(Deal {
time: row.get(0)?,
deal: row.get(1)?,
symbol: row.get(2)?,
deal_type: row.get(3)?,
entry: row.get(4)?,
volume: row.get(5)?,
price: row.get(6)?,
order: row.get(7)?,
commission: row.get(8)?,
swap: row.get(9)?,
profit: row.get(10)?,
balance: row.get(11)?,
comment: row.get(12)?,
magic: row.get(13)?,
})
})?
.filter_map(|r| r.ok())
.collect();
Ok(deals)
}
pub fn insert(&self, entry: &ReportEntry) -> Result<()> {
let conn = self.connect()?;
let tags_json = serde_json::to_string(&entry.tags)?;
@@ -375,6 +459,57 @@ impl ReportDb {
Ok(entry)
}
/// Get a specific report by its report_dir path (exact match)
pub fn get_by_report_dir(&self, report_dir: &str) -> Result<Option<ReportEntry>> {
let conn = self.connect()?;
let mut stmt = conn.prepare(
"SELECT id, expert, symbol, timeframe, model, from_date, to_date, \
created_at, set_file_original, set_snapshot_path, report_dir, charts_dir, \
net_profit, profit_factor, max_dd_pct, sharpe_ratio, total_trades, \
win_rate_pct, recovery_factor, deposit, currency, leverage, \
duration_seconds, tags, notes, verdict \
FROM reports WHERE report_dir = ?"
)?;
let entry = stmt
.query_map([report_dir], |row| {
let tags_json: String = row.get(23)?;
let tags: Vec<String> = serde_json::from_str(&tags_json).unwrap_or_default();
Ok(ReportEntry {
id: row.get(0)?,
expert: row.get(1)?,
symbol: row.get(2)?,
timeframe: row.get(3)?,
model: row.get(4)?,
from_date: row.get(5)?,
to_date: row.get(6)?,
created_at: row.get(7)?,
set_file_original: row.get(8)?,
set_snapshot_path: row.get(9)?,
report_dir: row.get(10)?,
charts_dir: row.get(11)?,
net_profit: row.get(12)?,
profit_factor: row.get(13)?,
max_dd_pct: row.get(14)?,
sharpe_ratio: row.get(15)?,
total_trades: row.get(16)?,
win_rate_pct: row.get(17)?,
recovery_factor: row.get(18)?,
deposit: row.get(19)?,
currency: row.get(20)?,
leverage: row.get(21)?,
duration_seconds: row.get(22)?,
tags,
notes: row.get(24)?,
verdict: row.get(25)?,
})
})?
.filter_map(|r| r.ok())
.next();
Ok(entry)
}
/// Get a specific report by ID
pub fn get_by_id(&self, id: &str) -> Result<Option<ReportEntry>> {
let conn = self.connect()?;
@@ -429,26 +564,39 @@ impl ReportDb {
/// Search reports by tags (at least one tag must match)
pub fn search_by_tags(&self, tags: &[String], limit: usize) -> Result<Vec<ReportEntry>> {
let conn = self.connect()?;
let mut sql = "SELECT id, expert, symbol, timeframe, model, from_date, to_date, \
let base_sql = "SELECT id, expert, symbol, timeframe, model, from_date, to_date, \
created_at, set_file_original, set_snapshot_path, report_dir, charts_dir, \
net_profit, profit_factor, max_dd_pct, sharpe_ratio, total_trades, \
win_rate_pct, recovery_factor, deposit, currency, leverage, \
duration_seconds, tags, notes, verdict \
FROM reports WHERE 1=1"
.to_string();
FROM reports WHERE 1=1";
// Build OR conditions for tags - use JSON1 extension for tag matching
if !tags.is_empty() {
let tag_conditions: Vec<String> = tags.iter()
.map(|tag| format!("tags LIKE '%{}%'", tag.replace("'", "''")))
let (sql, params): (String, Vec<rusqlite::types::Value>) = if tags.is_empty() {
(
format!("{} ORDER BY created_at DESC LIMIT ?1", base_sql),
vec![(limit as i64).into()],
)
} else {
let placeholders = tags.iter().enumerate()
.map(|(i, _)| format!("tags LIKE ?{}", i + 1))
.collect::<Vec<_>>()
.join(" OR ");
let sql = format!(
"{} AND ({}) ORDER BY created_at DESC LIMIT ?{}",
base_sql,
placeholders,
tags.len() + 1
);
let mut p: Vec<rusqlite::types::Value> = tags.iter()
.map(|t| format!("%{}%", t).into())
.collect();
sql.push_str(&format!(" AND ({})", tag_conditions.join(" OR ")));
}
sql.push_str(&format!(" ORDER BY created_at DESC LIMIT {}", limit));
p.push((limit as i64).into());
(sql, p)
};
let mut stmt = conn.prepare(&sql)?;
let entries: Vec<ReportEntry> = stmt
.query_map([], |row| {
.query_map(rusqlite::params_from_iter(params.iter()), |row| {
let tags_str: String = row.get(23).unwrap_or_else(|_| "[]".to_string());
let tags: Vec<String> = serde_json::from_str(&tags_str).unwrap_or_default();
Ok(ReportEntry {
@@ -489,19 +637,18 @@ impl ReportDb {
/// Search reports by notes text (case-insensitive LIKE)
pub fn search_by_notes(&self, query: &str, limit: usize) -> Result<Vec<ReportEntry>> {
let conn = self.connect()?;
let pattern = format!("%{}%", query.replace("'", "''"));
let pattern = format!("%{}%", query);
let mut stmt = conn.prepare(
&format!("SELECT id, expert, symbol, timeframe, model, from_date, to_date, \
"SELECT id, expert, symbol, timeframe, model, from_date, to_date, \
created_at, set_file_original, set_snapshot_path, report_dir, charts_dir, \
net_profit, profit_factor, max_dd_pct, sharpe_ratio, total_trades, \
win_rate_pct, recovery_factor, deposit, currency, leverage, \
duration_seconds, tags, notes, verdict \
FROM reports WHERE notes LIKE '{}' ORDER BY created_at DESC LIMIT {}",
pattern, limit)
FROM reports WHERE notes LIKE ?1 ORDER BY created_at DESC LIMIT ?2"
)?;
let entries: Vec<ReportEntry> = stmt
.query_map([], |row| {
.query_map(rusqlite::params![pattern, limit as i64], |row| {
let tags_str: String = row.get(23).unwrap_or_else(|_| "[]".to_string());
let tags: Vec<String> = serde_json::from_str(&tags_str).unwrap_or_default();
Ok(ReportEntry {
@@ -542,20 +689,19 @@ impl ReportDb {
/// Find reports by set file (original or snapshot)
pub fn search_by_set_file(&self, set_file: &str, limit: usize) -> Result<Vec<ReportEntry>> {
let conn = self.connect()?;
let pattern = format!("%{}%", set_file.replace("'", "''"));
let pattern = format!("%{}%", set_file);
let mut stmt = conn.prepare(
&format!("SELECT id, expert, symbol, timeframe, model, from_date, to_date, \
"SELECT id, expert, symbol, timeframe, model, from_date, to_date, \
created_at, set_file_original, set_snapshot_path, report_dir, charts_dir, \
net_profit, profit_factor, max_dd_pct, sharpe_ratio, total_trades, \
win_rate_pct, recovery_factor, deposit, currency, leverage, \
duration_seconds, tags, notes, verdict \
FROM reports WHERE (set_file_original LIKE '{}' OR set_snapshot_path LIKE '{}') \
ORDER BY created_at DESC LIMIT {}",
pattern, pattern, limit)
FROM reports WHERE (set_file_original LIKE ?1 OR set_snapshot_path LIKE ?1) \
ORDER BY created_at DESC LIMIT ?2"
)?;
let entries: Vec<ReportEntry> = stmt
.query_map([], |row| {
.query_map(rusqlite::params![pattern, limit as i64], |row| {
let tags_str: String = row.get(23).unwrap_or_else(|_| "[]".to_string());
let tags: Vec<String> = serde_json::from_str(&tags_str).unwrap_or_default();
Ok(ReportEntry {
+43 -29
View File
@@ -1,5 +1,8 @@
use serde_json::{json, Value};
// Common description suffix for all analytics tools
const REPORT_HINT: &str = "report_id (preferred), report_dir (legacy path), or omit for latest report.";
pub fn tool_analyze_report() -> Value {
json!({
"name": "analyze_report",
@@ -7,7 +10,8 @@ pub fn tool_analyze_report() -> Value {
"inputSchema": {
"type": "object",
"properties": {
"report_dir": { "type": "string", "description": "Path to report directory containing deals.csv" },
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory (use report_id instead)" },
"analytics": {
"type": "array",
"description": "Optional: specific analytics to run. If omitted, runs all.",
@@ -29,7 +33,8 @@ pub fn tool_analyze_monthly_pnl() -> Value {
"inputSchema": {
"type": "object",
"properties": {
"report_dir": { "type": "string", "description": "Path to report directory containing deals.csv" }
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" }
}
}
})
@@ -42,7 +47,8 @@ pub fn tool_analyze_drawdown_events() -> Value {
"inputSchema": {
"type": "object",
"properties": {
"report_dir": { "type": "string" }
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" }
}
}
})
@@ -55,7 +61,8 @@ pub fn tool_analyze_top_losses() -> Value {
"inputSchema": {
"type": "object",
"properties": {
"report_dir": { "type": "string" },
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" },
"limit": { "type": "integer", "description": "Number of losses to return (default: 10)", "default": 10 }
}
}
@@ -69,7 +76,8 @@ pub fn tool_analyze_loss_sequences() -> Value {
"inputSchema": {
"type": "object",
"properties": {
"report_dir": { "type": "string" }
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" }
}
}
})
@@ -82,7 +90,8 @@ pub fn tool_analyze_position_pairs() -> Value {
"inputSchema": {
"type": "object",
"properties": {
"report_dir": { "type": "string" }
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" }
}
}
})
@@ -95,7 +104,8 @@ pub fn tool_analyze_direction_bias() -> Value {
"inputSchema": {
"type": "object",
"properties": {
"report_dir": { "type": "string" }
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" }
}
}
})
@@ -108,7 +118,8 @@ pub fn tool_analyze_streaks() -> Value {
"inputSchema": {
"type": "object",
"properties": {
"report_dir": { "type": "string" }
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" }
}
}
})
@@ -121,7 +132,8 @@ pub fn tool_analyze_concurrent_peak() -> Value {
"inputSchema": {
"type": "object",
"properties": {
"report_dir": { "type": "string" }
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" }
}
}
})
@@ -133,9 +145,9 @@ pub fn tool_list_deals() -> Value {
"description": "List individual deals from a backtest report with optional filters",
"inputSchema": {
"type": "object",
"required": ["report_dir"],
"properties": {
"report_dir": { "type": "string", "description": "Path to report directory containing deals.csv" },
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" },
"deal_type": { "type": "string", "enum": ["buy", "sell"], "description": "Filter by deal type" },
"min_profit": { "type": "number", "description": "Minimum profit (use negative for losses)" },
"max_profit": { "type": "number", "description": "Maximum profit" },
@@ -155,9 +167,10 @@ pub fn tool_search_deals_by_comment() -> Value {
"description": "Search deals by comment text (case-insensitive partial match)",
"inputSchema": {
"type": "object",
"required": ["report_dir", "query"],
"required": ["query"],
"properties": {
"report_dir": { "type": "string" },
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" },
"query": { "type": "string", "description": "Search text in comments" },
"limit": { "type": "integer", "default": 50 }
}
@@ -171,9 +184,10 @@ pub fn tool_search_deals_by_magic() -> Value {
"description": "Filter deals by magic number (EA identifier)",
"inputSchema": {
"type": "object",
"required": ["report_dir", "magic"],
"required": ["magic"],
"properties": {
"report_dir": { "type": "string" },
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" },
"magic": { "type": "string", "description": "Magic number to filter by" },
"limit": { "type": "integer", "default": 100 }
}
@@ -187,9 +201,9 @@ pub fn tool_analyze_profit_distribution() -> Value {
"description": "Analyze profit distribution - small/medium/large wins and losses with detailed buckets",
"inputSchema": {
"type": "object",
"required": ["report_dir"],
"properties": {
"report_dir": { "type": "string" }
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" }
}
}
})
@@ -201,9 +215,9 @@ pub fn tool_analyze_time_performance() -> Value {
"description": "Analyze performance by hour of day and day of week",
"inputSchema": {
"type": "object",
"required": ["report_dir"],
"properties": {
"report_dir": { "type": "string" }
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" }
}
}
})
@@ -215,9 +229,9 @@ pub fn tool_analyze_hold_time_distribution() -> Value {
"description": "Analyze hold time distribution and correlation with profit",
"inputSchema": {
"type": "object",
"required": ["report_dir"],
"properties": {
"report_dir": { "type": "string" }
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" }
}
}
})
@@ -229,9 +243,9 @@ pub fn tool_analyze_layer_performance() -> Value {
"description": "Analyze performance by grid layer (extracted from deal comments)",
"inputSchema": {
"type": "object",
"required": ["report_dir"],
"properties": {
"report_dir": { "type": "string" }
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" }
}
}
})
@@ -243,9 +257,9 @@ pub fn tool_analyze_volume_vs_profit() -> Value {
"description": "Analyze correlation between volume and profit, plus performance by volume bucket",
"inputSchema": {
"type": "object",
"required": ["report_dir"],
"properties": {
"report_dir": { "type": "string" }
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" }
}
}
})
@@ -257,9 +271,9 @@ pub fn tool_analyze_costs() -> Value {
"description": "Analyze commission and swap costs impact on profitability",
"inputSchema": {
"type": "object",
"required": ["report_dir"],
"properties": {
"report_dir": { "type": "string" }
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" }
}
}
})
@@ -271,9 +285,9 @@ pub fn tool_analyze_efficiency() -> Value {
"description": "Calculate efficiency metrics: profit per hour/day, annualized return, trade frequency",
"inputSchema": {
"type": "object",
"required": ["report_dir"],
"properties": {
"report_dir": { "type": "string" }
"report_id": { "type": "string", "description": REPORT_HINT },
"report_dir": { "type": "string", "description": "Legacy: path to report directory" }
}
}
})
+24 -3
View File
@@ -23,7 +23,8 @@ pub fn tool_run_backtest() -> Value {
"shutdown": { "type": "boolean", "description": "Close MT5 after backtest completes" },
"kill_existing": { "type": "boolean", "description": "Kill any running MT5 instance first" },
"timeout": { "type": "integer", "description": "Max wait time in seconds (default: 900)" },
"gui": { "type": "boolean", "description": "Enable MT5 visualization window" }
"gui": { "type": "boolean", "description": "Enable MT5 visualization window" },
"startup_delay_secs": { "type": "integer", "description": "Seconds to wait for MT5 initialization (default: 10, set to 0 for default 10s)" }
}
}
})
@@ -48,7 +49,8 @@ pub fn tool_run_backtest_quick() -> Value {
"deep": { "type": "boolean", "description": "Run deep analysis" },
"shutdown": { "type": "boolean", "description": "Close MT5 after backtest" },
"timeout": { "type": "integer", "description": "Max wait time in seconds (default: 900)" },
"gui": { "type": "boolean", "description": "Enable MT5 visualization" }
"gui": { "type": "boolean", "description": "Enable MT5 visualization" },
"startup_delay_secs": { "type": "integer", "description": "Seconds to wait for MT5 initialization (default: 10)" }
}
}
})
@@ -97,7 +99,10 @@ pub fn tool_launch_backtest() -> Value {
"skip_compile": { "type": "boolean" },
"skip_clean": { "type": "boolean" },
"timeout": { "type": "integer", "description": "Max time in seconds to wait for backtest (default: 900)" },
"gui": { "type": "boolean", "description": "Enable visualization during backtest" }
"shutdown": { "type": "boolean", "description": "Shut down MT5 after test (default: true — required for HTML report to be written)" },
"gui": { "type": "boolean", "description": "Enable visualization during backtest" },
"startup_delay_secs": { "type": "integer", "description": "Seconds to wait for MT5 initialization (default: 10)" },
"inactivity_kill_secs": { "type": "integer", "description": "Kill MT5 if tester log hasn't grown for this many seconds (0 = disabled). Use to abort EAs that stop trading mid-test." }
}
}
})
@@ -116,6 +121,22 @@ pub fn tool_get_backtest_status() -> Value {
})
}
pub fn tool_get_tester_log() -> Value {
json!({
"name": "get_tester_log",
"description": "Read the active MT5 tester agent journal log. Returns parsed deals, final balance, test progress, and raw log tail. Works during a backtest (if the log is being written) or after completion. Use this to inspect what trades occurred, check for EA activity, or debug issues when the HTML report wasn't produced.",
"inputSchema": {
"type": "object",
"properties": {
"tail_lines": {
"type": "integer",
"description": "Number of log tail lines to return (default: 100)"
}
}
}
})
}
pub fn tool_cache_status() -> Value {
json!({
"name": "cache_status",
+4
View File
@@ -18,6 +18,7 @@ pub fn get_tools_list() -> Value {
backtest::tool_run_backtest_only(), // Minimal: skip compile, do clean + backtest + extract only
backtest::tool_launch_backtest(), // Fire-and-forget: compile + clean + launch MT5
backtest::tool_get_backtest_status(), // Poll for completion
backtest::tool_get_tester_log(), // Live journal reading mid-backtest or after
backtest::tool_cache_status(),
backtest::tool_clean_cache(),
// Optimization
@@ -63,6 +64,8 @@ pub fn get_tools_list() -> Value {
system::tool_list_symbols(),
system::tool_healthcheck(),
system::tool_get_active_account(),
system::tool_check_update(),
system::tool_update(),
// Utility (8 tools)
utility::tool_check_symbol_data_status(),
utility::tool_get_backtest_history(),
@@ -110,6 +113,7 @@ pub fn get_tools_list() -> Value {
reports::tool_search_reports_by_notes(),
reports::tool_get_reports_by_set_file(),
reports::tool_get_comparable_reports(),
reports::tool_export_deals_csv(),
];
serde_json::json!(tools)
+14
View File
@@ -278,3 +278,17 @@ pub fn tool_get_comparable_reports() -> Value {
}
})
}
pub fn tool_export_deals_csv() -> Value {
json!({
"name": "export_deals_csv",
"description": "Export deals for a report to a CSV file on demand. Deals are stored in the database — use this to get a CSV file for external tools.",
"inputSchema": {
"type": "object",
"properties": {
"report_id": { "type": "string", "description": "Report ID to export (default: latest report)" },
"output_path": { "type": "string", "description": "File path for the CSV output (default: <report_dir>/deals.csv)" }
}
}
})
}
+22
View File
@@ -45,3 +45,25 @@ pub fn tool_get_active_account() -> Value {
}
})
}
pub fn tool_check_update() -> Value {
json!({
"name": "check_update",
"description": "Check if a newer version of mt5-quant is available on GitHub. A background check runs automatically on the first tool call of each session; this tool returns that cached result instantly or fetches it on demand.",
"inputSchema": {
"type": "object",
"properties": {}
}
})
}
pub fn tool_update() -> Value {
json!({
"name": "update",
"description": "Download and install the latest mt5-quant binary from GitHub Releases, then replace the current executable in place. Restart the MCP connection after updating to load the new version.",
"inputSchema": {
"type": "object",
"properties": {}
}
})
}
+1 -1
View File
@@ -289,7 +289,7 @@ pub fn tool_get_wine_prefix_info() -> Value {
pub fn tool_get_backtest_crash_info() -> Value {
json!({
"name": "get_backtest_crash_info",
"description": "Investigate backtest crashes/failures. Checks for incomplete markers, missing deals.csv, error logs. Can scan recent reports.",
"description": "Investigate backtest crashes/failures. Checks for incomplete markers, missing metrics.json, DB deal count, error logs. Can scan recent reports.",
"inputSchema": {
"type": "object",
"properties": {
+53 -91
View File
@@ -7,6 +7,7 @@ use crate::analytics::DealAnalyzer;
use crate::models::deals::Deal;
use crate::models::metrics::Metrics;
use crate::models::Config;
use crate::storage::ReportDb;
// ── Internal helpers ──────────────────────────────────────────────────────────
@@ -30,66 +31,46 @@ fn err_response(msg: impl std::fmt::Display) -> Value {
})
}
fn load_report_data(report_dir: &str) -> Result<(Vec<Deal>, Metrics)> {
let deals_csv = Path::new(report_dir).join("deals.csv");
let metrics_json = Path::new(report_dir).join("metrics.json");
/// Resolve a report from args (report_id > report_dir > latest).
/// Returns (deals, metrics, report_dir).
fn resolve_report(args: &Value) -> Result<(Vec<Deal>, Metrics, String)> {
let db = ReportDb::new(&Config::db_path());
db.init()?;
if !deals_csv.exists() {
return Err(anyhow::anyhow!("deals.csv not found in {}", report_dir));
}
let entry = if let Some(id) = args.get("report_id").and_then(|v| v.as_str()) {
db.get_by_id(id)?
.ok_or_else(|| anyhow::anyhow!("Report '{}' not found in DB", id))?
} else if let Some(dir) = args.get("report_dir").and_then(|v| v.as_str()) {
db.get_by_report_dir(dir)?
.ok_or_else(|| anyhow::anyhow!(
"No DB entry for report_dir '{}'. This report may predate DB storage.", dir
))?
} else {
db.get_latest()?
.ok_or_else(|| anyhow::anyhow!("No reports in DB. Run a backtest first."))?
};
let deals = read_deals_from_csv(&deals_csv)?;
let metrics = if metrics_json.exists() {
serde_json::from_str(&fs::read_to_string(&metrics_json)?)?
let deals = db.get_deals(&entry.id)?;
let metrics_path = Path::new(&entry.report_dir).join("metrics.json");
let metrics = if metrics_path.exists() {
serde_json::from_str(&fs::read_to_string(&metrics_path)?)?
} else {
Metrics::default()
};
Ok((deals, metrics))
Ok((deals, metrics, entry.report_dir))
}
fn prepare_analysis(report_dir: &str) -> Result<(Vec<Deal>, Metrics, DealAnalyzer)> {
let (deals, metrics) = load_report_data(report_dir)?;
Ok((deals, metrics, DealAnalyzer::new()))
}
fn read_deals_from_csv(path: &Path) -> Result<Vec<Deal>> {
let content = fs::read_to_string(path)?;
let mut deals = Vec::new();
let mut lines = content.lines();
let _header = lines.next();
for line in lines {
let parts: Vec<&str> = line.split(',').collect();
if parts.len() >= 12 {
deals.push(Deal {
time: parts[0].to_string(),
deal: parts[1].to_string(),
symbol: parts[2].to_string(),
deal_type: parts[3].to_string(),
entry: parts[4].to_string(),
volume: parts[5].parse().unwrap_or(0.0),
price: parts[6].parse().unwrap_or(0.0),
order: parts[7].to_string(),
commission: parts[8].parse().unwrap_or(0.0),
swap: parts[9].parse().unwrap_or(0.0),
profit: parts[10].parse().unwrap_or(0.0),
balance: parts[11].parse().unwrap_or(0.0),
comment: parts.get(12).unwrap_or(&"").to_string(),
magic: parts.get(13).map(|s| s.to_string()),
});
}
}
Ok(deals)
fn prepare_analysis(args: &Value) -> Result<(Vec<Deal>, Metrics, DealAnalyzer, String)> {
let (deals, metrics, report_dir) = resolve_report(args)?;
Ok((deals, metrics, DealAnalyzer::new(), report_dir))
}
// ── Composite analytics ───────────────────────────────────────────────────────
pub async fn handle_analyze_report(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, metrics, analyzer) = prepare_analysis(report_dir)?;
let (deals, metrics, analyzer, report_dir) = prepare_analysis(args)?;
let requested: Option<HashSet<String>> = args.get("analytics")
.and_then(|v| v.as_array())
@@ -111,7 +92,7 @@ pub async fn handle_analyze_report(_config: &Config, args: &Value) -> Result<Val
if req("streak_analysis") { result["streak_analysis"] = json!(analyzer.streak_analysis(&deals)); }
if req("concurrent_peak") { result["concurrent_peak"] = json!(analyzer.concurrent_peak(&deals)); }
let analysis_path = Path::new(report_dir).join("analysis.json");
let analysis_path = Path::new(&report_dir).join("analysis.json");
fs::write(&analysis_path, serde_json::to_string_pretty(&result)?)?;
Ok(ok_response(json!({
@@ -123,10 +104,10 @@ pub async fn handle_analyze_report(_config: &Config, args: &Value) -> Result<Val
}
pub async fn handle_compare_baseline(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (_, _, _, report_dir) = prepare_analysis(args)?;
let baseline_path = Path::new("config/baseline.json");
let metrics_path = Path::new(report_dir).join("metrics.json");
let metrics_path = Path::new(&report_dir).join("metrics.json");
if !baseline_path.exists() {
return Ok(ok_response(json!("No baseline.json found in config/")));
@@ -150,93 +131,78 @@ pub async fn handle_compare_baseline(_config: &Config, args: &Value) -> Result<V
// ── Granular analytics handlers ───────────────────────────────────────────────
pub async fn handle_analyze_monthly_pnl(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, _, analyzer) = prepare_analysis(report_dir)?;
let (deals, _, analyzer, _) = prepare_analysis(args)?;
Ok(ok_response(json!({ "success": true, "monthly_pnl": analyzer.monthly_pnl(&deals) })))
}
pub async fn handle_analyze_drawdown_events(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, metrics, analyzer) = prepare_analysis(report_dir)?;
let (deals, metrics, analyzer, _) = prepare_analysis(args)?;
Ok(ok_response(json!({ "success": true, "drawdown_events": analyzer.reconstruct_dd_events(&deals, &metrics) })))
}
pub async fn handle_analyze_top_losses(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let limit = args.get("limit").and_then(|v| v.as_u64()).unwrap_or(10) as usize;
let (deals, _, analyzer) = prepare_analysis(report_dir)?;
let (deals, _, analyzer, _) = prepare_analysis(args)?;
Ok(ok_response(json!({ "success": true, "limit": limit, "top_losses": analyzer.top_losses(&deals, limit) })))
}
pub async fn handle_analyze_loss_sequences(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, _, analyzer) = prepare_analysis(report_dir)?;
let (deals, _, analyzer, _) = prepare_analysis(args)?;
Ok(ok_response(json!({ "success": true, "loss_sequences": analyzer.loss_sequences(&deals) })))
}
pub async fn handle_analyze_position_pairs(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, _, analyzer) = prepare_analysis(report_dir)?;
let (deals, _, analyzer, _) = prepare_analysis(args)?;
Ok(ok_response(json!({ "success": true, "position_pairs": analyzer.position_pairs(&deals) })))
}
pub async fn handle_analyze_direction_bias(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, _, analyzer) = prepare_analysis(report_dir)?;
let (deals, _, analyzer, _) = prepare_analysis(args)?;
Ok(ok_response(json!({ "success": true, "direction_bias": analyzer.direction_bias(&deals) })))
}
pub async fn handle_analyze_streaks(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, _, analyzer) = prepare_analysis(report_dir)?;
let (deals, _, analyzer, _) = prepare_analysis(args)?;
Ok(ok_response(json!({ "success": true, "streak_analysis": analyzer.streak_analysis(&deals) })))
}
pub async fn handle_analyze_concurrent_peak(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, _, analyzer) = prepare_analysis(report_dir)?;
let (deals, _, analyzer, _) = prepare_analysis(args)?;
Ok(ok_response(json!({ "success": true, "concurrent_peak": analyzer.concurrent_peak(&deals) })))
}
pub async fn handle_analyze_profit_distribution(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, _, analyzer) = prepare_analysis(report_dir)?;
let (deals, _, analyzer, _) = prepare_analysis(args)?;
Ok(ok_response(json!({ "success": true, "profit_distribution": analyzer.profit_distribution(&deals) })))
}
pub async fn handle_analyze_time_performance(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, _, analyzer) = prepare_analysis(report_dir)?;
let (deals, _, analyzer, _) = prepare_analysis(args)?;
Ok(ok_response(json!({ "success": true, "time_performance": analyzer.time_performance(&deals) })))
}
pub async fn handle_analyze_hold_time_distribution(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, _, analyzer) = prepare_analysis(report_dir)?;
let (deals, _, analyzer, _) = prepare_analysis(args)?;
Ok(ok_response(json!({ "success": true, "hold_time_analysis": analyzer.hold_time_analysis(&deals) })))
}
pub async fn handle_analyze_layer_performance(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, _, analyzer) = prepare_analysis(report_dir)?;
let (deals, _, analyzer, _) = prepare_analysis(args)?;
Ok(ok_response(json!({ "success": true, "layer_performance": analyzer.layer_performance(&deals) })))
}
pub async fn handle_analyze_volume_vs_profit(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, _, analyzer) = prepare_analysis(report_dir)?;
let (deals, _, analyzer, _) = prepare_analysis(args)?;
Ok(ok_response(json!({ "success": true, "volume_analysis": analyzer.volume_analysis(&deals) })))
}
pub async fn handle_analyze_costs(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, _, analyzer) = prepare_analysis(report_dir)?;
let (deals, _, analyzer, _) = prepare_analysis(args)?;
Ok(ok_response(json!({ "success": true, "cost_analysis": analyzer.cost_analysis(&deals) })))
}
pub async fn handle_analyze_efficiency(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, metrics, analyzer) = prepare_analysis(report_dir)?;
let (deals, metrics, analyzer, _) = prepare_analysis(args)?;
Ok(ok_response(json!({ "success": true, "efficiency_analysis": analyzer.efficiency_analysis(&deals, &metrics) })))
}
@@ -259,12 +225,13 @@ fn deal_to_json(d: &Deal) -> Value {
}
fn is_closed_trade(d: &Deal) -> bool {
d.entry.to_lowercase().contains("out") && d.profit != 0.0
// Accept any "out" entry. Profit may legitimately be 0.0 for journal-extracted
// deals (where per-deal P&L is unavailable), so we don't gate on profit != 0.0.
d.entry.to_lowercase().contains("out")
}
pub async fn handle_list_deals(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let (deals, _) = load_report_data(report_dir)?;
let (deals, _, _, _) = prepare_analysis(args)?;
let deal_type = args.get("deal_type").and_then(|v| v.as_str());
let min_profit = args.get("min_profit").and_then(|v| v.as_f64());
@@ -299,11 +266,9 @@ pub async fn handle_list_deals(_config: &Config, args: &Value) -> Result<Value>
}
pub async fn handle_search_deals_by_comment(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let query = required_str(args, "query")?;
let limit = args.get("limit").and_then(|v| v.as_u64()).unwrap_or(50) as usize;
let (deals, _) = load_report_data(report_dir)?;
let (deals, _, _, _) = prepare_analysis(args)?;
let query_lower = query.to_lowercase();
let mut filtered: Vec<&Deal> = deals.iter()
@@ -322,11 +287,9 @@ pub async fn handle_search_deals_by_comment(_config: &Config, args: &Value) -> R
}
pub async fn handle_search_deals_by_magic(_config: &Config, args: &Value) -> Result<Value> {
let report_dir = required_str(args, "report_dir")?;
let magic = required_str(args, "magic")?;
let limit = args.get("limit").and_then(|v| v.as_u64()).unwrap_or(100) as usize;
let (deals, _) = load_report_data(report_dir)?;
let (deals, _, _, _) = prepare_analysis(args)?;
let mut filtered: Vec<&Deal> = deals.iter()
.filter(|d| is_closed_trade(d) && d.magic.as_ref().map(|m| m.contains(magic)).unwrap_or(false))
@@ -343,6 +306,5 @@ pub async fn handle_search_deals_by_magic(_config: &Config, args: &Value) -> Res
})))
}
// suppress unused warning — err_response is available for future handlers
#[allow(dead_code)]
fn _use_err_response() { let _ = err_response(""); }
fn _err_response_available() { let _ = err_response(""); }
+251 -85
View File
@@ -74,66 +74,87 @@ pub async fn handle_run_backtest(config: &Config, args: &Value) -> Result<Value>
.and_then(|v| v.as_str())
.unwrap_or("");
let symbol = if requested_symbol.is_empty() {
// Use config default or first available symbol
if let Some(default) = config.backtest_symbol.clone() {
if preflight.available_symbols.contains(&default) {
default
} else if let Some(first) = preflight.available_symbols.first() {
tracing::warn!("Default symbol {} not found for server {}; using {}", default, active_server, first);
first.clone()
} else {
// No tester data at all → hard fail with helpful context
let no_symbols_error = || json!({
"content": [{ "type": "text", "text": json!({
"error": format!("No symbols available for backtesting on server '{}'.", active_server),
"account": { "login": active_login, "server": active_server },
"hint": "Open MT5 → View → Strategy Tester → download history for at least one symbol.",
"pre_check": "no_symbols"
}).to_string() }],
"isError": true
});
let symbol: String = if requested_symbol.is_empty() {
// No symbol requested — use config default or first available tester symbol.
let candidate = config.backtest_symbol.as_deref().unwrap_or("");
if candidate.is_empty() {
preflight.available_symbols.first()
.cloned()
.ok_or(()
).unwrap_or_else(|_| return String::new())
} else {
match Config::resolve_symbol(candidate, &preflight.available_symbols) {
Some(resolved) => {
if resolved != candidate {
tracing::warn!(
"Config symbol '{}' not in tester data for '{}'; using '{}' instead",
candidate, active_server, resolved
);
}
resolved.to_string()
}
None => {
// Config default has no tester data — pick first available
preflight.available_symbols.first()
.cloned()
.unwrap_or_default()
}
}
}
} else {
// Caller specified a symbol — resolve it against actual tester data.
if preflight.available_symbols.is_empty() {
return Ok(no_symbols_error());
}
match Config::resolve_symbol(requested_symbol, &preflight.available_symbols) {
Some(resolved) if resolved == requested_symbol => {
// Exact match — use as-is
resolved.to_string()
}
Some(resolved) => {
// Fuzzy match — proceed with the corrected symbol, surface the substitution
tracing::warn!(
"Symbol '{}' not in tester data for '{}'; substituting '{}'",
requested_symbol, active_server, resolved
);
resolved.to_string()
}
None => {
// No match at all — fail with full context so the caller can act
return Ok(json!({
"content": [{ "type": "text", "text": json!({
"error": format!("No symbols available for backtesting on server '{}'.", active_server),
"account": {
"login": active_login,
"server": active_server
},
"hint": "Download historical data in MT5 Strategy Tester for this server.",
"pre_check": "no_symbols",
"suggestion": "Use get_active_account to see available symbols."
"error": format!(
"Symbol '{}' has no tester data on server '{}' and no close match was found.",
requested_symbol, active_server
),
"account": { "login": active_login, "server": active_server },
"requested_symbol": requested_symbol,
"available_symbols": preflight.available_symbols,
"hint": "The tester data for this symbol hasn't been downloaded yet. \
Open MT5 Strategy Tester select the symbol and click Download.",
"pre_check": "symbol_not_available"
}).to_string() }],
"isError": true
}));
}
} else if let Some(first) = preflight.available_symbols.first() {
first.clone()
} else {
return Ok(json!({
"content": [{ "type": "text", "text": json!({
"error": format!("No symbols available for backtesting on server '{}'.", active_server),
"account": {
"login": active_login,
"server": active_server
},
"hint": "Download historical data in MT5 Strategy Tester for this server.",
"pre_check": "no_symbols",
"suggestion": "Use get_active_account to see available symbols."
}).to_string() }],
"isError": true
}));
}
} else {
if !preflight.available_symbols.is_empty() && !preflight.available_symbols.contains(&requested_symbol.to_string()) {
return Ok(json!({
"content": [{ "type": "text", "text": json!({
"error": format!("Symbol '{}' is not available for server '{}'.", requested_symbol, active_server),
"account": {
"login": active_login,
"server": active_server
},
"requested_symbol": requested_symbol,
"available_symbols": preflight.available_symbols,
"hint": "The symbol may not have history data for this account's server. Use list_symbols to see available symbols.",
"pre_check": "symbol_not_available",
"suggestion": "Either switch to a different MT5 account with this symbol's data, or download history for this symbol on the current server."
}).to_string() }],
"isError": true
}));
}
requested_symbol.to_string()
};
// Guard against the empty-string edge case (no symbols at all)
if symbol.is_empty() {
return Ok(no_symbols_error());
}
// EA existence check with context
if !preflight.ea_exists {
@@ -147,12 +168,12 @@ pub async fn handle_run_backtest(config: &Config, args: &Value) -> Result<Value>
}));
}
// Date defaulting: past complete calendar month
// Date defaulting: current month
let (from_date, to_date) = {
let f = args.get("from_date").and_then(|v| v.as_str()).unwrap_or("");
let t = args.get("to_date").and_then(|v| v.as_str()).unwrap_or("");
if f.is_empty() || t.is_empty() {
super::past_complete_month()
super::current_month()
} else {
(f.to_string(), t.to_string())
}
@@ -172,10 +193,12 @@ pub async fn handle_run_backtest(config: &Config, args: &Value) -> Result<Value>
skip_clean: args.get("skip_clean").and_then(|v| v.as_bool()).unwrap_or(false),
skip_analyze: args.get("skip_analyze").and_then(|v| v.as_bool()).unwrap_or(false),
deep_analyze: args.get("deep").and_then(|v| v.as_bool()).unwrap_or(false),
shutdown: args.get("shutdown").and_then(|v| v.as_bool()).unwrap_or(false),
shutdown: args.get("shutdown").and_then(|v| v.as_bool()).unwrap_or(true),
kill_existing: args.get("kill_existing").and_then(|v| v.as_bool()).unwrap_or(false),
timeout: args.get("timeout").and_then(|v| v.as_u64()).unwrap_or(900),
gui: args.get("gui").and_then(|v| v.as_bool()).unwrap_or(false),
startup_delay_secs: args.get("startup_delay_secs").and_then(|v| v.as_u64()).unwrap_or(0),
inactivity_kill_secs: args.get("inactivity_kill_secs").and_then(|v| v.as_u64()),
};
let pipeline = BacktestPipeline::new(config.clone());
@@ -192,33 +215,35 @@ pub async fn handle_run_backtest(config: &Config, args: &Value) -> Result<Value>
}))
}
pub async fn handle_run_backtest_quick(config: &Config, args: &Value) -> Result<Value> {
// Quick backtest: skip compile, do clean → backtest → extract → analyze
pub async fn handle_run_backtest_quick(handler: &crate::tools::handlers::ToolHandler, args: &Value) -> Result<Value> {
// Quick backtest: skip compile, clean → launch → background monitor → return job.
// Uses the fire-and-forget launch path so the MCP response is returned immediately
// and the result is available via get_backtest_status / get_latest_report once done.
// (The synchronous blocking path exceeds MCP request timeouts for any backtest >2 min.)
let mut args = args.clone();
if let Some(obj) = args.as_object_mut() {
obj.insert("skip_compile".to_string(), json!(true));
// keep skip_analyze as false (default) to run analysis
}
handle_run_backtest(config, &args).await
handle_launch_backtest(handler, &args).await
}
pub async fn handle_run_backtest_only(config: &Config, args: &Value) -> Result<Value> {
// Backtest only: skip compile, skip analyze - just backtest and extract
pub async fn handle_run_backtest_only(handler: &crate::tools::handlers::ToolHandler, args: &Value) -> Result<Value> {
// Backtest only: skip compile and analyze — launch and return job immediately.
let mut args = args.clone();
if let Some(obj) = args.as_object_mut() {
obj.insert("skip_compile".to_string(), json!(true));
obj.insert("skip_analyze".to_string(), json!(true));
}
handle_run_backtest(config, &args).await
handle_launch_backtest(handler, &args).await
}
pub async fn handle_launch_backtest(config: &Config, args: &Value) -> Result<Value> {
pub async fn handle_launch_backtest(handler: &crate::tools::handlers::ToolHandler, args: &Value) -> Result<Value> {
let expert = args.get("expert")
.and_then(|v| v.as_str())
.ok_or_else(|| anyhow::anyhow!("expert is required"))?;
// Run pre-flight check
let preflight = BacktestPreflight::check(config, expert);
let preflight = BacktestPreflight::check(&handler.config, expert);
// Check account session
if preflight.account.is_none() {
@@ -231,17 +256,50 @@ pub async fn handle_launch_backtest(config: &Config, args: &Value) -> Result<Val
}));
}
// Get symbol
// Get symbol — resolve against actual tester data (same logic as handle_run_backtest)
let active_server = preflight.server.as_deref().unwrap_or("unknown");
let requested_symbol = args.get("symbol")
.and_then(|v| v.as_str())
.unwrap_or("");
let symbol = if requested_symbol.is_empty() {
config.backtest_symbol.clone()
.or_else(|| preflight.available_symbols.first().cloned())
.unwrap_or_else(|| "EURUSD".to_string())
let symbol: String = if requested_symbol.is_empty() {
let candidate = handler.config.backtest_symbol.as_deref().unwrap_or("");
if candidate.is_empty() {
preflight.available_symbols.first().cloned().unwrap_or_default()
} else {
Config::resolve_symbol(candidate, &preflight.available_symbols)
.map(|s| s.to_string())
.or_else(|| preflight.available_symbols.first().cloned())
.unwrap_or_else(|| candidate.to_string())
}
} else {
requested_symbol.to_string()
match Config::resolve_symbol(requested_symbol, &preflight.available_symbols) {
Some(resolved) => {
if resolved != requested_symbol {
tracing::warn!(
"launch_backtest: symbol '{}' not in tester data for '{}'; using '{}'",
requested_symbol, active_server, resolved
);
}
resolved.to_string()
}
None if preflight.available_symbols.is_empty() => requested_symbol.to_string(),
None => {
return Ok(json!({
"content": [{ "type": "text", "text": json!({
"error": format!(
"Symbol '{}' has no tester data on server '{}' and no close match was found.",
requested_symbol, active_server
),
"requested_symbol": requested_symbol,
"available_symbols": preflight.available_symbols,
"hint": "Open MT5 → Strategy Tester → select the symbol and click Download.",
"pre_check": "symbol_not_available"
}).to_string() }],
"isError": true
}));
}
}
};
// EA existence check
@@ -260,7 +318,7 @@ pub async fn handle_launch_backtest(config: &Config, args: &Value) -> Result<Val
let f = args.get("from_date").and_then(|v| v.as_str()).unwrap_or("");
let t = args.get("to_date").and_then(|v| v.as_str()).unwrap_or("");
if f.is_empty() || t.is_empty() {
super::past_complete_month()
super::current_month()
} else {
(f.to_string(), t.to_string())
}
@@ -280,13 +338,24 @@ pub async fn handle_launch_backtest(config: &Config, args: &Value) -> Result<Val
skip_clean: args.get("skip_clean").and_then(|v| v.as_bool()).unwrap_or(false),
skip_analyze: true, // Not needed for launch mode
deep_analyze: false,
shutdown: false, // Don't shutdown so we can poll
// On Wine/macOS, ShutdownTerminal=1 does NOT reliably cause terminal64.exe to exit.
// When inactivity_kill_secs is set, the monitor waits that many seconds after the
// tester log goes quiet, then polls for the HTML report for 30 s, then kills MT5.
// If no inactivity_kill_secs is given (default None → disabled), the monitor relies
// solely on timeout (900 s) or natural MT5 exit for completion detection.
shutdown: args.get("shutdown").and_then(|v| v.as_bool()).unwrap_or(true),
kill_existing: false,
timeout: args.get("timeout").and_then(|v| v.as_u64()).unwrap_or(900),
gui: args.get("gui").and_then(|v| v.as_bool()).unwrap_or(false),
startup_delay_secs: args.get("startup_delay_secs").and_then(|v| v.as_u64()).unwrap_or(0),
inactivity_kill_secs: args.get("inactivity_kill_secs").and_then(|v| v.as_u64()),
};
let pipeline = BacktestPipeline::new(config.clone());
let pipeline = if let Some(ref callback) = handler.notification_callback {
BacktestPipeline::with_notification_callback(handler.config.clone(), callback.clone())
} else {
BacktestPipeline::new(handler.config.clone())
};
let job = pipeline.launch_backtest(params).await?;
Ok(json!({
@@ -342,16 +411,27 @@ pub async fn handle_get_backtest_status(_config: &Config, args: &Value) -> Resul
// Check if MT5 is running
let mt5_running = is_mt5_running();
// Check if report file exists
// Check if report file exists (still on disk — it's deleted after extraction)
let report_found = job.as_ref()
.map(|j| Path::new(&j.expected_report_path).exists())
.unwrap_or(false);
// Check for completed artifacts
let metrics_exists = report_path.join("metrics.json").exists();
let deals_exists = report_path.join("deals.csv").exists();
let is_complete = stage == "DONE" || (report_found && metrics_exists);
// Read the authoritative status written by the background monitor into job.json.
// This avoids false "failed" when the HTML was extracted+deleted (report_found=false)
// or when journal extraction ran instead of HTML extraction.
let job_status = job.as_ref()
.and_then(|j| j.status.as_deref())
.unwrap_or("");
let monitor_says_complete = matches!(job_status, "completed" | "completed_no_html");
let monitor_says_failed = matches!(job_status, "failed" | "timeout" | "timeout_inactive");
let is_complete = monitor_says_complete
|| stage == "DONE"
|| (report_found && metrics_exists);
// Calculate elapsed time if job exists
let elapsed_seconds = job.as_ref()
.and_then(|j| {
@@ -360,26 +440,32 @@ pub async fn handle_get_backtest_status(_config: &Config, args: &Value) -> Resul
.map(|t| (chrono::Utc::now() - t.with_timezone(&chrono::Utc)).num_seconds())
})
.unwrap_or(0);
// Determine status message
// Determine status message — trust monitor's job.json first
let status_msg = if is_complete {
"completed"
} else if monitor_says_failed {
if job_status == "timeout" || job_status == "timeout_inactive" { "timeout" } else { "failed" }
} else if stage == "BACKTEST" && mt5_running {
"running"
} else if stage == "BACKTEST" && !mt5_running && !report_found {
} else if stage == "BACKTEST" && !mt5_running {
"failed"
} else if progress_lines > 0 {
"in_progress"
} else {
"not_started"
};
let message = if is_complete {
"Backtest completed successfully"
if job_status == "completed_no_html" {
"Backtest completed (extracted from tester journal — no HTML report)"
} else {
"Backtest completed successfully"
}
} else if stage == "BACKTEST" && mt5_running {
"MT5 is running the backtest"
} else if stage == "BACKTEST" && !mt5_running {
"MT5 process exited but report not found - backtest may have failed"
"MT5 process exited report not yet found"
} else {
&format!("Backtest is at stage: {}", stage)
};
@@ -394,7 +480,6 @@ pub async fn handle_get_backtest_status(_config: &Config, args: &Value) -> Resul
"mt5_running": mt5_running,
"report_found": report_found,
"metrics_extracted": metrics_exists,
"deals_extracted": deals_exists,
"elapsed_seconds": elapsed_seconds,
"message": message,
"job": job.map(|j| {
@@ -429,6 +514,87 @@ fn is_mt5_running() -> bool {
})
}
/// Read the active tester agent log for live/post-test deal inspection.
/// Returns the last N lines and a parsed deal summary.
pub async fn handle_get_tester_log(config: &Config, args: &Value) -> Result<Value> {
use crate::pipeline::backtest::BacktestPipeline;
let tail_lines = args.get("tail_lines").and_then(|v| v.as_u64()).unwrap_or(100) as usize;
let log_path = match BacktestPipeline::find_active_tester_agent_log(config) {
Some(p) => p,
None => {
return Ok(json!({
"content": [{ "type": "text", "text": json!({
"error": "No tester agent log found for today.",
"hint": "Run a backtest first. The log appears after the tester starts."
}).to_string() }],
"isError": true
}));
}
};
let lines = match BacktestPipeline::read_tester_agent_log(&log_path) {
Some(l) => l,
None => {
return Ok(json!({
"content": [{ "type": "text", "text": json!({
"error": format!("Could not read log at {}", log_path.display())
}).to_string() }],
"isError": true
}));
}
};
let (deals, final_balance_pips, progress) = BacktestPipeline::parse_journal_deals(&lines);
// Collect tail lines
let tail: Vec<&str> = lines.iter()
.rev()
.take(tail_lines)
.rev()
.map(|s| s.as_str())
.collect();
// Detect last sim timestamp for progress estimation
let last_sim_time = lines.iter().rev()
.find_map(|l| {
let parts: Vec<&str> = l.split_whitespace().collect();
// Format: XX 0 HH:MM:SS.mmm Core NN YYYY.MM.DD HH:MM:SS ...
if parts.len() >= 6 {
let date = parts[4];
let time = parts[5];
if date.contains('.') && time.contains(':') {
return Some(format!("{} {}", date, time));
}
}
None
})
.unwrap_or_default();
Ok(json!({
"content": [{ "type": "text", "text": json!({
"log_path": log_path.to_string_lossy(),
"total_lines": lines.len(),
"deals_found": deals.len(),
"final_balance_pips": final_balance_pips,
"progress": progress,
"last_sim_time": last_sim_time,
"is_complete": !progress.is_empty(),
"tail_lines": tail,
"deals_summary": deals.iter().map(|d| json!({
"deal": d.deal,
"time": d.time,
"type": d.deal_type,
"volume": d.volume,
"price": d.price,
"symbol": d.symbol,
})).collect::<Vec<_>>()
}).to_string() }],
"isError": false
}))
}
pub async fn handle_cache_status(config: &Config) -> Result<Value> {
let cache_dir = config.tester_cache_dir.as_ref()
.map(|s| Path::new(s))
+58 -8
View File
@@ -2,8 +2,12 @@ use anyhow::Result;
use chrono::Datelike;
use serde_json::{json, Value};
use std::path::Path;
use std::sync::{Arc, OnceLock};
use std::sync::atomic::{AtomicBool, Ordering};
use crate::models::Config;
type NotificationCallback = Arc<dyn Fn(&str, serde_json::Value) + Send + Sync>;
mod system;
mod experts;
mod backtest;
@@ -13,23 +17,45 @@ mod setfiles;
mod reports;
mod utility;
#[derive(Debug)]
/// Cached result of the background update check.
/// - Not yet initialized: check still in flight (or not spawned yet)
/// - Some(version): a newer version is available
/// - None: already on latest, or GitHub unreachable
pub(crate) static LATEST_VERSION: OnceLock<Option<String>> = OnceLock::new();
static BACKGROUND_CHECK_SPAWNED: AtomicBool = AtomicBool::new(false);
#[derive(Clone)]
pub struct ToolHandler {
pub config: Config,
pub notification_callback: Option<NotificationCallback>,
}
impl ToolHandler {
pub fn new(config: Config) -> Self {
Self { config }
Self { config, notification_callback: None }
}
pub fn with_notification_callback(config: Config, callback: NotificationCallback) -> Self {
Self { config, notification_callback: Some(callback) }
}
pub async fn handle(&self, name: &str, args: &Value) -> Result<Value> {
// Spawn a one-shot background update check on the very first tool call of the session.
if !BACKGROUND_CHECK_SPAWNED.swap(true, Ordering::Relaxed) {
tokio::spawn(async {
let result = system::fetch_latest_version().await;
let _ = LATEST_VERSION.set(result);
});
}
match name {
// System handlers
"verify_setup" => system::handle_verify_setup(&self.config).await,
"list_symbols" => system::handle_list_symbols(&self.config).await,
"healthcheck" => system::handle_healthcheck(&self.config, args).await,
"verify_setup" => system::handle_verify_setup(&self.config).await,
"list_symbols" => system::handle_list_symbols(&self.config).await,
"healthcheck" => system::handle_healthcheck(&self.config, args).await,
"get_active_account" => system::handle_get_active_account(&self.config).await,
"check_update" => system::handle_check_update(&self.config).await,
"update" => system::handle_update(&self.config).await,
// Expert/Indicator/Script handlers
"list_experts" => experts::handle_list_experts(&self.config, args).await,
@@ -44,10 +70,11 @@ impl ToolHandler {
// Backtest handlers - Granular pipeline options
"run_backtest" => backtest::handle_run_backtest(&self.config, args).await, // Full: compile + clean + backtest + extract + analyze
"run_backtest_quick" => backtest::handle_run_backtest_quick(&self.config, args).await, // Quick: skip compile, do backtest + extract + analyze
"run_backtest_only" => backtest::handle_run_backtest_only(&self.config, args).await, // Minimal: skip compile, do backtest + extract only
"launch_backtest" => backtest::handle_launch_backtest(&self.config, args).await, // Fire-and-forget mode
"run_backtest_quick" => backtest::handle_run_backtest_quick(self, args).await, // Quick: skip compile, fire-and-forget launch
"run_backtest_only" => backtest::handle_run_backtest_only(self, args).await, // Minimal: skip compile+analyze, fire-and-forget launch
"launch_backtest" => backtest::handle_launch_backtest(self, args).await, // Fire-and-forget mode
"get_backtest_status" => backtest::handle_get_backtest_status(&self.config, args).await,
"get_tester_log" => backtest::handle_get_tester_log(&self.config, args).await,
"cache_status" => backtest::handle_cache_status(&self.config).await,
"clean_cache" => backtest::handle_clean_cache(&self.config, args).await,
@@ -109,6 +136,7 @@ impl ToolHandler {
"search_reports_by_notes" => reports::handle_search_reports_by_notes(args).await,
"get_reports_by_set_file" => reports::handle_get_reports_by_set_file(args).await,
"get_comparable_reports" => reports::handle_get_comparable_reports(args).await,
"export_deals_csv" => reports::handle_export_deals_csv(&self.config, args).await,
// Utility handlers
"check_symbol_data_status" => utility::handle_check_symbol_data_status(&self.config, args).await,
@@ -152,6 +180,7 @@ pub(crate) fn dir_size(path: &Path) -> u64 {
.sum()
}
#[allow(dead_code)]
pub(crate) fn past_complete_month() -> (String, String) {
let now = chrono::Utc::now();
let today = chrono::NaiveDate::from_ymd_opt(now.year(), now.month(), 1)
@@ -165,3 +194,24 @@ pub(crate) fn past_complete_month() -> (String, String) {
last_of_prev.format("%Y.%m.%d").to_string(),
)
}
pub(crate) fn current_month() -> (String, String) {
let now = chrono::Utc::now();
let first_of_month = chrono::NaiveDate::from_ymd_opt(now.year(), now.month(), 1)
.unwrap_or_else(|| chrono::NaiveDate::from_ymd_opt(2024, 1, 1).unwrap());
let last_of_month = if now.month() == 12 {
chrono::NaiveDate::from_ymd_opt(now.year() + 1, 1, 1)
.unwrap_or(first_of_month)
.pred_opt()
.unwrap_or(first_of_month)
} else {
chrono::NaiveDate::from_ymd_opt(now.year(), now.month() + 1, 1)
.unwrap_or(first_of_month)
.pred_opt()
.unwrap_or(first_of_month)
};
(
first_of_month.format("%Y.%m.%d").to_string(),
last_of_month.format("%Y.%m.%d").to_string(),
)
}
+43
View File
@@ -3,6 +3,7 @@ use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
use serde_json::{json, Value};
use std::fs;
use std::path::Path;
use crate::analytics::ReportExtractor;
use crate::models::Config;
use crate::storage::{ReportDb, ReportFilters};
@@ -848,3 +849,45 @@ pub async fn handle_get_comparable_reports(args: &Value) -> Result<Value> {
"isError": false
}))
}
pub async fn handle_export_deals_csv(_config: &Config, args: &Value) -> Result<Value> {
let db = ReportDb::new(&Config::db_path());
if let Err(e) = db.init() {
return Ok(json!({ "content": [{ "type": "text", "text": format!("DB error: {}", e) }], "isError": true }));
}
let report_id_opt = args.get("report_id").and_then(|v| v.as_str());
let entry = match report_id_opt {
Some(id) => db.get_by_id(id)?,
None => db.get_latest()?,
};
let entry = match entry {
Some(e) => e,
None => return Ok(json!({ "content": [{ "type": "text", "text": "No report found" }], "isError": true })),
};
let deals = db.get_deals(&entry.id)?;
if deals.is_empty() {
return Ok(json!({ "content": [{ "type": "text", "text": format!("No deals stored for report {}", entry.id) }], "isError": false }));
}
let output_path = match args.get("output_path").and_then(|v| v.as_str()) {
Some(p) => std::path::PathBuf::from(p),
None => Path::new(&entry.report_dir).join("deals.csv"),
};
let extractor = ReportExtractor::new();
extractor.write_deals_to_csv(&deals, &output_path)
.map_err(|e| anyhow::anyhow!("Failed to write CSV: {}", e))?;
Ok(json!({
"content": [{ "type": "text", "text": json!({
"success": true,
"report_id": entry.id,
"deals_count": deals.len(),
"output_path": output_path.to_string_lossy(),
}).to_string() }],
"isError": false
}))
}
+172
View File
@@ -3,6 +3,178 @@ use serde_json::{json, Value};
use std::path::Path;
use crate::models::Config;
// ── Update helpers ────────────────────────────────────────────────────────────
fn platform_tag() -> &'static str {
#[cfg(all(target_os = "macos", target_arch = "aarch64"))] return "macos-aarch64";
#[cfg(all(target_os = "macos", target_arch = "x86_64"))] return "macos-x86_64";
#[cfg(all(target_os = "linux", target_arch = "x86_64"))] return "linux-x86_64";
#[cfg(not(any(
all(target_os = "macos", target_arch = "aarch64"),
all(target_os = "macos", target_arch = "x86_64"),
all(target_os = "linux", target_arch = "x86_64"),
)))] return "unsupported";
}
fn semver_newer(latest: &str, current: &str) -> bool {
let parse = |s: &str| -> (u32, u32, u32) {
let mut p = s.trim_start_matches('v').splitn(3, '.');
let ma = p.next().and_then(|x| x.parse().ok()).unwrap_or(0);
let mi = p.next().and_then(|x| x.parse().ok()).unwrap_or(0);
let pa = p.next().and_then(|x| x.parse().ok()).unwrap_or(0);
(ma, mi, pa)
};
parse(latest) > parse(current)
}
/// Fetch the latest release tag from GitHub API (5 s timeout via curl).
/// Returns the version string without the leading "v", or None on failure.
pub(super) async fn fetch_latest_version() -> Option<String> {
let output = tokio::process::Command::new("curl")
.args([
"-sf", "--max-time", "5",
"-H", "Accept: application/vnd.github.v3+json",
"-H", "User-Agent: mt5-quant-updater",
"https://api.github.com/repos/masdevid/mt5-quant/releases/latest",
])
.output()
.await
.ok()?;
if !output.status.success() { return None; }
let body: Value = serde_json::from_slice(&output.stdout).ok()?;
body.get("tag_name")
.and_then(|v| v.as_str())
.map(|s| s.trim_start_matches('v').to_string())
}
fn ok_response(data: Value) -> Value {
json!({ "content": [{ "type": "text", "text": data.to_string() }], "isError": false })
}
fn err_response(msg: impl std::fmt::Display) -> Value {
json!({ "content": [{ "type": "text", "text": msg.to_string() }], "isError": true })
}
// ── Update tool handlers ──────────────────────────────────────────────────────
pub async fn handle_check_update(_config: &Config) -> Result<Value> {
let current = env!("CARGO_PKG_VERSION");
// Use cached background-check result if available; otherwise fetch now.
let latest_opt = match super::LATEST_VERSION.get() {
Some(v) => v.clone(),
None => fetch_latest_version().await,
};
let Some(latest) = latest_opt else {
return Ok(ok_response(json!({
"current_version": current,
"update_available": false,
"error": "Could not reach GitHub API — check network connectivity",
})));
};
let update_available = semver_newer(&latest, current);
Ok(ok_response(json!({
"current_version": current,
"latest_version": latest,
"update_available": update_available,
"hint": if update_available {
format!("Run the `update` tool to install v{latest}")
} else {
"You are on the latest version".to_string()
},
})))
}
pub async fn handle_update(_config: &Config) -> Result<Value> {
let current = env!("CARGO_PKG_VERSION");
let latest = match super::LATEST_VERSION.get().and_then(|v| v.as_deref()) {
Some(v) => v.to_string(),
None => match fetch_latest_version().await {
Some(v) => v,
None => return Ok(err_response(
r#"{"success":false,"error":"Could not determine latest version — check network"}"#
)),
},
};
if !semver_newer(&latest, current) {
return Ok(ok_response(json!({
"up_to_date": true,
"version": current,
})));
}
let tag = platform_tag();
if tag == "unsupported" {
return Ok(err_response(
r#"{"success":false,"error":"Auto-update not supported on this platform — build from source"}"#
));
}
let url = format!(
"https://github.com/masdevid/mt5-quant/releases/download/v{latest}/mcp-mt5-quant-{tag}.tar.gz"
);
// Download tarball to a temp file
let tmp_tar = tempfile::NamedTempFile::new()?;
let dl = tokio::process::Command::new("curl")
.args(["-sfL", "--max-time", "120",
"-o", tmp_tar.path().to_str().unwrap_or_default(),
&url])
.status()
.await?;
if !dl.success() {
return Ok(err_response(format!(
r#"{{"success":false,"error":"Download failed","url":"{}"}}"#, url
)));
}
// Extract binary (tarball root dir is mcp-mt5-quant-{platform}/)
let tmp_dir = tempfile::tempdir()?;
let extract = tokio::process::Command::new("tar")
.args(["-xzf", tmp_tar.path().to_str().unwrap_or_default(),
"-C", tmp_dir.path().to_str().unwrap_or_default(),
"--strip-components=1"])
.status()
.await?;
if !extract.success() {
return Ok(err_response(r#"{"success":false,"error":"Failed to extract archive"}"#));
}
let new_bin = tmp_dir.path().join("mt5-quant");
if !new_bin.exists() {
return Ok(err_response(r#"{"success":false,"error":"Binary not found in archive"}"#));
}
// Atomic replace: write to sibling .tmp, then rename (safe on same FS)
let current_exe = std::env::current_exe()?;
let tmp_dest = current_exe.with_extension("update_tmp");
std::fs::copy(&new_bin, &tmp_dest)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&tmp_dest, std::fs::Permissions::from_mode(0o755))?;
}
std::fs::rename(&tmp_dest, &current_exe)?;
Ok(ok_response(json!({
"success": true,
"previous_version": current,
"updated_to": latest,
"binary": current_exe.to_string_lossy(),
"hint": format!("Updated to v{latest}. Restart the MCP connection to load the new binary."),
})))
}
pub async fn handle_verify_setup(config: &Config) -> Result<Value> {
let mut checks = serde_json::Map::new();
let mut all_ok = true;
+32 -20
View File
@@ -3,6 +3,7 @@ use serde_json::{json, Value};
use std::fs;
use std::path::{Path, PathBuf};
use crate::models::Config;
use crate::storage::ReportDb;
/// Validate that `user_path` resolves to a location within `allowed_base`.
/// Returns the canonicalized absolute path on success.
@@ -785,7 +786,6 @@ pub async fn handle_export_report(_config: &Config, args: &Value) -> Result<Valu
let path = Path::new(report_dir);
let metrics_path = path.join("metrics.json");
let _deals_path = path.join("deals.csv");
// Read metrics
let metrics: Value = if metrics_path.exists() {
@@ -1640,21 +1640,33 @@ pub async fn handle_get_backtest_crash_info(config: &Config, args: &Value) -> Re
}
}
// Check if deals.csv is missing or empty
let deals_csv = path.join("deals.csv");
if !deals_csv.exists() {
result["crashes_found"].as_array_mut().unwrap().push(json!({
"report_dir": dir,
"type": "missing_deals",
"reason": "deals.csv not found - backtest likely failed",
}));
} else if let Ok(meta) = deals_csv.metadata() {
if meta.len() < 100 {
result["crashes_found"].as_array_mut().unwrap().push(json!({
"report_dir": dir,
"type": "empty_deals",
"reason": "deals.csv is nearly empty - no trades were made",
}));
// Check deal count in DB
let db = ReportDb::new(&Config::db_path());
if db.init().is_ok() {
match db.get_by_report_dir(dir) {
Ok(Some(entry)) => {
let deal_count = db.get_deals(&entry.id)
.map(|d| d.len())
.unwrap_or(0);
if deal_count == 0 {
result["crashes_found"].as_array_mut().unwrap().push(json!({
"report_dir": dir,
"type": "empty_deals",
"reason": "No deals stored in DB - EA did not trade or extraction failed",
}));
}
}
Ok(None) | Err(_) => {
// Not in DB at all means extraction never completed
let metrics_exists = path.join("metrics.json").exists();
if !metrics_exists {
result["crashes_found"].as_array_mut().unwrap().push(json!({
"report_dir": dir,
"type": "missing_deals",
"reason": "Report not found in DB and no metrics.json - backtest likely crashed before extraction",
}));
}
}
}
}
}
@@ -1676,10 +1688,10 @@ pub async fn handle_get_backtest_crash_info(config: &Config, args: &Value) -> Re
if let Ok(modified) = meta.modified() {
if modified >= cutoff {
// Check for failure indicators
let has_deals = path.join("deals.csv").exists();
let has_metrics = path.join("metrics.json").exists();
let has_incomplete = path.join(".incomplete").exists();
if !has_deals || has_incomplete {
if !has_metrics || has_incomplete {
failures += 1;
}
}
@@ -1701,7 +1713,7 @@ pub async fn handle_get_backtest_crash_info(config: &Config, args: &Value) -> Re
if types.contains(&"missing_deals".to_string()) {
result["common_patterns"].as_array_mut().unwrap().push(
json!("Missing deals.csv suggests MT5 crashed during backtest")
json!("Report not in DB and no metrics.json suggests MT5 crashed before extraction completed")
);
}
if types.contains(&"incomplete".to_string()) {
+193
View File
@@ -0,0 +1,193 @@
#!/usr/bin/env python3
"""MCP test harness: run backtest then exercise all granular analysis tools."""
import json
import subprocess
import sys
import time
import threading
BINARY = "/Users/masdevid/.cargo/bin/mt5-quant"
TIMEOUT = 1200 # 20 min for backtest
def send(proc, msg):
line = json.dumps(msg) + "\n"
proc.stdin.write(line.encode())
proc.stdin.flush()
def recv(proc, timeout=30):
"""Read one JSON-RPC response line."""
proc.stdout.readline() # skip blank / notification lines
deadline = time.time() + timeout
while time.time() < deadline:
line = proc.stdout.readline().decode("utf-8", errors="replace").strip()
if not line:
continue
try:
return json.loads(line)
except json.JSONDecodeError:
pass # skip non-JSON lines (logs etc)
return None
def recv_with_id(proc, expected_id, timeout=1200):
"""Read lines until we get a response with the expected id."""
deadline = time.time() + timeout
while time.time() < deadline:
line = proc.stdout.readline().decode("utf-8", errors="replace").strip()
if not line:
continue
try:
msg = json.loads(line)
if msg.get("id") == expected_id:
return msg
except json.JSONDecodeError:
pass
return None
def tool_call(proc, call_id, name, arguments=None):
send(proc, {
"jsonrpc": "2.0",
"id": call_id,
"method": "tools/call",
"params": {"name": name, "arguments": arguments or {}}
})
def extract_text(resp):
if not resp:
return "NO RESPONSE"
result = resp.get("result", {})
content = result.get("content", [])
if content:
text = content[0].get("text", "")
try:
parsed = json.loads(text)
return json.dumps(parsed, indent=2)[:1500]
except Exception:
return text[:1500]
if "error" in resp:
return f"ERROR: {resp['error']}"
return str(resp)[:500]
def ok(resp):
if not resp:
return False
result = resp.get("result", {})
content = result.get("content", [])
is_error = result.get("isError", True)
if is_error:
return False
if content:
text = content[0].get("text", "")
try:
parsed = json.loads(text)
return parsed.get("success", True) is not False
except Exception:
return True
return not is_error
# ── Start server ──────────────────────────────────────────────────────────────
print("Starting mt5-quant MCP server...")
proc = subprocess.Popen(
[BINARY],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
# ── Handshake ─────────────────────────────────────────────────────────────────
send(proc, {
"jsonrpc": "2.0", "id": 0,
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": {"name": "test-harness", "version": "1.0"}
}
})
init_resp = recv_with_id(proc, 0, timeout=10)
if not init_resp:
print("FATAL: no initialize response")
proc.terminate()
sys.exit(1)
print("Initialized OK")
send(proc, {"jsonrpc": "2.0", "method": "notifications/initialized", "params": {}})
# ── Run backtest ──────────────────────────────────────────────────────────────
print("\n=== run_backtest (DPS21, XAUUSD, M5, OHLC, skip_compile) ===")
tool_call(proc, 1, "run_backtest", {
"expert": "DPS21",
"symbol": "XAUUSDc",
"timeframe": "M5",
"model": 1, # OHLC faster
"skip_compile": True,
"timeout": 900,
"startup_delay_secs": 15,
})
resp = recv_with_id(proc, 1, timeout=TIMEOUT)
print(extract_text(resp))
backtest_ok = ok(resp)
print(f"backtest_ok={backtest_ok}")
if not backtest_ok:
print("Backtest failed aborting analysis tests")
proc.terminate()
sys.exit(1)
# ── Full analysis ─────────────────────────────────────────────────────────────
print("\n=== analyze_report (latest, all analytics) ===")
tool_call(proc, 2, "analyze_report", {})
resp = recv_with_id(proc, 2, timeout=60)
print(extract_text(resp))
print(f"analyze_report ok={ok(resp)}")
# ── Granular analytics ────────────────────────────────────────────────────────
granular = [
("analyze_monthly_pnl", {}),
("analyze_drawdown_events", {}),
("analyze_top_losses", {"limit": 5}),
("analyze_loss_sequences", {}),
("analyze_position_pairs", {}),
("analyze_direction_bias", {}),
("analyze_streaks", {}),
("analyze_concurrent_peak", {}),
("analyze_profit_distribution", {}),
("analyze_time_performance", {}),
("analyze_hold_time_distribution",{}),
("analyze_layer_performance", {}),
("analyze_volume_vs_profit", {}),
("analyze_costs", {}),
("analyze_efficiency", {}),
("list_deals", {"limit": 5}),
("search_deals_by_comment", {"query": "tp"}),
("search_deals_by_magic", {"magic": "0"}),
]
results = {}
for i, (tool, args) in enumerate(granular, start=10):
print(f"\n=== {tool} ===")
tool_call(proc, i, tool, args)
resp = recv_with_id(proc, i, timeout=30)
status = "OK" if ok(resp) else "FAIL"
results[tool] = status
print(f" {status}")
if status == "FAIL":
print(" " + extract_text(resp)[:400])
# ── Summary ───────────────────────────────────────────────────────────────────
print("\n\n========== SUMMARY ==========")
passed = [t for t, s in results.items() if s == "OK"]
failed = [t for t, s in results.items() if s != "OK"]
print(f"PASSED ({len(passed)}): {', '.join(passed)}")
if failed:
print(f"FAILED ({len(failed)}): {', '.join(failed)}")
else:
print("All granular analytics PASSED")
proc.terminate()