MQL's OOP notes: Introduction

16 September 2016, 21:06
Stanislav Korotky
Both MetaTrader 4 and MetaTrader 5 provide powerfull tools for trading automation, which can be extended even more - to almost any feasible idea - thanks to the built-in programming language - MQL.

MQL5 supports object oriented paradigm of programming from very beginning, and MQL4 has been improved to meet MQL5, so basically there is no difference between the languages (only core APIs of MetaTrader 4 and MetaTrader 5 are different).

OOP is a great thing, yet many MetaTrader users are not programmers, and they often ask questions about OOP in MQL: why, when, and how should they use it.

This series of articles try to answer some of these questions, explain applicational value of OOP, and provide examples of using it in MetaTrader.



OOP is intended to make complex code simplier.

This works in many aspects, such as:

  1. brevity - lower number of code lines needed to implement a required product via reuse of existing classes (without any changes in the classes);
  2. reliability - error minimization as a consequence of decomposed code with managable access rules for different contexts (classes, variables);
  3. readability - short and clear presentation of algorithm in a notation, which is close to natural language and hierarchy of real-life entities;
  4. maintainability - when it becomes necessary, one need to change as little code lines as possible;
  5. standartization - best practices of solving common programming tasks are collected and available as compendiums of design patterns;

Main question here is - how can one know whether a specific code is complex enough to make it a good candidate for simplification by using OOP? Or maybe it will only become more complex?

Well, indeed, OOP can seem an overkill for a single buffer indicator, but if you have a bunch of similar indicators it is already an occasion for extracting common structures and behavioral patterns into classes. Anytime you find yourself making "copy&paste" of code you're probably missing the point to get the benefits of OOP world. You may say you could use plain old functions and includes for eliminating "copy&paste", and this works in many cases, but then it usually gets more and more agglomerated over time. And this is when OOP comes in handy. Just a simple example: when you include a library of functions, you have only 2 options - either use it "as is" (if it meets your needs entirely), or customize for specific purpose (with these specific changes affecting every other code relying on the library, which can be error prone). With OOP, you can include a class with base functionality, derive new descendant class from it, and customize it specifically for your code.

Of course, back-compatibility and intergation with diversity of non-OOP legacy code is supported, so OOP transformation does not have to be instant and ubiquitous. You can move step by step.

Just as a short reminder, let us formulate main principles of OOP. This is the last time we speak about theory, and will from now on concentrate on practice.

Main Principles 

Data abstraction and encapsulation

In OOP you define a class with an interface, beyond which all implementation details are hidden from the rest of code. This makes it easy to improve or completely change implementation when necessary without affecting other parts of code.

A class and its objects can store internal state. This provides a unified context for class methods (functions) and does not mess up global context.

Decomposition and inheritance

All tasks to be performed by a code are distributed among logically decoupled code parts - classes with clear and unique responsibility (purpose) and minimal dependencies.

Derived classes can inherit internal state and functions from base classes, extend them.


An object pointer or reference can hold instance of a derived class. This allows you to call methods declared in the base class and actually invoke an overridden implemenation, thus customize base behavior partially and utilize all the other useful features of the parent.


Below you'll find a list of articles. All parts are not interconnected (unless it's explicitly stated in titles), so you can freely jump to any one of them if it concerns a topic you are interested in.

Table of contents

Multiple timers with publisher/subscriber design pattern and abstract class 

Converting MetaTrader 4 indicators to MetaTrader 5 

HashMap supports old-fashioned indicators in MetaTrader 5

Singleton, Command queue, and Template method patterns in simple order manager

Object hierarchies and serialization 

Self-made profiler on static & automatic objects 

Rubber arrays, safe numbers, exception handling and other tricks: part 1

Rubber arrays, safe numbers, exception handling and other tricks: part 2

On The Fly Self-Optimization of Expert Advisers: part 1

On The Fly Self-Optimization of Expert Advisers: part 2

Particle Swarm Speeds Up Expert Adviser Optimization On The Fly

Optimystic library for On The Fly Optimization of EA meets fast particle swarm algorithm 

Online Analytical Processing of trading hypercubes: part 1

Online Analytical Processing of trading hypercubes: part 2 



You can download a single booklet containing all the articles and source codes.

Share it with friends: