Converting an .mq4 file to MetaTrader 5
You have a program as an .mq4 file and a MetaTrader 5 terminal. Renaming the file, or pasting the code into MetaEditor, does not work. Here is why, what a conversion really involves, and how to check the result before you trust it with an account.
Why the file will not just compile
An .mq4 file is written in the older language of the MetaTrader family. MQL5 looks much the same — the same C-like syntax, the same OnInit and OnDeinit — but the parts that matter most to a trading program were redesigned:
- Orders, positions and deals are separate things. In the older language an open trade is an "order" you loop through with OrderSelect. In MQL5 an open trade is a position, the instruction that opened it is an order, and each fill is a deal. Code that counts, modifies or closes trades has to be rewritten, not renamed.
- Indicators return a handle, not a value. The older iMA call hands back a number. In MQL5, iMA creates the indicator once and returns a handle; the values are read with CopyBuffer, which can fail and has to be checked every time.
- Sending a trade is a request and a result. MQL5 fills in a request structure and reads back a result code — most programs use the standard CTrade class for this — so the error handling changes with it.
- An account can be netting or hedging. On a netting account a new trade in the same symbol changes the one position instead of opening a second. Logic that assumes several separate trades per symbol has to know which kind of account it is on.
- Prices and times are read differently. Ready-made arrays such as Close[] and Time[] are gone; the program copies the data it needs.
That is why automatic converters usually produce code that either does not compile, or compiles and then behaves differently. The syntax translates. The behaviour does not.
Rewrite the rules, not the lines
The safer way is to treat the .mq4 file as a description of rules: write those rules down in plain words, and build the MetaTrader 5 program from what is written. A line-by-line translation inherits every quirk of the old code; a rewrite from rules inherits only what you decided to keep.
Before anything is written, answer these about the old program:
- When exactly does it open a trade — once when a new candle starts, or on every price tick?
- Where are the stop loss and take profit: sent with the order, or watched by the program and closed by hand? A level kept only inside the program protects nothing while the terminal is closed.
- What does it do with trades that are already open when it starts again?
- Which inputs does it have, and what does 0 mean for each of them?
- Does it read another timeframe, another symbol, or a custom indicator file?
Check the converted program before you trust it
- The same inputs, with the same names and the same defaults.
- Back-test the old and the new over the same period and compare them trade by trade, not only the total. Different entry times on the same candles mean a rule was read differently.
- The stop loss and take profit visible on the position in the terminal, not only in the program's own comments.
- Restart the terminal with a trade open: the program should pick it up, not open a second one.
- The Experts and Journal tabs quiet during normal running. A program that prints on every tick buries the one message you need.
How uBotDesign does it
Attach the .mq4 file in the chat in the app. The assistant reads it and writes back, in your language, what it understood each rule to be, and you correct anything it got wrong. The written summary is what you agree to. The factory then writes a MetaTrader 5 program from that summary, compiles it, checks it against every rule and tests it on past prices before the file is on your shelf. An .mq4 indicator is read the same way, and built as a MetaTrader 5 indicator.
Talking to the assistant costs nothing. A build's price is shown before you confirm.