Edgoro
MQL5 examples

MQL5 examples: working code for MetaTrader 5

Answers to the questions MetaTrader 5 developers search for most, from opening an order to drawing on the chart. Each page has a short answer, a complete Expert Advisor or script that compiles in MetaEditor, and the pitfalls that make the obvious version fail.

Orders and positions

Open, close, protect and size trades with CTrade, within the broker's price, volume and distance rules.

Signals, timing and rules

When the EA acts: new bars, indicator values, sessions, timers and limits on how often it trades.

Chart, files, messages and testing

What the EA shows and sends: objects and panels on the chart, CSV files, alerts, webhooks and the Strategy Tester.

How to run an example

Each page says whether its program is an Expert Advisor, which stays on its chart and reacts to every tick until you remove it, or a script, which runs once when you drop it on a chart.

  1. In MetaTrader 5, open File > Open Data Folder, then MQL5/Experts for an Expert Advisor or MQL5/Scripts for a script, and save the downloaded .mq5 file there.
  2. Open it in MetaEditor and press F7 (Compile). The Errors tab should end with “0 errors, 0 warnings”.
  3. Back in the terminal, drag the program from the Navigator onto a chart. Programs that trade need the Algo Trading button on and “Allow Algo Trading” ticked in their settings.
  4. Start on a demo account. For an Expert Advisor, run it in the Strategy Tester first.

The full walk-through, with the usual problems and their fixes, is in how to install an Expert Advisor on MT5.

What every example does the same way

The programs are short, but they follow the rules that keep code working on real brokers, so you can lift a function into your own EA:

  • Orders go through the standard library's CTrade, and every result is checked with ResultRetcode(): a true return only means the request was sent.
  • Prices are rounded to the symbol's tick size and volumes to its volume step, never to a fixed number of decimals.
  • Stops are measured from the price a position closes at: the Bid for a buy, the Ask for a sell.
  • Only positions with the program's symbol and magic number are touched, so manual trades and other EAs are left alone.
  • Indicator handles are created once in OnInit(), and signals are read on closed bars.
  • Anything that must survive a restart, such as the trades taken today or a partial close already done, is read from the server's deal history rather than kept in a variable.

Every program on these pages was compiled in MetaEditor 5 (build 6275) on November 12, 2026, with 0 errors and 0 warnings. A clean build shows that the code compiles, not that a strategy makes money: programs that send orders are marked, and belong on a demo account until you've tested them.

Need a whole Expert Advisor, not a snippet?

Describe your strategy in plain English and Edgoro's MT5 EA generator writes the complete MQL5 Expert Advisor, with the order handling, rounding and checks shown here, and compiles it in MetaEditor before you get it. Test it on a demo account: a clean build isn't proof that a strategy works.