Running the MT5 Strategy Tester From the Command Line

公開: 2026/11/12 UTC 更新: 2026/11/12 UTC この記事の URL

Clicking through the MT5 Strategy Tester is fine for one backtest. It is not fine for forty: every EA on every pair, over three date windows, re-run each time the code changes. We run ours from a script instead. MT5 launches, runs one test, writes the report, and closes, with nobody at the keyboard.

This article shows how, using the real script we run, and the four ways it silently went wrong before we fixed it. It is part of our guide to how to backtest an EA in MetaTrader 5, step by step.

The Mechanism: A Config File and One Command

MT5’s terminal accepts a configuration file on its command line. Put a [Tester] section in that file and the terminal runs the test it describes instead of opening normally. Our script writes this section for each run:

        "[Tester]",
        f"Expert={expert}",
        f"ExpertParameters={a['set']}",
        f"Symbol={a['symbol']}",
        f"Period={a['period']}",
        f"Model={model}",
        "ExecutionMode=0",
        "Optimization=0",
        f"FromDate={a['from']}",
        f"ToDate={a['to']}",
        "ForwardMode=0",
        f"Deposit={a.get('deposit', 3000)}",
        "Currency=USD",
        "Leverage=100",
        f"Report={name}",
        "ReplaceReport=1",
        "ShutdownTerminal=1",
        "Visual=0",

Then it starts the terminal with that file and waits:

    proc = subprocess.run([str(TERMINAL), "/portable", f"/config:{ini}"], timeout=a.get("timeout", 3600))

The keys that make it work unattended:

  • ShutdownTerminal=1 closes MT5 when the test ends, so the script knows when to read the result.
  • Report= names the HTML report the tester writes, and ReplaceReport=1 lets a re-run overwrite it.
  • Model= is the tick mode as a number. Ours defaults to 4, every tick based on real ticks. The modes are compared in Every Tick vs Real Ticks in MT5.
  • Optimization=0 and ForwardMode=0 make it a single plain run. We hold back out-of-sample windows as separate runs; why is in forward testing in MT5.

Use a Separate, Portable Terminal

We never run tests in the terminal that trades. Ours is a second MT5 installation in its own folder, logged in once, started with /portable so it keeps its data next to the program rather than in the user profile. Two reasons:

  • A test run closes the terminal. ShutdownTerminal=1 in the live terminal would stop every EA on every chart.
  • One terminal runs one test at a time. The script runs a batch in sequence, and the live terminal is never touched.

The cost is a copy to keep in step. The test terminal has its own copy of the EAs and settings files, and after every code or preset change they have to be copied across again. Forget, and you test last week’s code.

Four Ways It Went Wrong Without Saying So

The tester rarely reports an error. When something is wrong in the config, it usually runs something else and writes a perfectly normal-looking report. Each of these four caught us once.

1. A forward slash runs a different EA. We wrote the EA path with /. MT5 did not find it, did not complain, and ran its stock Moving Average sample instead: 4,905 trades, and none of our inputs in the report. The comment in the script now reads:

    # MT5 resolves Expert= relative to MQL5\Experts with BACKSLASHES only. With a
    # forward slash it silently falls back to a stock sample (Moving Average) and the
    # report carries no Inp* inputs at all.

2. The settings file is looked up in the wrong folder. ExpertParameters= is read from MQL5\Profiles\Tester\, not from MQL5\Presets\ where you save sets from the chart. A set the tester cannot find is ignored, and the EA runs on its default inputs. Our first filed run was exactly that: a real report, for settings nobody chose. We now keep the sets in both folders.

3. A zero-trade run looks like a result. Wrong timeframe, wrong set, a filter that never opens: the report still appears, with zero trades. The script refuses to publish one:

    # Never file a run that did not trade: it is a misconfiguration (wrong set, wrong TF),
    # and filing it would create a version row from the engine's DEFAULT inputs.

It also checks that the EA named in the report is the EA it asked for, which is what catches mistake 1.

4. The report speaks the terminal’s language. Our test terminal’s interface is Japanese, so its reports are labelled in Japanese. A parser written against an English report finds nothing. Ours carries both sets of labels; we checked that the numbers match an English export of the same run.

Reading the Results

The report is the same HTML file the tester writes from the interface. Our script parses it and files the numbers to our site, where each run is published with its window and tick source; you can see them on any EA’s page under presets.

When a run fails outright, there is no report at all. The places to look are the terminal’s log and the tester’s own log under Tester\logs, one file per day. The tester log is UTF-16 text: open it in an editor that detects that, or it looks empty.

The first run over a date range you have not tested before is slow, because the terminal downloads that history first. Later runs over the same range start immediately.

Is It Worth Setting Up?

If you test one EA occasionally, no. If you test several EAs across several windows, and want to re-run all of it after a change, yes, and the reason is not speed. It is that every run is described by a file. You can see exactly what was tested and run the same test again a year later. A backtest you cannot reproduce is a screenshot, not evidence.

Start small: one config file, one command, one report. Then check the four traps above before you trust the first result.

EA づくりの参考になったら、ぜひ共有してください。
X Facebook LinkedIn

Keisuke Kurosawa
Hello

コメント

0
まだコメントはありません。

コメントするにはログインしてください。
共有
https://1kpips.com/ja/blog/mt5-strategy-tester-command-line
カテゴリ
MT5 Strategy Testing & Optimization
タグ
Strategy Tester,Command Line,Automation,Python,MT5 Backtesting,techniques

関連記事

次にやること
手を動かすなら、時間帯フィルタを 1 つ足してみてください。入れた場合と外した場合でバックテストを回せば、効いているかどうかがそのまま数字に出ます。