MT5 guide

How to Make a Trading Bot in MetaTrader 5

Learning how to make a trading bot means choosing one of three routes: write MQL5 code in MetaEditor, generate a starting point with the MQL5 Wizard or a builder, or install an Expert Advisor you have licensed. Every route needs the same final step: testing in the Strategy Tester and on a demo account.

Trading forex and CFDs involves substantial risk of loss. Automation applies rules; it does not remove trading risk. Read our risk disclosure.

Key takeaways

How to make a trading bot: the three routes

A trading bot in MetaTrader 5 is almost always an Expert Advisor (EA): an MQL5 program that attaches to a chart and can send orders to your broker. The three routes differ in who writes the rules. The table shows what each one asks of you.

Three routes to a trading bot compared
CriterionWrite MQL5 yourselfGenerate with a wizard or builderInstall a licensed EA
Skill neededProgramming and trading logicTrading logic; little or no codeAbility to read settings and documentation
Control over rulesFullLimited to the generator’s optionsLimited to the publisher’s settings
Cost typeYour time; MetaEditor is part of the platformDepends on the tool; read its termsLicence terms set by the publisher
Time to a first testLongest of the three, because you write every partUsually shorterOften shortest, but setup still takes time
Testing burdenYou test every rule you wroteYou test all generated logic, including parts you did not chooseYou test the EA as supplied, with only the visibility its documentation gives you
Three route cards, labelled Write MQL5, Generate with a wizard or builder and Install a licensed EA, lead into one testing path. The path runs from the Strategy Tester to a forward test on demo and then to a journal.
Concept diagram, not an app screenshot.

Whichever route you take, the checks after it are the same. Only the way the rules are produced changes.

Route 1: write the bot in MQL5

MetaEditor is the development environment included with MetaTrader 5. It creates, edits and compiles MQL5 source, and it includes a step-by-step debugger. The MQL5 Reference documents the language and its functions.

  1. Learn the event handlers. OnInit runs once when the program starts, OnTick runs on each new price update, and OnDeinit runs when the program stops, for example when it is removed from the chart.
  2. Write the entry and exit rules as code. Keep the signal logic separate from the order handling so each part can be checked.
  3. Compile in MetaEditor and fix every error and warning before you go further.
  4. Test in the Strategy Tester, then run a forward test on a demo account.
  5. Record each run in a journal before you change any setting.

Writing your own code gives the most control and the most responsibility. A small logic error can send far more orders than you intended, so review every order path before the first test. The expert advisor coding guide covers the first lessons with a working example.

Route 2: generate a starting point

The MQL5 Wizard, built into MetaEditor, creates an EA from your selections. The MetaTrader 5 help describes choosing trading signals, money management and trailing stop algorithms, and generating the code from those choices. Third-party no-code builders work in a similar way, each with its own options and terms.

A generated bot is not finished when it compiles. Open the code, check what each setting does, and test it the same way you would test your own work. If you cannot explain a rule, do not run it. Read the licence of any builder and of the code it produces before you use or share it.

Route 3: install an EA you have licensed

Some traders license a ready-made EA from a publisher. This saves build time, but you can only judge what you can see: the settings, the documentation and the results you produce yourself.

For the installation steps, see how to install an EA on MT5.

After any route: test, forward test and keep a journal

The MetaTrader 5 help describes the Strategy Tester as the place to test and optimise Expert Advisors before they are used for live trading. Use the same sequence whichever route built the bot.

  1. Backtest over a fixed date range. Where the tester offers it, choose the “Every tick based on real ticks” modelling mode, which the help says uses real ticks collected by brokers.
  2. Run a forward test. The Forward option repeats the run on a later part of the period, with settings for No, 1/2, 1/3 and 1/4.
  3. Run the bot on a demo account with the same settings, for a period you fixed in advance.
  4. Record each run: settings, dates, symbol, broker, the report, and any change you made afterwards.

Keep optimisation off for a first run. Settings tuned to past data often look better in the tester than they perform on new data. The expert advisor tester guide walks through each setting, and how to verify a trading bot explains what the evidence can and cannot show. A trading journal keeps the record in one place.

Where YO4X fits

YO4X is a Windows desktop workspace for supported MetaTrader 5 strategies, bot configuration and trading journals. It runs on 64-bit Windows 10 and Windows 11. The current desktop workflow is gated to supported MT5 demo accounts, and live-money trading is not released. The staged release allows one active strategy per MT5 account.

Local operation needs the PC awake, YO4X open and the connection active. YO4X does not execute EX5 files or arbitrary uploaded code, although MQ5 source can be submitted for restricted compatibility analysis. Cloud execution is planned and is not currently available to purchase. YO4X is software, not a broker, so your account and funds stay with your broker. You can find the Windows app on the download page.

What a trading bot cannot do

A bot applies rules to price data. No bot, indicator or pattern can tell you what the market will do next. A test result describes how a rule behaved under the conditions you set, and a demo result describes a demo account, not a live one.

Check the rules that apply to you before you run any bot. Is an EA allowed in trading covers broker terms and prop firm rules. Each prop firm sets its own rules, and you must read them yourself. Trading involves a risk of loss, and this page is education, not financial advice.

Frequently asked questions

Do I need to know programming to make a trading bot?

Not for the wizard or builder routes, but you still need to understand every rule the generated code uses. Writing MQL5 requires programming skills, and you can learn them in MetaEditor and the MQL5 Reference. A bot you cannot read is a bot you cannot test or fix.

Can a trading bot make money automatically?

No tool can promise a profit. A bot runs the rules you define under the conditions you test, and those conditions change over time. Backtests and demo results do not guarantee how a bot will perform on a live account.

Which language is used for MetaTrader 5 bots?

MT5 bots are Expert Advisors written in MQL5, the language built into MetaTrader 5. Source files use the .mq5 extension and compile to .ex5 in MetaEditor. The MQL5 Reference documents the functions you call.

Is it allowed to run a trading bot with my broker?

That depends on your broker, your account type and, for a funded account, the firm’s rules. Read the broker terms and ask support for an answer in writing. This is not legal advice, so check the rules that apply to you.

Can YO4X run a bot I bought somewhere else?

Only supported strategies run in the YO4X workspace, and only on supported MT5 demo accounts. MQ5 source can be submitted for restricted compatibility analysis, so a compiled file from another seller is not the route.

Keep reading

Sources: How to Create an Expert Advisor or an Indicator - Algorithmic Trading, Trading Robots - MetaTrader 5 Help · Strategy Testing - Algorithmic Trading, Trading Robots - MetaTrader 5 Help · MQL5 Reference – How to use algorithmic/automated trading language for MetaTrader 5

Trading forex and CFDs involves substantial risk of loss. Past performance and backtests do not guarantee future results. Read our risk disclosure before you trade.