MT5 tester script failing / not waiting for terminal to finish

 

For a while I have been using this .bat script to automate testing on the MT5 strategy tester to optimise my EA.

It edits the .ini file for adjust for set file to load, the pair, timeframe and also then loops how many times to do this.  Recently I have had an issue where it would do several sets like 3/4 and then the .bat script would ignore the fact mt5 is loaded and just skip .sets and keep attempting to load more and more tests which obviously just fails while MT5 is just sat there in the background working away.

I have checked all set files and saved them, no issues there but if anyone had any ideas or insight I would be most grateful.  Looked into other methods of automating MT5 testing for multiple sets but so far have failed.  Honestly i use chatGPT to write most of this as my understanding of coding is very limited.

Kind regards

@echo off
echo Startscript to start MT5 optimization/backtester automated.

setlocal enabledelayedexpansion
:: D:\MT5Tester2\MQL5\Profiles\Tester
:: set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU testing file.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU BB+RSI.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU BB+STOCH.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU HL+RSI.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU HL+STOCH.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU ENV+RSI.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU ENV+STOCH.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU SMA+RSI.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU SMA+STOCH.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU BB+HL+RSI.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU BB+ENV+RSI.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU BB+SMA+RSI.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU DMA+RSI.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU DMA+STOCH.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU BB+HL+STOCH.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU BB+ENV+STOCH.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU BB+SMA+STOCH.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU HL+ENV+RSI.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU HL+SMA+RSI.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU HL+ENV+STOCH.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU HL+SMA+STOCH.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU BB+RSI+STOCH.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU HL+RSI+STOCH.set\""
set "expertParameters=!expertParameters! \"MT5 v1.14 bat XU ENV+RSI+STOCH.set\""

:: Define the list of currency pairs to loop over
set "pairs=XAUUSD"

:: Define the list of optimization criteria to loop over
set "criteria=6"

:: Define the list of periods (time frames) to loop over M5 M15 H1 H4
set "periods=M5"

:: Define ini file
:: set "iniFile=D:\MT5Tester2\MT5 v1.14 bat testing.ini"
set "iniFile=D:\MT5Tester2\MT5 v1.14 bat testing.ini"

:: Define log file path
set "logFile=D:\MT5Tester2\script_log.txt"

:: Loop for the desired number of iterations, adjust outermost number for loop count.
for /L %%i in (1,1,100) do (
    :: Loop over each expert parameter (set file)
    for %%e in (!expertParameters!) do (
        :: Set the expert parameter in the ini file
        powershell -Command "(Get-Content \"%iniFile%\") -replace 'ExpertParameters=.*', 'ExpertParameters=%%e' | Set-Content \"%iniFile%\""

        :: Loop over each currency pair
        for %%p in (%pairs%) do (
            :: Set the currency pair in the ini file
            powershell -Command "(Get-Content \"%iniFile%\") -replace 'Symbol=.*', 'Symbol=%%p' | Set-Content \"%iniFile%\""
        
            :: Loop over each optimization criterion
            for %%c in (%criteria%) do (
                :: Set the optimization criterion in the ini file
                powershell -Command "(Get-Content \"%iniFile%\") -replace 'OptimizationCriterion=.*', 'OptimizationCriterion=%%c' | Set-Content \"%iniFile%\""
            
                :: Loop over each period (time frame)
                for %%t in (%periods%) do (
                    :: Set the period in the ini file
                    powershell -Command "(Get-Content \"%iniFile%\") -replace 'Period=.*', 'Period=%%t' | Set-Content \"%iniFile%\""
                
                    :: Log the current iteration details
                    echo ExpertParameters: %%e, Pair: %%p, Criterion: %%c, Period: %%t, Iteration: %%i
                    echo ExpertParameters: %%e, Pair: %%p, Criterion: %%c, Period: %%t, Iteration: %%i >> "%logFile%"
                    echo !date! - !time!
                    echo !date! - !time! >> "%logFile%"

                    :: Execute MT5 terminal
                    D:\MT5Tester2\terminal64.exe /portable /config:"%iniFile%" >> "%logFile%" 2>&1
                    if !ERRORLEVEL! NEQ 0 (
                        echo Error encountered with terminal64.exe. Error Level: !ERRORLEVEL!
                        echo Error encountered with terminal64.exe. Error Level: !ERRORLEVEL! >> "%logFile%"
                    )

                    :: Log the completion time
                    echo !date! - !time!
                    echo !date! - !time! >> "%logFile%"

                    :: Delay to ensure proper shutdown/startup of MT5 terminal
                    timeout /t 10 /nobreak >nul
                )
            )
        )
    )
)

echo "Finished!"
echo "Finished!" >> "%logFile%"
PAUSE
[Tester]
Expert="MT5 White Wizard v1.15 no exp"
;Expert=Examples\MACD\MACD Sample
ExpertParameters="MT5 v1.14 bat XU BB+STOCH.set"
; to find in: D:\MT5Tester2\MQL5\Profiles\Tester\
Symbol=XAUUSD
Period=M5
Deposit=20000
Leverage=1:10
Model=4
;0 — "Every tick"
;1 — "1 minute OHLC"
;2 — "Open price only"
;3 — "Math calculations"
;4 — "Every tick based on real ticks"
ExecutionMode=200
Optimization=2
OptimizationCriterion=6
;6 - Custom Max
FromDate=2023.02.01
ToDate=2023.06.01
Report=reports\MT5 v1.15 Percent"
ShutdownTerminal=1
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
MQL5 forum: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
  • www.mql5.com
How to create an Expert Advisor (a trading robot) for Forex trading
 

Please, don't request help for ChatGPT (or other A.I.) generated code.

  • Usually people who can't code don't receive free help on this forum.
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community. Use the CODE button (Alt-S) when inserting code.
  • To learn MQL programming, you can research the many available Articles on the subject, or examples in the Codebase, as well as reference the online Book and Documentation
  • If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free). However, recommendations or suggestions for Market products are not allowed on the forum, so you will have to do your own research.
  • Finally, you also have the option to hire a programmer in the Freelance section.
 
Fernando Carreiro #:

Please, don't request help for ChatGPT (or other A.I.) generated code.

  • Usually people who can't code don't receive free help on this forum.
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community. Use the CODE button (Alt-S) when inserting code.
  • To learn MQL programming, you can research the many available Articles on the subject, or examples in the Codebase, as well as reference the online Book and Documentation
  • If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free). However, recommendations or suggestions for Market products are not allowed on the forum, so you will have to do your own research.
  • Finally, you also have the option to hire a programmer in the Freelance section.

I do not completely agree with your comments, it suggests many assumptions of a person in general.  My post could be better yes but assuming many things is not a good trait.

You may as well say, people who are assumed lazy don't get help (lazy vs ability are very different).  Instantly we have diverted from the goal of the post..

 
Stefan Marjoram #: I do not completely agree with your comments, it suggests many assumptions of a person in general.  My post could be better yes but assuming many things is not a good trait. You may as well say, people who are assumed lazy don't get help (lazy vs ability are very different).  Instantly we have diverted from the goal of the post..

No assumptions were made. It is a "boiler plate" comment, with the aim of covering most situations of the sort without prejudice (search the forum and you will find it repeated many times over).

You yourself, admitted to using ChatGPT, and even stated (and I quote) ... "my understanding of coding is very limited".

So, the "boiler plate" comment is quite fitting!

 
Fernando Carreiro #:

No assumptions were made. It is a "boiler plate" comment, with the aim of covering most situations of the sort without prejudice (search the forum and you will find it repeated many times over).

You yourself, admitted to using ChatGPT, and even stated (and I quote) ... "my understanding of coding is very limited".

So, the "boiler plate" comment is quite fitting!

Maybe a coding quiz before being able to post on forums would be more appropriate than having to boiler plate every [redacted] post :) ?

Please delete this whole forum when you get time, its not going anywhere.  I will resort to freelance instead where they accept non coders.

 
Stefan Marjoram #: Maybe a coding quiz before being able to post on forums would be more appropriate than having to boiler plate every [redacted] post :) ? Please delete this whole forum when you get time, its not going anywhere.  I will resort to freelance instead where they accept non coders.

You are aware that this is mainly a site about MQL5 programming and not so much about how to make Windows Command line batch files, or Microsoft PowerShell scripts, right?

In all honesty the question would be more fittingly posted on a Microsoft related forum. However, your post has remained because maybe someone has tried something similar and is willing to help you out.

So, please relax, and don't get so high-strung about some boiler-plate comment that held no prejudice towards you in the least!

 
Fernando Carreiro #:

You are aware that this is mainly a site about MQL5 programming and not so much about how to make Windows Command line batch files, or Microsoft PowerShell scripts, right?

In all honesty the question would be more fittingly posted on a Microsoft related forum. However, your post has remained because maybe someone has tried something similar and is willing to help you out.

So, please relax, and don't so high-strung about some boiler-plate comment that held no prejudice towards you in the least!

You are right I should relax, sorry.

 

in the mean time, i have moved to a powershell script which uses the argument Start-Process and -Wait which waits for the terminal to close before proceeding with a new test, this should fix the issue I was having with my bat script.

Anyone should be able to use the code below to make their own version if they wish.  Just make a text file, copy the code and save it as a .ps1 to be able to run it as powershell.

# Manually set .ini settings CTRL + C on MT5 Tester settings page to find exact values to input
# To get .ini setup MT5 terminal, close and copy common.ini then add [Tester] as per above to the file
# Paste required values into ini and confirm ExpertParameters= remains. D:\MT5Tester2\MT5 v1.15 script testing.ini

# Set files to cycle through, in \MQL5\Profiles\Tester
$expertParameters = @(
        "MT5 v1.14 bat XU BB+RSI.set",
        "MT5 v1.14 bat XU BB+STOCH.set",
        "MT5 v1.14 bat XU HL+RSI.set",
        "MT5 v1.14 bat XU HL+STOCH.set",
        "MT5 v1.14 bat XU ENV+RSI.set",
        "MT5 v1.14 bat XU ENV+STOCH.set",
        "MT5 v1.14 bat XU SMA+RSI.set",
        "MT5 v1.14 bat XU SMA+STOCH.set",
        "MT5 v1.14 bat XU BB+HL+RSI.set",
        "MT5 v1.14 bat XU BB+ENV+RSI.set",
        "MT5 v1.14 bat XU BB+SMA+RSI.set",
        "MT5 v1.14 bat XU DMA+RSI.set",
        "MT5 v1.14 bat XU DMA+STOCH.set",
        "MT5 v1.14 bat XU BB+HL+STOCH.set",
        "MT5 v1.14 bat XU BB+ENV+STOCH.set",
        "MT5 v1.14 bat XU BB+SMA+STOCH.set",
        "MT5 v1.14 bat XU HL+ENV+RSI.set",
        "MT5 v1.14 bat XU HL+SMA+RSI.set",
        "MT5 v1.14 bat XU HL+ENV+STOCH.set",
        "MT5 v1.14 bat XU HL+SMA+STOCH.set",
        "MT5 v1.14 bat XU BB+RSI+STOCH.set",
        "MT5 v1.14 bat XU HL+RSI+STOCH.set",
        "MT5 v1.14 bat XU ENV+RSI+STOCH.set"
)
$pairs = @("XAUUSD")                                    	# Multi Pair example ("XAUUSD", "GBPUSD", "EURUSD")
$criteria = @("6")                                      	# Multi Criteria example ("1", "2", "3", "4")
$periods = @("M5")                                      	# Multi Timeframe example ("M5", "M15", "M30")
$iniFile = "D:\MT5Tester2\MT5 v1.15 script testing.ini" 	# See https://www.metatrader5.com/en/terminal/help/start_advanced/start
$logFile = "D:\MT5Tester2\script_log.txt"               	# Logging file
$mt5Path = "D:\MT5Tester2\terminal64.exe"               	# Specific terminal to load, runs in /portable mode

# Function to update ini file
function Update-IniFile($path, $pattern, $replacement) {
    (Get-Content $path) -replace $pattern, $replacement | Set-Content $path
}

# Main loop
foreach ($e in $expertParameters) {
    Update-IniFile $iniFile 'ExpertParameters=.*' "ExpertParameters=$e"

    foreach ($p in $pairs) {
        Update-IniFile $iniFile 'Symbol=.*' "Symbol=$p"

        foreach ($c in $criteria) {
            Update-IniFile $iniFile 'OptimizationCriterion=.*' "OptimizationCriterion=$c"

            foreach ($t in $periods) {
                Update-IniFile $iniFile 'Period=.*' "Period=$t"

                # Print combined information for Expert Set, Pair, and Criterion
                Write-Host "Pair: $p, Criterion: $c, Period: $t, Expert Set: $e"

                # Start MT5 terminal
                $startTime = Get-Date
                Write-Host "MT5 Start $startTime"
                $process = Start-Process -FilePath $mt5Path -ArgumentList "/portable /config:`"$iniFile`"" -PassThru -Wait -NoNewWindow

                $endTime = Get-Date
                Write-Host "MT5 End $endTime"

                if ($process.ExitCode -ne 0) {
                    Write-Host "MT5 Terminal encountered an error. Exit Code: $($process.ExitCode)" -ForegroundColor Red
                }

                # Logging
                Add-Content -Path $logFile -Value "ExpertParameters: $e, Pair: $p, Criterion: $c, Period: $t, Start Time: $startTime, End Time: $endTime, Exit Code: $($process.ExitCode)"
            }
        }
    }
}

Write-Host "Script execution finished!"
Platform Start - For Advanced Users - Getting Started - MetaTrader 5 Help
  • www.metatrader5.com
After installation, a group of programs of the trading platform is added to the Start menu, and the program shortcut is created on the desktop. Use...
Reason: