Generate an MT4 or MT5 expert advisor without coding: backtest to EA
A strategy generator that exports MQL4 or MQL5 source gives you the expert advisor as a file. Compile it in MetaEditor, run it in the strategy tester on aligned data and costs, compare the trade lists from the first trade, fix the one setting that differs, then go to demo and to live in stages.
By Soren Huang · Published · updated · Editorial policy
Contents
- How do you get a tested strategy into an MT4 or MT5 expert advisor without writing MQL?
- Why does the MetaTrader backtest start from different data than the original?
- How do you run the strategy tester and confirm it agrees with the original backtest?
- Which EA settings cause the most mismatches: lot size, magic number, broker settings?
- How do you go live with an EA and know it is still doing what the backtest did?
- Frequently asked questions
How do you get a tested strategy into an MT4 or MT5 expert advisor without writing MQL?
You do not need to write MQL. A strategy generator that exports MQL4 or MQL5 source, such as AlphaPrime (Valynox), gives you the expert advisor as a source file. You open the file in MetaEditor, compile it, run it in the strategy tester, compare its trades with the original backtest, and then attach it to a chart.
An expert advisor (EA) is a program written in MQL4 (for MetaTrader 4) or MQL5 (for MetaTrader 5) that MetaTrader runs on one chart and that can open, modify and close positions on its own. An expert advisor generator is a program that turns trading rules you have already tested into that source file, so the coding step is done for you. MetaEditor is the editor that ships with every MetaTrader terminal; one click compiles a .mq4 file into an .ex4 file, or a .mq5 file into an .ex5 file, and the terminal can then load it. The same generated strategy can also go to other platforms, with separate guides for TradeStation and TradingView.
AlphaPrime exports strategy source code to six platforms, and MetaTrader 4 (MQL4) and MetaTrader 5 (MQL5) are two of them; the full list is on the supported platforms for export page. The strategy you export has already been through AlphaPrime’s backtesting with fees, slippage and next-bar execution, so the EA is the last step, not the first test.
- Finish the research first. Validate the strategy in the research program on the bar data you trust. Do not plan to "fix it in MetaTrader".
- Export the MQL4 or MQL5 source file. Choose the file that matches the terminal your broker gives you; MQL4 and MQL5 are different languages and a file for one does not compile in the other.
- Compile in MetaEditor. Copy the file into the terminal’s Experts folder, open it in MetaEditor and compile. A clean compile shows zero errors.
- Run the strategy tester and compare. This is the step most people skip, and it is the one that catches a wrong session or lot size before money is involved.
- Attach the EA to a chart. First on a demo account, then live with the smallest size.
One check before you export: MetaTrader offers a fixed list of timeframes. MetaTrader 4 has nine, from M1 to MN1; MetaTrader 5 adds several more, for example M2, M10 and H2. If your research used a bar size MetaTrader does not offer, repeat the research on one it does, then export.
Why does the MetaTrader backtest start from different data than the original?
MetaTrader builds its bars from your broker’s price feed, in your broker’s time zone, for your broker’s symbol, so the same rules see different bar opens and closes than the original backtest until you align session, time zone, symbol and costs. The rules did not change. The bars they read did.
Server time is the clock of the broker’s trade server, and every bar in MetaTrader is stamped in that clock, not in your local time and not in the exchange’s time. A research backtest on index futures usually uses exchange time and a session the trader chose. In MetaTrader the symbol is often a CFD on the same index with the broker’s own trading hours, and an H1 bar closes on the server hour whether or not the exchange session starts on the hour. Exchanges and brokers change session hours and symbol specifications; check the symbol specification window in MetaTrader and the exchange’s contract page before you compare, and use the futures market hours tool to see the exchange session in your own time zone.
| What differs | Original backtest | MetaTrader | Make equal before comparing |
|---|---|---|---|
| Bar time stamp | Exchange time | Broker server time | Yes: convert session times to server time |
| Session | Set by the trader | Broker’s trading hours for the symbol | Yes: restrict the EA to the same hours |
| Symbol | Exchange futures contract | Broker symbol, often a CFD on the same index | Yes: same underlying and same bar size |
| Bar close | On the exchange session boundary | On the broker’s clock boundary | Yes: align the first bar of the session |
| Costs | Commission plus slippage per side | Spread plus commission and the tester’s delay setting | Yes: set the tester spread and delay to the same total round-trip cost |
Costs need the most care. A research backtest models commission and slippage per side; MetaTrader models a spread (the gap between bid and ask), a commission set by the broker and, in the MetaTrader 5 tester, an execution delay. Add up the round-trip cost in both and make the totals equal, as explained in trading costs in backtests. If you want the EA to read exactly the bars the research used, MetaTrader 5 lets you create a custom symbol and import your own bar history, and MetaTrader 4 lets you import history for a symbol in the History Center; then the only differences left are costs and execution.
How do you run the strategy tester and confirm it agrees with the original backtest?
Run the tester on the same symbol, timeframe, date range and cost settings, then compare the two trade lists trade by trade from the first entry; a good match has the same entry times and directions with price differences no larger than the spread or slippage you set, and the first trade that diverges tells you which setting is wrong. Think of it like checking two copies of the same invoice line by line: the first line that differs points to the error.
- Select the EA, symbol and timeframe. In the Strategy Tester window choose the compiled EA, the symbol you aligned in the previous section and the timeframe of the research.
- Set the date range. Use the same start and end dates as the original backtest. Make sure the terminal has downloaded history for the whole range; a tester that starts late because history is missing looks like a strategy that stopped trading.
- Choose the modelling mode. A strategy that decides on the close of a bar and acts on the next open can be tested with "Open prices only"; it is fast and the entries match the research convention. If the strategy uses stops or targets inside a bar, use a tick mode ("Every tick" in MetaTrader 4; "Every tick based on real ticks" or "1 minute OHLC" in MetaTrader 5) so that the exit prices are reached in the right order.
- Set costs. Set a fixed spread rather than "current" so the test is repeatable, and set the delay in MetaTrader 5 to match the slippage you assumed.
- Set the inputs. Enter the parameter values the research chose and the lot size that corresponds to one contract (see the next section).
- Run and save the report. Open the report, save the trade list, and put it next to the trade list from the research program.
Compare the lists in this order: trade count, then the first entry time and direction, then prices. A difference of one tick on most entries is the spread or the delay doing its job. An entry that is one bar later in MetaTrader means the bar boundaries are shifted: check the server time offset and the first bar of the session. An entry that is missing means a session filter or a stop level blocked it. Two lists that agree on every trade time and direction, with prices inside the cost you set, are as close as two different programs on two different feeds get; this is also a good moment to confirm that no trade uses a price from its own signal bar, because such a fill would be a look-ahead leak that the live EA can never repeat.
Which EA settings cause the most mismatches: lot size, magic number, broker settings?
Once the data is aligned, most remaining differences come from a handful of settings: a fixed lot size that does not equal one contract, a missing or shared magic number, the broker’s stop level and freeze level, the symbol’s contract size and tick value, and the broker’s fill policy. Each one has a symptom you can see in the tester report and a place to check in MetaTrader.
A magic number is an integer the EA writes into every order it sends, so that it can later recognise its own positions among others on the same account. The stop level is the minimum distance, in points, that the broker requires between the current price and a pending order or stop. The freeze level is the distance inside which an existing order cannot be modified or deleted. The fill policy is the broker’s rule for partial fills: fill or kill, immediate or cancel, or return (fill what is available and keep the rest). All four live in the symbol specification window, which you open by right-clicking the symbol in Market Watch.
| Symptom | Likely cause | What to check |
|---|---|---|
| Profit per trade is 10 or 100 times off | Lot size versus contracts | Contract size and volume step of the symbol |
| EA closes trades it did not open, or ignores its own | Magic number | A unique magic number per EA and per chart |
| Stop orders rejected | Broker stop level and freeze level | Minimum stop distance in the symbol specification |
| Entries fill at a different price | Fill policy and spread | Execution mode and the spread used by the tester |
| EA trades outside the intended hours | Broker server time | Session filter converted to server time |
Lot size is the one that produces the largest surprise, because MetaTrader sizes in lots and the research sizes in contracts. One lot of a broker symbol can be worth a different amount per point than one exchange contract, and the symbol’s volume step decides which lot sizes you may send at all. Use the futures contract calculator to get the point value of the exchange contract, then read the contract size of the broker symbol and work out the lot that gives the same value per point.
Hypothetical example
The research backtest trades one contract of an index future worth 50 dollars per index point. The broker’s CFD on the same index has a contract size of 1, so one lot earns 1 dollar per point, and the volume step is 0.1 lot. To match the research, the EA must send 50 lots, not 1 lot. With 1 lot the tester shows the same trade times but every result is 50 times smaller, and the fixed commission per lot now takes a larger share of each trade than the research assumed.
For the magic number, give every EA on every chart its own value and keep a short list of them. For stop levels, check that the smallest stop distance the strategy uses is larger than the broker’s stop level in points; if it is not, the broker rejects the order and the tester logs an error rather than a trade. For the fill policy, pick the one the broker’s symbol specification lists as allowed; an EA that sends a policy the broker does not accept gets a rejection, not a fill.
How do you go live with an EA and know it is still doing what the backtest did?
Go live in stages, and at every stage compare what the EA actually did with what the backtest would have done on the same bar, so that a drift in slippage, a missed session or a broker change is seen within days rather than months. An EA runs only while the terminal is running and connected; a virtual private server or an always-on computer keeps it running through the full session. Before export, thresholds are fixed in AlphaPrime's strategy validation, so the EA's demo and live trades are compared against a standard you set in advance.
- Demo account, planned size. Run the EA for at least a few weeks of trading. Each day, record for every trade: the signal bar time in the research program, the fill time in the terminal, and the fill price against the bar open. The three should agree the way the tester did. Record the three numbers: trades taken, trades expected, average fill difference in ticks.
- Live account, smallest lot. Now the fills are real. Record the slippage of every entry and exit against the price the backtest assumed, and compare the running average with the slippage setting you used in the research. If live slippage is twice the assumption, rerun the research backtest with that value before you grow size. Record: average slippage per side, worst slippage, number of rejected orders.
- Live account, planned size. Switch to a weekly review. Record the number of trades for the week against the number the backtest shows for a typical week, the average slippage, and whether every session hour the strategy expects was actually covered by a connected terminal. A missing session shows up as a low trade count before it shows up in the result.
Any difference you cannot explain sends you back to the demo stage, with the same trade-by-trade comparison as in the tester. The usual causes are a broker change (new server time offset, new symbol name after a contract roll, new stop level), a terminal that was disconnected for part of a session, or a change in the spread at the hours the strategy trades. Keep the EA’s journal file; it shows every order sent, every rejection and every reconnect with a time stamp.
What this means if you use AlphaPrime: the strategy you export to MQL4 or MQL5 was evaluated with costs and next-bar execution, so the tester comparison is about aligning MetaTrader’s bars, lots and broker settings, and the trade list from the Performance Report is the reference you compare against at every stage.
Frequently asked questions
Is an EA generator the same as an EA builder?
Both names describe software that produces expert advisor code without the trader writing MQL. An EA builder usually lets you assemble rules by hand from a menu; a strategy generator searches many rule combinations, tests them and exports the ones that pass as source files. In both cases the output is a .mq4 or .mq5 file you compile in MetaEditor.
Can I run an MT5 expert advisor on MT4?
No. MQL4 and MQL5 are different languages with different trading functions, and a file written for one does not compile in the other. Export the file that matches your terminal; AlphaPrime exports both MQL4 and MQL5, so you choose at export time rather than translating by hand.
Why does the strategy tester show fewer trades than my original backtest?
The most common reasons are missing history for part of the date range, a session filter that was not converted to server time, and stop orders rejected because the distance is below the broker’s stop level. Check the tester journal for errors first, then compare the first missing trade with the original list to see which cause applies.
Can I just download an expert advisor instead of exporting one?
A compiled .ex4 or .ex5 file runs, but you cannot read its rules, so you cannot compare its trades with a backtest you control or change a setting when a broker changes its symbol. A source file you exported from your own research can be compiled, read and matched trade by trade, which is the whole point of the tester comparison in this article.
Do I need a VPS to run an EA?
An EA runs only while its terminal is open and connected to the broker. If the strategy trades during hours when your computer may be off or offline, a virtual private server or an always-on machine keeps the session covered. A missed session shows up first as a lower weekly trade count than the backtest expects.

