what is mql4 and mql5

What Is MQL4 and MQL5?

MQL4 and MQL5 are the programming languages used to write automated trading programs for the MetaTrader 4 and MetaTrader 5 platforms respectively. MQL stands for MetaQuotes Language, named after MetaQuotes Software Corp, the company that develops the MetaTrader platforms. MQL4 is the language for MT4; MQL5 is the language for MT5. The two are closely related but not interchangeable: a program written in one language will not run on the other platform without rewriting.

Both languages are used to create Expert Advisors (automated trading robots), custom indicators, scripts (one-time execution programs), and libraries that other programs can reference. Code is written in MetaEditor, the development environment that ships with MetaTrader, and compiled into the platform’s executable format (.ex4 for MT4, .ex5 for MT5) before it can run.

This article explains what MQL4 and MQL5 are, how they relate to each other, what they can and cannot do, and how a trader who is not a programmer can still benefit from understanding them.

What MQL Is Used For

MQL is a domain-specific programming language. Its primary purpose is to interact with the MetaTrader platform to read market data, perform calculations, and place or modify trades.

MQL programs fall into four main categories:

Expert Advisors are continuous trading programs that read market data on every price tick and can place, modify, and close trades automatically. They form the core of automated trading on MetaTrader.

Custom indicators read market data and perform calculations to produce values plotted on the chart. They do not place trades. Their function is purely analytical.

Scripts are one-time-execution programs. A script runs once when attached to a chart, performs its action (such as closing all open positions or opening a specific order pattern), and exits. Scripts differ from EAs in that they do not run continuously.

Libraries are reusable code modules that other programs can call. A library might contain commonly used functions for position sizing, indicator calculations, or order management.

All four types use the same MQL syntax for their core logic; what differs is the program structure and the events the program responds to.

MQL4 and MQL5 Syntax

Both languages have C-like syntax. Programmers familiar with C, C++, Java, or C# generally find MQL straightforward to read. Variables, functions, conditional statements, loops, and arrays follow conventions that resemble C++.

A simple MQL4 example shows the basic structure of an Expert Advisor:

int OnInit() {
    // Code that runs once when the EA starts
    return INIT_SUCCEEDED;
}

void OnTick() {
    // Code that runs on every price tick
    if (Bid > 1.1000) {
        // Place an order or perform an action
    }
}

void OnDeinit(const int reason) {
    // Code that runs when the EA stops
}

The same logical structure exists in MQL5, though some function names and parameters differ. MQL5 has a more extensive object-oriented framework, including built-in classes for handling trades, indicators, and account information. Programmers writing in MQL5 can use these classes for more concise and maintainable code.

Key Differences Between MQL4 and MQL5

The two languages have substantial differences that prevent direct compatibility.

Order management. MT4 uses an order-based system where each trade is a separate order with a unique ticket number. MT5 uses a position-based system where multiple deals can be combined into a single position. The functions used to interact with trades differ accordingly. MQL4’s OrderSend, OrderSelect, and similar functions have MQL5 equivalents with different signatures.

Hedging versus netting. MT4 always operates in hedging mode, where a trader can hold simultaneous long and short positions in the same symbol. MT5 supports both hedging and netting modes, with the broker setting which is available on a given account. In netting mode, opposing orders on the same symbol offset each other rather than coexisting. MQL5 code must be aware of which mode the account is using.

Object orientation. MQL5 was designed from the start as an object-oriented language with classes, inheritance, and polymorphism. MQL4 was originally procedural and later added some object-oriented features. Code written in modern MQL5 style is generally more class-based than equivalent MQL4 code.

Strategy Tester capabilities. Both languages have their respective Strategy Tester environments, but MT5’s tester supports features the MT4 tester does not, including multi-currency testing, cloud-based parallel optimisation, and tick-by-tick simulation with separate bid and ask data.

Indicator buffers. MT4 limits each custom indicator to eight buffers (data series). MT5 allows more buffers per indicator, supporting more complex visualisations.

Trade execution functions. Specific MQL functions for sending and modifying orders are named differently. OrderSend in MQL4 corresponds to OrderSend in MQL5 but with a different parameter structure. Migrating code requires updating these calls.

The practical result is that an EA written in MQL4 cannot be compiled or run as an MQL5 EA without being rewritten. The amount of work involved depends on the complexity of the original code.

Who Writes in MQL

Several groups produce MQL code:

Independent developers create EAs and indicators for sale or free distribution. Most commercial EAs on marketplaces are written by independent developers, ranging from individuals to small commercial entities.

Retail traders sometimes write their own EAs and indicators. The learning curve for basic MQL is approachable for anyone with some programming background, and many resources are available for self-study.

Brokers and institutional providers create proprietary indicators and tools, sometimes for branded distribution to clients.

The MQL5 Market is the official MetaQuotes marketplace where MQL4 and MQL5 programs can be bought and sold. It applies a review process before listings are approved, although quality and effectiveness still vary widely.

Working with MQL Without Programming Knowledge

A trader who does not program can still benefit from understanding what MQL is and how it interacts with the platform.

Reading code in EAs. Before running a third-party EA, a basic ability to read the source code (when available as .mq4 or .mq5) helps identify whether the EA does what the developer claims. Even without writing skills, recognising the names of functions like OrderSend, IsTradeAllowed, and AccountBalance reveals whether the code is interacting with trades, account state, or external data.

Understanding error messages. EAs and custom indicators report errors via the Experts tab and Journal tab at the bottom of the MetaTrader interface. The messages typically use MQL function names. Knowing the basic vocabulary helps interpret why something is not working.

Communicating with developers. When commissioning custom MQL development, a trader who understands the basic capabilities and constraints of the language can specify requirements more clearly and recognise when a proposal is unrealistic.

Learning Resources

MetaQuotes provides extensive official documentation for both MQL4 and MQL5, including function references, code examples, and tutorials. The documentation is available at mql4.com and mql5.com respectively. The MQL5 community forum is active and includes both beginner-friendly threads and advanced technical discussions.

Books, video courses, and online communities offer additional learning resources. Beginners typically focus on understanding the main event functions (OnInit, OnTick, OnDeinit), trade management functions, and basic indicator structure before attempting more complex projects.

Frequently Asked Questions

What is the difference between MQL4 and MQL5? MQL4 is the programming language for MetaTrader 4. MQL5 is the programming language for MetaTrader 5. Both use C-like syntax, but they have different function libraries, different order management systems, and are not directly compatible with each other.

Can MQL4 code run on MT5? No. MQL4 code is specific to MT4. To run the same logic on MT5, the code must be rewritten in MQL5, accounting for differences in order management, function signatures, and platform architecture.

Do I need to learn MQL to use MetaTrader? No. Using built-in indicators, manually placing trades, and running pre-built EAs require no programming knowledge. Learning MQL is only necessary for developing custom EAs, custom indicators, or scripts.

Is MQL difficult to learn? For someone with prior programming experience, particularly in C, C++, Java, or C#, the syntax is familiar and the language can be learned relatively quickly. For complete beginners, learning MQL involves both learning programming concepts and learning the specifics of how MetaTrader functions. A few months of consistent study is a realistic starting point.

Where do I write MQL code? MQL code is written in MetaEditor, the integrated development environment that ships with both MT4 and MT5. MetaEditor can be opened from within MetaTrader via the toolbar or Tools menu. It includes a code editor, debugger, and compilation tools.

Can I sell MQL code I write? Yes. The MQL5 Market is the official marketplace for selling and distributing MQL programs. Independent developer websites also distribute commercial MQL programs. Pricing, distribution model, and licensing are at the developer’s discretion within MetaQuotes’ marketplace terms.

Is MQL5 better than MQL4? MQL5 is more powerful and modern, with better object-oriented support, more advanced testing capabilities, and a richer function library. MT4 and MQL4 remain widely used because of their long-established user base and the large existing library of MQL4 programs. The right choice depends on which platform the trader uses and which tools they wish to develop or run.