-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbacktest_runner.ps1
More file actions
520 lines (464 loc) · 24.5 KB
/
Copy pathbacktest_runner.ps1
File metadata and controls
520 lines (464 loc) · 24.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<#
.SYNOPSIS
MT5 Auto-Backtest & Optimization Pipeline v2.0 — Main Orchestrator
.DESCRIPTION
Based on official MT5 docs: https://www.metatrader5.com/en/terminal/help/start_advanced/start
CRITICAL: MT5 must NOT be already running. Script will close it first.
.EXAMPLE
.\backtest_runner.ps1 -EAName "PhoenixDCAPro" -Symbol "XAUUSD" -TF "M5"
.\backtest_runner.ps1 -Mode Batch -Symbols "XAUUSD,EURUSD" -TFs "M5,M15"
.\backtest_runner.ps1 -EAName "PhoenixDCAPro" -Symbol "XAUUSD" -TF "M5" -Mode Optimize
#>
param(
[string]$EAName = "",
[string]$Symbol = "XAUUSD",
[string]$TF = "M5",
[string]$From = "2024.01.01",
[string]$To = "2024.12.31",
[int]$Deposit = 10000,
[ValidateSet("Single","Batch","Optimize")][string]$Mode = "Single",
[string]$Symbols = "XAUUSD",
[string]$TFs = "M5",
[string]$Model = "OneMinOHLC",
[int]$TimeoutMinutes = 30,
[string]$SetFile = "",
[ValidateSet("Balance","BalancePF","BalancePayoff","BalanceDD","BalanceRF","BalanceSharpe","Custom","Complex")][string]$Criterion = "Balance",
[int]$RankMinTrades = 30,
[double]$RankMaxDD = 30.0,
[double]$RankMinPF = 1.2,
[double]$RankMinTradesPerDay = 0.0,
[int]$RankTestDays = 0,
[int]$MaxOptimizeInputs = 99,
[switch]$ExtractOptCache,
[switch]$RequireOptParse,
[string]$OptCacheDir = "",
[string]$OptArchiveDir = "",
[string]$BestSetName = "",
[int]$OptLookbackMinutes = 120
)
$ErrorActionPreference = "Stop"
$BaseDir = "D:\code\ex5-backtest"
$MT5InstallDir = "C:\Program Files\MetaTrader 5 EXNESS"
$MetaEditor = "$MT5InstallDir\MetaEditor64.exe"
$Terminal = "$MT5InstallDir\terminal64.exe"
# MT5 data dir (where MQL5\Experts, MQL5\Include live)
$Mql5Root = (Get-ChildItem "C:\Users\ppnh1\AppData\Roaming\MetaQuotes\Terminal" -Directory `
| Where-Object { Test-Path (Join-Path $_.FullName "MQL5\Include") } `
| Select-Object -First 1).FullName + "\MQL5"
$ExpertDir = "$Mql5Root\Experts"
# Report saved in terminal DATA dir (AppData), NOT install dir!
$TerminalDataDir = (Get-ChildItem "C:\Users\ppnh1\AppData\Roaming\MetaQuotes\Terminal" -Directory `
| Where-Object { Test-Path (Join-Path $_.FullName "MQL5\Include") } `
| Select-Object -First 1).FullName
$MT5ReportDir = $TerminalDataDir
$ConfigDir = "$BaseDir\configs"
$ResultsDir = "$BaseDir\results"
$LocalReportDir = "$BaseDir\reports"
$TesterProfileDir = "$Mql5Root\Profiles\Tester"
@($ConfigDir, $ResultsDir, $LocalReportDir) | ForEach-Object {
if (-not (Test-Path $_)) { New-Item -ItemType Directory -Path $_ -Force | Out-Null }
}
if (-not (Test-Path $TesterProfileDir)) { New-Item -ItemType Directory -Path $TesterProfileDir -Force | Out-Null }
# ══════════════════════════════════════════════
# COMPILE (MetaEditor64 CLI)
# ══════════════════════════════════════════════
function Get-EASource {
param([string]$Name)
Get-ChildItem $BaseDir -Recurse -Filter "$Name.mq5" -File | Select-Object -First 1
}
function Test-EAHasOnTester {
param([System.IO.FileInfo]$Source)
if (-not $Source) { return $false }
return [bool](Select-String -Path $Source.FullName -Pattern 'double\s+OnTester\s*\(' -Quiet)
}
function Get-OptimizeInputCount {
param([string]$InputPath)
if (-not $InputPath -or -not (Test-Path $InputPath)) { return 0 }
$count = 0
foreach ($line in Get-Content $InputPath -ErrorAction SilentlyContinue) {
$trimmed = $line.Trim()
if ($trimmed -match '^\s*[;#]' -or $trimmed -notmatch '=') { continue }
$parts = $trimmed -split '\|\|'
if ($parts.Count -ge 5 -and $parts[-1].Trim().ToUpperInvariant() -eq 'Y') { $count++ }
}
return $count
}
function Assert-OptimizeInputLimit {
param([string]$InputPath)
$count = Get-OptimizeInputCount -InputPath $InputPath
if ($count -gt $MaxOptimizeInputs) {
Write-Error "Optimization has $count enabled inputs in $InputPath. Reduce to $MaxOptimizeInputs or fewer to avoid multi-hour MT5 runs."
return $false
}
if ($count -gt 0) {
Write-Host " [OPTIMIZE] Enabled inputs: $count / $MaxOptimizeInputs" -ForegroundColor DarkGray
}
return $true
}
function Find-LatestOptimizationCache {
param([string]$Name, [string]$Sym, [string]$Timeframe, [datetime]$StartTime)
$cacheDir = if ($OptCacheDir) { $OptCacheDir } else { "$TerminalDataDir\Tester\cache" }
if (-not (Test-Path $cacheDir)) { return $null }
$minTime = $StartTime.AddMinutes(-[math]::Abs($OptLookbackMinutes))
$safeName = [regex]::Escape($Name)
$safeSym = [regex]::Escape($Sym)
$safeTf = [regex]::Escape($Timeframe)
Get-ChildItem $cacheDir -Filter "*.opt" -File -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTime -ge $minTime } |
Where-Object { $_.Name -match $safeName -or ($_.Name -match $safeSym -and $_.Name -match $safeTf) } |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
}
function New-OptimizationArchiveDir {
param([string]$Name, [string]$Sym, [string]$Timeframe)
$root = if ($OptArchiveDir) { $OptArchiveDir } else { "$ResultsDir\optimization_cache" }
$stamp = Get-Date -Format "yyyyMMdd_HHmmss"
$safe = ("${Name}_${Sym}_${Timeframe}_${stamp}" -replace '[\\/:*?"<>|]', '_')
$dir = Join-Path $root $safe
New-Item -ItemType Directory -Path $dir -Force | Out-Null
return $dir
}
function Resolve-OptimizationInputPath {
if ($SetFile -and (Test-Path $SetFile)) { return $SetFile }
$profileSet = Get-ChildItem $TesterProfileDir -Filter "$EAName*.set" -ErrorAction SilentlyContinue |
Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($profileSet) { return $profileSet.FullName }
return ""
}
function Export-OptimizationCacheResult {
param([string]$Name, [string]$Sym, [string]$Timeframe, [datetime]$StartTime)
if (-not $ExtractOptCache) { return $null }
$opt = Find-LatestOptimizationCache -Name $Name -Sym $Sym -Timeframe $Timeframe -StartTime $StartTime
if (-not $opt) {
$message = "No matching .opt cache found for $Name $Sym $Timeframe"
if ($RequireOptParse) { Write-Error $message }
Write-Warning " [OPT] $message"
return $null
}
$archive = New-OptimizationArchiveDir -Name $Name -Sym $Sym -Timeframe $Timeframe
$sourceOpt = Join-Path $archive "source.opt"
Copy-Item $opt.FullName -Destination $sourceOpt -Force
$inputPath = Resolve-OptimizationInputPath
$metadata = [PSCustomObject]@{
EA = $Name
Symbol = $Sym
Period = $Timeframe
From = $From
To = $To
Criterion = $Criterion
Model = $Model
Deposit = $Deposit
SourceOpt = $opt.FullName
ArchivedOpt = $sourceOpt
InputsPath = $inputPath
StartedAt = $StartTime.ToString('o')
ArchivedAt = (Get-Date).ToString('o')
}
$metadata | ConvertTo-Json -Depth 4 | Set-Content -Path (Join-Path $archive "metadata.json") -Encoding UTF8
Write-Host " [OPT] Cache archived: $sourceOpt" -ForegroundColor DarkGray
$parserArgs = @('-OptPath', $sourceOpt, '-OutDir', $archive, '-EAName', $Name, '-Symbol', $Sym, '-TF', $Timeframe)
if ($inputPath) { $parserArgs += @('-InputsPath', $inputPath) }
if ($RequireOptParse) { $parserArgs += '-Strict' }
$passFile = & "$BaseDir\opt_parser.ps1" @parserArgs
if (-not $passFile) { return $null }
$best = & "$BaseDir\rank_optimization_passes.ps1" -PassCsv $passFile.FullName -OutDir $archive -MinTrades $RankMinTrades -MaxDD $RankMaxDD -MinPF $RankMinPF -MinTradesPerDay $RankMinTradesPerDay -TestDays $RankTestDays
if (-not $best -or -not $inputPath) { return $null }
$outName = if ($BestSetName) { $BestSetName } else { "${Name}_${Sym}_${Timeframe}_best.set" }
$bestSet = Join-Path $archive ($outName -replace '[\\/:*?"<>|]', '_')
& "$BaseDir\write_set_from_pass.ps1" -InputsPath $inputPath -PassCsv (Join-Path $archive "optimization_ranked.csv") -PassId ([int]$best.Pass) -OutSetPath $bestSet | Out-Null
Copy-Item $bestSet -Destination "$ResultsDir\$(Split-Path $bestSet -Leaf)" -Force
Write-Host " [OPT] Best generated set: $ResultsDir\$(Split-Path $bestSet -Leaf)" -ForegroundColor Green
return $bestSet
}
function Compile-EA {
param([string]$Name)
$src = Get-EASource -Name $Name
if (-not $src) { Write-Error "Source not found: $Name.mq5"; return $false }
# Copy to MQL5\Experts
$dest = "$ExpertDir\$($src.Name)"
Copy-Item $src.FullName -Destination $dest -Force
Write-Host " [COMPILE] $Name..." -ForegroundColor Cyan
# MetaEditor syntax: /compile:"path" /include:"path" /log
$compArgs = @(
"/compile:$dest"
"/include:$Mql5Root"
"/log"
)
$proc = Start-Process -FilePath $MetaEditor -ArgumentList $compArgs -Wait -PassThru -NoNewWindow
Start-Sleep -Seconds 3
$logFile = [System.IO.Path]::ChangeExtension($dest, ".log")
$success = $false
if (Test-Path $logFile) {
$logContent = Get-Content $logFile -Encoding Unicode -ErrorAction SilentlyContinue
$resultLine = ($logContent | Where-Object { $_ -match "Result:" }) -join ""
if ($resultLine -match "0 error") {
Write-Host " [COMPILE] OK: $resultLine" -ForegroundColor Green
$success = $true
} else {
Write-Host " [COMPILE] FAIL: $resultLine" -ForegroundColor Red
}
Remove-Item $logFile -ErrorAction SilentlyContinue
} else {
Write-Host " [COMPILE] WARN: No log file found. Check MetaEditor." -ForegroundColor Yellow
}
if (-not $success) {
Remove-Item $dest -ErrorAction SilentlyContinue
Remove-Item ([System.IO.Path]::ChangeExtension($dest, ".ex5")) -ErrorAction SilentlyContinue
}
return $success
}
# ══════════════════════════════════════════════
# CLOSE MT5 (required before /config launch)
# ══════════════════════════════════════════════
function Close-MT5 {
$existing = Get-Process -Name "terminal64" -ErrorAction SilentlyContinue
if ($existing) {
Write-Host " [MT5] Closing existing MT5 instance (PID: $($existing.Id))..." -ForegroundColor Yellow
$existing | ForEach-Object { $_.CloseMainWindow() | Out-Null }
Start-Sleep -Seconds 5
$still = Get-Process -Name "terminal64" -ErrorAction SilentlyContinue
if ($still) {
Write-Host " [MT5] Force killing..." -ForegroundColor Red
$still | Stop-Process -Force
Start-Sleep -Seconds 3
}
Write-Host " [MT5] Closed." -ForegroundColor Green
}
}
# ══════════════════════════════════════════════
# BACKTEST (terminal64.exe /config:ini)
# ══════════════════════════════════════════════
function Run-Backtest {
param([string]$Name, [string]$Sym, [string]$Timeframe, [string]$OptMode = "Off")
Write-Host " [BACKTEST] $Name @ $Sym $Timeframe (Opt: $OptMode)..." -ForegroundColor Yellow
$setName = ""
if ($SetFile) {
if (-not (Test-Path $SetFile)) {
Write-Warning " [SET] File not found: $SetFile"
} else {
$setName = Split-Path $SetFile -Leaf
Copy-Item $SetFile -Destination "$TesterProfileDir\$setName" -Force
Write-Host " [SET] $TesterProfileDir\$setName" -ForegroundColor DarkGray
if ($OptMode -ne "Off" -and -not (Assert-OptimizeInputLimit -InputPath "$TesterProfileDir\$setName")) { return $null }
}
}
# Generate INI (report path will be "reports\<name>" relative to MT5 install dir)
$iniFile = & "$BaseDir\ini_generator.ps1" -EAName $Name -Symbol $Sym -TF $Timeframe `
-From $From -To $To -Deposit $Deposit -Optimization $OptMode -Criterion $Criterion -Model $Model `
-SetFile $setName -OutputDir $ConfigDir
Write-Host " [CONFIG] $iniFile" -ForegroundColor DarkGray
# Close MT5 first (CRITICAL: /config doesn't work well with existing instance)
Close-MT5
# Launch terminal with config
$proc = Start-Process -FilePath $Terminal -ArgumentList "/config:$iniFile" -PassThru
Start-Sleep -Seconds 10
if ($proc.HasExited) {
Write-Warning " [ERROR] Terminal exited immediately (code: $($proc.ExitCode)). Check INI file."
return $null
}
Write-Host " [MT5] Terminal running (PID: $($proc.Id)). Waiting for test to complete..." -ForegroundColor Green
# Wait for terminal to close (ShutdownTerminal=1 in INI)
$sw = [Diagnostics.Stopwatch]::StartNew()
$timeout = [TimeSpan]::FromMinutes($TimeoutMinutes)
while (-not $proc.HasExited -and $sw.Elapsed -lt $timeout) {
Start-Sleep -Seconds 10
$elapsed = [math]::Round($sw.Elapsed.TotalMinutes, 1)
Write-Host "`r [TESTING] ${elapsed}m / ${TimeoutMinutes}m..." -NoNewline -ForegroundColor DarkGray
}
Write-Host ""
if (-not $proc.HasExited) {
Write-Warning " [TIMEOUT] Killing after $TimeoutMinutes min"
$proc.Kill()
return $null
}
$elapsed = [math]::Round($sw.Elapsed.TotalMinutes, 1)
Write-Host " [DONE] Test completed in ${elapsed}m" -ForegroundColor Green
# Find report in MT5 install dir (Report= is relative to MT5 install dir)
$reportName = "${Name}_${Sym}_${Timeframe}"
$report = Get-ChildItem $MT5ReportDir -Filter "$reportName*" -ErrorAction SilentlyContinue |
Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($report) {
# Copy to our local reports dir
$localCopy = "$LocalReportDir\$($report.Name)"
Copy-Item $report.FullName -Destination $localCopy -Force
Write-Host " [REPORT] $localCopy" -ForegroundColor Green
return $localCopy
}
Write-Warning " [WARN] Report not found: $reportName in $MT5ReportDir"
# Fallback: check any htm file in MT5 install dir
$fallback = Get-ChildItem $MT5InstallDir -Filter "$reportName*" -ErrorAction SilentlyContinue |
Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($fallback) {
$localCopy = "$LocalReportDir\$($fallback.Name)"
Copy-Item $fallback.FullName -Destination $localCopy -Force
Write-Host " [REPORT-FALLBACK] $localCopy" -ForegroundColor Yellow
return $localCopy
}
return $null
}
# ══════════════════════════════════════════════
# PARSE REPORT
# ══════════════════════════════════════════════
function Parse-Report {
param([string]$ReportPath, [string]$Name, [string]$Sym, [string]$Timeframe)
$result = & "$BaseDir\report_parser.ps1" -ReportPath $ReportPath
if ($result) {
# Use Add-Member in case properties don't exist or are read-only
$result | Add-Member -NotePropertyName "EA" -NotePropertyValue $Name -Force
$result | Add-Member -NotePropertyName "Symbol" -NotePropertyValue $Sym -Force
$result | Add-Member -NotePropertyName "Period" -NotePropertyValue $Timeframe -Force
return $result
}
return $null
}
# ══════════════════════════════════════════════
# ZERO-TRADE DETECTION & LOG ANALYSIS
# ══════════════════════════════════════════════
function Analyze-ZeroTrades {
param([string]$Name, [string]$Sym, [string]$Timeframe, $ParsedResult)
$trades = if ($ParsedResult) { [int]$ParsedResult.TotalTrades } else { 0 }
if ($trades -gt 0) { return $true }
Write-Host ""
Write-Host " ⚠️ ZERO TRADES DETECTED — Analyzing logs..." -ForegroundColor Red
Write-Host " ────────────────────────────────────────────" -ForegroundColor Red
# Read tester logs
$testerLogDir = "$TerminalDataDir\tester\logs"
$agentLogDir = "$TerminalDataDir\tester"
$diagnosis = @()
# 1. Main tester log
$testerLog = Get-ChildItem $testerLogDir -Filter "*.log" -ErrorAction SilentlyContinue |
Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($testerLog) {
$lines = Get-Content $testerLog.FullName -Tail 50 -ErrorAction SilentlyContinue
$relevant = $lines | Where-Object {
$_ -match "error|fail|cannot|not exist|not found|invalid|warning|stopped|crash" -and
$_ -notmatch "Cloud servers"
}
if ($relevant) {
Write-Host " [TESTER LOG] Errors found:" -ForegroundColor Yellow
$relevant | ForEach-Object { Write-Host " $_" -ForegroundColor DarkYellow }
$diagnosis += $relevant
}
}
# 2. Agent logs (where EA actually runs)
$agentLogs = Get-ChildItem $agentLogDir -Filter "*.log" -Recurse -ErrorAction SilentlyContinue |
Where-Object { $_.DirectoryName -match "Agent" } |
Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($agentLogs) {
$alines = Get-Content $agentLogs.FullName -Tail 50 -ErrorAction SilentlyContinue
$eaOutput = $alines | Where-Object { $_ -match "$Name|error|warning|failed|invalid|not found" }
if ($eaOutput) {
Write-Host " [AGENT LOG] EA output:" -ForegroundColor Yellow
$eaOutput | ForEach-Object { Write-Host " $_" -ForegroundColor DarkYellow }
$diagnosis += $eaOutput
}
}
# 3. Provide diagnosis
Write-Host "" -ForegroundColor Red
$diagText = $diagnosis -join "`n"
if ($diagText -match "not exist") {
Write-Host " 📋 DIAGNOSIS: Symbol '$Sym' may not exist in this broker" -ForegroundColor Red
} elseif ($diagText -match "init") {
Write-Host " 📋 DIAGNOSIS: EA failed to initialize — check params or dependencies" -ForegroundColor Red
} elseif ($diagText -match "error") {
Write-Host " 📋 DIAGNOSIS: Runtime errors detected — EA may have code bugs" -ForegroundColor Red
} else {
Write-Host " 📋 DIAGNOSIS: EA ran OK but params too restrictive for $Sym $Timeframe" -ForegroundColor Yellow
Write-Host " → Try different timeframe (M15/H1) or adjust entry signal params" -ForegroundColor DarkYellow
}
Write-Host " ────────────────────────────────────────────" -ForegroundColor Red
Write-Host ""
return $false
}
# ══════════════════════════════════════════════
# MAIN
# ══════════════════════════════════════════════
Write-Host ""
Write-Host "╔════════════════════════════════════════════════════╗" -ForegroundColor Magenta
Write-Host "║ Antigravity Auto-Backtest Pipeline v2.0 ║" -ForegroundColor Magenta
Write-Host "╚════════════════════════════════════════════════════╝" -ForegroundColor Magenta
Write-Host " Mode: $Mode | Model: $Model" -ForegroundColor White
Write-Host " Period: $From -> $To | Deposit: $Deposit" -ForegroundColor White
Write-Host ""
$allResults = @()
switch ($Mode) {
"Single" {
if (-not $EAName) { Write-Error "Specify -EAName"; return }
if (-not (Compile-EA -Name $EAName)) { Write-Error "Compile failed"; return }
$report = Run-Backtest -Name $EAName -Sym $Symbol -Timeframe $TF
if ($report) {
$result = Parse-Report -ReportPath $report -Name $EAName -Sym $Symbol -Timeframe $TF
if ($result) {
$allResults += $result; Write-Host ""; $result | Format-List
# Zero-trade detection
Analyze-ZeroTrades -Name $EAName -Sym $Symbol -Timeframe $TF -ParsedResult $result
}
} else {
Write-Host " ⚠️ No report generated — test likely failed" -ForegroundColor Red
Analyze-ZeroTrades -Name $EAName -Sym $Symbol -Timeframe $TF -ParsedResult $null
}
Remove-Item "$ExpertDir\$EAName.ex5" -ErrorAction SilentlyContinue
Remove-Item "$ExpertDir\$EAName.mq5" -ErrorAction SilentlyContinue
}
"Batch" {
$symbolList = $Symbols -split ","
$tfList = $TFs -split ","
$eaFiles = Get-ChildItem "$BaseDir\EAs","$BaseDir\Others" -Filter "*.mq5" -File -ErrorAction SilentlyContinue
$uniqueEAs = @{}
foreach ($f in $eaFiles) {
$n = $f.BaseName -replace '_[0-9a-f]{5}$', ''
if (-not $uniqueEAs.ContainsKey($n)) { $uniqueEAs[$n] = $f }
}
$total = $uniqueEAs.Count * $symbolList.Count * $tfList.Count
Write-Host " EAs: $($uniqueEAs.Count) | Total runs: $total" -ForegroundColor White
$idx = 0
foreach ($entry in $uniqueEAs.GetEnumerator()) {
if (-not (Compile-EA -Name $entry.Key)) { $idx += $symbolList.Count * $tfList.Count; continue }
foreach ($sym in $symbolList) { foreach ($tf in $tfList) {
$idx++; Write-Host "`n[$idx/$total] $($entry.Key) @ $sym $tf" -ForegroundColor Cyan
$report = Run-Backtest -Name $entry.Key -Sym $sym -Timeframe $tf
if ($report) { $r = Parse-Report -ReportPath $report -Name $entry.Key -Sym $sym -Timeframe $tf; if ($r) { $allResults += $r } }
}}
Remove-Item "$ExpertDir\$($entry.Key).ex5" -ErrorAction SilentlyContinue
Remove-Item "$ExpertDir\$($entry.Key).mq5" -ErrorAction SilentlyContinue
}
}
"Optimize" {
if (-not $EAName) { Write-Error "Specify -EAName"; return }
$src = Get-EASource -Name $EAName
if ($Criterion -eq "Custom" -and -not (Test-EAHasOnTester -Source $src)) {
Write-Error "Custom optimization requires double OnTester() in $EAName.mq5. Add OnTester() to the source or rerun with -Criterion BalanceDD."
return
}
if (-not (Compile-EA -Name $EAName)) { Write-Error "Compile failed"; return }
Write-Host " Running Genetic Optimization (Criterion: $Criterion)..." -ForegroundColor Magenta
$optStartTime = Get-Date
$report = Run-Backtest -Name $EAName -Sym $Symbol -Timeframe $TF -OptMode "Genetic"
if ($report) {
$result = Parse-Report -ReportPath $report -Name $EAName -Sym $Symbol -Timeframe $TF
if ($result) { $allResults += $result; $result | Format-List }
}
$generatedSet = Export-OptimizationCacheResult -Name $EAName -Sym $Symbol -Timeframe $TF -StartTime $optStartTime
if (-not $generatedSet) {
$setFile = Get-ChildItem "$Mql5Root\Profiles\Tester" -Filter "$EAName*.set" -ErrorAction SilentlyContinue |
Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($setFile) {
Copy-Item $setFile.FullName -Destination "$ResultsDir\$($setFile.Name)" -Force
Write-Host " [SET-FALLBACK] Copied latest tester-profile set, not proven best pass: $ResultsDir\$($setFile.Name)" -ForegroundColor Yellow
} else {
Write-Warning " [SET] No generated best .set available. Export from MT5 Optimization Results may still be required."
}
}
Remove-Item "$ExpertDir\$EAName.ex5" -ErrorAction SilentlyContinue
Remove-Item "$ExpertDir\$EAName.mq5" -ErrorAction SilentlyContinue
}
}
if ($allResults.Count -gt 0) {
$csvOut = "$ResultsDir\all_results.csv"
$allResults | Export-Csv $csvOut -NoTypeInformation -Encoding UTF8 -Append
Write-Host "`n Results: $csvOut ($($allResults.Count) entries)" -ForegroundColor Green
& "$BaseDir\rank_results.ps1" -ResultsDir $ResultsDir -MinTrades $RankMinTrades -MaxDD $RankMaxDD `
-MinPF $RankMinPF -MinTradesPerDay $RankMinTradesPerDay -TestDays $RankTestDays
}
Write-Host "`nPipeline complete!" -ForegroundColor Green
Write-Host "NOTE: MT5 has been shut down. Reopen it manually if needed." -ForegroundColor Yellow