Getting Started with MQL5

Set up MetaEditor, write your first script, and understand the MQL5 development workflow.

What is MQL5?

MQL5 (MetaQuotes Language 5) is a high-level, C++-like programming language designed specifically for developing trading applications on the MetaTrader 5 platform. With MQL5, you can create custom indicators, Expert Advisors (automated trading systems), scripts, and libraries.

MQL5 is a significant upgrade from MQL4. It supports object-oriented programming, has a richer standard library, faster execution speed, and access to more market data. If you know C, C++, or Java, MQL5 syntax will feel familiar.

Opening MetaEditor

MetaEditor is the integrated development environment (IDE) that comes with MetaTrader 5. To open it:

  • From MT5: Press F4 or click Tools > MetaQuotes Language Editor
  • From Windows: Find MetaEditor in your Start Menu under the MetaTrader 5 folder

MetaEditor provides syntax highlighting, auto-completion, a built-in compiler, a debugger, and direct access to the MQL5 Reference documentation.

MQL5 Program Types

There are four types of MQL5 programs, each serving a different purpose:

  • Scripts (.mq5) — Run once and exit. Use for one-time tasks like closing all orders, exporting data, or sending a notification.
  • Indicators (.mq5) — Run continuously on a chart, processing every new tick or bar. They calculate and display visual information (lines, arrows, histograms) on the chart.
  • Expert Advisors (.mq5) — Run continuously and can place, modify, and close trades automatically. They implement your trading strategy in code.
  • Libraries (.mq5) — Reusable code modules that can be imported by other programs.

Your First MQL5 Script

Let us write a simple script that prints a message to the Experts log:

1
Create a new script

In MetaEditor, go to File > New (or press Ctrl + N). Select "Script" and click Next. Name it "HelloWorld" and click Finish.

2
Write the code

Replace the generated code with:

//+------------------------------------------------------------------+
//| HelloWorld.mq5                                                    |
//| A simple script that prints to the Experts log                    |
//+------------------------------------------------------------------+
void OnStart()
{
    Print("Hello, MQL5 World!");
    Print("Account balance: ", AccountInfoDouble(ACCOUNT_BALANCE));
    Print("Current symbol: ", _Symbol);
    Print("Current timeframe: ", EnumToString(_Period));

    Alert("Script executed successfully!");
}
3
Compile

Press F7 or click Compile. If there are no errors, you will see "0 errors, 0 warnings" in the output panel.

4
Run the script

Switch back to MT5 (F4). In the Navigator panel, expand Scripts, find HelloWorld, and drag it onto any chart. Check the Experts tab in the Toolbox to see the output.

Understanding the File Structure

MQL5 programs are stored in the MT5 data folder:

MQL5/
  Experts/       <-- Expert Advisors (.mq5 and .ex5)
  Indicators/    <-- Custom Indicators
  Scripts/       <-- Scripts
  Include/       <-- Header files (.mqh)
  Libraries/     <-- Library files

Source files have the .mq5 extension. When you compile, MetaEditor creates an .ex5 file (executable) in the same directory. MT5 runs the .ex5 files — the .mq5 source is only needed for development.

The Compilation Process

MQL5 is a compiled language, not interpreted. This means:

  • You write source code in .mq5 files
  • MetaEditor compiles it into optimized .ex5 bytecode
  • MT5 executes the bytecode at near-native speed
  • If you share an .ex5 file without the .mq5 source, others can run your program but cannot see or modify your code
💡
Protect your intellectual property

When selling or distributing indicators and EAs, you only need to share the compiled .ex5 file. Your source code (.mq5) remains private. This is how the MQL5 Market works — buyers get executables, not source code.

Key MQL5 Resources

  • MQL5 Reference — Press F1 in MetaEditor or access it from Help > MQL5 Reference. This is the complete language documentation.
  • MQL5.com Community — Forum, articles, Code Base (free code), and the Market (paid indicators/EAs).
  • MetaEditor's Code Completion — Type the first few letters of any function and press Ctrl + Space for auto-completion suggestions.

Next Steps

Now that you can create and run MQL5 programs, the next tutorial covers variables, data types, and operators — the building blocks of every MQL5 program.